Nit: Starting with C++17 you don't need to specify redundant template parameters to constructor calls. Also some other style nits (feel free to ignore):
diff --git a/src/addrdb.cpp b/src/addrdb.cpp
index cee8d5e66a..900ba41025 100644
--- a/src/addrdb.cpp
+++ b/src/addrdb.cpp
@@ -34,7 +34,7 @@ bool SerializeDB(Stream& stream, const Data& data)
{
// Write and commit header, data
try {
- CHashedSourceWriter<Stream> hashwriter(&stream);
+ CHashedSourceWriter hashwriter{stream};
hashwriter << Params().MessageStart() << data;
hashwriter << hashwriter.GetHash();
} catch (const std::exception& e) {
diff --git a/src/hash.h b/src/hash.h
index 7b2f82c456..31370c301a 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -6,6 +6,7 @@
#ifndef BITCOIN_HASH_H
#define BITCOIN_HASH_H
+#include <attributes.h>
#include <crypto/common.h>
#include <crypto/ripemd160.h>
#include <crypto/sha256.h>
@@ -204,23 +205,22 @@ template <typename Source>
class CHashedSourceWriter : public CHashWriter
{
private:
- Source* source;
+ Source& m_source;
public:
- explicit CHashedSourceWriter(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
+ explicit CHashedSourceWriter(Source& source LIFETIMEBOUND) : CHashWriter{source.GetType(), source.GetVersion()}, m_source{source} {}
void write(Span<const std::byte> src)
{
- source->write(src);
+ m_source.write(src);
CHashWriter::write(src);
}
template <typename T>
CHashedSourceWriter<Source>& operator<<(const T& obj)
{
- // Serialize to this stream
::Serialize(*this, obj);
- return (*this);
+ return *this;
}
};
diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp
index 7d2a1b6818..90ad61ca6c 100644
--- a/src/test/streams_tests.cpp
+++ b/src/test/streams_tests.cpp
@@ -503,11 +503,11 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
BOOST_AUTO_TEST_CASE(streams_hashed)
{
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
- CHashedSourceWriter<CDataStream> hash_writer(&stream);
+ CHashedSourceWriter hash_writer{stream};
const std::string data{"bitcoin"};
hash_writer << data;
- CHashVerifier<CDataStream> hash_verifier(&stream);
+ CHashVerifier hash_verifier{stream};
std::string result;
hash_verifier >> result;
BOOST_CHECK_EQUAL(data, result);