Bitcoin Core  25.99.0
P2P Digital Currency
partially_downloaded_block.cpp
Go to the documentation of this file.
1 #include <blockencodings.h>
2 #include <consensus/merkle.h>
3 #include <consensus/validation.h>
4 #include <primitives/block.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
10 #include <test/util/setup_common.h>
11 #include <test/util/txmempool.h>
12 #include <txmempool.h>
13 
14 #include <cstddef>
15 #include <cstdint>
16 #include <limits>
17 #include <memory>
18 #include <optional>
19 #include <set>
20 #include <vector>
21 
22 namespace {
23 const TestingSetup* g_setup;
24 } // namespace
25 
27 {
28  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
29  g_setup = testing_setup.get();
30 }
31 
32 PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional<BlockValidationResult> result)
33 {
34  return [result](const CBlock&, BlockValidationState& state, const Consensus::Params&, bool, bool) {
35  if (result) {
36  return state.Invalid(*result);
37  }
38 
39  return true;
40  };
41 }
42 
43 FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
44 {
45  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
46 
47  auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider)};
48  if (!block || block->vtx.size() == 0 ||
49  block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
50  return;
51  }
52 
53  CBlockHeaderAndShortTxIDs cmpctblock{*block};
54 
55  CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
56  PartiallyDownloadedBlock pdb{&pool};
57 
58  // Set of available transactions (mempool or extra_txn)
59  std::set<uint16_t> available;
60  // The coinbase is always available
61  available.insert(0);
62 
63  std::vector<std::pair<uint256, CTransactionRef>> extra_txn;
64  for (size_t i = 1; i < block->vtx.size(); ++i) {
65  auto tx{block->vtx[i]};
66 
67  bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
68  bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
69 
70  if (add_to_extra_txn) {
71  extra_txn.emplace_back(tx->GetWitnessHash(), tx);
72  available.insert(i);
73  }
74 
75  if (add_to_mempool) {
76  LOCK2(cs_main, pool.cs);
77  pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
78  available.insert(i);
79  }
80  }
81 
82  auto init_status{pdb.InitData(cmpctblock, extra_txn)};
83 
84  std::vector<CTransactionRef> missing;
85  // Whether we skipped a transaction that should be included in `missing`.
86  // FillBlock should never return READ_STATUS_OK if that is the case.
87  bool skipped_missing{false};
88  for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
89  // If init_status == READ_STATUS_OK then a available transaction in the
90  // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
91  // that transaction as available above (i.e. available.count(i) > 0).
92  // The reverse is not true, due to possible compact block short id
93  // collisions (i.e. available.count(i) > 0 does not imply
94  // IsTxAvailable(i) == true).
95  if (init_status == READ_STATUS_OK) {
96  assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
97  }
98 
99  bool skip{fuzzed_data_provider.ConsumeBool()};
100  if (!pdb.IsTxAvailable(i) && !skip) {
101  missing.push_back(block->vtx[i]);
102  }
103 
104  skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
105  }
106 
107  // Mock CheckBlock
108  bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
109  auto validation_result =
110  fuzzed_data_provider.PickValueInArray(
122  pdb.m_check_block_mock = FuzzedCheckBlock(
123  fail_check_block ?
124  std::optional<BlockValidationResult>{validation_result} :
125  std::nullopt);
126 
127  CBlock reconstructed_block;
128  auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
129  switch (fill_status) {
130  case READ_STATUS_OK:
131  assert(!skipped_missing);
132  assert(!fail_check_block);
133  assert(block->GetHash() == reconstructed_block.GetHash());
134  break;
135  case READ_STATUS_CHECKBLOCK_FAILED: [[fallthrough]];
136  case READ_STATUS_FAILED:
137  assert(fail_check_block);
138  break;
139  case READ_STATUS_INVALID:
140  break;
141  }
142 }
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_FAILED
std::vector< std::pair< uint256, CTransactionRef > > extra_txn
Definition: block.h:69
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:302
std::function< bool(const CBlock &, BlockValidationState &, const Consensus::Params &, bool, bool)> CheckBlockFn
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_RECENT_CONSENSUS_CHANGE
Invalid by a change to consensus rules more recent than SegWit.
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
@ BLOCK_RESULT_UNSET
initial value. Block has not yet been rejected
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
FUZZ_TARGET(partially_downloaded_block,.init=initialize_pdb)
void initialize_pdb()
PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional< BlockValidationResult > result)
Parameters that influence chain consensus.
Definition: params.h:74
Testing setup that configures a complete environment.
Definition: setup_common.h:77
#define LOCK2(cs1, cs2)
Definition: sync.h:259
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: mempool.cpp:17
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition: txmempool.cpp:17
assert(!tx.IsCoinBase())