diff options
author | Jim Posen <jim.posen@gmail.com> | 2018-08-27 15:08:31 -0700 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2018-11-06 09:12:54 -0800 |
commit | c30620983d2e2c9aee6f52878ed14ba685e8683e (patch) | |
tree | ef4dc2869290ff9b0f3902c710ba8d94fdeccc64 /src | |
parent | 20b812993ae584e4a4b12faa5c8751aa04dfde81 (diff) | |
download | bitcoin-c30620983d2e2c9aee6f52878ed14ba685e8683e.tar.xz |
blockfilter: Additional constructors for BlockFilter.
Diffstat (limited to 'src')
-rw-r--r-- | src/blockfilter.cpp | 11 | ||||
-rw-r--r-- | src/blockfilter.h | 14 | ||||
-rw-r--r-- | src/test/blockfilter_tests.cpp | 11 |
3 files changed, 32 insertions, 4 deletions
diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp index 6bbf887b24..379d3ea170 100644 --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -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()) { diff --git a/src/blockfilter.h b/src/blockfilter.h index e53f813ab3..4d1f51dd60 100644 --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -103,11 +103,17 @@ private: 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 @@ -115,10 +121,10 @@ public: 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> diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp index 69149fad4f..625362f446 100644 --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -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) |