Bitcoin Core 32.99.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>
16#include <test/util/time.h>
17#include <test/util/txmempool.h>
18#include <txmempool.h>
19#include <util/check.h>
20#include <util/time.h>
21#include <util/translation.h>
22
23#include <cstddef>
24#include <cstdint>
25#include <limits>
26#include <memory>
27#include <optional>
28#include <set>
29#include <vector>
30
31namespace {
33constexpr ReachabilityGoal COMPACT_BLOCK_INITIALIZATION_SUCCEEDS{"compact block initialization succeeds"};
34constexpr ReachabilityGoal COMPACT_BLOCK_RECONSTRUCTION_SUCCEEDS{"compact block reconstruction succeeds"};
35} // namespace
36
44
46{
47 return [result](const CBlock& block, bool) {
48 return result;
49 };
50}
51
53{
55 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
57
59 if (!block || block->vtx.size() == 0 ||
60 block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
61 return;
62 }
63
65
66 bilingual_str error;
68 Assert(error.empty());
69 PartiallyDownloadedBlock pdb{&pool};
70
71 // Set of available transactions (mempool or extra_txn)
72 std::set<uint16_t> available;
73 // The coinbase is always available
74 available.insert(0);
75
76 std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
77 for (size_t i = 1; i < block->vtx.size(); ++i) {
78 auto tx{block->vtx[i]};
79
82
83 if (add_to_extra_txn) {
84 extra_txn.emplace_back(tx->GetWitnessHash(), tx);
85 available.insert(i);
86 }
87
88 if (add_to_mempool && !pool.exists(tx->GetHash())) {
89 LOCK2(cs_main, pool.cs);
91 available.insert(i);
92 }
93 }
94
95 auto init_status{pdb.InitData(cmpctblock, extra_txn)};
97
98 std::vector<CTransactionRef> missing;
99 // Whether we skipped a transaction that should be included in `missing`.
100 // FillBlock should never return READ_STATUS_OK if that is the case.
101 bool skipped_missing{false};
102 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
103 // If init_status == READ_STATUS_OK then a available transaction in the
104 // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
105 // that transaction as available above (i.e. available.contains(i)).
106 // The reverse is not true, due to possible compact block short id
107 // collisions (i.e. available.contains(i) does not imply
108 // IsTxAvailable(i) == true).
110 assert(!pdb.IsTxAvailable(i) || available.contains(i));
111 }
112
114 if (!pdb.IsTxAvailable(i) && !skip) {
115 missing.push_back(block->vtx[i]);
116 }
117
118 skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
119 }
120
122
123 // Mock IsBlockMutated
125 pdb.m_check_block_mutated_mock = FuzzedIsBlockMutated(fail_block_mutated);
126
130 switch (fill_status) {
131 case READ_STATUS_OK:
134 assert(block->GetHash() == reconstructed_block.GetHash());
135 break;
138 break;
140 break;
141 }
142}
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:119
Definition block.h:74
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:188
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
std::function< bool(const CBlock &, bool)> IsBlockMutatedFn
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
Definition basic.cpp:11
PartiallyDownloadedBlock::IsBlockMutatedFn FuzzedIsBlockMutated(bool result)
void initialize_pdb()
constexpr TransactionSerParams TX_WITH_WITNESS
void ObserveReachabilityGoal(bool reached, const ReachabilityGoal &goal)
Record whether a registered goal is satisfied by at least one fuzz input.
void RegisterReachabilityGoal(const ReachabilityGoal &goal)
Register a goal before executing any fuzz inputs.
node::NodeContext m_node
A reachability condition explicitly registered by a fuzz target.
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:269
SeedRandomStateForTest(SeedRand::ZEROS)
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
@ ZEROS
Seed with a compile time constant of zeros.
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition txmempool.cpp:21
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46
FakeNodeClock clock