0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

Merge bitcoin/bitcoin#31439: validation: In case of a continued reindex, only activate chain in the end

c9136ca906 validation: fix issue with an interrupted -reindex (Martin Zumsande)
a2675897e2 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 https://github.com/bitcoin/bitcoin/pull/31346#discussion_r1856824817, 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 c9136ca906
  ryanofsky:
    Code review ACK c9136ca906. 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 c9136ca906

Tree-SHA512: 6f34abc317ad7e605ccc0c2f4615e4ea6978223d207f80f768f39cc135a9ac0adf31681fadfa2aed45324a5d27a4f68c5e118ee7eec18ca5c40ef177caa9cc47
This commit is contained in:
Ava Chow 2025-02-14 13:59:34 -08:00
commit 504d0c21e2
No known key found for this signature in database
GPG key ID: 17565732E08E5E41

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;
}
}