From af455dcb39dbd53700105e29c87de5db65ecf43c Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 14 Mar 2025 22:19:17 +0100 Subject: [PATCH] refactor: Simplify pruning functions Move GetPruneRange from ChainstateManager to Chainstate. --- src/node/blockstorage.cpp | 7 +++---- src/node/blockstorage.h | 3 +-- src/validation.cpp | 12 ++++++------ src/validation.h | 11 +++++------ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index ec95f11b0b9..41931a81090 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -284,8 +284,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber) void BlockManager::FindFilesToPruneManual( std::set& setFilesToPrune, int nManualPruneHeight, - const Chainstate& chain, - ChainstateManager& chainman) + const Chainstate& chain) { assert(IsPruneMode() && nManualPruneHeight > 0); @@ -294,7 +293,7 @@ void BlockManager::FindFilesToPruneManual( return; } - const auto [min_block_to_prune, last_block_can_prune] = chainman.GetPruneRange(chain, nManualPruneHeight); + const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(nManualPruneHeight); int count = 0; for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) { @@ -337,7 +336,7 @@ void BlockManager::FindFilesToPrune( return; } - const auto [min_block_to_prune, last_block_can_prune] = chainman.GetPruneRange(chain, last_prune); + const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(last_prune); uint64_t nCurrentUsage = CalculateCurrentUsage(); // We don't check to prune until after we've allocated new space for files diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 9ba7873cdca..70baa273eb8 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -218,8 +218,7 @@ private: void FindFilesToPruneManual( std::set& setFilesToPrune, int nManualPruneHeight, - const Chainstate& chain, - ChainstateManager& chainman); + const Chainstate& chain); /** * Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a user-defined target. diff --git a/src/validation.cpp b/src/validation.cpp index 72872c5ce72..9ecfdfa1b1a 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2785,7 +2785,7 @@ bool Chainstate::FlushStateToDisk( m_blockman.FindFilesToPruneManual( setFilesToPrune, std::min(last_prune, nManualPruneHeight), - *this, m_chainman); + *this); } else { LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH); @@ -6399,22 +6399,22 @@ bool ChainstateManager::ValidatedSnapshotCleanup(Chainstate& validated_cs, Chain return true; } -std::pair ChainstateManager::GetPruneRange(const Chainstate& chainstate, int last_height_can_prune) +std::pair Chainstate::GetPruneRange(int last_height_can_prune) const { - if (chainstate.m_chain.Height() <= 0) { + if (m_chain.Height() <= 0) { return {0, 0}; } int prune_start{0}; - if (chainstate.m_from_snapshot_blockhash && chainstate.m_assumeutxo != Assumeutxo::VALIDATED) { + if (m_from_snapshot_blockhash && m_assumeutxo != Assumeutxo::VALIDATED) { // Only prune blocks _after_ the snapshot if this is a snapshot chain // that has not been fully validated yet. The earlier blocks need to be // kept to validate the snapshot - prune_start = Assert(chainstate.SnapshotBase())->nHeight + 1; + prune_start = Assert(SnapshotBase())->nHeight + 1; } int max_prune = std::max( - 0, chainstate.m_chain.Height() - static_cast(MIN_BLOCKS_TO_KEEP)); + 0, m_chain.Height() - static_cast(MIN_BLOCKS_TO_KEEP)); // last block to prune is the lesser of (caller-specified height, MIN_BLOCKS_TO_KEEP from the tip) // diff --git a/src/validation.h b/src/validation.h index e14dac74bbd..e810e872e87 100644 --- a/src/validation.h +++ b/src/validation.h @@ -835,6 +835,11 @@ public: return m_mempool ? &m_mempool->cs : nullptr; } + //! Return the [start, end] (inclusive) of block heights we can prune. + //! + //! start > end is possible, meaning no blocks can be pruned. + std::pair GetPruneRange(int last_height_can_prune) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + protected: bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs); bool ConnectTip( @@ -1325,12 +1330,6 @@ public: //! @sa node/chainstate:LoadChainstate() bool ValidatedSnapshotCleanup(Chainstate& validated_cs, Chainstate& unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - //! Return the [start, end] (inclusive) of block heights we can prune. - //! - //! start > end is possible, meaning no blocks can be pruned. - std::pair GetPruneRange( - const Chainstate& chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - //! Get range of historical blocks to download. std::optional> GetHistoricalBlockRange() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);