Skip to content

Commit a0bfc69

Browse files
committed
Merge pull request #5959
ede379f Add additional block index consistency checks (Suhas Daftuar)
2 parents 64b263c + ede379f commit a0bfc69

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,13 +3248,15 @@ void static CheckBlockIndex()
32483248
CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
32493249
CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
32503250
CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
3251+
CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
32513252
CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
32523253
CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
32533254
while (pindex != NULL) {
32543255
nNodes++;
32553256
if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
32563257
if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
32573258
if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
3259+
if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
32583260
if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
32593261
if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
32603262

@@ -3264,7 +3266,12 @@ void static CheckBlockIndex()
32643266
assert(pindex->GetBlockHash() == Params().HashGenesisBlock()); // Genesis block's hash must match.
32653267
assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
32663268
}
3269+
// HAVE_DATA is equivalent to VALID_TRANSACTIONS and equivalent to nTx > 0 (we stored the number of transactions in the block)
3270+
assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
3271+
assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0));
3272+
// All parents having data is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
32673273
assert((pindexFirstMissing != NULL) == (pindex->nChainTx == 0)); // nChainTx == 0 is used to signal that all parent block's transaction data is available.
3274+
assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
32683275
assert(pindex->nHeight == nHeight); // nHeight must be consistent.
32693276
assert(pindex->pprev == NULL || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
32703277
assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
@@ -3320,6 +3327,7 @@ void static CheckBlockIndex()
33203327
if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
33213328
if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
33223329
if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
3330+
if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
33233331
if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
33243332
if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
33253333
// Find our parent.

0 commit comments

Comments
 (0)