diff options
author | furszy <matiasfurszyfer@protonmail.com> | 2023-05-16 19:19:06 -0300 |
---|---|---|
committer | furszy <matiasfurszyfer@protonmail.com> | 2023-07-10 10:50:50 -0300 |
commit | 2ec89f1970935d27631bcd17b7923a79cdb1edbb (patch) | |
tree | 387c97e5187eb1a6cfdf8b80838f62b2507b561d /src/index | |
parent | c82ef91eae38f385d408b095ebbc8a180ce52ebb (diff) |
refactor: simplify pruning violation check
By generalizing 'GetFirstStoredBlock' and implementing
'CheckBlockDataAvailability' we can dedup code and
avoid repeating work when multiple indexes are enabled.
E.g. get the oldest block across all indexes and
perform the pruning violation check from that point
up to the tip only once (this feature is being introduced
in a follow-up commit).
This commit shouldn't change behavior in any way.
Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
Diffstat (limited to 'src/index')
-rw-r--r-- | src/index/base.cpp | 46 |
1 files changed, 16 insertions, 30 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp index ef3ba72cad..8accc2a6a4 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -111,37 +111,23 @@ bool BaseIndex::Init() const CBlockIndex* start_block = m_best_block_index.load(); bool synced = start_block == active_chain.Tip(); if (!synced && g_indexes_ready_to_sync) { - bool prune_violation = false; - if (!start_block) { - // index is not built yet - // make sure we have all block data back to the genesis - bool has_tip_data = active_chain.Tip()->nStatus & BLOCK_HAVE_DATA; - prune_violation = !has_tip_data || m_chainstate->m_blockman.GetFirstStoredBlock(*active_chain.Tip()) != active_chain.Genesis(); + const CBlockIndex* block_to_test = start_block ? start_block : active_chain.Genesis(); + + // Assert block_to_test is not null here. It can't be null because the + // genesis block can't be null here. The genesis block will be null + // during this BaseIndex::Init() call if the node is being started for + // the first time, or if -reindex is used. But in both of these cases + // m_best_block_index is also null so this branch is not reached. + assert(block_to_test); + + if (!active_chain.Contains(block_to_test)) { + // if the bestblock is not part of the mainchain, find the fork + // so we can make sure we have all data down to the fork + block_to_test = active_chain.FindFork(block_to_test); } - // 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 - else { - const CBlockIndex* block_to_test = start_block; - if (!active_chain.Contains(block_to_test)) { - // if the bestblock is not part of the mainchain, find the fork - // and make sure we have all data down to the fork - block_to_test = active_chain.FindFork(block_to_test); - } - const CBlockIndex* block = active_chain.Tip(); - prune_violation = true; - // check backwards from the tip if we have all block data until we reach the indexes bestblock - while (block_to_test && block && (block->nStatus & BLOCK_HAVE_DATA)) { - if (block_to_test == block) { - prune_violation = false; - break; - } - // block->pprev must exist at this point, since block_to_test is part of the chain - // and thus must be encountered when going backwards from the tip - assert(block->pprev); - block = block->pprev; - } - } - if (prune_violation) { + + // make sure we have all block data back to the start block + if (!m_chainstate->m_blockman.CheckBlockDataAvailability(*active_chain.Tip(), *Assert(block_to_test))) { return InitError(strprintf(Untranslated("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"), GetName())); } } |