Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 {
31} // namespace
32
34{
35 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
36 g_setup = testing_setup.get();
37}
38
40{
41 return [result](const CBlock& block, bool) {
42 return result;
43 };
44}
45
46FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
47{
49 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
50 SetMockTime(ConsumeTime(fuzzed_data_provider));
51
52 auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
53 if (!block || block->vtx.size() == 0 ||
54 block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
55 return;
56 }
57
58 CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
59
60 bilingual_str error;
61 CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
62 Assert(error.empty());
63 PartiallyDownloadedBlock pdb{&pool};
64
65 // Set of available transactions (mempool or extra_txn)
66 std::set<uint16_t> available;
67 // The coinbase is always available
68 available.insert(0);
69
70 std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
71 for (size_t i = 1; i < block->vtx.size(); ++i) {
72 auto tx{block->vtx[i]};
73
74 bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
75 bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
76
77 if (add_to_extra_txn) {
78 extra_txn.emplace_back(tx->GetWitnessHash(), tx);
79 available.insert(i);
80 }
81
82 if (add_to_mempool && !pool.exists(tx->GetHash())) {
83 LOCK2(cs_main, pool.cs);
84 TryAddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
85 available.insert(i);
86 }
87 }
88
89 auto init_status{pdb.InitData(cmpctblock, extra_txn)};
90
91 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 bool skipped_missing{false};
95 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.contains(i)).
99 // The reverse is not true, due to possible compact block short id
100 // collisions (i.e. available.contains(i) does not imply
101 // IsTxAvailable(i) == true).
102 if (init_status == READ_STATUS_OK) {
103 assert(!pdb.IsTxAvailable(i) || available.contains(i));
104 }
105
106 bool skip{fuzzed_data_provider.ConsumeBool()};
107 if (!pdb.IsTxAvailable(i) && !skip) {
108 missing.push_back(block->vtx[i]);
109 }
110
111 skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
112 }
113
114 bool segwit_active{fuzzed_data_provider.ConsumeBool()};
115
116 // Mock IsBlockMutated
117 bool fail_block_mutated{fuzzed_data_provider.ConsumeBool()};
118 pdb.m_check_block_mutated_mock = FuzzedIsBlockMutated(fail_block_mutated);
119
120 CBlock reconstructed_block;
121 auto fill_status{pdb.FillBlock(reconstructed_block, missing, segwit_active)};
122 switch (fill_status) {
123 case READ_STATUS_OK:
124 assert(!skipped_missing);
125 assert(!fail_block_mutated);
126 assert(block->GetHash() == reconstructed_block.GetHash());
127 break;
129 assert(fail_block_mutated);
130 break;
132 break;
133 }
134}
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
const TestingSetup * g_setup
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_FAILED
#define Assert(val)
Identity function.
Definition check.h:113
uint256 GetHash() const
Definition block.cpp:15
Definition block.h:74
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:187
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition txmempool.h:260
bool exists(const Txid &txid) const
Definition txmempool.h:503
IsBlockMutatedFn m_check_block_mutated_mock
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing, bool segwit_active)
std::function< bool(const CBlock &, bool)> IsBlockMutatedFn
bool IsTxAvailable(size_t index) const
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< Wtxid, CTransactionRef > > &extra_txn)
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
PartiallyDownloadedBlock::IsBlockMutatedFn FuzzedIsBlockMutated(bool result)
void initialize_pdb()
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.
Testing setup that configures a complete environment.
Bilingual messages:
Definition translation.h:24
bool empty() const
Definition translation.h:35
#define LOCK2(cs1, cs2)
Definition sync.h:259
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx, uint32_t max_height) noexcept
Definition mempool.cpp:17
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition util.cpp:34
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:100
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.
Definition random.h:19
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition txmempool.cpp:21
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:44
assert(!tx.IsCoinBase())