diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -371,11 +371,6 @@ */ uint64_t CalculateCurrentUsage(); -/** - * Mark one block file as pruned. - */ -void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main); - /** * Actually unlink the specified files */ @@ -1216,6 +1211,10 @@ CChain &ValidatedChain() const { return ValidatedChainstate().m_chain; } CBlockIndex *ValidatedTip() const { return ValidatedChain().Tip(); } + //! Mark one block file as pruned (modify associated database entries) + void PruneOneBlockFile(const int fileNumber) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + //! Load the block tree and coins database from disk, initializing state if //! we're running with -reindex bool LoadBlockIndex(const Consensus::Params ¶ms) diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -163,9 +163,11 @@ std::unique_ptr pblocktree; // See definition for documentation -static void FindFilesToPruneManual(std::set &setFilesToPrune, +static void FindFilesToPruneManual(ChainstateManager &chainman, + std::set &setFilesToPrune, int nManualPruneHeight); -static void FindFilesToPrune(std::set &setFilesToPrune, +static void FindFilesToPrune(ChainstateManager &chainman, + std::set &setFilesToPrune, uint64_t nPruneAfterHeight); static uint32_t GetNextBlockScriptFlags(const Consensus::Params ¶ms, const CBlockIndex *pindex); @@ -1935,9 +1937,10 @@ if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) { if (nManualPruneHeight > 0) { - FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight); + FindFilesToPruneManual(g_chainman, setFilesToPrune, + nManualPruneHeight); } else { - FindFilesToPrune(setFilesToPrune, + FindFilesToPrune(g_chainman, setFilesToPrune, chainparams.PruneAfterHeight()); fCheckForPruning = false; } @@ -4327,13 +4330,11 @@ return retval; } -/** - * Prune a block file (modify associated database entries) - */ -void PruneOneBlockFile(const int fileNumber) { +void ChainstateManager::PruneOneBlockFile(const int fileNumber) { + AssertLockHeld(cs_main); LOCK(cs_LastBlockFile); - for (const auto &entry : g_chainman.BlockIndex()) { + for (const auto &entry : m_blockman.m_block_index) { CBlockIndex *pindex = entry.second; if (pindex->nFile == fileNumber) { pindex->nStatus = pindex->nStatus.withData(false).withUndo(false); @@ -4346,14 +4347,14 @@ // to be downloaded again in order to consider its chain, at which // point it would be considered as a candidate for // m_blocks_unlinked or setBlockIndexCandidates. - auto range = g_chainman.m_blockman.m_blocks_unlinked.equal_range( - pindex->pprev); + auto range = + m_blockman.m_blocks_unlinked.equal_range(pindex->pprev); while (range.first != range.second) { std::multimap::iterator _it = range.first; range.first++; if (_it->second == pindex) { - g_chainman.m_blockman.m_blocks_unlinked.erase(_it); + m_blockman.m_blocks_unlinked.erase(_it); } } } @@ -4376,7 +4377,8 @@ * Calculate the block/rev files to delete based on height specified by user * with RPC command pruneblockchain */ -static void FindFilesToPruneManual(std::set &setFilesToPrune, +static void FindFilesToPruneManual(ChainstateManager &chainman, + std::set &setFilesToPrune, int nManualPruneHeight) { assert(fPruneMode && nManualPruneHeight > 0); @@ -4396,7 +4398,7 @@ vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) { continue; } - PruneOneBlockFile(fileNumber); + chainman.PruneOneBlockFile(fileNumber); setFilesToPrune.insert(fileNumber); count++; } @@ -4436,7 +4438,8 @@ * @param[out] setFilesToPrune The set of file indices that can be unlinked * will be returned */ -static void FindFilesToPrune(std::set &setFilesToPrune, +static void FindFilesToPrune(ChainstateManager &chainman, + std::set &setFilesToPrune, uint64_t nPruneAfterHeight) { LOCK2(cs_main, cs_LastBlockFile); if (::ChainActive().Tip() == nullptr || nPruneTarget == 0) { @@ -4486,7 +4489,7 @@ continue; } - PruneOneBlockFile(fileNumber); + chainman.PruneOneBlockFile(fileNumber); // Queue up the files for removal setFilesToPrune.insert(fileNumber); nCurrentUsage -= nBytesToPrune; diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -93,7 +93,7 @@ // Prune the older block file. { LOCK(cs_main); - PruneOneBlockFile(oldTip->GetBlockPos().nFile); + EnsureChainman(m_node).PruneOneBlockFile(oldTip->GetBlockPos().nFile); } UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); @@ -123,7 +123,7 @@ // Prune the remaining block file. { LOCK(cs_main); - PruneOneBlockFile(newTip->GetBlockPos().nFile); + EnsureChainman(m_node).PruneOneBlockFile(newTip->GetBlockPos().nFile); } UnlinkPrunedFiles({newTip->GetBlockPos().nFile}); @@ -163,7 +163,7 @@ // Prune the older block file. { LOCK(cs_main); - PruneOneBlockFile(oldTip->GetBlockPos().nFile); + EnsureChainman(m_node).PruneOneBlockFile(oldTip->GetBlockPos().nFile); } UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});