0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00

validation: Move PruneOneBlockFile to BlockManager

[META] This is a pure refactor commit.

Move PruneBlockFile to BlockManager because:
1. PruneOneBlockFile only acts on BlockManager
2. Eliminates the need for callers (FindFilesToPrune{,Manual}) to have a
   reference to the larger ChainstateManager, just a reference to
   BlockManager is enough. See following commits.
This commit is contained in:
Carl Dong 2020-08-25 19:05:50 -04:00
parent 74f73c783d
commit f8d4975ab3
No known key found for this signature in database
GPG key ID: 0CC52153197991A5
3 changed files with 12 additions and 12 deletions

View file

@ -3912,12 +3912,12 @@ uint64_t CalculateCurrentUsage()
return retval; return retval;
} }
void ChainstateManager::PruneOneBlockFile(const int fileNumber) void BlockManager::PruneOneBlockFile(const int fileNumber)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
for (const auto& entry : m_blockman.m_block_index) { for (const auto& entry : m_block_index) {
CBlockIndex* pindex = entry.second; CBlockIndex* pindex = entry.second;
if (pindex->nFile == fileNumber) { if (pindex->nFile == fileNumber) {
pindex->nStatus &= ~BLOCK_HAVE_DATA; pindex->nStatus &= ~BLOCK_HAVE_DATA;
@ -3931,12 +3931,12 @@ void ChainstateManager::PruneOneBlockFile(const int fileNumber)
// to be downloaded again in order to consider its chain, at which // to be downloaded again in order to consider its chain, at which
// point it would be considered as a candidate for // point it would be considered as a candidate for
// m_blocks_unlinked or setBlockIndexCandidates. // m_blocks_unlinked or setBlockIndexCandidates.
auto range = m_blockman.m_blocks_unlinked.equal_range(pindex->pprev); auto range = m_blocks_unlinked.equal_range(pindex->pprev);
while (range.first != range.second) { while (range.first != range.second) {
std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first; std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
range.first++; range.first++;
if (_it->second == pindex) { if (_it->second == pindex) {
m_blockman.m_blocks_unlinked.erase(_it); m_blocks_unlinked.erase(_it);
} }
} }
} }
@ -3972,7 +3972,7 @@ static void FindFilesToPruneManual(ChainstateManager& chainman, std::set<int>& s
for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) { for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
continue; continue;
chainman.PruneOneBlockFile(fileNumber); chainman.m_blockman.PruneOneBlockFile(fileNumber);
setFilesToPrune.insert(fileNumber); setFilesToPrune.insert(fileNumber);
count++; count++;
} }
@ -4047,7 +4047,7 @@ static void FindFilesToPrune(ChainstateManager& chainman, std::set<int>& setFile
if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
continue; continue;
chainman.PruneOneBlockFile(fileNumber); chainman.m_blockman.PruneOneBlockFile(fileNumber);
// Queue up the files for removal // Queue up the files for removal
setFilesToPrune.insert(fileNumber); setFilesToPrune.insert(fileNumber);
nCurrentUsage -= nBytesToPrune; nCurrentUsage -= nBytesToPrune;

View file

@ -407,6 +407,9 @@ public:
/** Create a new block index entry for a given block hash */ /** Create a new block index entry for a given block hash */
CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main); CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
//! Mark one block file as pruned (modify associated database entries)
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** /**
* If a block header hasn't already been seen, call CheckBlockHeader on it, ensure * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
* that it doesn't descend from an invalid block, and then add it to m_block_index. * that it doesn't descend from an invalid block, and then add it to m_block_index.
@ -903,9 +906,6 @@ public:
*/ */
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, BlockValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main); bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, BlockValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
//! 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 //! Load the block tree and coins database from disk, initializing state if we're running with -reindex
bool LoadBlockIndex(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool LoadBlockIndex(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

View file

@ -126,7 +126,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
// Prune the older block file. // Prune the older block file.
{ {
LOCK(cs_main); LOCK(cs_main);
Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile); Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
} }
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
@ -152,7 +152,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
// Prune the remaining block file. // Prune the remaining block file.
{ {
LOCK(cs_main); LOCK(cs_main);
Assert(m_node.chainman)->PruneOneBlockFile(newTip->GetBlockPos().nFile); Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(newTip->GetBlockPos().nFile);
} }
UnlinkPrunedFiles({newTip->GetBlockPos().nFile}); UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
@ -189,7 +189,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
// Prune the older block file. // Prune the older block file.
{ {
LOCK(cs_main); LOCK(cs_main);
Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile); Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
} }
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});