aboutsummaryrefslogtreecommitdiff
path: root/src/hash.h
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2023-01-30 15:57:06 +0000
committerfanquake <fanquake@gmail.com>2023-01-30 15:57:12 +0000
commit0a1d372ad0b83bf5379cd57e5d26eb2e934c593d (patch)
treee997f5ea7e78d6d6a4d20267d5e15e8d9166e3ac /src/hash.h
parent228edafc663403990e8fa7b9768bf7254358b59e (diff)
parenteeee61065fe165dcce9625f7cc4cfce9e432aafa (diff)
Merge bitcoin/bitcoin#26649: refactor: Use AutoFile and HashVerifier (without ser-type and ser-version) where possible
eeee61065fe165dcce9625f7cc4cfce9e432aafa Use AutoFile and HashVerifier where possible (MarcoFalke) fa961141f7fc515fbb6fcb9d8417108034b256ce Add HashVerifier (MarcoFalke) Pull request description: This was done in the context of https://github.com/bitcoin/bitcoin/pull/25284 , but I think it also makes sense standalone. The basic idea is that serialization type should not be initialized when it is not needed. Same for the serialization version. So do this here for `AutoFile` and `HashVerifier`. `CAutoFile` and `CHashVerifier` remain in places where it is not yet possible. ACKs for top commit: stickies-v: ACK eeee61065fe165dcce9625f7cc4cfce9e432aafa Tree-SHA512: 93786778c309ecfdc1ed43552d24ff9d966954d69a47f66faaa6de24daacd25c651f3f62bde5abbb362700298fb3c04ffbd3207a0dd13d0bd8bff7fd6d07dcf8
Diffstat (limited to 'src/hash.h')
-rw-r--r--src/hash.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/hash.h b/src/hash.h
index 951f0458ea..2e3ed11b43 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -167,6 +167,39 @@ public:
};
/** Reads data from an underlying stream, while hashing the read data. */
+template <typename Source>
+class HashVerifier : public HashWriter
+{
+private:
+ Source& m_source;
+
+public:
+ explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
+
+ void read(Span<std::byte> 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<size_t>(num_bytes, 1024);
+ read({data, now});
+ num_bytes -= now;
+ }
+ }
+
+ template <typename T>
+ HashVerifier<Source>& operator>>(T&& obj)
+ {
+ ::Unserialize(*this, obj);
+ return *this;
+ }
+};
+
template<typename Source>
class CHashVerifier : public CHashWriter
{