Skip to content

Commit 5eabb61

Browse files
committed
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.
1 parent da6c7ae commit 5eabb61

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
@@ -34,10 +34,9 @@ bool SerializeDB(Stream& stream, const Data& data)
3434
{
3535
// Write and commit header, data
3636
try {
37-
CHashWriter hasher(stream.GetType(), stream.GetVersion());
38-
stream << Params().MessageStart() << data;
39-
hasher << Params().MessageStart() << data;
40-
stream << hasher.GetHash();
37+
HashedSourceWriter hashwriter{stream};
38+
hashwriter << Params().MessageStart() << data;
39+
stream << hashwriter.GetHash();
4140
} catch (const std::exception& e) {
4241
return error("%s: Serialize or I/O error - %s", __func__, e.what());
4342
}

src/addrman.cpp

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

11801180
// explicit instantiation
1181-
template void AddrMan::Serialize(CHashWriter& s) const;
1182-
template void AddrMan::Serialize(CAutoFile& s) const;
1181+
template void AddrMan::Serialize(HashedSourceWriter<CAutoFile>& s) const;
11831182
template void AddrMan::Serialize(CDataStream& s) const;
11841183
template void AddrMan::Unserialize(CAutoFile& s);
11851184
template void AddrMan::Unserialize(CHashVerifier<CAutoFile>& s);

0 commit comments

Comments
 (0)