Bitcoin Core  27.99.0
P2P Digital Currency
merkleblock.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <merkleblock.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 #include <uint256.h>
10 
11 #include <cstdint>
12 #include <optional>
13 #include <string>
14 #include <vector>
15 
16 FUZZ_TARGET(merkleblock)
17 {
18  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19  CPartialMerkleTree partial_merkle_tree;
20  CallOneOf(
21  fuzzed_data_provider,
22  [&] {
23  const std::optional<CPartialMerkleTree> opt_partial_merkle_tree = ConsumeDeserializable<CPartialMerkleTree>(fuzzed_data_provider);
24  if (opt_partial_merkle_tree) {
25  partial_merkle_tree = *opt_partial_merkle_tree;
26  }
27  },
28  [&] {
29  CMerkleBlock merkle_block;
30  const std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS);
31  CBloomFilter bloom_filter;
32  std::set<Txid> txids;
33  if (opt_block && !opt_block->vtx.empty()) {
34  if (fuzzed_data_provider.ConsumeBool()) {
35  merkle_block = CMerkleBlock{*opt_block, bloom_filter};
36  } else if (fuzzed_data_provider.ConsumeBool()) {
37  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
38  txids.insert(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
39  }
40  merkle_block = CMerkleBlock{*opt_block, txids};
41  }
42  }
43  partial_merkle_tree = merkle_block.txn;
44  });
45  (void)partial_merkle_tree.GetNumTransactions();
46  std::vector<uint256> matches;
47  std::vector<unsigned int> indices;
48  (void)partial_merkle_tree.ExtractMatches(matches, indices);
49 }
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:126
CPartialMerkleTree txn
Definition: merkleblock.h:130
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:56
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid's represented by this partial merkle tree and their respective indices with...
unsigned int GetNumTransactions() const
Get number of transactions the merkle proof is indicating for cross-reference with local blockchain k...
Definition: merkleblock.h:114
static transaction_identifier FromUint256(const uint256 &id)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:23
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
FUZZ_TARGET(merkleblock)
Definition: merkleblock.cpp:16
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:169
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35