From fd94befbc62e5f6b93f87979cc2011508378043d Mon Sep 17 00:00:00 2001 From: Martin Zumsande Date: Sun, 15 Jan 2023 20:18:11 -0500 Subject: hash: add HashedSourceWriter This class is the counterpart to CHashVerifier, in that it writes data to an underlying source stream, while keeping a hash of the written data. Github-Pull: #26909 Rebased-From: da6c7aeca38e1d0ab5839a374c26af0504d603fc --- src/hash.h | 24 ++++++++++++++++++++++++ src/test/streams_tests.cpp | 14 ++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'src') diff --git a/src/hash.h b/src/hash.h index 9f582842c1..7515fc45a6 100644 --- a/src/hash.h +++ b/src/hash.h @@ -188,6 +188,30 @@ public: } }; +/** Writes data to an underlying source stream, while hashing the written data. */ +template +class HashedSourceWriter : public CHashWriter +{ +private: + Source& m_source; + +public: + explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : CHashWriter{source.GetType(), source.GetVersion()}, m_source{source} {} + + void write(Span src) + { + m_source.write(src); + CHashWriter::write(src); + } + + template + HashedSourceWriter& operator<<(const T& obj) + { + ::Serialize(*this, obj); + return *this; + } +}; + /** Compute the 256-bit hash of an object's serialization. */ template uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 0925e2e9ee..e30ae845ef 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -441,4 +441,18 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand) fs::remove(streams_test_filename); } +BOOST_AUTO_TEST_CASE(streams_hashed) +{ + CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION); + HashedSourceWriter hash_writer{stream}; + const std::string data{"bitcoin"}; + hash_writer << data; + + CHashVerifier hash_verifier{&stream}; + std::string result; + hash_verifier >> result; + BOOST_CHECK_EQUAL(data, result); + BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash()); +} + BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3 From 412cd1a34e035c1a2f35f4b4ac14eb192835950c Mon Sep 17 00:00:00 2001 From: Martin Zumsande Date: Fri, 13 Jan 2023 14:12:25 -0500 Subject: addrdb: Only call Serialize() once The previous logic would call it once for serializing into the filestream, and then again for serializing into the hasher. If AddrMan was changed in between these calls by another thread, the resulting peers.dat would be corrupt with non-matching checksum and data. Fix this by using HashedSourceWriter, which writes the data to the underlying stream and keeps track of the hash in one go. Github-Pull: #26909 Rebased-From: 5eabb61b2386d00e93e6bbb2f493a56d1b326ad9 --- src/addrdb.cpp | 7 +++---- src/addrman.cpp | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 0fa8f3c3da..0d68719478 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -33,10 +33,9 @@ bool SerializeDB(Stream& stream, const Data& data) { // Write and commit header, data try { - CHashWriter hasher(stream.GetType(), stream.GetVersion()); - stream << Params().MessageStart() << data; - hasher << Params().MessageStart() << data; - stream << hasher.GetHash(); + HashedSourceWriter hashwriter{stream}; + hashwriter << Params().MessageStart() << data; + stream << hashwriter.GetHash(); } catch (const std::exception& e) { return error("%s: Serialize or I/O error - %s", __func__, e.what()); } diff --git a/src/addrman.cpp b/src/addrman.cpp index f91a979934..64137e1523 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -1171,8 +1171,7 @@ void AddrMan::Unserialize(Stream& s_) } // explicit instantiation -template void AddrMan::Serialize(CHashWriter& s) const; -template void AddrMan::Serialize(CAutoFile& s) const; +template void AddrMan::Serialize(HashedSourceWriter& s) const; template void AddrMan::Serialize(CDataStream& s) const; template void AddrMan::Unserialize(CAutoFile& s); template void AddrMan::Unserialize(CHashVerifier& s); -- cgit v1.2.3 From 398768769f85cc1b6ff212ed931646b59fa1acd6 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 19 Jan 2023 19:35:43 +0100 Subject: Add missing includes to fix gcc-13 compile error Github-Pull: #26924 Rebased-From: fadeb6b103cb441e0e91ef506ef29febabb10715 --- src/support/lockedpool.cpp | 3 +++ src/support/lockedpool.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/support/lockedpool.cpp b/src/support/lockedpool.cpp index 6965f40253..2ad3161563 100644 --- a/src/support/lockedpool.cpp +++ b/src/support/lockedpool.cpp @@ -22,6 +22,9 @@ #endif #include +#include +#include +#include #ifdef ARENA_DEBUG #include #include diff --git a/src/support/lockedpool.h b/src/support/lockedpool.h index 03e4e371a3..66fbc218ab 100644 --- a/src/support/lockedpool.h +++ b/src/support/lockedpool.h @@ -5,11 +5,11 @@ #ifndef BITCOIN_SUPPORT_LOCKEDPOOL_H #define BITCOIN_SUPPORT_LOCKEDPOOL_H -#include +#include #include #include -#include #include +#include #include /** -- cgit v1.2.3 From af862661654966d5de614755ab9bd1b5913e0959 Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 20 Jan 2023 11:55:29 +0000 Subject: 23.x Add missing includes to fix gcc-13 compile error Additional include fixes are required to make the 23.x branch compile using GCC 13. --- src/util/bip32.h | 1 + src/util/string.h | 1 + 2 files changed, 2 insertions(+) (limited to 'src') diff --git a/src/util/bip32.h b/src/util/bip32.h index 8f86f2aaa6..b1d53616a4 100644 --- a/src/util/bip32.h +++ b/src/util/bip32.h @@ -6,6 +6,7 @@ #define BITCOIN_UTIL_BIP32_H #include +#include #include #include diff --git a/src/util/string.h b/src/util/string.h index a3b8df8d78..5f4859f1d7 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include -- cgit v1.2.3