Branch data Line data Source code
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 : : #include <string_view>
8 : :
9 : : #include <blockfilter.h>
10 : : #include <crypto/siphash.h>
11 : : #include <hash.h>
12 : : #include <primitives/block.h>
13 : : #include <primitives/transaction.h>
14 : : #include <script/script.h>
15 : : #include <streams.h>
16 : : #include <undo.h>
17 : : #include <util/golombrice.h>
18 : : #include <util/string.h>
19 : :
20 : : using util::Join;
21 : :
22 : : static const std::map<BlockFilterType, std::string> g_filter_types = {
23 : : {BlockFilterType::BASIC, "basic"},
24 : : };
25 : :
26 : 544659 : uint64_t GCSFilter::HashToRange(const Element& element) const
27 : : {
28 [ - + ]: 544659 : uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)
29 : 544659 : .Write(element)
30 : 544659 : .Finalize();
31 : 544659 : return FastRange64(hash, m_F);
32 : : }
33 : :
34 : 17352 : std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const
35 : : {
36 : 17352 : std::vector<uint64_t> hashed_elements;
37 [ + - ]: 17352 : hashed_elements.reserve(elements.size());
38 [ + + + - ]: 561902 : for (const Element& element : elements) {
39 [ + - + - ]: 544550 : hashed_elements.push_back(HashToRange(element));
40 : : }
41 : 17352 : std::sort(hashed_elements.begin(), hashed_elements.end());
42 : 17352 : return hashed_elements;
43 : 0 : }
44 : :
45 : 20102 : GCSFilter::GCSFilter(const Params& params)
46 : 20102 : : m_params(params), m_N(0), m_F(0), m_encoded{0}
47 : 20102 : {}
48 : :
49 : 1399 : GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
50 [ - + ]: 1399 : : m_params(params), m_encoded(std::move(encoded_filter))
51 : : {
52 [ - + ]: 1399 : SpanReader stream{m_encoded};
53 : :
54 [ + - ]: 1399 : uint64_t N = ReadCompactSize(stream);
55 : 1399 : m_N = static_cast<uint32_t>(N);
56 [ - + ]: 1399 : if (m_N != N) {
57 [ # # ]: 0 : throw std::ios_base::failure("N must be <2^32");
58 : : }
59 : 1399 : m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
60 : :
61 [ + + ]: 1399 : if (skip_decode_check) return;
62 : :
63 : : // Verify that the encoded filter contains exactly N elements. If it has too much or too little
64 : : // data, a std::ios_base::failure exception will be raised.
65 : 1 : BitStreamReader bitreader{stream};
66 [ + + ]: 6 : for (uint64_t i = 0; i < m_N; ++i) {
67 [ + - ]: 5 : GolombRiceDecode(bitreader, m_params.m_P);
68 : : }
69 [ - + ]: 1 : if (!stream.empty()) {
70 [ # # ]: 0 : throw std::ios_base::failure("encoded_filter contains excess data");
71 : : }
72 : 0 : }
73 : :
74 : 17188 : GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
75 [ - + ]: 17188 : : m_params(params)
76 : : {
77 [ - + ]: 17188 : size_t N = elements.size();
78 : 17188 : m_N = static_cast<uint32_t>(N);
79 [ - + ]: 17188 : if (m_N != N) {
80 [ # # ]: 0 : throw std::invalid_argument("N must be <2^32");
81 : : }
82 : 17188 : m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
83 : :
84 [ + - ]: 17188 : VectorWriter stream{m_encoded, 0};
85 : :
86 [ + - ]: 17188 : WriteCompactSize(stream, m_N);
87 : :
88 [ + + ]: 17188 : if (elements.empty()) {
89 : : return;
90 : : }
91 : :
92 [ + - ]: 16217 : BitStreamWriter bitwriter{stream};
93 : :
94 : 16217 : uint64_t last_value = 0;
95 [ + - + + ]: 32873 : for (uint64_t value : BuildHashedSet(elements)) {
96 : 16656 : uint64_t delta = value - last_value;
97 [ + - ]: 16656 : GolombRiceEncode(bitwriter, m_params.m_P, delta);
98 : 16656 : last_value = value;
99 : : }
100 : :
101 [ + - ]: 16217 : bitwriter.Flush();
102 : 16217 : }
103 : :
104 : 1244 : bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
105 : : {
106 [ - + ]: 1244 : SpanReader stream{m_encoded};
107 : :
108 : : // Seek forward by size of N
109 : 1244 : uint64_t N = ReadCompactSize(stream);
110 [ - + ]: 1244 : assert(N == m_N);
111 : :
112 : 1244 : BitStreamReader bitreader{stream};
113 : :
114 : 1244 : uint64_t value = 0;
115 : 1244 : size_t hashes_index = 0;
116 [ + + ]: 11114 : for (uint32_t i = 0; i < m_N; ++i) {
117 : 10359 : uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P);
118 : 10359 : value += delta;
119 : :
120 : 498303 : while (true) {
121 [ + + ]: 254331 : if (hashes_index == size) {
122 : : return false;
123 [ + + ]: 254121 : } else if (element_hashes[hashes_index] == value) {
124 : : return true;
125 [ + + ]: 253842 : } else if (element_hashes[hashes_index] > value) {
126 : : break;
127 : : }
128 : :
129 : 243972 : hashes_index++;
130 : : }
131 : : }
132 : :
133 : : return false;
134 : : }
135 : :
136 : 109 : bool GCSFilter::Match(const Element& element) const
137 : : {
138 : 109 : uint64_t query = HashToRange(element);
139 : 109 : return MatchInternal(&query, 1);
140 : : }
141 : :
142 : 1135 : bool GCSFilter::MatchAny(const ElementSet& elements) const
143 : : {
144 : 1135 : const std::vector<uint64_t> queries = BuildHashedSet(elements);
145 [ - + + - ]: 1135 : return MatchInternal(queries.data(), queries.size());
146 : 1135 : }
147 : :
148 : 4841 : const std::string& BlockFilterTypeName(BlockFilterType filter_type)
149 : : {
150 [ + + + - ]: 6193 : static std::string unknown_retval;
151 : 4841 : auto it = g_filter_types.find(filter_type);
152 [ + + ]: 4841 : return it != g_filter_types.end() ? it->second : unknown_retval;
153 : : }
154 : :
155 : 50 : bool BlockFilterTypeByName(std::string_view name, BlockFilterType& filter_type)
156 : : {
157 [ + + ]: 55 : for (const auto& entry : g_filter_types) {
158 [ - + + + ]: 50 : if (entry.second == name) {
159 : 45 : filter_type = entry.first;
160 : 45 : return true;
161 : : }
162 : : }
163 : : return false;
164 : : }
165 : :
166 : 58 : const std::set<BlockFilterType>& AllBlockFilterTypes()
167 : : {
168 [ + - + - ]: 58 : static std::set<BlockFilterType> types;
169 : :
170 : 58 : static std::once_flag flag;
171 : 58 : std::call_once(flag, []() {
172 [ + + ]: 116 : for (const auto& entry : g_filter_types) {
173 : 58 : types.insert(entry.first);
174 : : }
175 : 58 : });
176 : :
177 : 58 : return types;
178 : : }
179 : :
180 : 1805 : const std::string& ListBlockFilterTypes()
181 : : {
182 [ + + + - : 5687 : static std::string type_list{Join(g_filter_types, ", ", [](const auto& entry) { return entry.second; })};
+ - - + -
+ ]
183 : :
184 : 1805 : return type_list;
185 : : }
186 : :
187 : 17187 : static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
188 : : const CBlockUndo& block_undo)
189 : : {
190 : 17187 : GCSFilter::ElementSet elements;
191 : :
192 [ + + ]: 34684 : for (const CTransactionRef& tx : block.vtx) {
193 [ + + ]: 51251 : for (const CTxOut& txout : tx->vout) {
194 : 33754 : const CScript& script = txout.scriptPubKey;
195 [ + + + + : 84688 : if (script.empty() || script[0] == OP_RETURN) continue;
+ + + + ]
196 [ + - ]: 16592 : elements.emplace(script.begin(), script.end());
197 : : }
198 : : }
199 : :
200 [ + + ]: 17497 : for (const CTxUndo& tx_undo : block_undo.vtxundo) {
201 [ + + ]: 637 : for (const Coin& prevout : tx_undo.vprevout) {
202 : 327 : const CScript& script = prevout.out.scriptPubKey;
203 [ + + + + ]: 333 : if (script.empty()) continue;
204 [ + + + - ]: 646 : elements.emplace(script.begin(), script.end());
205 : : }
206 : : }
207 : :
208 : 17187 : return elements;
209 : 0 : }
210 : :
211 : 1398 : BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
212 : 1398 : std::vector<unsigned char> filter, bool skip_decode_check)
213 : 1398 : : m_filter_type(filter_type), m_block_hash(block_hash)
214 : : {
215 [ + - ]: 1398 : GCSFilter::Params params;
216 [ + - - + ]: 1398 : if (!BuildParams(params)) {
217 [ # # ]: 0 : throw std::invalid_argument("unknown filter_type");
218 : : }
219 [ + - ]: 1398 : m_filter = GCSFilter(params, std::move(filter), skip_decode_check);
220 : 1398 : }
221 : :
222 : 17187 : BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
223 : 17187 : : m_filter_type(filter_type), m_block_hash(block.GetHash())
224 : : {
225 [ + - ]: 17187 : GCSFilter::Params params;
226 [ + - - + ]: 17187 : if (!BuildParams(params)) {
227 [ # # ]: 0 : throw std::invalid_argument("unknown filter_type");
228 : : }
229 [ + - + - ]: 17187 : m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));
230 : 17187 : }
231 : :
232 : 18586 : bool BlockFilter::BuildParams(GCSFilter::Params& params) const
233 : : {
234 [ + - ]: 18586 : switch (m_filter_type) {
235 : 18586 : case BlockFilterType::BASIC:
236 : 18586 : params.m_siphash_k0 = m_block_hash.GetUint64(0);
237 : 18586 : params.m_siphash_k1 = m_block_hash.GetUint64(1);
238 : 18586 : params.m_P = BASIC_FILTER_P;
239 : 18586 : params.m_M = BASIC_FILTER_M;
240 : 18586 : return true;
241 : : case BlockFilterType::INVALID:
242 : : return false;
243 : : }
244 : :
245 : : return false;
246 : : }
247 : :
248 : 34818 : uint256 BlockFilter::GetHash() const
249 : : {
250 : 34818 : return Hash(GetEncodedFilter());
251 : : }
252 : :
253 : 17186 : uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
254 : : {
255 : 17186 : return Hash(GetHash(), prev_header);
256 : : }
|