@@ -79,7 +79,7 @@ static uint64_t MapIntoRange(uint64_t x, uint64_t n)
7979
8080uint64_t GCSFilter::HashToRange (const Element& element) const
8181{
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 )
8383 .Write (element.data (), element.size ())
8484 .Finalize ();
8585 return MapIntoRange (hash, m_F);
@@ -96,46 +96,42 @@ std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) cons
9696 return hashed_elements;
9797}
9898
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 }
101101{}
102102
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))
106105{
107- m_encoded = std::move (encoded_filter);
108-
109106 VectorReader stream (GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0 );
110107
111108 uint64_t N = ReadCompactSize (stream);
112109 m_N = static_cast <uint32_t >(N);
113110 if (m_N != N) {
114111 throw std::ios_base::failure (" N must be <2^32" );
115112 }
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 );
117114
118115 // Verify that the encoded filter contains exactly N elements. If it has too much or too little
119116 // data, a std::ios_base::failure exception will be raised.
120117 BitStreamReader<VectorReader> bitreader (stream);
121118 for (uint64_t i = 0 ; i < m_N; ++i) {
122- GolombRiceDecode (bitreader, m_P);
119+ GolombRiceDecode (bitreader, m_params. m_P );
123120 }
124121 if (!stream.empty ()) {
125122 throw std::ios_base::failure (" encoded_filter contains excess data" );
126123 }
127124}
128125
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)
132128{
133129 size_t N = elements.size ();
134130 m_N = static_cast <uint32_t >(N);
135131 if (m_N != N) {
136132 throw std::invalid_argument (" N must be <2^32" );
137133 }
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 );
139135
140136 CVectorWriter stream (GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0 );
141137
@@ -150,7 +146,7 @@ GCSFilter::GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32
150146 uint64_t last_value = 0 ;
151147 for (uint64_t value : BuildHashedSet (elements)) {
152148 uint64_t delta = value - last_value;
153- GolombRiceEncode (bitwriter, m_P, delta);
149+ GolombRiceEncode (bitwriter, m_params. m_P , delta);
154150 last_value = value;
155151 }
156152
@@ -170,7 +166,7 @@ bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
170166 uint64_t value = 0 ;
171167 size_t hashes_index = 0 ;
172168 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 );
174170 value += delta;
175171
176172 while (true ) {
@@ -227,17 +223,29 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
227223
228224BlockFilter::BlockFilter (BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
229225 : m_filter_type(filter_type), m_block_hash(block.GetHash())
226+ {
227+ GCSFilter::Params params;
228+ if (!BuildParams (params)) {
229+ throw std::invalid_argument (" unknown filter_type" );
230+ }
231+ m_filter = GCSFilter (params, BasicFilterElements (block, block_undo));
232+ }
233+
234+ bool BlockFilter::BuildParams (GCSFilter::Params& params) const
230235{
231236 switch (m_filter_type) {
232237 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));
238+ params.m_siphash_k0 = m_block_hash.GetUint64 (0 );
239+ params.m_siphash_k1 = m_block_hash.GetUint64 (1 );
240+ params.m_P = BASIC_FILTER_P;
241+ params.m_M = BASIC_FILTER_M;
236242 break ;
237243
238244 default :
239- throw std::invalid_argument ( " unknown filter_type " ) ;
245+ return false ;
240246 }
247+
248+ return true ;
241249}
242250
243251uint256 BlockFilter::GetHash () const
0 commit comments