Skip to content

Commit 593f5e2

Browse files
committed
Merge #17850: Serialization improvements (minimal initial commits)
9250a08 Convert addrdb/addrman to new serialization (Pieter Wuille) ca33451 Introduce new serialization macros without casts (Pieter Wuille) Pull request description: This is a minimal subset of #10785 that still does *something*. It adds a new saner serialization macro, which can be used in parallel with the old one. Then the addrdb code is converted to use this new macro. I'll add follow-up PRs that add more functionality + converting of other modules as things get merged. ACKs for top commit: jamesob: ACK 9250a08 ([`jamesob/ackr/17850.1.sipa.serialization_improvemen`](https://github.com/jamesob/bitcoin/tree/ackr/17850.1.sipa.serialization_improvemen)) kallewoof: ACK 9250a08 laanwj: code review ACK 9250a08 Tree-SHA512: d4f58c7f85d8ada7543ee43159be57d320746abe003af11395508d280d339fac7faa198e707d1a689fb0a775fc36b3945178c3ae1c0cf9ffe685773c6ddc10c1
2 parents 392edbd + 9250a08 commit 593f5e2

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/addrdb.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,7 @@ class CBanEntry
4949
banReason = ban_reason_in;
5050
}
5151

52-
ADD_SERIALIZE_METHODS;
53-
54-
template <typename Stream, typename Operation>
55-
inline void SerializationOp(Stream& s, Operation ser_action) {
56-
READWRITE(this->nVersion);
57-
READWRITE(nCreateTime);
58-
READWRITE(nBanUntil);
59-
READWRITE(banReason);
60-
}
52+
SERIALIZE_METHODS(CBanEntry, obj) { READWRITE(obj.nVersion, obj.nCreateTime, obj.nBanUntil, obj.banReason); }
6153

6254
void SetNull()
6355
{

src/addrman.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ class CAddrInfo : public CAddress
5353

5454
public:
5555

56-
ADD_SERIALIZE_METHODS;
57-
58-
template <typename Stream, typename Operation>
59-
inline void SerializationOp(Stream& s, Operation ser_action) {
60-
READWRITEAS(CAddress, *this);
61-
READWRITE(source);
62-
READWRITE(nLastSuccess);
63-
READWRITE(nAttempts);
56+
SERIALIZE_METHODS(CAddrInfo, obj)
57+
{
58+
READWRITEAS(CAddress, obj);
59+
READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
6460
}
6561

6662
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
@@ -294,7 +290,7 @@ class CAddrMan
294290
* This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
295291
* changes to the ADDRMAN_ parameters without breaking the on-disk structure.
296292
*
297-
* We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
293+
* We don't use SERIALIZE_METHODS since the serialization and deserialization code has
298294
* very little in common.
299295
*/
300296
template<typename Stream>

src/serialize.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
199199
SerializationOp(s, CSerActionUnserialize()); \
200200
}
201201

202+
/**
203+
* Implement the Serialize and Unserialize methods by delegating to a single templated
204+
* static method that takes the to-be-(de)serialized object as a parameter. This approach
205+
* has the advantage that the constness of the object becomes a template parameter, and
206+
* thus allows a single implementation that sees the object as const for serializing
207+
* and non-const for deserializing, without casts.
208+
*/
209+
#define SERIALIZE_METHODS(cls, obj) \
210+
template<typename Stream> \
211+
void Serialize(Stream& s) const \
212+
{ \
213+
static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
214+
SerializationOps(*this, s, CSerActionSerialize()); \
215+
} \
216+
template<typename Stream> \
217+
void Unserialize(Stream& s) \
218+
{ \
219+
static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
220+
SerializationOps(*this, s, CSerActionUnserialize()); \
221+
} \
222+
template<typename Stream, typename Type, typename Operation> \
223+
static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
224+
225+
202226
#ifndef CHAR_EQUALS_INT8
203227
template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
204228
#endif

0 commit comments

Comments
 (0)