Bitcoin Core  22.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);
31  CBloomFilter bloom_filter;
32  std::set<uint256> 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  while (fuzzed_data_provider.ConsumeBool()) {
38  txids.insert(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 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
CPartialMerkleTree::GetNumTransactions
unsigned int GetNumTransactions() const
Get number of transactions the merkle proof is indicating for cross-reference with local blockchain k...
Definition: merkleblock.h:113
merkleblock.h
CPartialMerkleTree
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:54
CBloomFilter
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
CMerkleBlock
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:124
uint256.h
util.h
CBloomFilter::insert
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:46
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
CPartialMerkleTree::ExtractMatches
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...
Definition: merkleblock.cpp:149
CMerkleBlock::txn
CPartialMerkleTree txn
Definition: merkleblock.h:129
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FUZZ_TARGET
FUZZ_TARGET(merkleblock)
Definition: merkleblock.cpp:16
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288