Skip to content

Commit

Permalink
blockfilter: Additional constructors for BlockFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Nov 6, 2018
1 parent 20b8129 commit c306209
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/blockfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
return elements;
}

BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
std::vector<unsigned char> filter)
: m_filter_type(filter_type), m_block_hash(block_hash)
{
GCSFilter::Params params;
if (!BuildParams(params)) {
throw std::invalid_argument("unknown filter_type");
}
m_filter = GCSFilter(params, std::move(filter));
}

BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
: m_filter_type(filter_type), m_block_hash(block.GetHash())
{
Expand Down
14 changes: 10 additions & 4 deletions src/blockfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,28 @@ class BlockFilter

public:

// Construct a new BlockFilter of the specified type from a block.
BlockFilter() = default;

//! Reconstruct a BlockFilter from parts.
BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
std::vector<unsigned char> filter);

//! Construct a new BlockFilter of the specified type from a block.
BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);

BlockFilterType GetFilterType() const { return m_filter_type; }

const uint256& GetBlockHash() const { return m_block_hash; }
const GCSFilter& GetFilter() const { return m_filter; }

const std::vector<unsigned char>& GetEncodedFilter() const
{
return m_filter.GetEncoded();
}

// Compute the filter hash.
//! Compute the filter hash.
uint256 GetHash() const;

// Compute the filter header given the previous one.
//! Compute the filter header given the previous one.
uint256 ComputeHeader(const uint256& prev_header) const;

template <typename Stream>
Expand Down
11 changes: 11 additions & 0 deletions src/test/blockfilter_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
for (const CScript& script : excluded_scripts) {
BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
}

// Test serialization/unserialization.
BlockFilter block_filter2;

CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << block_filter;
stream >> block_filter2;

BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
}

BOOST_AUTO_TEST_CASE(blockfilters_json_test)
Expand Down

0 comments on commit c306209

Please sign in to comment.