Bitcoin Core 29.99.0
P2P Digital Currency
partially_downloaded_block.cpp
Go to the documentation of this file.
1// Copyright (c) 2023-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit.
4
5#include <blockencodings.h>
6#include <consensus/merkle.h>
8#include <primitives/block.h>
11#include <test/fuzz/fuzz.h>
12#include <test/fuzz/util.h>
15#include <test/util/txmempool.h>
16#include <txmempool.h>
17#include <util/check.h>
18#include <util/time.h>
19#include <util/translation.h>
20
21#include <cstddef>
22#include <cstdint>
23#include <limits>
24#include <memory>
25#include <optional>
26#include <set>
27#include <vector>
28
29namespace {
30const TestingSetup* g_setup;
31} // namespace
32
34{
35 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
36 g_setup = testing_setup.get();
37}
38
39PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional<BlockValidationResult> result)
40{
41 return [result](const CBlock&, BlockValidationState& state, const Consensus::Params&, bool, bool) {
42 if (result) {
43 return state.Invalid(*result);
44 }
45
46 return true;
47 };
48}
49
50FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
51{
53 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
54 SetMockTime(ConsumeTime(fuzzed_data_provider));
55
56 auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
57 if (!block || block->vtx.size() == 0 ||
58 block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
59 return;
60 }
61
62 CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
63
64 bilingual_str error;
65 CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
66 Assert(error.empty());
67 PartiallyDownloadedBlock pdb{&pool};
68
69 // Set of available transactions (mempool or extra_txn)
70 std::set<uint16_t> available;
71 // The coinbase is always available
72 available.insert(0);
73
74 std::vector<CTransactionRef> extra_txn;
75 for (size_t i = 1; i < block->vtx.size(); ++i) {
76 auto tx{block->vtx[i]};
77
78 bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
79 bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
80
81 if (add_to_extra_txn) {
82 extra_txn.emplace_back(tx);
83 available.insert(i);
84 }
85
86 if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
87 LOCK2(cs_main, pool.cs);
88 AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
89 available.insert(i);
90 }
91 }
92
93 auto init_status{pdb.InitData(cmpctblock, extra_txn)};
94
95 std::vector<CTransactionRef> missing;
96 // Whether we skipped a transaction that should be included in `missing`.
97 // FillBlock should never return READ_STATUS_OK if that is the case.
98 bool skipped_missing{false};
99 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
100 // If init_status == READ_STATUS_OK then a available transaction in the
101 // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
102 // that transaction as available above (i.e. available.count(i) > 0).
103 // The reverse is not true, due to possible compact block short id
104 // collisions (i.e. available.count(i) > 0 does not imply
105 // IsTxAvailable(i) == true).
106 if (init_status == READ_STATUS_OK) {
107 assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
108 }
109
110 bool skip{fuzzed_data_provider.ConsumeBool()};
111 if (!pdb.IsTxAvailable(i) && !skip) {
112 missing.push_back(block->vtx[i]);
113 }
114
115 skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
116 }
117
118 // Mock CheckBlock
119 bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
120 auto validation_result =
121 fuzzed_data_provider.PickValueInArray(
131 pdb.m_check_block_mock = FuzzedCheckBlock(
132 fail_check_block ?
133 std::optional<BlockValidationResult>{validation_result} :
134 std::nullopt);
135
136 CBlock reconstructed_block;
137 auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
138 switch (fill_status) {
139 case READ_STATUS_OK:
140 assert(!skipped_missing);
141 assert(!fail_check_block);
142 assert(block->GetHash() == reconstructed_block.GetHash());
143 break;
144 case READ_STATUS_CHECKBLOCK_FAILED: [[fallthrough]];
146 assert(fail_check_block);
147 break;
149 break;
150 }
151}
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_FAILED
#define Assert(val)
Identity function.
Definition: check.h:106
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)
Definition: transaction.h:434
std::function< bool(const CBlock &, BlockValidationState &, const Consensus::Params &, bool, bool)> CheckBlockFn
@ 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
AddToMempool(pool, CTxMemPoolEntry(tx, fee, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp))
FUZZ_TARGET(partially_downloaded_block,.init=initialize_pdb)
void initialize_pdb()
PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional< BlockValidationResult > result)
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
Parameters that influence chain consensus.
Definition: params.h:83
Testing setup that configures a complete environment.
Definition: setup_common.h:121
Bilingual messages:
Definition: translation.h:24
bool empty() const
Definition: translation.h:35
#define LOCK2(cs1, cs2)
Definition: sync.h:258
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx, uint32_t max_height) noexcept
Definition: mempool.cpp:17
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition: txmempool.cpp:20
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
assert(!tx.IsCoinBase())