Skip to content

Commit 7005766

Browse files
committed
Merge bitcoin/bitcoin#29299: validation: fix misleading checkblockindex comments
9819db4 validation: move nChainTx assert down in CheckBlockIndex (Martin Zumsande) 033477d doc: fix checkblockindex comments (Martin Zumsande) Pull request description: The two assumptions there were described as test-only, which has led to confusion whether they should exist. However, they are necessary in general, as the changed comment explains - without them, the check would fail everywhere where it is enabled. The second commit moves this assert down to the other checks. Closes #29261 ACKs for top commit: maflcko: ACK 9819db4 🌦 naumenkogs: ACK 9819db4 ryanofsky: Code review ACK 9819db4. Thanks for figuring this issue out and fixing it. Would suggest changing pr name from "improve comments" to "fix misleading comments" since previous comments were wrong about the reasons the conditions are needed. Tree-SHA512: 3f77791253eb0c97f8153dd8ae1c567f43f6387ea7a53efea94817463c672a4e11d548aa7eff62235346ff0713ff4d6fe08f9ec50d0c30a1e6b6d27b9918b419
2 parents 78c06a3 + 9819db4 commit 7005766

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/validation.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -4862,16 +4862,6 @@ void ChainstateManager::CheckBlockIndex()
48624862
CBlockIndex* pindexFirstAssumeValid = nullptr; // Oldest ancestor of pindex which has BLOCK_ASSUMED_VALID
48634863
while (pindex != nullptr) {
48644864
nNodes++;
4865-
// Make sure nChainTx sum is correctly computed.
4866-
unsigned int prev_chain_tx = pindex->pprev ? pindex->pprev->nChainTx : 0;
4867-
assert((pindex->nChainTx == pindex->nTx + prev_chain_tx)
4868-
// For testing, allow transaction counts to be completely unset.
4869-
|| (pindex->nChainTx == 0 && pindex->nTx == 0)
4870-
// For testing, allow this nChainTx to be unset if previous is also unset.
4871-
|| (pindex->nChainTx == 0 && prev_chain_tx == 0 && pindex->pprev)
4872-
// Transaction counts prior to snapshot are unknown.
4873-
|| pindex->IsAssumedValid());
4874-
48754865
if (pindexFirstAssumeValid == nullptr && pindex->nStatus & BLOCK_ASSUMED_VALID) pindexFirstAssumeValid = pindex;
48764866
if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
48774867
if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
@@ -4954,6 +4944,15 @@ void ChainstateManager::CheckBlockIndex()
49544944
// Checks for not-invalid blocks.
49554945
assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
49564946
}
4947+
// Make sure nChainTx sum is correctly computed.
4948+
unsigned int prev_chain_tx = pindex->pprev ? pindex->pprev->nChainTx : 0;
4949+
assert((pindex->nChainTx == pindex->nTx + prev_chain_tx)
4950+
// Transaction may be completely unset - happens if only the header was accepted but the block hasn't been processed.
4951+
|| (pindex->nChainTx == 0 && pindex->nTx == 0)
4952+
// nChainTx may be unset, but nTx set (if a block has been accepted, but one of its predecessors hasn't been processed yet)
4953+
|| (pindex->nChainTx == 0 && prev_chain_tx == 0 && pindex->pprev)
4954+
// Transaction counts prior to snapshot are unknown.
4955+
|| pindex->IsAssumedValid());
49574956
// Chainstate-specific checks on setBlockIndexCandidates
49584957
for (auto c : GetAll()) {
49594958
if (c->m_chain.Tip() == nullptr) continue;

0 commit comments

Comments
 (0)