aboutsummaryrefslogtreecommitdiff
path: root/src/blockfilter.h
diff options
context:
space:
mode:
authorJim Posen <jimpo@coinbase.com>2018-01-23 17:27:06 -0800
committerJim Posen <jim.posen@gmail.com>2018-08-25 10:02:37 -0700
commitcd09c7925b5af4104834971cfe072251e3ac2bda (patch)
treec9205cf5497ba8a07b64b15738613b0133a3231e /src/blockfilter.h
parentc1855f6052aca806fdb51be01b30dfeee8b55f40 (diff)
downloadbitcoin-cd09c7925b5af4104834971cfe072251e3ac2bda.tar.xz
blockfilter: Serialization methods on BlockFilter.
Diffstat (limited to 'src/blockfilter.h')
-rw-r--r--src/blockfilter.h32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/blockfilter.h b/src/blockfilter.h
index afb8d7a7b7..010f868aee 100644
--- a/src/blockfilter.h
+++ b/src/blockfilter.h
@@ -82,7 +82,8 @@ enum BlockFilterType : uint8_t
};
/**
- * Complete block filter struct as defined in BIP 157.
+ * Complete block filter struct as defined in BIP 157. Serialization matches
+ * payload of "cfilter" messages.
*/
class BlockFilter
{
@@ -104,6 +105,35 @@ public:
{
return m_filter.GetEncoded();
}
+
+ template <typename Stream>
+ void Serialize(Stream& s) const {
+ s << m_block_hash
+ << static_cast<uint8_t>(m_filter_type)
+ << m_filter.GetEncoded();
+ }
+
+ template <typename Stream>
+ void Unserialize(Stream& s) {
+ std::vector<unsigned char> encoded_filter;
+ uint8_t filter_type;
+
+ s >> m_block_hash
+ >> filter_type
+ >> encoded_filter;
+
+ m_filter_type = static_cast<BlockFilterType>(filter_type);
+
+ switch (m_filter_type) {
+ case BlockFilterType::BASIC:
+ m_filter = GCSFilter(m_block_hash.GetUint64(0), m_block_hash.GetUint64(1),
+ BASIC_FILTER_P, BASIC_FILTER_M, std::move(encoded_filter));
+ break;
+
+ default:
+ throw std::ios_base::failure("unknown filter_type");
+ }
+ }
};
#endif // BITCOIN_BLOCKFILTER_H