Skip to content

Commit 142fb05

Browse files
mzumsandepsgreco
authored andcommitted
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: 5eabb61
1 parent 723eab5 commit 142fb05

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/addrdb.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ bool SerializeDB(Stream& stream, const Data& data)
3333
{
3434
// Write and commit header, data
3535
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();
4039
} catch (const std::exception& e) {
4140
return error("%s: Serialize or I/O error - %s", __func__, e.what());
4241
}

src/addrman.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,7 @@ void AddrMan::Unserialize(Stream& s_)
11711171
}
11721172

11731173
// explicit instantiation
1174-
template void AddrMan::Serialize(CHashWriter& s) const;
1175-
template void AddrMan::Serialize(CAutoFile& s) const;
1174+
template void AddrMan::Serialize(HashedSourceWriter<CAutoFile>& s) const;
11761175
template void AddrMan::Serialize(CDataStream& s) const;
11771176
template void AddrMan::Unserialize(CAutoFile& s);
11781177
template void AddrMan::Unserialize(CHashVerifier<CAutoFile>& s);

0 commit comments

Comments
 (0)