Bitcoin Core  22.99.0
P2P Digital Currency
bloom_filter.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 <bloom.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <uint256.h>
11 
12 #include <cassert>
13 #include <cstdint>
14 #include <optional>
15 #include <string>
16 #include <vector>
17 
18 FUZZ_TARGET(bloom_filter)
19 {
20  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
21 
22  CBloomFilter bloom_filter{
23  fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000),
24  1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()),
25  fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
26  static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))};
27  while (fuzzed_data_provider.remaining_bytes() > 0) {
28  CallOneOf(
29  fuzzed_data_provider,
30  [&] {
31  const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
32  (void)bloom_filter.contains(b);
33  bloom_filter.insert(b);
34  const bool present = bloom_filter.contains(b);
35  assert(present);
36  },
37  [&] {
38  const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
39  if (!out_point) {
40  return;
41  }
42  (void)bloom_filter.contains(*out_point);
43  bloom_filter.insert(*out_point);
44  const bool present = bloom_filter.contains(*out_point);
45  assert(present);
46  },
47  [&] {
48  const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
49  if (!u256) {
50  return;
51  }
52  (void)bloom_filter.contains(*u256);
53  bloom_filter.insert(*u256);
54  const bool present = bloom_filter.contains(*u256);
55  assert(present);
56  },
57  [&] {
58  const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
59  if (!mut_tx) {
60  return;
61  }
62  const CTransaction tx{*mut_tx};
63  (void)bloom_filter.IsRelevantAndUpdate(tx);
64  });
65  (void)bloom_filter.IsWithinSizeConstraints();
66  }
67 }
assert
assert(!tx.IsCoinBase())
CBloomFilter
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
transaction.h
uint256.h
util.h
BLOOM_UPDATE_P2PUBKEY_ONLY
@ BLOOM_UPDATE_P2PUBKEY_ONLY
Definition: bloom.h:29
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
bloom.h
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
BLOOM_UPDATE_MASK
@ BLOOM_UPDATE_MASK
Definition: bloom.h:30
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
BLOOM_UPDATE_ALL
@ BLOOM_UPDATE_ALL
Definition: bloom.h:27
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
FUZZ_TARGET
FUZZ_TARGET(bloom_filter)
Definition: bloom_filter.cpp:18
BLOOM_UPDATE_NONE
@ BLOOM_UPDATE_NONE
Definition: bloom.h:26
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204