File tree Expand file tree Collapse file tree 4 files changed +42
-6
lines changed Expand file tree Collapse file tree 4 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -33,10 +33,9 @@ bool SerializeDB(Stream& stream, const Data& data)
33
33
{
34
34
// Write and commit header, data
35
35
try {
36
- CHashWriter hasher (stream.GetType (), stream.GetVersion ());
37
- stream << Params ().MessageStart () << data;
38
- hasher << Params ().MessageStart () << data;
39
- stream << hasher.GetHash ();
36
+ HashedSourceWriter hashwriter{stream};
37
+ hashwriter << Params ().MessageStart () << data;
38
+ stream << hashwriter.GetHash ();
40
39
} catch (const std::exception& e) {
41
40
return error (" %s: Serialize or I/O error - %s" , __func__, e.what ());
42
41
}
Original file line number Diff line number Diff line change @@ -1168,8 +1168,7 @@ void AddrMan::Unserialize(Stream& s_)
1168
1168
}
1169
1169
1170
1170
// explicit instantiation
1171
- template void AddrMan::Serialize (CHashWriter& s) const ;
1172
- template void AddrMan::Serialize (CAutoFile& s) const ;
1171
+ template void AddrMan::Serialize (HashedSourceWriter<CAutoFile>& s) const ;
1173
1172
template void AddrMan::Serialize (CDataStream& s) const ;
1174
1173
template void AddrMan::Unserialize (CAutoFile& s);
1175
1174
template void AddrMan::Unserialize (CHashVerifier<CAutoFile>& s);
Original file line number Diff line number Diff line change @@ -201,6 +201,30 @@ class CHashVerifier : public CHashWriter
201
201
}
202
202
};
203
203
204
+ /* * Writes data to an underlying source stream, while hashing the written data. */
205
+ template <typename Source>
206
+ class HashedSourceWriter : public CHashWriter
207
+ {
208
+ private:
209
+ Source& m_source;
210
+
211
+ public:
212
+ explicit HashedSourceWriter (Source& source LIFETIMEBOUND) : CHashWriter{source.GetType (), source.GetVersion ()}, m_source{source} {}
213
+
214
+ void write (Span<const std::byte> src)
215
+ {
216
+ m_source.write (src);
217
+ CHashWriter::write (src);
218
+ }
219
+
220
+ template <typename T>
221
+ HashedSourceWriter& operator <<(const T& obj)
222
+ {
223
+ ::Serialize (*this , obj);
224
+ return *this ;
225
+ }
226
+ };
227
+
204
228
/* * Compute the 256-bit hash of an object's serialization. */
205
229
template <typename T>
206
230
uint256 SerializeHash (const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Original file line number Diff line number Diff line change @@ -437,4 +437,18 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
437
437
fs::remove (" streams_test_tmp" );
438
438
}
439
439
440
+ BOOST_AUTO_TEST_CASE (streams_hashed)
441
+ {
442
+ CDataStream stream (SER_NETWORK, INIT_PROTO_VERSION);
443
+ HashedSourceWriter hash_writer{stream};
444
+ const std::string data{" bitcoin" };
445
+ hash_writer << data;
446
+
447
+ CHashVerifier hash_verifier{&stream};
448
+ std::string result;
449
+ hash_verifier >> result;
450
+ BOOST_CHECK_EQUAL (data, result);
451
+ BOOST_CHECK_EQUAL (hash_writer.GetHash (), hash_verifier.GetHash ());
452
+ }
453
+
440
454
BOOST_AUTO_TEST_SUITE_END ()
You can’t perform that action at this time.
0 commit comments