Bitcoin Core  22.99.0
P2P Digital Currency
blockfilter.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <blockfilter.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <cstdint>
11 #include <optional>
12 #include <string>
13 #include <vector>
14 
15 FUZZ_TARGET(blockfilter)
16 {
17  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
18  const std::optional<BlockFilter> block_filter = ConsumeDeserializable<BlockFilter>(fuzzed_data_provider);
19  if (!block_filter) {
20  return;
21  }
22  {
23  (void)block_filter->ComputeHeader(ConsumeUInt256(fuzzed_data_provider));
24  (void)block_filter->GetBlockHash();
25  (void)block_filter->GetEncodedFilter();
26  (void)block_filter->GetHash();
27  }
28  {
29  const BlockFilterType block_filter_type = block_filter->GetFilterType();
30  (void)BlockFilterTypeName(block_filter_type);
31  }
32  {
33  const GCSFilter gcs_filter = block_filter->GetFilter();
34  (void)gcs_filter.GetN();
35  (void)gcs_filter.GetParams();
36  (void)gcs_filter.GetEncoded();
37  (void)gcs_filter.Match(ConsumeRandomLengthByteVector(fuzzed_data_provider));
38  GCSFilter::ElementSet element_set;
39  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30000)
40  {
41  element_set.insert(ConsumeRandomLengthByteVector(fuzzed_data_provider));
42  }
43  gcs_filter.MatchAny(element_set);
44  }
45 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
BlockFilterTypeName
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
Definition: blockfilter.cpp:178
GCSFilter::Match
bool Match(const Element &element) const
Checks if the element may be in the set.
Definition: blockfilter.cpp:166
util.h
GCSFilter::GetParams
const Params & GetParams() const
Definition: blockfilter.h:68
GCSFilter
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:24
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
GCSFilter::MatchAny
bool MatchAny(const ElementSet &elements) const
Checks if any of the given elements may be in the set.
Definition: blockfilter.cpp:172
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
GCSFilter::GetN
uint32_t GetN() const
Definition: blockfilter.h:67
GCSFilter::ElementSet
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:28
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FUZZ_TARGET
FUZZ_TARGET(blockfilter)
Definition: blockfilter.cpp:15
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
GCSFilter::GetEncoded
const std::vector< unsigned char > & GetEncoded() const
Definition: blockfilter.h:69
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288