Compare commits

...

3 commits

Author SHA1 Message Date
Martin Zumsande
12a731db20
Merge c9136ca906 into 433412fd84 2025-01-07 01:12:07 +01:00
Martin Zumsande
c9136ca906 validation: fix issue with an interrupted -reindex
If a reindex was interrupted while it was iterating
through the block files, genesis will already be connected
when the reindex resumes at the next startup.
In this case, a call to ActivateBestChainState() is not only unnecessary,
but it would connect multiple blocks without applying
-assumevalid, which is much slower.
This is because assumevalid requires us to have a header above
the minimum chainwork, but that header is unknown to us if it's in
a later blockfile not indexed yet.
2024-12-17 11:35:07 -05:00
Martin Zumsande
a2675897e2 validation: Don't loop over all chainstates in LoadExternalBlock
This simplifies the code. The only reason to call ActivateBestChain()
here is to allow the main init thread to finish startup in a case of
-reindex. In this situation no second chainstate can exist anyway
because -reindex would have deleted any snapshot chainstate earlier.

This could change behavior slightly if -loadblocks was used when there is a
snapshot chainstate. In this case, there is no reason to call
ActivateBestChain() for that chainstate here - it will be called in
ImportBlocks() after all blocks have been indexed.
2024-12-17 11:34:29 -05:00

View file

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