Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
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
>
7
#include <
consensus/validation.h
>
8
#include <
primitives/block.h
>
9
#include <
primitives/transaction.h
>
10
#include <
test/fuzz/FuzzedDataProvider.h
>
11
#include <
test/fuzz/fuzz.h
>
12
#include <
test/fuzz/util.h
>
13
#include <
test/fuzz/util/mempool.h
>
14
#include <
test/util/setup_common.h
>
15
#include <
test/util/time.h
>
16
#include <
test/util/txmempool.h
>
17
#include <
txmempool.h
>
18
#include <
util/check.h
>
19
#include <util/time.h>
20
#include <
util/translation.h
>
21
22
#include <cstddef>
23
#include <cstdint>
24
#include <limits>
25
#include <memory>
26
#include <optional>
27
#include <set>
28
#include <vector>
29
30
namespace
{
31
const
TestingSetup
*
g_setup
;
32
}
// namespace
33
34
void
initialize_pdb
()
35
{
36
static
const
auto
testing_setup =
MakeNoLogFileContext<const TestingSetup>
();
37
g_setup
= testing_setup.get();
38
}
39
40
PartiallyDownloadedBlock::IsBlockMutatedFn
FuzzedIsBlockMutated
(
bool
result)
41
{
42
return
[result](
const
CBlock
& block, bool) {
43
return
result;
44
};
45
}
46
47
FUZZ_TARGET
(partially_downloaded_block, .
init
=
initialize_pdb
)
48
{
49
SeedRandomStateForTest
(
SeedRand::ZEROS
);
50
FuzzedDataProvider
fuzzed_data_provider{buffer.data(), buffer.size()};
51
FakeNodeClock
clock{
ConsumeTime
(fuzzed_data_provider)};
52
53
auto
block{
ConsumeDeserializable<CBlock>
(fuzzed_data_provider,
TX_WITH_WITNESS
)};
54
if
(!block || block->vtx.size() == 0 ||
55
block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
56
return
;
57
}
58
59
CBlockHeaderAndShortTxIDs
cmpctblock{*block, fuzzed_data_provider.
ConsumeIntegral
<uint64_t>()};
60
61
bilingual_str
error;
62
CTxMemPool
pool{
MemPoolOptionsForTest
(
g_setup
->
m_node
), error};
63
Assert
(error.
empty
());
64
PartiallyDownloadedBlock
pdb{&pool};
65
66
// Set of available transactions (mempool or extra_txn)
67
std::set<uint16_t> available;
68
// The coinbase is always available
69
available.insert(0);
70
71
std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
72
for
(
size_t
i = 1; i < block->vtx.size(); ++i) {
73
auto
tx{block->vtx[i]};
74
75
bool
add_to_extra_txn{fuzzed_data_provider.
ConsumeBool
()};
76
bool
add_to_mempool{fuzzed_data_provider.
ConsumeBool
()};
77
78
if
(add_to_extra_txn) {
79
extra_txn.emplace_back(tx->GetWitnessHash(), tx);
80
available.insert(i);
81
}
82
83
if
(add_to_mempool && !pool.
exists
(tx->GetHash())) {
84
LOCK2
(
cs_main
, pool.cs);
85
TryAddToMempool
(pool,
ConsumeTxMemPoolEntry
(fuzzed_data_provider, *tx));
86
available.insert(i);
87
}
88
}
89
90
auto
init_status{pdb.
InitData
(cmpctblock, extra_txn)};
91
92
std::vector<CTransactionRef> missing;
93
// Whether we skipped a transaction that should be included in `missing`.
94
// FillBlock should never return READ_STATUS_OK if that is the case.
95
bool
skipped_missing{
false
};
96
for
(
size_t
i = 0; i < cmpctblock.
BlockTxCount
(); i++) {
97
// If init_status == READ_STATUS_OK then a available transaction in the
98
// compact block (i.e. IsTxAvailable(i) == true) implies that we marked
99
// that transaction as available above (i.e. available.contains(i)).
100
// The reverse is not true, due to possible compact block short id
101
// collisions (i.e. available.contains(i) does not imply
102
// IsTxAvailable(i) == true).
103
if
(init_status ==
READ_STATUS_OK
) {
104
assert
(!pdb.
IsTxAvailable
(i) || available.contains(i));
105
}
106
107
bool
skip{fuzzed_data_provider.
ConsumeBool
()};
108
if
(!pdb.
IsTxAvailable
(i) && !skip) {
109
missing.push_back(block->vtx[i]);
110
}
111
112
skipped_missing |= (!pdb.
IsTxAvailable
(i) && skip);
113
}
114
115
bool
segwit_active{fuzzed_data_provider.
ConsumeBool
()};
116
117
// Mock IsBlockMutated
118
bool
fail_block_mutated{fuzzed_data_provider.
ConsumeBool
()};
119
pdb.
m_check_block_mutated_mock
=
FuzzedIsBlockMutated
(fail_block_mutated);
120
121
CBlock
reconstructed_block;
122
auto
fill_status{pdb.
FillBlock
(reconstructed_block, missing, segwit_active)};
123
switch
(fill_status) {
124
case
READ_STATUS_OK
:
125
assert
(!skipped_missing);
126
assert
(!fail_block_mutated);
127
assert
(block->GetHash() == reconstructed_block.
GetHash
());
128
break
;
129
case
READ_STATUS_FAILED
:
130
assert
(fail_block_mutated);
131
break
;
132
case
READ_STATUS_INVALID
:
133
break
;
134
}
135
}
FuzzedDataProvider.h
TryAddToMempool
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
block.h
g_setup
const TestingSetup * g_setup
Definition
block_index_tree.cpp:22
blockencodings.h
READ_STATUS_OK
@ READ_STATUS_OK
Definition
blockencodings.h:85
READ_STATUS_INVALID
@ READ_STATUS_INVALID
Definition
blockencodings.h:86
READ_STATUS_FAILED
@ READ_STATUS_FAILED
Definition
blockencodings.h:87
check.h
Assert
#define Assert(val)
Identity function.
Definition
check.h:116
CBlockHeaderAndShortTxIDs
Definition
blockencodings.h:90
CBlockHeaderAndShortTxIDs::BlockTxCount
size_t BlockTxCount() const
Definition
blockencodings.h:119
CBlockHeader::GetHash
uint256 GetHash() const
Definition
block.cpp:14
CBlock
Definition
block.h:74
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition
txmempool.h:187
CTxMemPool::exists
bool exists(const Txid &txid) const
Definition
txmempool.h:513
FakeNodeClock
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition
time.h:54
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition
FuzzedDataProvider.h:195
PartiallyDownloadedBlock
Definition
blockencodings.h:133
PartiallyDownloadedBlock::m_check_block_mutated_mock
IsBlockMutatedFn m_check_block_mutated_mock
Definition
blockencodings.h:143
PartiallyDownloadedBlock::FillBlock
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing, bool segwit_active)
Definition
blockencodings.cpp:188
PartiallyDownloadedBlock::IsBlockMutatedFn
std::function< bool(const CBlock &, bool)> IsBlockMutatedFn
Definition
blockencodings.h:142
PartiallyDownloadedBlock::IsTxAvailable
bool IsTxAvailable(size_t index) const
Definition
blockencodings.cpp:180
PartiallyDownloadedBlock::InitData
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< Wtxid, CTransactionRef > > &extra_txn)
Definition
blockencodings.cpp:59
validation.h
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition
cs_main.cpp:8
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
merkle.h
init
Definition
basic.cpp:11
FuzzedIsBlockMutated
PartiallyDownloadedBlock::IsBlockMutatedFn FuzzedIsBlockMutated(bool result)
Definition
partially_downloaded_block.cpp:40
initialize_pdb
void initialize_pdb()
Definition
partially_downloaded_block.cpp:34
transaction.h
TX_WITH_WITNESS
constexpr TransactionSerParams TX_WITH_WITNESS
Definition
transaction.h:181
setup_common.h
MakeNoLogFileContext
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.
Definition
setup_common.h:241
BasicTestingSetup::m_node
node::NodeContext m_node
Definition
setup_common.h:60
TestingSetup
Testing setup that configures a complete environment.
Definition
setup_common.h:115
bilingual_str
Bilingual messages:
Definition
translation.h:24
bilingual_str::empty
bool empty() const
Definition
translation.h:35
LOCK2
#define LOCK2(cs1, cs2)
Definition
sync.h:269
SeedRandomStateForTest
SeedRandomStateForTest(SeedRand::ZEROS)
ConsumeTxMemPoolEntry
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx, uint32_t max_height) noexcept
Definition
mempool.cpp:17
mempool.h
ConsumeTime
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition
util.cpp:34
util.h
ConsumeDeserializable
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:106
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
Definition
random.h:19
time.h
MemPoolOptionsForTest
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition
txmempool.cpp:21
txmempool.h
translation.h
txmempool.h
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.18.0