Branch data Line data Source code
1 : : #include <blockencodings.h>
2 : : #include <consensus/merkle.h>
3 : : #include <consensus/validation.h>
4 : : #include <primitives/block.h>
5 : : #include <primitives/transaction.h>
6 : : #include <test/fuzz/FuzzedDataProvider.h>
7 : : #include <test/fuzz/fuzz.h>
8 : : #include <test/fuzz/util.h>
9 : : #include <test/fuzz/util/mempool.h>
10 : : #include <test/util/setup_common.h>
11 : : #include <test/util/txmempool.h>
12 : : #include <txmempool.h>
13 : : #include <util/check.h>
14 : : #include <util/time.h>
15 : : #include <util/translation.h>
16 : :
17 : : #include <cstddef>
18 : : #include <cstdint>
19 : : #include <limits>
20 : : #include <memory>
21 : : #include <optional>
22 : : #include <set>
23 : : #include <vector>
24 : :
25 : : namespace {
26 : : const TestingSetup* g_setup;
27 : : } // namespace
28 : :
29 : 1 : void initialize_pdb()
30 : : {
31 [ + - + - ]: 2 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
32 : 1 : g_setup = testing_setup.get();
33 [ + - ]: 2 : }
34 : :
35 : 941 : PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional<BlockValidationResult> result)
36 : : {
37 : 1250 : return [result](const CBlock&, BlockValidationState& state, const Consensus::Params&, bool, bool) {
38 [ + + ]: 309 : if (result) {
39 [ + - + - ]: 26 : return state.Invalid(*result);
40 : : }
41 : :
42 : : return true;
43 : 941 : };
44 : : }
45 : :
46 [ + - ]: 1495 : FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
47 : : {
48 : 1063 : SeedRandomStateForTest(SeedRand::ZEROS);
49 : 1063 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
50 : 1063 : SetMockTime(ConsumeTime(fuzzed_data_provider));
51 : :
52 : 1063 : auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
53 [ + + + + : 1063 : if (!block || block->vtx.size() == 0 ||
+ + ]
54 [ + + ]: 942 : block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
55 : 122 : return;
56 : : }
57 : :
58 [ + - ]: 941 : CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
59 : :
60 [ + - ]: 941 : bilingual_str error;
61 [ + - + - ]: 941 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
62 [ + - ]: 941 : Assert(error.empty());
63 : 941 : PartiallyDownloadedBlock pdb{&pool};
64 : :
65 : : // Set of available transactions (mempool or extra_txn)
66 [ + - ]: 941 : std::set<uint16_t> available;
67 : : // The coinbase is always available
68 [ + - ]: 941 : available.insert(0);
69 : :
70 : 941 : std::vector<CTransactionRef> extra_txn;
71 [ + + ]: 1739544 : for (size_t i = 1; i < block->vtx.size(); ++i) {
72 [ + - ]: 1738603 : auto tx{block->vtx[i]};
73 : :
74 : 1738603 : bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
75 : 1738603 : bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
76 : :
77 [ + + ]: 1738603 : if (add_to_extra_txn) {
78 [ + - ]: 1417640 : extra_txn.emplace_back(tx);
79 [ + - ]: 1417640 : available.insert(i);
80 : : }
81 : :
82 [ + + + - : 1738603 : if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
+ + ]
83 [ + - + - ]: 125078 : LOCK2(cs_main, pool.cs);
84 [ + - ]: 125078 : AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
85 [ + - ]: 125078 : available.insert(i);
86 [ + - ]: 250156 : }
87 : 1738603 : }
88 : :
89 [ + - ]: 941 : auto init_status{pdb.InitData(cmpctblock, extra_txn)};
90 : :
91 : 941 : std::vector<CTransactionRef> missing;
92 : : // Whether we skipped a transaction that should be included in `missing`.
93 : : // FillBlock should never return READ_STATUS_OK if that is the case.
94 : 941 : bool skipped_missing{false};
95 [ + + ]: 1740485 : for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
96 : : // If init_status == READ_STATUS_OK then a available transaction in the
97 : : // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
98 : : // that transaction as available above (i.e. available.count(i) > 0).
99 : : // The reverse is not true, due to possible compact block short id
100 : : // collisions (i.e. available.count(i) > 0 does not imply
101 : : // IsTxAvailable(i) == true).
102 [ + + ]: 1739544 : if (init_status == READ_STATUS_OK) {
103 [ + - + + : 6087 : assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
- + ]
104 : : }
105 : :
106 : 1739544 : bool skip{fuzzed_data_provider.ConsumeBool()};
107 [ + - + + : 1739544 : if (!pdb.IsTxAvailable(i) && !skip) {
+ + ]
108 [ + - ]: 1169003 : missing.push_back(block->vtx[i]);
109 : : }
110 : :
111 [ + - + + : 2913904 : skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
+ + ]
112 : : }
113 : :
114 : : // Mock CheckBlock
115 : 941 : bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
116 : 941 : auto validation_result =
117 : 941 : fuzzed_data_provider.PickValueInArray(
118 : : {BlockValidationResult::BLOCK_RESULT_UNSET,
119 : : BlockValidationResult::BLOCK_CONSENSUS,
120 : : BlockValidationResult::BLOCK_CACHED_INVALID,
121 : : BlockValidationResult::BLOCK_INVALID_HEADER,
122 : : BlockValidationResult::BLOCK_MUTATED,
123 : : BlockValidationResult::BLOCK_MISSING_PREV,
124 : : BlockValidationResult::BLOCK_INVALID_PREV,
125 : : BlockValidationResult::BLOCK_TIME_FUTURE,
126 : : BlockValidationResult::BLOCK_CHECKPOINT,
127 : 941 : BlockValidationResult::BLOCK_HEADER_LOW_WORK});
128 [ + + ]: 941 : pdb.m_check_block_mock = FuzzedCheckBlock(
129 : : fail_check_block ?
130 : : std::optional<BlockValidationResult>{validation_result} :
131 : 941 : std::nullopt);
132 : :
133 : 941 : CBlock reconstructed_block;
134 [ + - ]: 941 : auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
135 [ + + + ]: 941 : switch (fill_status) {
136 : 283 : case READ_STATUS_OK:
137 [ - + ]: 283 : assert(!skipped_missing);
138 [ - + ]: 283 : assert(!fail_check_block);
139 [ + - + - : 283 : assert(block->GetHash() == reconstructed_block.GetHash());
- + ]
140 : : break;
141 : 26 : case READ_STATUS_CHECKBLOCK_FAILED: [[fallthrough]];
142 : 26 : case READ_STATUS_FAILED:
143 [ - + ]: 26 : assert(fail_check_block);
144 : : break;
145 : : case READ_STATUS_INVALID:
146 : : break;
147 : : }
148 : 3886 : }
|