Skip to content

Commit 1063339

Browse files
ryanofskysipa
authored andcommitted
Add DifferenceFormatter
1 parent 56dd9f0 commit 1063339

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/blockencodings.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ struct TransactionCompressor {
2525
}
2626
};
2727

28+
class DifferenceFormatter
29+
{
30+
uint64_t m_shift = 0;
31+
32+
public:
33+
template<typename Stream, typename I>
34+
void Ser(Stream& s, I v)
35+
{
36+
if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
37+
WriteCompactSize(s, v - m_shift);
38+
m_shift = uint64_t(v) + 1;
39+
}
40+
template<typename Stream, typename I>
41+
void Unser(Stream& s, I& v)
42+
{
43+
uint64_t n = ReadCompactSize(s);
44+
m_shift += n;
45+
if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() || m_shift < std::numeric_limits<I>::min() || m_shift > std::numeric_limits<I>::max()) throw std::ios_base::failure("differential value overflow");
46+
v = I(m_shift++);
47+
}
48+
};
49+
2850
class BlockTransactionsRequest {
2951
public:
3052
// A BlockTransactionsRequest message

0 commit comments

Comments
 (0)