Skip to content

Commit bf5d225

Browse files
committed
add asserts in CheckBlockIndex, remove BLOCK_FAILED_(CHILD|MASK) in fuzz test
- also modify block_index_tree.cpp to add blocks
1 parent 675bfec commit bf5d225

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/test/fuzz/block_index_tree.cpp

+5-3
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];
@@ -54,18 +55,19 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
5455
// Receive a header building on an existing one. This assumes headers are valid, so PoW is not relevant here.
5556
LOCK(cs_main);
5657
CBlockIndex* prev_block = PickValue(fuzzed_data_provider, blocks);
57-
if (!(prev_block->nStatus & BLOCK_FAILED_MASK)) {
58+
if (!(prev_block->nStatus & BLOCK_FAILED_VALID)) {
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
[&] {
6466
// Receive a full block (valid or invalid) for an existing header, but don't attempt to connect it yet
6567
LOCK(cs_main);
6668
CBlockIndex* index = PickValue(fuzzed_data_provider, blocks);
6769
// Must be new to us and not known to be invalid (e.g. because of an invalid ancestor).
68-
if (index->nTx == 0 && !(index->nStatus & BLOCK_FAILED_MASK)) {
70+
if (index->nTx == 0 && !(index->nStatus & BLOCK_FAILED_VALID)) {
6971
if (fuzzed_data_provider.ConsumeBool()) { // Invalid
7072
BlockValidationState state;
7173
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "consensus-invalid");
@@ -118,7 +120,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
118120
}
119121
// Connect blocks, possibly fail
120122
for (CBlockIndex* block : to_connect | std::views::reverse) {
121-
assert(!(block->nStatus & BLOCK_FAILED_MASK));
123+
assert(!(block->nStatus & BLOCK_FAILED_VALID));
122124
assert(block->nStatus & BLOCK_HAVE_DATA);
123125
if (!block->IsValid(BLOCK_VALID_SCRIPTS)) {
124126
if (fuzzed_data_provider.ConsumeBool()) { // Invalid

src/validation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -5389,6 +5389,10 @@ void ChainstateManager::CheckBlockIndex()
53895389
if (pindexFirstInvalid == nullptr) {
53905390
// Checks for not-invalid blocks.
53915391
assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0); // The failed mask cannot be set for blocks without invalid parents.
5392+
} else {
5393+
// pindexFirstInvalid -> ...... -> pindex
5394+
// we check that descendants and block itself are BLOCK_FAILED_VALID
5395+
assert(pindex->nStatus & BLOCK_FAILED_VALID);
53925396
}
53935397
// Make sure m_chain_tx_count sum is correctly computed.
53945398
if (!pindex->pprev) {

0 commit comments

Comments
 (0)