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.
This commit is contained in:
Martin Zumsande 2024-12-06 11:49:10 -05:00
parent a2675897e2
commit c9136ca906

View file

@ -5157,7 +5157,13 @@ void ChainstateManager::LoadExternalBlockFile(
}
// Activate the genesis block so normal node progress can continue
if (hash == params.GetConsensus().hashGenesisBlock) {
// 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;