validation: Add more checks to CheckBlockIndex()

This adds checks that
1) Descendants of invalid block indexes are also marked invalid
2) m_best_header cannot be invalid, and there can be no valid
block with more work than it.
This commit is contained in:
Martin Zumsande
2024-12-02 11:22:26 -05:00
parent 9a70883002
commit ed764ea2b4

View File

@@ -5305,6 +5305,7 @@ void ChainstateManager::CheckBlockIndex()
// are not yet validated.
CChain best_hdr_chain;
assert(m_best_header);
assert(!(m_best_header->nStatus & BLOCK_FAILED_MASK));
best_hdr_chain.SetTip(*m_best_header);
std::multimap<CBlockIndex*,CBlockIndex*> forward;
@@ -5418,6 +5419,8 @@ void ChainstateManager::CheckBlockIndex()
if (pindexFirstInvalid == nullptr) {
// Checks for not-invalid blocks.
assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
} else {
assert(pindex->nStatus & BLOCK_FAILED_MASK); // Invalid blocks and their descendants must be marked as invalid
}
// Make sure m_chain_tx_count sum is correctly computed.
if (!pindex->pprev) {
@@ -5431,6 +5434,8 @@ void ChainstateManager::CheckBlockIndex()
// block, and must be set if it is.
assert((pindex->m_chain_tx_count != 0) == (pindex == snap_base));
}
// There should be no block with more work than m_best_header, unless it's known to be invalid
assert((pindex->nStatus & BLOCK_FAILED_MASK) || pindex->nChainWork <= m_best_header->nChainWork);
// Chainstate-specific checks on setBlockIndexCandidates
for (auto c : GetAll()) {