Skip to content

Commit d45e82f

Browse files
committed
add asserts in CheckBlockIndex, fix SetBlockFailureFlags
- also modify block_index_tree.cpp to add blocks
1 parent 98d68e0 commit d45e82f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/test/fuzz/block_index_tree.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void initialize_block_index_tree()
3939
FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
4040
{
4141
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
42+
SetMockTime(ConsumeTime(fuzzed_data_provider));
4243
ChainstateManager& chainman = *g_setup->m_node.chainman;
4344
auto& blockman = chainman.m_blockman;
4445
CBlockIndex* genesis = chainman.ActiveChainstate().m_chain[0];
@@ -58,6 +59,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
5859
CBlockHeader header = ConsumeBlockHeader(fuzzed_data_provider, prev_block->GetBlockHash(), nonce_counter);
5960
CBlockIndex* index = blockman.AddToBlockIndex(header, chainman.m_best_header);
6061
assert(index->nStatus & BLOCK_VALID_TREE);
62+
blocks.push_back(index);
6163
}
6264
},
6365
[&] {

src/validation.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -3826,8 +3826,8 @@ void Chainstate::SetBlockFailureFlags(CBlockIndex* invalid_block)
38263826
AssertLockHeld(cs_main);
38273827

38283828
for (auto& [_, block_index] : m_blockman.m_block_index) {
3829-
if (block_index.GetAncestor(invalid_block->nHeight) == invalid_block && !(block_index.nStatus & BLOCK_FAILED_MASK)) {
3830-
block_index.nStatus |= BLOCK_FAILED_CHILD;
3829+
if (&block_index != invalid_block && block_index.GetAncestor(invalid_block->nHeight) == invalid_block) {
3830+
block_index.nStatus = (block_index.nStatus & ~BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
38313831
}
38323832
}
38333833
}
@@ -5400,6 +5400,14 @@ void ChainstateManager::CheckBlockIndex()
54005400
// Checks for not-invalid blocks.
54015401
assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
54025402
}
5403+
else if (pindexFirstInvalid != pindex) {
5404+
// pindexFirstInvalid -> ...... -> pindex
5405+
// we check that descendants are BLOCK_FAILED_CHILD and not BLOCK_FAILED_VALID
5406+
assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0);
5407+
assert(pindex->nStatus & BLOCK_FAILED_CHILD);
5408+
} else {
5409+
assert(pindex->nStatus & BLOCK_FAILED_VALID);
5410+
}
54035411
// Make sure m_chain_tx_count sum is correctly computed.
54045412
if (!pindex->pprev) {
54055413
// If no previous block, nTx and m_chain_tx_count must be the same.

0 commit comments

Comments
 (0)