Skip to content

Commit cd09c79

Browse files
Jim Posenjimpo
authored andcommitted
blockfilter: Serialization methods on BlockFilter.
1 parent c1855f6 commit cd09c79

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/blockfilter.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ enum BlockFilterType : uint8_t
8282
};
8383

8484
/**
85-
* Complete block filter struct as defined in BIP 157.
85+
* Complete block filter struct as defined in BIP 157. Serialization matches
86+
* payload of "cfilter" messages.
8687
*/
8788
class BlockFilter
8889
{
@@ -104,6 +105,35 @@ class BlockFilter
104105
{
105106
return m_filter.GetEncoded();
106107
}
108+
109+
template <typename Stream>
110+
void Serialize(Stream& s) const {
111+
s << m_block_hash
112+
<< static_cast<uint8_t>(m_filter_type)
113+
<< m_filter.GetEncoded();
114+
}
115+
116+
template <typename Stream>
117+
void Unserialize(Stream& s) {
118+
std::vector<unsigned char> encoded_filter;
119+
uint8_t filter_type;
120+
121+
s >> m_block_hash
122+
>> filter_type
123+
>> encoded_filter;
124+
125+
m_filter_type = static_cast<BlockFilterType>(filter_type);
126+
127+
switch (m_filter_type) {
128+
case BlockFilterType::BASIC:
129+
m_filter = GCSFilter(m_block_hash.GetUint64(0), m_block_hash.GetUint64(1),
130+
BASIC_FILTER_P, BASIC_FILTER_M, std::move(encoded_filter));
131+
break;
132+
133+
default:
134+
throw std::ios_base::failure("unknown filter_type");
135+
}
136+
}
107137
};
108138

109139
#endif // BITCOIN_BLOCKFILTER_H

0 commit comments

Comments
 (0)