0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

refactor: Introduce GetFirstStoredBlock helper function

This commit is contained in:
Fabian Jahr 2021-04-18 16:48:52 +02:00
parent 1e7db37e76
commit 231fc7b035
No known key found for this signature in database
GPG key ID: F13D1E9D890798CD
4 changed files with 18 additions and 14 deletions

View file

@ -75,11 +75,7 @@ bool BaseIndex::Init()
if (!m_best_block_index) { if (!m_best_block_index) {
// index is not built yet // index is not built yet
// make sure we have all block data back to the genesis // make sure we have all block data back to the genesis
const CBlockIndex* block = active_chain.Tip(); prune_violation = node::GetFirstStoredBlock(active_chain.Tip()) != active_chain.Genesis();
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
block = block->pprev;
}
prune_violation = block != active_chain.Genesis();
} }
// in case the index has a best block set and is not fully synced // in case the index has a best block set and is not fully synced
// check if we have the required blocks to continue building the index // check if we have the required blocks to continue building the index

View file

@ -397,6 +397,16 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0); return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
} }
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) {
AssertLockHeld(::cs_main);
assert(start_block);
const CBlockIndex* last_block = start_block;
while (last_block->pprev && (last_block->pprev->nStatus & BLOCK_HAVE_DATA)) {
last_block = last_block->pprev;
}
return last_block;
}
// If we're using -prune with -reindex, then delete block files that will be ignored by the // If we're using -prune with -reindex, then delete block files that will be ignored by the
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile // reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
// is missing, do the same here to delete any later block files after a gap. Also delete all // is missing, do the same here to delete any later block files after a gap. Also delete all

View file

@ -181,6 +181,9 @@ public:
} }
}; };
//! Find the first block that is not pruned
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
void CleanupBlockRevFiles(); void CleanupBlockRevFiles();
/** Open a block file (blk?????.dat) */ /** Open a block file (blk?????.dat) */

View file

@ -787,10 +787,9 @@ static RPCHelpMan pruneblockchain()
PruneBlockFilesManual(active_chainstate, height); PruneBlockFilesManual(active_chainstate, height);
const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip()); const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip());
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { const CBlockIndex* last_block = node::GetFirstStoredBlock(block);
block = block->pprev;
} return static_cast<uint64_t>(last_block->nHeight);
return uint64_t(block->nHeight);
}, },
}; };
} }
@ -1217,11 +1216,7 @@ RPCHelpMan getblockchaininfo()
obj.pushKV("pruned", node::fPruneMode); obj.pushKV("pruned", node::fPruneMode);
if (node::fPruneMode) { if (node::fPruneMode) {
const CBlockIndex* block = CHECK_NONFATAL(tip); const CBlockIndex* block = CHECK_NONFATAL(tip);
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { obj.pushKV("pruneheight", node::GetFirstStoredBlock(block)->nHeight);
block = block->pprev;
}
obj.pushKV("pruneheight", block->nHeight);
// if 0, execution bypasses the whole if block. // if 0, execution bypasses the whole if block.
bool automatic_pruning{args.GetIntArg("-prune", 0) != 1}; bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};