From fa961141f7fc515fbb6fcb9d8417108034b256ce Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 3 Jan 2023 12:56:06 +0100 Subject: [PATCH] Add HashVerifier It is similar to CHashVerifier, but HashVerifier does not need a serialize type and version --- src/hash.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/hash.h b/src/hash.h index b18a0312685..b2ef29fcdbd 100644 --- a/src/hash.h +++ b/src/hash.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_HASH_H #define BITCOIN_HASH_H +#include #include #include #include @@ -165,6 +166,39 @@ public: }; /** Reads data from an underlying stream, while hashing the read data. */ +template +class HashVerifier : public HashWriter +{ +private: + Source& m_source; + +public: + explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} + + void read(Span dst) + { + m_source.read(dst); + this->write(dst); + } + + void ignore(size_t num_bytes) + { + std::byte data[1024]; + while (num_bytes > 0) { + size_t now = std::min(num_bytes, 1024); + read({data, now}); + num_bytes -= now; + } + } + + template + HashVerifier& operator>>(T&& obj) + { + ::Unserialize(*this, obj); + return *this; + } +}; + template class CHashVerifier : public CHashWriter {