Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
deserialize.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <addrdb.h>
6#include <addrman.h>
7#include <addrman_impl.h>
8#include <blockencodings.h>
9#include <blockfilter.h>
10#include <chain.h>
11#include <coins.h>
12#include <common/args.h>
13#include <compressor.h>
14#include <consensus/merkle.h>
15#include <key.h>
16#include <merkleblock.h>
17#include <net.h>
18#include <netbase.h>
19#include <netgroup.h>
20#include <node/blockstorage.h>
21#include <node/utxo_snapshot.h>
22#include <primitives/block.h>
23#include <protocol.h>
24#include <psbt.h>
25#include <pubkey.h>
26#include <script/keyorigin.h>
27#include <streams.h>
28#include <test/fuzz/fuzz.h>
29#include <test/fuzz/util.h>
31#include <undo.h>
32
33#include <cstdint>
34#include <exception>
35#include <optional>
36#include <stdexcept>
37
40
42{
43 static const auto testing_setup = MakeNoLogFileContext<>();
44}
45
46#define FUZZ_TARGET_DESERIALIZE(name, code) \
47 FUZZ_TARGET(name, .init = initialize_deserialize) \
48 { \
49 try { \
50 code \
51 } catch (const invalid_fuzzing_input_exception&) { \
52 } \
53 }
54
55namespace {
56
57struct invalid_fuzzing_input_exception : public std::exception {
58};
59
60template <typename T, typename P>
61DataStream Serialize(const T& obj, const P& params)
62{
63 DataStream ds{};
64 ds << params(obj);
65 return ds;
66}
67
68template <typename T, typename P>
69T Deserialize(DataStream&& ds, const P& params)
70{
71 T obj;
72 ds >> params(obj);
73 return obj;
74}
75
76template <typename T, typename P>
77void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const P& params)
78{
79 try {
80 SpanReader{buffer} >> params(obj);
81 } catch (const std::ios_base::failure&) {
82 throw invalid_fuzzing_input_exception();
83 }
84 assert(buffer.empty() || !Serialize(obj, params).empty());
85}
86
87template <typename T>
89{
90 DataStream ds{};
91 ds << obj;
92 return ds;
93}
94
95template <typename T>
96T Deserialize(DataStream ds)
97{
98 T obj;
99 ds >> obj;
100 return obj;
101}
102
103template <typename T>
105{
106 try {
107 SpanReader{buffer} >> obj;
108 } catch (const std::ios_base::failure&) {
109 throw invalid_fuzzing_input_exception();
110 }
111 assert(buffer.empty() || !Serialize(obj).empty());
112}
113
114template <typename T, typename P>
115void AssertEqualAfterSerializeDeserialize(const T& obj, const P& params)
116{
117 assert(Deserialize<T>(Serialize(obj, params), params) == obj);
118}
119template <typename T>
121{
123}
124
125} // namespace
126
130})
132{
133 FuzzedDataProvider fdp{buffer.data(), buffer.size()};
135}
139})
143})
148})
152})
157})
161})
166})
170})
172 CTxIn tx_in;
175})
180})
185})
189})
193})
197})
201})
203 CBlock block;
205})
209})
211 CBlock block;
213 bool mutated;
214 BlockMerkleRoot(block, &mutated);
215})
219})
221 CTxUndo tu;
223})
227})
229 Coin coin;
230 DeserializeFromFuzzingInput(buffer, coin);
231})
233{
234 FuzzedDataProvider fdp{buffer.data(), buffer.size()};
236 if (!maybe_na) return;
237 const CNetAddr& na{*maybe_na};
238 if (na.IsAddrV1Compatible()) {
240 }
242}
244{
245 FuzzedDataProvider fdp{buffer.data(), buffer.size()};
248 if (!maybe_s) return;
249 const CService& s{*maybe_s};
250 if (s.IsAddrV1Compatible()) {
252 }
255 assert(s.IsAddrV1Compatible());
256 }
257}
261 (void)mh.IsMessageTypeValid();
262})
264{
265 FuzzedDataProvider fdp{buffer.data(), buffer.size()};
268 if (!maybe_a) return;
269 const CAddress& a{*maybe_a};
270 // A CAddress in V1 mode will roundtrip
271 // in all 4 formats (v1/v2, network/disk)
272 if (ser_enc.enc == CNetAddr::Encoding::V1) {
277 } else {
278 // A CAddress in V2 mode will roundtrip in both V2 formats, and also in the V1 formats
279 // if it's V1 compatible.
280 if (a.IsAddrV1Compatible()) {
283 }
286 }
287}
289 CInv i;
291})
295})
299})
301 CTxOut to;
302 auto toc = Using<TxOutCompression>(to);
304})
308})
312})
314 auto msg_start = Params().MessageStart();
317})
322})
327})
328// Classes intentionally not covered in this file since their deserialization code is
329// fuzzed elsewhere:
330// * Deserialization of CTxOut is fuzzed in test/fuzz/tx_out.cpp
331// * Deserialization of CMutableTransaction is fuzzed in src/test/fuzz/transaction.cpp
const CChainParams & Params()
Return the currently selected parameters.
Complete block filter struct as defined in BIP 157.
A CService with information about it as peer.
Definition protocol.h:367
static constexpr SerParams V1_NETWORK
Definition protocol.h:408
static constexpr SerParams V2_NETWORK
Definition protocol.h:409
static constexpr SerParams V1_DISK
Definition protocol.h:410
static constexpr SerParams V2_DISK
Definition protocol.h:411
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:27
Definition block.h:74
Undo information for a CBlock.
Definition undo.h:63
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition bloom.h:45
const MessageStartChars & MessageStart() const
Definition chainparams.h:90
Used to marshal pointers into hashes for db storage.
Definition chain.h:318
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition feerate.h:32
inv message data
Definition protocol.h:494
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Message header.
Definition protocol.h:29
Network address.
Definition netaddress.h:113
static constexpr SerParams V1
Definition netaddress.h:231
static constexpr SerParams V2
Definition netaddress.h:232
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:29
Data structure that represents a partial merkle tree.
Definition merkleblock.h:57
An encapsulated public key.
Definition pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition script.h:405
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:530
An input of a transaction.
Definition transaction.h:62
An output of a transaction.
Undo information for a CTransaction.
Definition undo.h:53
A UTXO entry.
Definition coins.h:35
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:133
Minimal stream for reading from an existing byte array by std::span.
Definition streams.h:83
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
160-bit opaque blob.
Definition uint256.h:183
256-bit opaque blob.
Definition uint256.h:195
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition merkle.cpp:66
#define FUZZ_TARGET_DESERIALIZE(name, code)
void initialize_deserialize()
#define FUZZ_TARGET(...)
Definition fuzz.h:35
std::span< const uint8_t > FuzzBufferType
Definition fuzz.h:25
#define T(expected, seed, data)
static constexpr TransactionSerParams TX_WITH_WITNESS
void Serialize(Stream &, V)=delete
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition block.h:117
A structure for PSBTs which contain per-input information.
Definition psbt.h:262
A structure for PSBTs which contains per output information.
Definition psbt.h:878
A version of CTransaction with the PSBT format.
Definition psbt.h:1139
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())