Bitcoin Core 28.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 <common/bloom.h>
8#include <test/fuzz/fuzz.h>
9#include <test/fuzz/util.h>
10#include <uint256.h>
11
12#include <cassert>
13#include <limits>
14#include <optional>
15#include <vector>
16
17FUZZ_TARGET(bloom_filter)
18{
19 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 bool good_data{true};
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 LIMITED_WHILE(good_data && fuzzed_data_provider.remaining_bytes() > 0, 10'000)
28 {
30 fuzzed_data_provider,
31 [&] {
32 const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
33 (void)bloom_filter.contains(b);
34 bloom_filter.insert(b);
35 const bool present = bloom_filter.contains(b);
36 assert(present);
37 },
38 [&] {
39 const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
40 if (!out_point) {
41 good_data = false;
42 return;
43 }
44 (void)bloom_filter.contains(*out_point);
45 bloom_filter.insert(*out_point);
46 const bool present = bloom_filter.contains(*out_point);
47 assert(present);
48 },
49 [&] {
50 const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
51 if (!u256) {
52 good_data = false;
53 return;
54 }
55 (void)bloom_filter.contains(*u256);
56 bloom_filter.insert(*u256);
57 const bool present = bloom_filter.contains(*u256);
58 assert(present);
59 },
60 [&] {
61 const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
62 if (!mut_tx) {
63 good_data = false;
64 return;
65 }
66 const CTransaction tx{*mut_tx};
67 (void)bloom_filter.IsRelevantAndUpdate(tx);
68 });
69 (void)bloom_filter.IsWithinSizeConstraints();
70 }
71}
@ BLOOM_UPDATE_NONE
Definition: bloom.h:26
@ BLOOM_UPDATE_P2PUBKEY_ONLY
Definition: bloom.h:29
@ BLOOM_UPDATE_ALL
Definition: bloom.h:27
@ BLOOM_UPDATE_MASK
Definition: bloom.h:30
FUZZ_TARGET(bloom_filter)
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
assert(!tx.IsCoinBase())