mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
Merge #17850: Serialization improvements (minimal initial commits)
9250a087d2
Convert addrdb/addrman to new serialization (Pieter Wuille)ca33451535
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: ACK9250a087d2
([`jamesob/ackr/17850.1.sipa.serialization_improvemen`](https://github.com/jamesob/bitcoin/tree/ackr/17850.1.sipa.serialization_improvemen)) kallewoof: ACK9250a087d2
laanwj: code review ACK9250a087d2
Tree-SHA512: d4f58c7f85d8ada7543ee43159be57d320746abe003af11395508d280d339fac7faa198e707d1a689fb0a775fc36b3945178c3ae1c0cf9ffe685773c6ddc10c1
This commit is contained in:
commit
593f5e239f
3 changed files with 30 additions and 18 deletions
10
src/addrdb.h
10
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()
|
||||
{
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue