Bitcoin Core  24.99.0
P2P Digital Currency
blockfilter.cpp
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 #include <mutex>
6 #include <set>
7 
8 #include <blockfilter.h>
9 #include <crypto/siphash.h>
10 #include <hash.h>
11 #include <primitives/transaction.h>
12 #include <script/script.h>
13 #include <streams.h>
14 #include <util/golombrice.h>
15 #include <util/string.h>
16 
18 static constexpr int GCS_SER_TYPE = SER_NETWORK;
19 
21 static constexpr int GCS_SER_VERSION = 0;
22 
23 static const std::map<BlockFilterType, std::string> g_filter_types = {
24  {BlockFilterType::BASIC, "basic"},
25 };
26 
27 uint64_t GCSFilter::HashToRange(const Element& element) const
28 {
30  .Write(element.data(), element.size())
31  .Finalize();
32  return FastRange64(hash, m_F);
33 }
34 
35 std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const
36 {
37  std::vector<uint64_t> hashed_elements;
38  hashed_elements.reserve(elements.size());
39  for (const Element& element : elements) {
40  hashed_elements.push_back(HashToRange(element));
41  }
42  std::sort(hashed_elements.begin(), hashed_elements.end());
43  return hashed_elements;
44 }
45 
47  : m_params(params), m_N(0), m_F(0), m_encoded{0}
48 {}
49 
50 GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
51  : m_params(params), m_encoded(std::move(encoded_filter))
52 {
54 
55  uint64_t N = ReadCompactSize(stream);
56  m_N = static_cast<uint32_t>(N);
57  if (m_N != N) {
58  throw std::ios_base::failure("N must be <2^32");
59  }
60  m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
61 
62  if (skip_decode_check) return;
63 
64  // Verify that the encoded filter contains exactly N elements. If it has too much or too little
65  // data, a std::ios_base::failure exception will be raised.
66  BitStreamReader<SpanReader> bitreader{stream};
67  for (uint64_t i = 0; i < m_N; ++i) {
68  GolombRiceDecode(bitreader, m_params.m_P);
69  }
70  if (!stream.empty()) {
71  throw std::ios_base::failure("encoded_filter contains excess data");
72  }
73 }
74 
75 GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
76  : m_params(params)
77 {
78  size_t N = elements.size();
79  m_N = static_cast<uint32_t>(N);
80  if (m_N != N) {
81  throw std::invalid_argument("N must be <2^32");
82  }
83  m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
84 
86 
87  WriteCompactSize(stream, m_N);
88 
89  if (elements.empty()) {
90  return;
91  }
92 
93  BitStreamWriter<CVectorWriter> bitwriter(stream);
94 
95  uint64_t last_value = 0;
96  for (uint64_t value : BuildHashedSet(elements)) {
97  uint64_t delta = value - last_value;
98  GolombRiceEncode(bitwriter, m_params.m_P, delta);
99  last_value = value;
100  }
101 
102  bitwriter.Flush();
103 }
104 
105 bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
106 {
108 
109  // Seek forward by size of N
110  uint64_t N = ReadCompactSize(stream);
111  assert(N == m_N);
112 
113  BitStreamReader<SpanReader> bitreader{stream};
114 
115  uint64_t value = 0;
116  size_t hashes_index = 0;
117  for (uint32_t i = 0; i < m_N; ++i) {
118  uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P);
119  value += delta;
120 
121  while (true) {
122  if (hashes_index == size) {
123  return false;
124  } else if (element_hashes[hashes_index] == value) {
125  return true;
126  } else if (element_hashes[hashes_index] > value) {
127  break;
128  }
129 
130  hashes_index++;
131  }
132  }
133 
134  return false;
135 }
136 
137 bool GCSFilter::Match(const Element& element) const
138 {
139  uint64_t query = HashToRange(element);
140  return MatchInternal(&query, 1);
141 }
142 
143 bool GCSFilter::MatchAny(const ElementSet& elements) const
144 {
145  const std::vector<uint64_t> queries = BuildHashedSet(elements);
146  return MatchInternal(queries.data(), queries.size());
147 }
148 
149 const std::string& BlockFilterTypeName(BlockFilterType filter_type)
150 {
151  static std::string unknown_retval;
152  auto it = g_filter_types.find(filter_type);
153  return it != g_filter_types.end() ? it->second : unknown_retval;
154 }
155 
156 bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type) {
157  for (const auto& entry : g_filter_types) {
158  if (entry.second == name) {
159  filter_type = entry.first;
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 const std::set<BlockFilterType>& AllBlockFilterTypes()
167 {
168  static std::set<BlockFilterType> types;
169 
170  static std::once_flag flag;
171  std::call_once(flag, []() {
172  for (const auto& entry : g_filter_types) {
173  types.insert(entry.first);
174  }
175  });
176 
177  return types;
178 }
179 
180 const std::string& ListBlockFilterTypes()
181 {
182  static std::string type_list{Join(g_filter_types, ", ", [](const auto& entry) { return entry.second; })};
183 
184  return type_list;
185 }
186 
188  const CBlockUndo& block_undo)
189 {
190  GCSFilter::ElementSet elements;
191 
192  for (const CTransactionRef& tx : block.vtx) {
193  for (const CTxOut& txout : tx->vout) {
194  const CScript& script = txout.scriptPubKey;
195  if (script.empty() || script[0] == OP_RETURN) continue;
196  elements.emplace(script.begin(), script.end());
197  }
198  }
199 
200  for (const CTxUndo& tx_undo : block_undo.vtxundo) {
201  for (const Coin& prevout : tx_undo.vprevout) {
202  const CScript& script = prevout.out.scriptPubKey;
203  if (script.empty()) continue;
204  elements.emplace(script.begin(), script.end());
205  }
206  }
207 
208  return elements;
209 }
210 
211 BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
212  std::vector<unsigned char> filter, bool skip_decode_check)
213  : m_filter_type(filter_type), m_block_hash(block_hash)
214 {
215  GCSFilter::Params params;
216  if (!BuildParams(params)) {
217  throw std::invalid_argument("unknown filter_type");
218  }
219  m_filter = GCSFilter(params, std::move(filter), skip_decode_check);
220 }
221 
222 BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
223  : m_filter_type(filter_type), m_block_hash(block.GetHash())
224 {
225  GCSFilter::Params params;
226  if (!BuildParams(params)) {
227  throw std::invalid_argument("unknown filter_type");
228  }
229  m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));
230 }
231 
233 {
234  switch (m_filter_type) {
236  params.m_siphash_k0 = m_block_hash.GetUint64(0);
237  params.m_siphash_k1 = m_block_hash.GetUint64(1);
238  params.m_P = BASIC_FILTER_P;
239  params.m_M = BASIC_FILTER_M;
240  return true;
242  return false;
243  }
244 
245  return false;
246 }
247 
249 {
250  const std::vector<unsigned char>& data = GetEncodedFilter();
251 
252  uint256 result;
253  CHash256().Write(data).Finalize(result);
254  return result;
255 }
256 
257 uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
258 {
259  const uint256& filter_hash = GetHash();
260 
261  uint256 result;
262  CHash256()
263  .Write(filter_hash)
264  .Write(prev_header)
265  .Finalize(result);
266  return result;
267 }
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
static const std::map< BlockFilterType, std::string > g_filter_types
Definition: blockfilter.cpp:23
static constexpr int GCS_SER_VERSION
Protocol version used to serialize parameters in GCS filter encoding.
Definition: blockfilter.cpp:21
static GCSFilter::ElementSet BasicFilterElements(const CBlock &block, const CBlockUndo &block_undo)
static constexpr int GCS_SER_TYPE
SerType used to serialize parameters in GCS filter encoding.
Definition: blockfilter.cpp:18
const std::string & ListBlockFilterTypes()
Get a comma-separated list of known filter type names.
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.
BlockFilterType
Definition: blockfilter.h:90
constexpr uint8_t BASIC_FILTER_P
Definition: blockfilter.h:86
constexpr uint32_t BASIC_FILTER_M
Definition: blockfilter.h:87
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:477
GCSFilter m_filter
Definition: blockfilter.h:116
bool BuildParams(GCSFilter::Params &params) const
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType m_filter_type
Definition: blockfilter.h:114
BlockFilter()=default
uint256 GetHash() const
Compute the filter hash.
uint256 m_block_hash
Definition: blockfilter.h:115
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
Definition: blockfilter.h:135
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
Undo information for a CBlock.
Definition: undo.h:64
std::vector< CTxUndo > vtxundo
Definition: undo.h:66
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:25
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:38
void Finalize(Span< unsigned char > output)
Definition: hash.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
SipHash-2-4.
Definition: siphash.h:14
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:76
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Definition: siphash.cpp:28
An output of a transaction.
Definition: transaction.h:158
CScript scriptPubKey
Definition: transaction.h:161
Undo information for a CTransaction.
Definition: undo.h:54
std::vector< Coin > vprevout
Definition: undo.h:57
A UTXO entry.
Definition: coins.h:31
CTxOut out
unspent transaction output
Definition: coins.h:34
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
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.
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
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:135
uint64_t GetUint64(int pos) const
Definition: uint256.h:86
bool empty() const
Definition: prevector.h:288
iterator begin()
Definition: prevector.h:292
iterator end()
Definition: prevector.h:294
256-bit opaque blob.
Definition: uint256.h:119
static uint64_t FastRange64(uint64_t x, uint64_t n)
Fast range reduction with 64-bit input and 64-bit range.
Definition: fastrange.h:25
uint64_t GolombRiceDecode(BitStreamReader< IStream > &bitreader, uint8_t P)
Definition: golombrice.h:32
void GolombRiceEncode(BitStreamWriter< OStream > &bitwriter, uint8_t P, uint64_t x)
Definition: golombrice.h:15
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
const char * name
Definition: rest.cpp:46
@ OP_RETURN
Definition: script.h:107
@ SER_NETWORK
Definition: serialize.h:131
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:275
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1103
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:68
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
assert(!tx.IsCoinBase())