Skip to content

Commit c306209

Browse files
committed
blockfilter: Additional constructors for BlockFilter.
1 parent 20b8129 commit c306209

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/blockfilter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
221221
return elements;
222222
}
223223

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+
224235
BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
225236
: m_filter_type(filter_type), m_block_hash(block.GetHash())
226237
{

src/blockfilter.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,28 @@ class BlockFilter
103103

104104
public:
105105

106-
// 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.
107113
BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);
108114

109115
BlockFilterType GetFilterType() const { return m_filter_type; }
110-
116+
const uint256& GetBlockHash() const { return m_block_hash; }
111117
const GCSFilter& GetFilter() const { return m_filter; }
112118

113119
const std::vector<unsigned char>& GetEncodedFilter() const
114120
{
115121
return m_filter.GetEncoded();
116122
}
117123

118-
// Compute the filter hash.
124+
//! Compute the filter hash.
119125
uint256 GetHash() const;
120126

121-
// Compute the filter header given the previous one.
127+
//! Compute the filter header given the previous one.
122128
uint256 ComputeHeader(const uint256& prev_header) const;
123129

124130
template <typename Stream>

src/test/blockfilter_tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
101101
for (const CScript& script : excluded_scripts) {
102102
BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
103103
}
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());
104115
}
105116

106117
BOOST_AUTO_TEST_CASE(blockfilters_json_test)

0 commit comments

Comments
 (0)