@@ -79,7 +79,7 @@ static uint64_t MapIntoRange(uint64_t x, uint64_t n)
79
79
80
80
uint64_t GCSFilter::HashToRange (const Element& element) const
81
81
{
82
- uint64_t hash = CSipHasher (m_siphash_k0, m_siphash_k1)
82
+ uint64_t hash = CSipHasher (m_params. m_siphash_k0 , m_params. m_siphash_k1 )
83
83
.Write (element.data (), element.size ())
84
84
.Finalize ();
85
85
return MapIntoRange (hash, m_F);
@@ -96,46 +96,42 @@ std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) cons
96
96
return hashed_elements;
97
97
}
98
98
99
- GCSFilter::GCSFilter (uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M )
100
- : m_siphash_k0(siphash_k0 ), m_siphash_k1(siphash_k1), m_P(P), m_M(M), m_N(0 ), m_F(0 )
99
+ GCSFilter::GCSFilter (const Params& params )
100
+ : m_params(params ), m_N(0 ), m_F(0 ), m_encoded{ 0 }
101
101
{}
102
102
103
- GCSFilter::GCSFilter (uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
104
- std::vector<unsigned char > encoded_filter)
105
- : GCSFilter(siphash_k0, siphash_k1, P, M)
103
+ GCSFilter::GCSFilter (const Params& params, std::vector<unsigned char > encoded_filter)
104
+ : m_params(params), m_encoded(std::move(encoded_filter))
106
105
{
107
- m_encoded = std::move (encoded_filter);
108
-
109
106
VectorReader stream (GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0 );
110
107
111
108
uint64_t N = ReadCompactSize (stream);
112
109
m_N = static_cast <uint32_t >(N);
113
110
if (m_N != N) {
114
111
throw std::ios_base::failure (" N must be <2^32" );
115
112
}
116
- m_F = static_cast <uint64_t >(m_N) * static_cast <uint64_t >(m_M);
113
+ m_F = static_cast <uint64_t >(m_N) * static_cast <uint64_t >(m_params. m_M );
117
114
118
115
// Verify that the encoded filter contains exactly N elements. If it has too much or too little
119
116
// data, a std::ios_base::failure exception will be raised.
120
117
BitStreamReader<VectorReader> bitreader (stream);
121
118
for (uint64_t i = 0 ; i < m_N; ++i) {
122
- GolombRiceDecode (bitreader, m_P);
119
+ GolombRiceDecode (bitreader, m_params. m_P );
123
120
}
124
121
if (!stream.empty ()) {
125
122
throw std::ios_base::failure (" encoded_filter contains excess data" );
126
123
}
127
124
}
128
125
129
- GCSFilter::GCSFilter (uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
130
- const ElementSet& elements)
131
- : GCSFilter(siphash_k0, siphash_k1, P, M)
126
+ GCSFilter::GCSFilter (const Params& params, const ElementSet& elements)
127
+ : m_params(params)
132
128
{
133
129
size_t N = elements.size ();
134
130
m_N = static_cast <uint32_t >(N);
135
131
if (m_N != N) {
136
132
throw std::invalid_argument (" N must be <2^32" );
137
133
}
138
- m_F = static_cast <uint64_t >(m_N) * static_cast <uint64_t >(m_M);
134
+ m_F = static_cast <uint64_t >(m_N) * static_cast <uint64_t >(m_params. m_M );
139
135
140
136
CVectorWriter stream (GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0 );
141
137
@@ -150,7 +146,7 @@ GCSFilter::GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32
150
146
uint64_t last_value = 0 ;
151
147
for (uint64_t value : BuildHashedSet (elements)) {
152
148
uint64_t delta = value - last_value;
153
- GolombRiceEncode (bitwriter, m_P, delta);
149
+ GolombRiceEncode (bitwriter, m_params. m_P , delta);
154
150
last_value = value;
155
151
}
156
152
@@ -170,7 +166,7 @@ bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
170
166
uint64_t value = 0 ;
171
167
size_t hashes_index = 0 ;
172
168
for (uint32_t i = 0 ; i < m_N; ++i) {
173
- uint64_t delta = GolombRiceDecode (bitreader, m_P);
169
+ uint64_t delta = GolombRiceDecode (bitreader, m_params. m_P );
174
170
value += delta;
175
171
176
172
while (true ) {
@@ -225,19 +221,39 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
225
221
return elements;
226
222
}
227
223
224
+ BlockFilter::BlockFilter (BlockFilterType filter_type, const uint256& block_hash,
225
+ std::vector<unsigned char > filter)
226
+ : m_filter_type(filter_type), m_block_hash(block_hash)
227
+ {
228
+ GCSFilter::Params params;
229
+ if (!BuildParams (params)) {
230
+ throw std::invalid_argument (" unknown filter_type" );
231
+ }
232
+ m_filter = GCSFilter (params, std::move (filter));
233
+ }
234
+
228
235
BlockFilter::BlockFilter (BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
229
236
: m_filter_type(filter_type), m_block_hash(block.GetHash())
237
+ {
238
+ GCSFilter::Params params;
239
+ if (!BuildParams (params)) {
240
+ throw std::invalid_argument (" unknown filter_type" );
241
+ }
242
+ m_filter = GCSFilter (params, BasicFilterElements (block, block_undo));
243
+ }
244
+
245
+ bool BlockFilter::BuildParams (GCSFilter::Params& params) const
230
246
{
231
247
switch (m_filter_type) {
232
248
case BlockFilterType::BASIC:
233
- m_filter = GCSFilter (m_block_hash.GetUint64 (0 ), m_block_hash.GetUint64 (1 ),
234
- BASIC_FILTER_P, BASIC_FILTER_M,
235
- BasicFilterElements (block, block_undo));
236
- break ;
237
-
238
- default :
239
- throw std::invalid_argument (" unknown filter_type" );
249
+ params.m_siphash_k0 = m_block_hash.GetUint64 (0 );
250
+ params.m_siphash_k1 = m_block_hash.GetUint64 (1 );
251
+ params.m_P = BASIC_FILTER_P;
252
+ params.m_M = BASIC_FILTER_M;
253
+ return true ;
240
254
}
255
+
256
+ return false ;
241
257
}
242
258
243
259
uint256 BlockFilter::GetHash () const
0 commit comments