Skip to content

Commit

Permalink
blockfilter: Serialization methods on BlockFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Posen authored and jimpo committed Aug 25, 2018
1 parent c1855f6 commit cd09c79
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/blockfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -104,6 +105,35 @@ class BlockFilter
{
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

0 comments on commit cd09c79

Please sign in to comment.