Bitcoin Core 28.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 <common/bloom.h>
7#include <test/fuzz/fuzz.h>
8#include <test/fuzz/util.h>
9#include <test/util/random.h>
10#include <uint256.h>
11
12#include <cassert>
13#include <cstdint>
14#include <optional>
15#include <string>
16#include <vector>
17
18FUZZ_TARGET(rolling_bloom_filter)
19{
21 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
22
23 CRollingBloomFilter rolling_bloom_filter{
24 fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000),
25 0.999 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max())};
26 LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 3000)
27 {
29 fuzzed_data_provider,
30 [&] {
31 const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
32 (void)rolling_bloom_filter.contains(b);
33 rolling_bloom_filter.insert(b);
34 const bool present = rolling_bloom_filter.contains(b);
35 assert(present);
36 },
37 [&] {
38 const uint256 u256{ConsumeUInt256(fuzzed_data_provider)};
39 (void)rolling_bloom_filter.contains(u256);
40 rolling_bloom_filter.insert(u256);
41 const bool present = rolling_bloom_filter.contains(u256);
42 assert(present);
43 },
44 [&] {
45 rolling_bloom_filter.reset();
46 });
47 }
48}
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:109
T ConsumeIntegralInRange(T min, T max)
256-bit opaque blob.
Definition: uint256.h:190
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
FUZZ_TARGET(rolling_bloom_filter)
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:171
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
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
assert(!tx.IsCoinBase())