Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
partially_downloaded_block.cpp
Go to the documentation of this file.
1#include <blockencodings.h>
2#include <consensus/merkle.h>
4#include <primitives/block.h>
7#include <test/fuzz/fuzz.h>
8#include <test/fuzz/util.h>
11#include <test/util/txmempool.h>
12#include <txmempool.h>
13#include <util/check.h>
14#include <util/translation.h>
15
16#include <cstddef>
17#include <cstdint>
18#include <limits>
19#include <memory>
20#include <optional>
21#include <set>
22#include <vector>
23
24namespace {
25const TestingSetup* g_setup;
26} // namespace
27
29{
30 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
31 g_setup = testing_setup.get();
32}
33
34PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional<BlockValidationResult> result)
35{
36 return [result](const CBlock&, BlockValidationState& state, const Consensus::Params&, bool, bool) {
37 if (result) {
38 return state.Invalid(*result);
39 }
40
41 return true;
42 };
43}
44
45FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
46{
47 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
48
49 auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
50 if (!block || block->vtx.size() == 0 ||
51 block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
52 return;
53 }
54
55 CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
56
57 bilingual_str error;
58 CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
59 Assert(error.empty());
60 PartiallyDownloadedBlock pdb{&pool};
61
62 // Set of available transactions (mempool or extra_txn)
63 std::set<uint16_t> available;
64 // The coinbase is always available
65 available.insert(0);
66
67 std::vector<CTransactionRef> extra_txn;
68 for (size_t i = 1; i < block->vtx.size(); ++i) {
69 auto tx{block->vtx[i]};
70
71 bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
72 bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
73
74 if (add_to_extra_txn) {
75 extra_txn.emplace_back(tx);
76 available.insert(i);
77 }
78
79 if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
80 LOCK2(cs_main, pool.cs);
81 pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
82 available.insert(i);
83 }
84 }
85
86 auto init_status{pdb.InitData(cmpctblock, extra_txn)};
87
88 std::vector<CTransactionRef> missing;
89 // Whether we skipped a transaction that should be included in `missing`.
90 // FillBlock should never return READ_STATUS_OK if that is the case.
91 bool skipped_missing{false};
92 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
93 // If init_status == READ_STATUS_OK then a available transaction in the
94 // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
95 // that transaction as available above (i.e. available.count(i) > 0).
96 // The reverse is not true, due to possible compact block short id
97 // collisions (i.e. available.count(i) > 0 does not imply
98 // IsTxAvailable(i) == true).
99 if (init_status == READ_STATUS_OK) {
100 assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
101 }
102
103 bool skip{fuzzed_data_provider.ConsumeBool()};
104 if (!pdb.IsTxAvailable(i) && !skip) {
105 missing.push_back(block->vtx[i]);
106 }
107
108 skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
109 }
110
111 // Mock CheckBlock
112 bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
113 auto validation_result =
114 fuzzed_data_provider.PickValueInArray(
127 fail_check_block ?
128 std::optional<BlockValidationResult>{validation_result} :
129 std::nullopt);
130
131 CBlock reconstructed_block;
132 auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
133 switch (fill_status) {
134 case READ_STATUS_OK:
135 assert(!skipped_missing);
136 assert(!fail_check_block);
137 assert(block->GetHash() == reconstructed_block.GetHash());
138 break;
139 case READ_STATUS_CHECKBLOCK_FAILED: [[fallthrough]];
141 assert(fail_check_block);
142 break;
144 break;
145 }
146}
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_FAILED
#define Assert(val)
Identity function.
Definition check.h:77
Definition block.h:69
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:304
static GenTxid Txid(const uint256 &hash)
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
#define FUZZ_TARGET(...)
Definition fuzz.h:35
void initialize_pdb()
PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional< BlockValidationResult > result)
static constexpr TransactionSerParams TX_WITH_WITNESS
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Parameters that influence chain consensus.
Definition params.h:74
Testing setup that configures a complete environment.
Bilingual messages:
Definition translation.h:18
bool empty() const
Definition translation.h:29
#define LOCK2(cs1, cs2)
Definition sync.h:258
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition mempool.cpp:17
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:103
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition txmempool.cpp:20
assert(!tx.IsCoinBase())