diff options
author | Russell Yanofsky <russ@yanofsky.org> | 2020-02-15 19:05:11 -0800 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2020-02-15 19:49:24 -0800 |
commit | 10633398f2dddf929d3f535aa48d138ad5e6c50f (patch) | |
tree | c0f0d66e2920d53d43fca2fdd0596b0a4f8afe55 /src/blockencodings.h | |
parent | 56dd9f04c701aa3ac340e95065bf83de20373c8b (diff) |
Add DifferenceFormatter
Diffstat (limited to 'src/blockencodings.h')
-rw-r--r-- | src/blockencodings.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/blockencodings.h b/src/blockencodings.h index 55ed8989bb..606c11b417 100644 --- a/src/blockencodings.h +++ b/src/blockencodings.h @@ -25,6 +25,28 @@ public: } }; +class DifferenceFormatter +{ + uint64_t m_shift = 0; + +public: + template<typename Stream, typename I> + void Ser(Stream& s, I v) + { + if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow"); + WriteCompactSize(s, v - m_shift); + m_shift = uint64_t(v) + 1; + } + template<typename Stream, typename I> + void Unser(Stream& s, I& v) + { + uint64_t n = ReadCompactSize(s); + m_shift += n; + 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"); + v = I(m_shift++); + } +}; + class BlockTransactionsRequest { public: // A BlockTransactionsRequest message |