Skip to content

Commit 3e6ac5b

Browse files
committed
refactor: validation: mark CheckBlockIndex as const
As a check/test method, this function should not mutate logical state. Mark it as const to better help ensure this.
1 parent 61a51ec commit 3e6ac5b

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/validation.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,7 +5257,7 @@ bool ChainstateManager::ShouldCheckBlockIndex() const
52575257
return true;
52585258
}
52595259

5260-
void ChainstateManager::CheckBlockIndex()
5260+
void ChainstateManager::CheckBlockIndex() const
52615261
{
52625262
if (!ShouldCheckBlockIndex()) {
52635263
return;
@@ -5282,7 +5282,7 @@ void ChainstateManager::CheckBlockIndex()
52825282
assert(m_best_header);
52835283
best_hdr_chain.SetTip(*m_best_header);
52845284

5285-
std::multimap<CBlockIndex*,CBlockIndex*> forward;
5285+
std::multimap<const CBlockIndex*, const CBlockIndex*> forward;
52865286
for (auto& [_, block_index] : m_blockman.m_block_index) {
52875287
// Only save indexes in forward that are not part of the best header chain.
52885288
if (!best_hdr_chain.Contains(&block_index)) {
@@ -5293,27 +5293,27 @@ void ChainstateManager::CheckBlockIndex()
52935293
}
52945294
assert(forward.size() + best_hdr_chain.Height() + 1 == m_blockman.m_block_index.size());
52955295

5296-
CBlockIndex* pindex = best_hdr_chain[0];
5296+
const CBlockIndex* pindex = best_hdr_chain[0];
52975297
assert(pindex);
52985298
// Iterate over the entire block tree, using depth-first search.
52995299
// Along the way, remember whether there are blocks on the path from genesis
53005300
// block being explored which are the first to have certain properties.
53015301
size_t nNodes = 0;
53025302
int nHeight = 0;
5303-
CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
5304-
CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA, since assumeutxo snapshot if used.
5305-
CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0, since assumeutxo snapshot if used.
5306-
CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
5307-
CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not), since assumeutxo snapshot if used.
5308-
CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not), since assumeutxo snapshot if used.
5309-
CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not), since assumeutxo snapshot if used.
5303+
const CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
5304+
const CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA, since assumeutxo snapshot if used.
5305+
const CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0, since assumeutxo snapshot if used.
5306+
const CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
5307+
const CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not), since assumeutxo snapshot if used.
5308+
const CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not), since assumeutxo snapshot if used.
5309+
const CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not), since assumeutxo snapshot if used.
53105310

53115311
// After checking an assumeutxo snapshot block, reset pindexFirst pointers
53125312
// to earlier blocks that have not been downloaded or validated yet, so
53135313
// checks for later blocks can assume the earlier blocks were validated and
53145314
// be stricter, testing for more requirements.
53155315
const CBlockIndex* snap_base{GetSnapshotBaseBlock()};
5316-
CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
5316+
const CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
53175317
auto snap_update_firsts = [&] {
53185318
if (pindex == snap_base) {
53195319
std::swap(snap_first_missing, pindexFirstMissing);
@@ -5453,19 +5453,19 @@ void ChainstateManager::CheckBlockIndex()
54535453
// pindex only needs to be added if it is an ancestor of
54545454
// the snapshot that is being validated.
54555455
if (c == &ActiveChainstate() || snap_base->GetAncestor(pindex->nHeight) == pindex) {
5456-
assert(c->setBlockIndexCandidates.count(pindex));
5456+
assert(c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex)));
54575457
}
54585458
}
54595459
// If some parent is missing, then it could be that this block was in
54605460
// setBlockIndexCandidates but had to be removed because of the missing data.
54615461
// In this case it must be in m_blocks_unlinked -- see test below.
54625462
}
54635463
} else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
5464-
assert(c->setBlockIndexCandidates.count(pindex) == 0);
5464+
assert(!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex)));
54655465
}
54665466
}
54675467
// Check whether this block is in m_blocks_unlinked.
5468-
std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = m_blockman.m_blocks_unlinked.equal_range(pindex->pprev);
5468+
auto rangeUnlinked{m_blockman.m_blocks_unlinked.equal_range(pindex->pprev)};
54695469
bool foundInUnlinked = false;
54705470
while (rangeUnlinked.first != rangeUnlinked.second) {
54715471
assert(rangeUnlinked.first->first == pindex->pprev);
@@ -5495,7 +5495,7 @@ void ChainstateManager::CheckBlockIndex()
54955495
for (const Chainstate* c : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
54965496
if (!c) continue;
54975497
const bool is_active = c == &ActiveChainstate();
5498-
if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && c->setBlockIndexCandidates.count(pindex) == 0) {
5498+
if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && !c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))) {
54995499
if (pindexFirstInvalid == nullptr) {
55005500
if (is_active || snap_base->GetAncestor(pindex->nHeight) == pindex) {
55015501
assert(foundInUnlinked);
@@ -5510,7 +5510,7 @@ void ChainstateManager::CheckBlockIndex()
55105510

55115511
// Try descending into the first subnode. Always process forks first and the best header chain after.
55125512
snap_update_firsts();
5513-
std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
5513+
auto range{forward.equal_range(pindex)};
55145514
if (range.first != range.second) {
55155515
// A subnode not part of the best header chain was found.
55165516
pindex = range.first->second;
@@ -5539,7 +5539,7 @@ void ChainstateManager::CheckBlockIndex()
55395539
// Find our parent.
55405540
CBlockIndex* pindexPar = pindex->pprev;
55415541
// Find which child we just visited.
5542-
std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
5542+
auto rangePar{forward.equal_range(pindexPar)};
55435543
while (rangePar.first->second != pindex) {
55445544
assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
55455545
rangePar.first++;

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ class ChainstateManager
985985
*
986986
* By default this only executes fully when using the Regtest chain; see: m_options.check_block_index.
987987
*/
988-
void CheckBlockIndex();
988+
void CheckBlockIndex() const;
989989

990990
/**
991991
* Alias for ::cs_main.

0 commit comments

Comments
 (0)