From 61a51eccbba1e7ccbdaac2bb7c74503bcf6fc9a5 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Tue, 6 May 2025 14:24:56 +0100 Subject: [PATCH] validation: don't use GetAll() in CheckBlockIndex() GetAll() is non-const, preventing CheckBlockIndex() from being const. Rather than add a const GetAll() method, just iterate over the chainstates directly. Slight behaviour change by also subjecting non-`IsUsable()` chainstates to consistency checks. --- src/validation.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index cd53727d250..a9bde737401 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5354,8 +5354,8 @@ void ChainstateManager::CheckBlockIndex() if (pindex->pprev == nullptr) { // Genesis block checks. assert(pindex->GetBlockHash() == GetConsensus().hashGenesisBlock); // Genesis block's hash must match. - for (auto c : GetAll()) { - if (c->m_chain.Genesis() != nullptr) { + for (const Chainstate* c : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) { + if (c && c->m_chain.Genesis() != nullptr) { assert(pindex == c->m_chain.Genesis()); // The chain's genesis block must be this block. } } @@ -5408,8 +5408,8 @@ void ChainstateManager::CheckBlockIndex() } // Chainstate-specific checks on setBlockIndexCandidates - for (auto c : GetAll()) { - if (c->m_chain.Tip() == nullptr) continue; + for (const Chainstate* c : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) { + if (!c || c->m_chain.Tip() == nullptr) continue; // Two main factors determine whether pindex is a candidate in // setBlockIndexCandidates: // @@ -5492,7 +5492,8 @@ void ChainstateManager::CheckBlockIndex() // tip. // So if this block is itself better than any m_chain.Tip() and it wasn't in // setBlockIndexCandidates, then it must be in m_blocks_unlinked. - for (auto c : GetAll()) { + for (const Chainstate* c : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) { + if (!c) continue; const bool is_active = c == &ActiveChainstate(); if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && c->setBlockIndexCandidates.count(pindex) == 0) { if (pindexFirstInvalid == nullptr) {