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.
This commit is contained in:
stickies-v
2025-05-06 14:24:56 +01:00
parent d05481df64
commit 61a51eccbb

View File

@@ -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) {