Compare commits

...

2 Commits

Author SHA1 Message Date
Martin Zumsande
4ff54f97d6
Merge 5c5f88b80ad783f66cd0fe314bf657e128ddfa80 into cd8089c20baaaee329cbf7951195953a9f86d7cf 2025-03-16 17:18:20 +01:00
Martin Zumsande
5c5f88b80a validation: Do less work in NeedsRedownload
Iterating over hundred thousands of block indexes
with each startup doesn't seem necessary more than 7 years after segwit
activation. Only check the first segwit block instead.
This is less thorough, but the most typical case of upgrading a non-segwit client
that has synced beyond the segwit height will still lead to an error.
2025-01-22 16:53:59 -05:00
2 changed files with 9 additions and 15 deletions

View File

@ -177,9 +177,7 @@ public:
//! Verification status of this block. See enum BlockStatus
//!
//! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot
//! load to avoid a spurious startup failure requiring -reindex.
//! @sa NeedsRedownload
//! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot load.
//! @sa ActivateSnapshot
uint32_t nStatus GUARDED_BY(::cs_main){0};

View File

@ -2457,7 +2457,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
// may have let in a block that violates the rule prior to updating the
// software, and we would NOT be enforcing the rule here. Fully solving
// upgrade from one software version to the next after a consensus rule
// change is potentially tricky and issue-specific (see NeedsRedownload()
// change is potentially tricky and issue-specific (see historical versions of NeedsRedownload()
// for one approach that was used for BIP 141 deployment).
// Also, currently the rule against blocks more than 2 hours in the future
// is enforced in ContextualCheckBlockHeader(); we wouldn't want to
@ -4977,16 +4977,12 @@ bool Chainstate::NeedsRedownload() const
AssertLockHeld(cs_main);
// At and above m_params.SegwitHeight, segwit consensus rules must be validated
CBlockIndex* block{m_chain.Tip()};
while (block != nullptr && DeploymentActiveAt(*block, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
if (!(block->nStatus & BLOCK_OPT_WITNESS)) {
// block is insufficiently validated for a segwit client
return true;
}
block = block->pprev;
int segwit_height{m_chainman.GetConsensus().DeploymentHeight(Consensus::DEPLOYMENT_SEGWIT)};
CBlockIndex* block{m_chain[segwit_height]};
if (block && !(block->nStatus & BLOCK_OPT_WITNESS)) {
// block is insufficiently validated for a segwit client
return true;
}
return false;
}
@ -6056,8 +6052,8 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height(); ++i) {
index = snapshot_chainstate.m_chain[i];
// Fake BLOCK_OPT_WITNESS so that Chainstate::NeedsRedownload()
// won't ask for -reindex on startup.
// Set BLOCK_OPT_WITNESS, which would normally be done in ReceivedBlockTransactions
// if we had downloaded the block.
if (DeploymentActiveAt(*index, *this, Consensus::DEPLOYMENT_SEGWIT)) {
index->nStatus |= BLOCK_OPT_WITNESS;
}