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