Skip to content

Commit a4564b9

Browse files
author
MarcoFalke
committed
Merge #14172: Refactor and add tests for BlockFilter construction
e4ed8ce blockfilter: Remove default clause in switch statement. (Jim Posen) c306209 blockfilter: Additional constructors for BlockFilter. (Jim Posen) 20b8129 blockfilter: Refactor GCS params into struct. (Jim Posen) Pull request description: These commits have been split out of #14121 because they are fairly independent and that PR is very large. Tree-SHA512: b9643b159e114df50a295f433e807afe6082db55a2a3a17401c1509b850c71bf5011ab3638863b46663709726be4445be6fde1dec514aec7696135497a9f0183
2 parents cc7ad2f + e4ed8ce commit a4564b9

File tree

4 files changed

+98
-48
lines changed

4 files changed

+98
-48
lines changed

src/bench/gcs_filter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void ConstructGCSFilter(benchmark::State& state)
1717

1818
uint64_t siphash_k0 = 0;
1919
while (state.KeepRunning()) {
20-
GCSFilter filter(siphash_k0, 0, 20, 1 << 20, elements);
20+
GCSFilter filter({siphash_k0, 0, 20, 1 << 20}, elements);
2121

2222
siphash_k0++;
2323
}
@@ -32,7 +32,7 @@ static void MatchGCSFilter(benchmark::State& state)
3232
element[1] = static_cast<unsigned char>(i >> 8);
3333
elements.insert(std::move(element));
3434
}
35-
GCSFilter filter(0, 0, 20, 1 << 20, elements);
35+
GCSFilter filter({0, 0, 20, 1 << 20}, elements);
3636

3737
while (state.KeepRunning()) {
3838
filter.Match(GCSFilter::Element());

src/blockfilter.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static uint64_t MapIntoRange(uint64_t x, uint64_t n)
7979

8080
uint64_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) {
@@ -225,19 +221,39 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
225221
return elements;
226222
}
227223

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+
228235
BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
229236
: 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
230246
{
231247
switch (m_filter_type) {
232248
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;
240254
}
255+
256+
return false;
241257
}
242258

243259
uint256 BlockFilter::GetHash() const

src/blockfilter.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ class GCSFilter
2525
typedef std::vector<unsigned char> Element;
2626
typedef std::unordered_set<Element, ByteVectorHash> ElementSet;
2727

28+
struct Params
29+
{
30+
uint64_t m_siphash_k0;
31+
uint64_t m_siphash_k1;
32+
uint8_t m_P; //!< Golomb-Rice coding parameter
33+
uint32_t m_M; //!< Inverse false positive rate
34+
35+
Params(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 1)
36+
: m_siphash_k0(siphash_k0), m_siphash_k1(siphash_k1), m_P(P), m_M(M)
37+
{}
38+
};
39+
2840
private:
29-
uint64_t m_siphash_k0;
30-
uint64_t m_siphash_k1;
31-
uint8_t m_P; //!< Golomb-Rice coding parameter
32-
uint32_t m_M; //!< Inverse false positive rate
41+
Params m_params;
3342
uint32_t m_N; //!< Number of elements in the filter
3443
uint64_t m_F; //!< Range of element hashes, F = N * M
3544
std::vector<unsigned char> m_encoded;
@@ -45,19 +54,16 @@ class GCSFilter
4554
public:
4655

4756
/** Constructs an empty filter. */
48-
GCSFilter(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 0);
57+
explicit GCSFilter(const Params& params = Params());
4958

5059
/** Reconstructs an already-created filter from an encoding. */
51-
GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
52-
std::vector<unsigned char> encoded_filter);
60+
GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter);
5361

5462
/** Builds a new filter from the params and set of elements. */
55-
GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32_t M,
56-
const ElementSet& elements);
63+
GCSFilter(const Params& params, const ElementSet& elements);
5764

58-
uint8_t GetP() const { return m_P; }
5965
uint32_t GetN() const { return m_N; }
60-
uint32_t GetM() const { return m_M; }
66+
const Params& GetParams() const { return m_params; }
6167
const std::vector<unsigned char>& GetEncoded() const { return m_encoded; }
6268

