diff options
author | Wladimir J. van der Laan <laanwj@protonmail.com> | 2020-01-04 13:28:41 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@protonmail.com> | 2020-01-04 13:28:51 +0100 |
commit | 593f5e239f277e3fd917737122c2aed3c615430c (patch) | |
tree | 15200827bc6fad67a8ab1dfef05c77ea2072209d /src | |
parent | 392edbd49abec4a8c587979dbb1f44e8605b7ce1 (diff) | |
parent | 9250a087d2f450f37342010aea0a5d583eea508b (diff) |
Merge #17850: Serialization improvements (minimal initial commits)
9250a087d2f450f37342010aea0a5d583eea508b Convert addrdb/addrman to new serialization (Pieter Wuille)
ca33451535dc29f64d37c49af8b22142d7716d0d 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 9250a087d2f450f37342010aea0a5d583eea508b ([`jamesob/ackr/17850.1.sipa.serialization_improvemen`](https://github.com/jamesob/bitcoin/tree/ackr/17850.1.sipa.serialization_improvemen))
kallewoof:
ACK 9250a087d2f450f37342010aea0a5d583eea508b
laanwj:
code review ACK 9250a087d2f450f37342010aea0a5d583eea508b
Tree-SHA512: d4f58c7f85d8ada7543ee43159be57d320746abe003af11395508d280d339fac7faa198e707d1a689fb0a775fc36b3945178c3ae1c0cf9ffe685773c6ddc10c1
Diffstat (limited to 'src')
-rw-r--r-- | src/addrdb.h | 10 | ||||
-rw-r--r-- | src/addrman.h | 14 | ||||
-rw-r--r-- | src/serialize.h | 24 |
3 files changed, 30 insertions, 18 deletions
diff --git a/src/addrdb.h b/src/addrdb.h index d84901fcb8..c6d4307d69 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -49,15 +49,7 @@ public: banReason = ban_reason_in; } - ADD_SERIALIZE_METHODS; - - template <typename Stream, typename Operation> - inline void SerializationOp(Stream& s, Operation ser_action) { - READWRITE(this->nVersion); - READWRITE(nCreateTime); - READWRITE(nBanUntil); - READWRITE(banReason); - } + SERIALIZE_METHODS(CBanEntry, obj) { READWRITE(obj.nVersion, obj.nCreateTime, obj.nBanUntil, obj.banReason); } void SetNull() { diff --git a/src/addrman.h b/src/addrman.h index ee404f6939..c001d59da5 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -53,14 +53,10 @@ private: public: - ADD_SERIALIZE_METHODS; - - template <typename Stream, typename Operation> - inline void SerializationOp(Stream& s, Operation ser_action) { - READWRITEAS(CAddress, *this); - READWRITE(source); - READWRITE(nLastSuccess); - READWRITE(nAttempts); + SERIALIZE_METHODS(CAddrInfo, obj) + { + READWRITEAS(CAddress, obj); + READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts); } CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource) @@ -294,7 +290,7 @@ public: * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports * changes to the ADDRMAN_ parameters without breaking the on-disk structure. * - * We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has + * We don't use SERIALIZE_METHODS since the serialization and deserialization code has * very little in common. */ template<typename Stream> diff --git a/src/serialize.h b/src/serialize.h index c84a1567f1..c9e994f844 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -199,6 +199,30 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; } SerializationOp(s, CSerActionUnserialize()); \ } +/** + * Implement the Serialize and Unserialize methods by delegating to a single templated + * static method that takes the to-be-(de)serialized object as a parameter. This approach + * has the advantage that the constness of the object becomes a template parameter, and + * thus allows a single implementation that sees the object as const for serializing + * and non-const for deserializing, without casts. + */ +#define SERIALIZE_METHODS(cls, obj) \ + template<typename Stream> \ + void Serialize(Stream& s) const \ + { \ + static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \ + SerializationOps(*this, s, CSerActionSerialize()); \ + } \ + template<typename Stream> \ + void Unserialize(Stream& s) \ + { \ + static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \ + SerializationOps(*this, s, CSerActionUnserialize()); \ + } \ + template<typename Stream, typename Type, typename Operation> \ + static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \ + + #ifndef CHAR_EQUALS_INT8 template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char #endif |