refactor: use for loops in FindMostWorkChain
What changed, and why it matters
This commit is a straightforward code cleanup in Bitcoin Core. It rewrites two while loops as for loops in a function that selects the chain with the most proof-of-work. There is no change to program logic, no bug fix, and no security-relevant behavior change.
No security action needed. Treat as normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
In Chainstate::FindMostWorkChain(), the patch converts two while loops into equivalent for loops with loop variables declared in the for-init-statement. The loop bodies, conditions, and iteration steps are preserved exactly. The change is purely stylistic/refactoring and does not alter consensus logic, validation rules, or data structures.
Changed components
src/validation.cpp::Chainstate::FindMostWorkChainInspect captured patch +2 / −6
diff --git a/src/validation.cpp b/src/validation.cpp
index e0a8c1b3..ebad99f0 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3123,9 +3123,8 @@ CBlockIndex* Chainstate::FindMostWorkChain()
// Check whether all blocks on the path between the currently active chain and the candidate are valid.
// Just going until the active chain is an optimization, as we know all blocks in it are valid already.
- CBlockIndex *pindexTest = pindexNew;
bool fInvalidAncestor = false;
- while (pindexTest && !m_chain.Contains(pindexTest)) {
+ for (CBlockIndex *pindexTest = pindexNew; pindexTest && !m_chain.Contains(pindexTest); pindexTest = pindexTest->pprev) {
assert(pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0);
// Pruned nodes may have entries in setBlockIndexCandidates for
@@ -3139,9 +3138,8 @@ CBlockIndex* Chainstate::FindMostWorkChain()
if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork)) {
m_chainman.m_best_invalid = pindexNew;
}
- CBlockIndex *pindexFailed = pindexNew;
// Remove the entire chain from the set.
- while (pindexTest != pindexFailed) {
+ for (CBlockIndex *pindexFailed = pindexNew; pindexFailed != pindexTest; pindexFailed = pindexFailed->pprev) {
if (fMissingData && !fFailedChain) {
// If we're missing data and not a descendant of an invalid block,
// then add back to m_blocks_unlinked, so that if the block arrives in the future
@@ -3150,13 +3148,11 @@ CBlockIndex* Chainstate::FindMostWorkChain()
std::make_pair(pindexFailed->pprev, pindexFailed));
}
setBlockIndexCandidates.erase(pindexFailed);
- pindexFailed = pindexFailed->pprev;
}
setBlockIndexCandidates.erase(pindexTest);
fInvalidAncestor = true;
break;
}
- pindexTest = pindexTest->pprev;
}
if (!fInvalidAncestor)
return pindexNew;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.