Skip to content

Commit 504d0c2

Browse files
committed
Merge bitcoin/bitcoin#31439: validation: In case of a continued reindex, only activate chain in the end
c9136ca validation: fix issue with an interrupted -reindex (Martin Zumsande) a267589 validation: Don't loop over all chainstates in LoadExternalBlock (Martin Zumsande) Pull request description: If a user interrupts a reindex while it is iterating over the block files, it will continue to reindex with the next node start (if the `-reindex` arg is dropped, otherwise it will start reindexing from scratch). However, due to an early call to `ActivateBestChainState()` that only exists to connect the genesis block during the original `-reindex`, it wil start connecting blocks immediately before having iterated through all block files. Because later headers above the minchainwork threshold won't be loaded in this case, `-assumevalid` will not be applied and the process is much slower due to script validation being done. Fix this by only calling `ActivateBestChainState()` here if Genesis is not connected yet (equivalent to `ActiveHeight() == -1`). Also simplify this spot by only doing this for the active chainstate instead of looping over all chainstates (first commit). This issue was discussed in the thread below bitcoin/bitcoin#31346 (comment), the impact on assumevalid was found by l0rinc. The fix can be tested by manually aborting a `-reindex` e.g. on signet and observing in the debug log the order in which blockfiles are indexed / blocks are connected with this branch vs master. ACKs for top commit: achow101: ACK c9136ca ryanofsky: Code review ACK c9136ca. Only comments changed since last review. Appreciate the new comments, I think they make a little clearer what things code is trying to do and what things are just side-effects. TheCharlatan: Re-ACK c9136ca Tree-SHA512: 6f34abc317ad7e605ccc0c2f4615e4ea6978223d207f80f768f39cc135a9ac0adf31681fadfa2aed45324a5d27a4f68c5e118ee7eec18ca5c40ef177caa9cc47
2 parents 0b48f77 + c9136ca commit 504d0c2

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/validation.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,16 +5169,15 @@ void ChainstateManager::LoadExternalBlockFile(
51695169
}
51705170

51715171
// Activate the genesis block so normal node progress can continue
5172-
if (hash == params.GetConsensus().hashGenesisBlock) {
5173-
bool genesis_activation_failure = false;
5174-
for (auto c : GetAll()) {
5175-
BlockValidationState state;
5176-
if (!c->ActivateBestChain(state, nullptr)) {
5177-
genesis_activation_failure = true;
5178-
break;
5179-
}
5180-
}
5181-
if (genesis_activation_failure) {
5172+
// During first -reindex, this will only connect Genesis since
5173+
// ActivateBestChain only connects blocks which are in the block tree db,
5174+
// which only contains blocks whose parents are in it.
5175+
// But do this only if genesis isn't activated yet, to avoid connecting many blocks
5176+
// without assumevalid in the case of a continuation of a reindex that
5177+
// was interrupted by the user.
5178+
if (hash == params.GetConsensus().hashGenesisBlock && WITH_LOCK(::cs_main, return ActiveHeight()) == -1) {
5179+
BlockValidationState state;
5180+
if (!ActiveChainstate().ActivateBestChain(state, nullptr)) {
51825181
break;
51835182
}
51845183
}

0 commit comments

Comments
 (0)