BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 <set>
9 #include <stdint.h>
10 #include <vector>
11 
12 #include <primitives/block.h>
13 #include <serialize.h>
14 #include <uint256.h>
15 #include <undo.h>
16 
21 class GCSFilter
22 {
23 public:
24  typedef std::vector<unsigned char> Element;
25  typedef std::set<Element> ElementSet;
26 
27 private:
28  uint64_t m_siphash_k0;
29  uint64_t m_siphash_k1;
30  uint8_t m_P;
31  uint32_t m_M;
32  uint32_t m_N;
33  uint64_t m_F;
34  std::vector<unsigned char> m_encoded;
35 
37  uint64_t HashToRange(const Element& element) const;
38 
39  std::vector<uint64_t> BuildHashedSet(const ElementSet& elements) const;
40 
42  bool MatchInternal(const uint64_t* sorted_element_hashes, size_t size) const;
43 
44 public:
45 
47  GCSFilter(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 0);
48 
50  GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
51  std::vector<unsigned char> encoded_filter);
52 
54  GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
55  const ElementSet& elements);
56 
57  uint8_t GetP() const { return m_P; }
58  uint32_t GetN() const { return m_N; }
59  uint32_t GetM() const { return m_M; }
60  const std::vector<unsigned char>& GetEncoded() const { return m_encoded; }
61 
66  bool Match(const Element& element) const;
67 
73  bool MatchAny(const ElementSet& elements) const;
74 };
75 
76 constexpr uint8_t BASIC_FILTER_P = 19;
77 constexpr uint32_t BASIC_FILTER_M = 784931;
78 
79 enum BlockFilterType : uint8_t
80 {
81  BASIC = 0,
82 };
83 
89 {
90 private:
94 
95 public:
96 
97  // Construct a new BlockFilter of the specified type from a block.
98  BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);
99 
101 
102  const GCSFilter& GetFilter() const { return m_filter; }
103 
104  const std::vector<unsigned char>& GetEncodedFilter() const
105  {
106  return m_filter.GetEncoded();
107  }
108 
109  // Compute the filter hash.
110  uint256 GetHash() const;
111 
112  // Compute the filter header given the previous one.
113  uint256 ComputeHeader(const uint256& prev_header) const;
114 
115  template <typename Stream>
116  void Serialize(Stream& s) const {
117  s << m_block_hash
118  << static_cast<uint8_t>(m_filter_type)
119  << m_filter.GetEncoded();
120  }
121 
122  template <typename Stream>
123  void Unserialize(Stream& s) {
124  std::vector<unsigned char> encoded_filter;
125  uint8_t filter_type;
126 
127  s >> m_block_hash
128  >> filter_type
129  >> encoded_filter;
130 
131  m_filter_type = static_cast<BlockFilterType>(filter_type);
132 
133  switch (m_filter_type) {
136  BASIC_FILTER_P, BASIC_FILTER_M, std::move(encoded_filter));
137  break;
138 
139  default:
140  throw std::ios_base::failure("unknown filter_type");
141  }
142  }
143 };
144 
145 #endif // BITCOIN_BLOCKFILTER_H
constexpr uint32_t BASIC_FILTER_M
Definition: blockfilter.h:77
Definition: block.h:74
BlockFilter(BlockFilterType filter_type, const CBlock &block, const CBlockUndo &block_undo)
constexpr uint8_t BASIC_FILTER_P
Definition: blockfilter.h:76
const GCSFilter & GetFilter() const
Definition: blockfilter.h:102
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:87
GCSFilter m_filter
Definition: blockfilter.h:93
uint256 GetHash() const
bool Match(const Element &element) const
Checks if the element may be in the set.
void Serialize(Stream &s) const
Definition: blockfilter.h:116
bool MatchInternal(const uint64_t *sorted_element_hashes, size_t size) const
Helper method used to implement Match and MatchAny.
BlockFilterType
Definition: blockfilter.h:79
uint32_t GetM() const
Definition: blockfilter.h:59
const std::vector< unsigned char > & GetEncodedFilter() const
Definition: blockfilter.h:104
GCSFilter(uint64_t siphash_k0=0, uint64_t siphash_k1=0, uint8_t P=0, uint32_t M=0)
Constructs an empty filter.
Definition: blockfilter.cpp:98
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:88
uint64_t HashToRange(const Element &element) const
Hash a data element to an integer in the range [0, N * M).
Definition: blockfilter.cpp:79
uint8_t GetP() const
Definition: blockfilter.h:57
uint64_t m_siphash_k0
Definition: blockfilter.h:28
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:21
uint8_t m_P
Golomb-Rice coding parameter.
Definition: blockfilter.h:30
uint256 ComputeHeader(const uint256 &prev_header) const
256-bit opaque blob.
Definition: uint256.h:122
Undo information for a CBlock.
Definition: undo.h:100
BlockFilterType m_filter_type
Definition: blockfilter.h:91
uint32_t GetN() const
Definition: blockfilter.h:58
uint256 m_block_hash
Definition: blockfilter.h:92
std::vector< unsigned char > Element
Definition: blockfilter.h:24
uint32_t m_N
Number of elements in the filter.
Definition: blockfilter.h:32
BlockFilterType GetFilterType() const
Definition: blockfilter.h:100
uint64_t GetUint64(int pos) const
Definition: uint256.h:81
uint64_t m_F
Range of element hashes, F = N * M.
Definition: blockfilter.h:33
uint32_t m_M
Inverse false positive rate.
Definition: blockfilter.h:31
std::set< Element > ElementSet
Definition: blockfilter.h:25
uint64_t m_siphash_k1
Definition: blockfilter.h:29
std::vector< unsigned char > m_encoded
Definition: blockfilter.h:34
const std::vector< unsigned char > & GetEncoded() const
Definition: blockfilter.h:60
void Unserialize(Stream &s)
Definition: blockfilter.h:123