Bitcoin Core  22.99.0
P2P Digital Currency
rolling_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>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 #include <uint256.h>
10 
11 #include <cassert>
12 #include <cstdint>
13 #include <optional>
14 #include <string>
15 #include <vector>
16 
17 FUZZ_TARGET(rolling_bloom_filter)
18 {
19  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 
21  CRollingBloomFilter rolling_bloom_filter{
22  fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000),
23  0.999 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max())};
24  LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 3000)
25  {
26  CallOneOf(
27  fuzzed_data_provider,
28  [&] {
29  const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
30  (void)rolling_bloom_filter.contains(b);
31  rolling_bloom_filter.insert(b);
32  const bool present = rolling_bloom_filter.contains(b);
33  assert(present);
34  },
35  [&] {
36  const uint256 u256{ConsumeUInt256(fuzzed_data_provider)};
37  (void)rolling_bloom_filter.contains(u256);
38  rolling_bloom_filter.insert(u256);
39  const bool present = rolling_bloom_filter.contains(u256);
40  assert(present);
41  },
42  [&] {
43  rolling_bloom_filter.reset();
44  });
45  }
46 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
assert
assert(!tx.IsCoinBase())
uint256.h
util.h
FUZZ_TARGET
FUZZ_TARGET(rolling_bloom_filter)
Definition: rolling_bloom_filter.cpp:17
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
uint256
256-bit opaque blob.
Definition: uint256.h:124
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
CRollingBloomFilter
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:110
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204