Skip to content

Commit eff833c

Browse files
committed
refactor: Use BlockFilterBase within BlockFilter
This commit allows for `BlockFilter` to hold any type that implements the properties of a block filter, rather than concretely holding a GCS filter.
1 parent 84524f1 commit eff833c

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/blockfilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
216216
if (!BuildParams(params)) {
217217
throw std::invalid_argument("unknown filter_type");
218218
}
219-
m_filter = GCSFilter(params, std::move(filter), skip_decode_check);
219+
m_filter = std::make_unique<GCSFilter>(params, std::move(filter), skip_decode_check);
220220
}
221221

222222
BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
@@ -226,7 +226,7 @@ BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const
226226
if (!BuildParams(params)) {
227227
throw std::invalid_argument("unknown filter_type");
228228
}
229-
m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));
229+
m_filter = std::make_unique<GCSFilter>(params, BasicFilterElements(block, block_undo));
230230
}
231231

232232
bool BlockFilter::BuildParams(GCSFilter::Params& params) const

src/blockfilter.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cstddef>
99
#include <cstdint>
1010
#include <ios>
11+
#include <memory>
1112
#include <set>
1213
#include <string>
1314
#include <string_view>
@@ -135,13 +136,13 @@ class BlockFilter
135136
private:
136137
BlockFilterType m_filter_type = BlockFilterType::INVALID;
137138
uint256 m_block_hash;
138-
GCSFilter m_filter;
139+
std::unique_ptr<BlockFilterBase> m_filter;
139140

140141
bool BuildParams(GCSFilter::Params& params) const;
141142

142143
public:
143144

144-
BlockFilter() = default;
145+
BlockFilter() : m_filter(std::make_unique<GCSFilter>()) {}
145146

146147
//! Reconstruct a BlockFilter from parts.
147148
BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
@@ -152,11 +153,11 @@ class BlockFilter
152153

153154
BlockFilterType GetFilterType() const { return m_filter_type; }
154155
const uint256& GetBlockHash() const LIFETIMEBOUND { return m_block_hash; }
155-
const GCSFilter& GetFilter() const LIFETIMEBOUND { return m_filter; }
156+
const BlockFilterBase& GetFilter() const LIFETIMEBOUND { return *m_filter; }
156157

157158
const std::vector<unsigned char>& GetEncodedFilter() const LIFETIMEBOUND
158159
{
159-
return m_filter.GetEncoded();
160+
return m_filter->GetEncoded();
160161
}
161162

162163
//! Compute the filter hash.
@@ -169,7 +170,7 @@ class BlockFilter
169170
void Serialize(Stream& s) const {
170171
s << static_cast<uint8_t>(m_filter_type)
171172
<< m_block_hash
172-
<< m_filter.GetEncoded();
173+
<< m_filter->GetEncoded();
173174
}
174175

175176
template <typename Stream>
@@ -187,7 +188,7 @@ class BlockFilter
187188
if (!BuildParams(params)) {
188189
throw std::ios_base::failure("unknown filter_type");
189190
}
190-
m_filter = GCSFilter(params, std::move(encoded_filter), /*skip_decode_check=*/false);
191+
m_filter = std::make_unique<GCSFilter>(params, std::move(encoded_filter), /*skip_decode_check=*/false);
191192
}
192193
};
193194

src/test/blockfilter_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
100100
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[3]), 100000, false);
101101

102102
BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);
103-
const GCSFilter& filter = block_filter.GetFilter();
103+
const BlockFilterBase& filter = block_filter.GetFilter();
104104

105105
for (const CScript& script : included_scripts) {
106106
BOOST_CHECK(filter.Match(GCSFilter::Element(script.begin(), script.end())));

src/test/fuzz/blockfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ FUZZ_TARGET(blockfilter)
3232
(void)BlockFilterTypeName(block_filter_type);
3333
}
3434
{
35-
const GCSFilter gcs_filter = block_filter->GetFilter();
35+
const GCSFilter& gcs_filter = dynamic_cast<const GCSFilter&>(block_filter->GetFilter());
3636
(void)gcs_filter.GetN();
3737
(void)gcs_filter.GetParams();
3838
(void)gcs_filter.GetEncoded();

0 commit comments

Comments
 (0)