Skip to content

Commit 73700fb

Browse files
fjahrryanofsky
andcommitted
validation, test: Improve and document nChainTx check for testability
Co-authored-by: Ryan Ofsky <[email protected]>
1 parent 2c9354f commit 73700fb

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/kernel/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class CRegTestParams : public CChainParams
495495
{
496496
.height = 110,
497497
.hash_serialized = AssumeutxoHash{uint256S("0x1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618")},
498-
.nChainTx = 110,
498+
.nChainTx = 111,
499499
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
500500
},
501501
{

src/test/validation_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ BOOST_AUTO_TEST_CASE(test_assumeutxo)
138138

139139
const auto out110 = *params->AssumeutxoForHeight(110);
140140
BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
141-
BOOST_CHECK_EQUAL(out110.nChainTx, 110U);
141+
BOOST_CHECK_EQUAL(out110.nChainTx, 111U);
142142

143143
const auto out110_2 = *params->AssumeutxoForBlockhash(uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"));
144144
BOOST_CHECK_EQUAL(out110_2.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
145-
BOOST_CHECK_EQUAL(out110_2.nChainTx, 110U);
145+
BOOST_CHECK_EQUAL(out110_2.nChainTx, 111U);
146146
}
147147

148148
BOOST_AUTO_TEST_SUITE_END()

src/validation.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4844,10 +4844,14 @@ void ChainstateManager::CheckBlockIndex()
48444844
CBlockIndex* pindexFirstAssumeValid = nullptr; // Oldest ancestor of pindex which has BLOCK_ASSUMED_VALID
48454845
while (pindex != nullptr) {
48464846
nNodes++;
4847-
if (pindex->pprev && pindex->nTx > 0) {
4848-
// nChainTx should increase monotonically
4849-
assert(pindex->pprev->nChainTx <= pindex->nChainTx);
4850-
}
4847+
// Make sure nChainTx sum is correctly computed.
4848+
unsigned int prev_chain_tx = pindex->pprev ? pindex->pprev->nChainTx : 0;
4849+
assert((pindex->nChainTx == pindex->nTx + prev_chain_tx)
4850+
// For testing, allow transaction counts to be completely unset.
4851+
|| (pindex->nChainTx == 0 && pindex->nTx == 0)
4852+
// For testing, allow this nChainTx to be unset if previous is also unset.
4853+
|| (pindex->nChainTx == 0 && prev_chain_tx == 0 && pindex->pprev));
4854+
48514855
if (pindexFirstAssumeValid == nullptr && pindex->nStatus & BLOCK_ASSUMED_VALID) pindexFirstAssumeValid = pindex;
48524856
if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
48534857
if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) {

0 commit comments

Comments
 (0)