6369
/**
@@ -93,24 +99,32 @@ class BlockFilter
9399
uint256 m_block_hash;
94100
GCSFilter m_filter;
95101

102+
bool BuildParams(GCSFilter::Params& params) const;
103+
96104
public:
97105

98-
// Construct a new BlockFilter of the specified type from a block.
106+
BlockFilter() = default;
107+
108+
//! Reconstruct a BlockFilter from parts.
109+
BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
110+
std::vector<unsigned char> filter);
111+
112+
//! Construct a new BlockFilter of the specified type from a block.
99113
BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);
100114

101115
BlockFilterType GetFilterType() const { return m_filter_type; }
102-
116+
const uint256& GetBlockHash() const { return m_block_hash; }
103117
const GCSFilter& GetFilter() const { return m_filter; }
104118

105119
const std::vector<unsigned char>& GetEncodedFilter() const
106120
{
107121
return m_filter.GetEncoded();
108122
}
109123

110-
// Compute the filter hash.
124+
//! Compute the filter hash.
111125
uint256 GetHash() const;
112126

113-
// Compute the filter header given the previous one.
127+
//! Compute the filter header given the previous one.
114128
uint256 ComputeHeader(const uint256& prev_header) const;
115129

116130
template <typename Stream>
@@ -131,15 +145,11 @@ class BlockFilter
131145

132146
m_filter_type = static_cast<BlockFilterType>(filter_type);
133147

134-
switch (m_filter_type) {
135-
case BlockFilterType::BASIC:
136-
m_filter = GCSFilter(m_block_hash.GetUint64(0), m_block_hash.GetUint64(1),
137-
BASIC_FILTER_P, BASIC_FILTER_M, std::move(encoded_filter));
138-
break;
139-
140-
default:
148+
GCSFilter::Params params;
149+
if (!BuildParams(params)) {
141150
throw std::ios_base::failure("unknown filter_type");
142151
}
152+
m_filter = GCSFilter(params, std::move(encoded_filter));
143153
}
144154
};
145155

src/test/blockfilter_tests.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BOOST_AUTO_TEST_CASE(gcsfilter_test)
2929
excluded_elements.insert(std::move(element2));
3030
}
3131

32-
GCSFilter filter(0, 0, 10, 1 << 10, included_elements);
32+
GCSFilter filter({0, 0, 10, 1 << 10}, included_elements);
3333
for (const auto& element : included_elements) {
3434
BOOST_CHECK(filter.Match(element));
3535

@@ -39,6 +39,19 @@ BOOST_AUTO_TEST_CASE(gcsfilter_test)
3939
}
4040
}
4141

42+
BOOST_AUTO_TEST_CASE(gcsfilter_default_constructor)
43+
{
44+
GCSFilter filter;
45+
BOOST_CHECK_EQUAL(filter.GetN(), 0);
46+
BOOST_CHECK_EQUAL(filter.GetEncoded().size(), 1);
47+
48+
const GCSFilter::Params& params = filter.GetParams();
49+
BOOST_CHECK_EQUAL(params.m_siphash_k0, 0);
50+
BOOST_CHECK_EQUAL(params.m_siphash_k1, 0);
51+
BOOST_CHECK_EQUAL(params.m_P, 0);
52+
BOOST_CHECK_EQUAL(params.m_M, 1);
53+
}
54+
4255
BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
4356
{
4457
CScript included_scripts[5], excluded_scripts[3];
@@ -88,6 +101,17 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
88101
for (const CScript& script : excluded_scripts) {
89102
BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
90103
}
104+
105+
// Test serialization/unserialization.
106+
BlockFilter block_filter2;
107+
108+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
109+
stream << block_filter;
110+
stream >> block_filter2;
111+
112+
BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
113+
BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
114+
BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
91115
}
92116

93117
BOOST_AUTO_TEST_CASE(blockfilters_json_test)

0 commit comments

Comments
 (0)