Bitcoin Core  24.99.0
P2P Digital Currency
blockfilter.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2022 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 #ifndef BITCOIN_BLOCKFILTER_H
6 #define BITCOIN_BLOCKFILTER_H
7 
8 #include <stdint.h>
9 #include <string>
10 #include <set>
11 #include <unordered_set>
12 #include <vector>
13 
14 #include <attributes.h>
15 #include <primitives/block.h>
16 #include <serialize.h>
17 #include <uint256.h>
18 #include <undo.h>
19 #include <util/bytevectorhash.h>
20 
25 class GCSFilter
26 {
27 public:
28  typedef std::vector<unsigned char> Element;
29  typedef std::unordered_set<Element, ByteVectorHash> ElementSet;
30 
31  struct Params
32  {
33  uint64_t m_siphash_k0;
34  uint64_t m_siphash_k1;
35  uint8_t m_P;
36  uint32_t m_M;
37 
38  Params(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 1)
39  : m_siphash_k0(siphash_k0), m_siphash_k1(siphash_k1), m_P(P), m_M(M)
40  {}
41  };
42 
43 private:
45  uint32_t m_N;
46  uint64_t m_F;
47  std::vector<unsigned char> m_encoded;
48 
50  uint64_t HashToRange(const Element& element) const;
51 
52  std::vector<uint64_t> BuildHashedSet(const ElementSet& elements) const;
53 
55  bool MatchInternal(const uint64_t* sorted_element_hashes, size_t size) const;
56 
57 public:
58 
60  explicit GCSFilter(const Params& params = Params());
61 
63  GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check);
64 
66  GCSFilter(const Params& params, const ElementSet& elements);
67 
68  uint32_t GetN() const { return m_N; }
69  const Params& GetParams() const LIFETIMEBOUND { return m_params; }
70  const std::vector<unsigned char>& GetEncoded() const LIFETIMEBOUND { return m_encoded; }
71 
76  bool Match(const Element& element) const;
77 
83  bool MatchAny(const ElementSet& elements) const;
84 };
85 
86 constexpr uint8_t BASIC_FILTER_P = 19;
87 constexpr uint32_t BASIC_FILTER_M = 784931;
88 
89 enum class BlockFilterType : uint8_t
90 {
91  BASIC = 0,
92  INVALID = 255,
93 };
94 
96 const std::string& BlockFilterTypeName(BlockFilterType filter_type);
97 
99 bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type);
100 
102 const std::set<BlockFilterType>& AllBlockFilterTypes();
103 
105 const std::string& ListBlockFilterTypes();
106 
112 {
113 private:
117 
118  bool BuildParams(GCSFilter::Params& params) const;
119 
120 public:
121 
122  BlockFilter() = default;
123 
125  BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
126  std::vector<unsigned char> filter, bool skip_decode_check);
127 
129  BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);
130 
132  const uint256& GetBlockHash() const LIFETIMEBOUND { return m_block_hash; }
133  const GCSFilter& GetFilter() const LIFETIMEBOUND { return m_filter; }
134 
135  const std::vector<unsigned char>& GetEncodedFilter() const LIFETIMEBOUND
136  {
137  return m_filter.GetEncoded();
138  }
139 
141  uint256 GetHash() const;
142 
144  uint256 ComputeHeader(const uint256& prev_header) const;
145 
146  template <typename Stream>
147  void Serialize(Stream& s) const {
148  s << static_cast<uint8_t>(m_filter_type)
149  << m_block_hash
150  << m_filter.GetEncoded();
151  }
152 
153  template <typename Stream>
154  void Unserialize(Stream& s) {
155  std::vector<unsigned char> encoded_filter;
156  uint8_t filter_type;
157 
158  s >> filter_type
159  >> m_block_hash
160  >> encoded_filter;
161 
162  m_filter_type = static_cast<BlockFilterType>(filter_type);
163 
164  GCSFilter::Params params;
165  if (!BuildParams(params)) {
166  throw std::ios_base::failure("unknown filter_type");
167  }
168  m_filter = GCSFilter(params, std::move(encoded_filter), /*skip_decode_check=*/false);
169  }
170 };
171 
172 #endif // BITCOIN_BLOCKFILTER_H
#define LIFETIMEBOUND
Definition: attributes.h:16
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
BlockFilterType
Definition: blockfilter.h:90
constexpr uint8_t BASIC_FILTER_P
Definition: blockfilter.h:86
const std::string & ListBlockFilterTypes()
Get a comma-separated list of known filter type names.
constexpr uint32_t BASIC_FILTER_M
Definition: blockfilter.h:87
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
const std::set< BlockFilterType > & AllBlockFilterTypes()
Get a list of known filter types.
const CChainParams & Params()
Return the currently selected parameters.
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:112
GCSFilter m_filter
Definition: blockfilter.h:116
const GCSFilter & GetFilter() const LIFETIMEBOUND
Definition: blockfilter.h:133
void Serialize(Stream &s) const
Definition: blockfilter.h:147
bool BuildParams(GCSFilter::Params &params) const
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType GetFilterType() const
Definition: blockfilter.h:131
BlockFilterType m_filter_type
Definition: blockfilter.h:114
BlockFilter()=default
uint256 GetHash() const
Compute the filter hash.
void Unserialize(Stream &s)
Definition: blockfilter.h:154
uint256 m_block_hash
Definition: blockfilter.h:115
const uint256 & GetBlockHash() const LIFETIMEBOUND
Definition: blockfilter.h:132
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
Definition: blockfilter.h:135
Definition: block.h:69
Undo information for a CBlock.
Definition: undo.h:64
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:26
std::vector< unsigned char > Element
Definition: blockfilter.h:28
uint64_t m_F
Range of element hashes, F = N * M.
Definition: blockfilter.h:46
bool MatchInternal(const uint64_t *sorted_element_hashes, size_t size) const
Helper method used to implement Match and MatchAny.
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:29
uint64_t HashToRange(const Element &element) const
Hash a data element to an integer in the range [0, N * M).
Definition: blockfilter.cpp:27
const std::vector< unsigned char > & GetEncoded() const LIFETIMEBOUND
Definition: blockfilter.h:70
uint32_t m_N
Number of elements in the filter.
Definition: blockfilter.h:45
bool Match(const Element &element) const
Checks if the element may be in the set.
uint32_t GetN() const
Definition: blockfilter.h:68
GCSFilter(const Params &params=Params())
Constructs an empty filter.
Definition: blockfilter.cpp:46
bool MatchAny(const ElementSet &elements) const
Checks if any of the given elements may be in the set.
std::vector< uint64_t > BuildHashedSet(const ElementSet &elements) const
Definition: blockfilter.cpp:35
Params m_params
Definition: blockfilter.h:44
std::vector< unsigned char > m_encoded
Definition: blockfilter.h:47
const Params & GetParams() const LIFETIMEBOUND
Definition: blockfilter.h:69
256-bit opaque blob.
Definition: uint256.h:105
const char * name
Definition: rest.cpp:46
Params(uint64_t siphash_k0=0, uint64_t siphash_k1=0, uint8_t P=0, uint32_t M=1)
Definition: blockfilter.h:38
uint32_t m_M
Inverse false positive rate.
Definition: blockfilter.h:36
uint64_t m_siphash_k1
Definition: blockfilter.h:34
uint8_t m_P
Golomb-Rice coding parameter.
Definition: blockfilter.h:35
uint64_t m_siphash_k0
Definition: blockfilter.h:33