diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 0fcb5ed5c97..c5b474339b9 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -47,16 +47,16 @@ bool SerializeDB(Stream& stream, const Data& data) } template -bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version) +bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data) { // Generate random temporary filename const uint16_t randv{GetRand()}; std::string tmpfn = strprintf("%s.%04x", prefix, randv); - // open temp output file, and associate with CAutoFile + // open temp output file fs::path pathTmp = gArgs.GetDataDirNet() / fs::u8path(tmpfn); FILE *file = fsbridge::fopen(pathTmp, "wb"); - CAutoFile fileout(file, SER_DISK, version); + AutoFile fileout{file}; if (fileout.IsNull()) { fileout.fclose(); remove(pathTmp); @@ -86,9 +86,9 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data } template -void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true) +void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true) { - CHashVerifier verifier(&stream); + HashVerifier verifier{stream}; // de-serialize file header (network specific magic number) and .. unsigned char pchMsgTmp[4]; verifier >> pchMsgTmp; @@ -111,11 +111,10 @@ void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true) } template -void DeserializeFileDB(const fs::path& path, Data& data, int version) +void DeserializeFileDB(const fs::path& path, Data&& data) { - // open input file, and associate with CAutoFile FILE* file = fsbridge::fopen(path, "rb"); - CAutoFile filein(file, SER_DISK, version); + AutoFile filein{file}; if (filein.IsNull()) { throw DbNotFoundError{}; } @@ -175,10 +174,10 @@ bool CBanDB::Read(banmap_t& banSet) bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr) { const auto pathAddr = args.GetDataDirNet() / "peers.dat"; - return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION); + return SerializeFileDB("peers", pathAddr, addr); } -void ReadFromStream(AddrMan& addr, CDataStream& ssPeers) +void ReadFromStream(AddrMan& addr, DataStream& ssPeers) { DeserializeDB(ssPeers, addr, false); } @@ -191,7 +190,7 @@ util::Result> LoadAddrman(const NetGroupManager& netgro const auto start{SteadyClock::now()}; const auto path_addr{args.GetDataDirNet() / "peers.dat"}; try { - DeserializeFileDB(path_addr, *addrman, CLIENT_VERSION); + DeserializeFileDB(path_addr, *addrman); LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman->Size(), Ticks(SteadyClock::now() - start)); } catch (const DbNotFoundError&) { // Addrman can be in an inconsistent state after failure, reset it @@ -217,14 +216,14 @@ util::Result> LoadAddrman(const NetGroupManager& netgro void DumpAnchors(const fs::path& anchors_db_path, const std::vector& anchors) { LOG_TIME_SECONDS(strprintf("Flush %d outbound block-relay-only peer addresses to anchors.dat", anchors.size())); - SerializeFileDB("anchors", anchors_db_path, anchors, CLIENT_VERSION | ADDRV2_FORMAT); + SerializeFileDB("anchors", anchors_db_path, WithParams(CAddress::V2_DISK, anchors)); } std::vector ReadAnchors(const fs::path& anchors_db_path) { std::vector anchors; try { - DeserializeFileDB(anchors_db_path, anchors, CLIENT_VERSION | ADDRV2_FORMAT); + DeserializeFileDB(anchors_db_path, WithParams(CAddress::V2_DISK, anchors)); LogPrintf("Loaded %i addresses from %s\n", anchors.size(), fs::quoted(fs::PathToString(anchors_db_path.filename()))); } catch (const std::exception&) { anchors.clear(); diff --git a/src/addrdb.h b/src/addrdb.h index 0037495d188..cc3014dce29 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -16,12 +16,13 @@ class ArgsManager; class AddrMan; class CAddress; -class CDataStream; +class DataStream; class NetGroupManager; -bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr); /** Only used by tests. */ -void ReadFromStream(AddrMan& addr, CDataStream& ssPeers); +void ReadFromStream(AddrMan& addr, DataStream& ssPeers); + +bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr); /** Access to the banlist database (banlist.json) */ class CBanDB diff --git a/src/addrman.cpp b/src/addrman.cpp index 9ccf71774ae..212baab9d45 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -171,8 +171,7 @@ void AddrManImpl::Serialize(Stream& s_) const */ // Always serialize in the latest version (FILE_FORMAT). - - OverrideStream s(&s_, s_.GetType(), s_.GetVersion() | ADDRV2_FORMAT); + ParamsStream s{CAddress::V2_DISK, s_}; s << static_cast(FILE_FORMAT); @@ -236,14 +235,8 @@ void AddrManImpl::Unserialize(Stream& s_) Format format; s_ >> Using>(format); - int stream_version = s_.GetVersion(); - if (format >= Format::V3_BIP155) { - // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress - // unserialize methods know that an address in addrv2 format is coming. - stream_version |= ADDRV2_FORMAT; - } - - OverrideStream s(&s_, s_.GetType(), stream_version); + const auto ser_params = (format >= Format::V3_BIP155 ? CAddress::V2_DISK : CAddress::V1_DISK); + ParamsStream s{ser_params, s_}; uint8_t compat; s >> compat; @@ -1249,12 +1242,12 @@ void AddrMan::Unserialize(Stream& s_) } // explicit instantiation -template void AddrMan::Serialize(HashedSourceWriter& s) const; -template void AddrMan::Serialize(CDataStream& s) const; -template void AddrMan::Unserialize(CAutoFile& s); -template void AddrMan::Unserialize(CHashVerifier& s); -template void AddrMan::Unserialize(CDataStream& s); -template void AddrMan::Unserialize(CHashVerifier& s); +template void AddrMan::Serialize(HashedSourceWriter&) const; +template void AddrMan::Serialize(DataStream&) const; +template void AddrMan::Unserialize(AutoFile&); +template void AddrMan::Unserialize(HashVerifier&); +template void AddrMan::Unserialize(DataStream&); +template void AddrMan::Unserialize(HashVerifier&); size_t AddrMan::Size(std::optional net, std::optional in_new) const { diff --git a/src/addrman_impl.h b/src/addrman_impl.h index 9aff408e342..1cfaca04a3d 100644 --- a/src/addrman_impl.h +++ b/src/addrman_impl.h @@ -65,8 +65,7 @@ public: SERIALIZE_METHODS(AddrInfo, obj) { - READWRITEAS(CAddress, obj); - READWRITE(obj.source, Using>(obj.m_last_success), obj.nAttempts); + READWRITE(AsBase(obj), obj.source, Using>(obj.m_last_success), obj.nAttempts); } AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource) diff --git a/src/hash.h b/src/hash.h index 89c6f0dab9a..f2b627ff4f7 100644 --- a/src/hash.h +++ b/src/hash.h @@ -199,53 +199,20 @@ public: } }; -template -class CHashVerifier : public CHashWriter -{ -private: - Source* source; - -public: - explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {} - - void read(Span dst) - { - source->read(dst); - this->write(dst); - } - - void ignore(size_t nSize) - { - std::byte data[1024]; - while (nSize > 0) { - size_t now = std::min(nSize, 1024); - read({data, now}); - nSize -= now; - } - } - - template - CHashVerifier& operator>>(T&& obj) - { - ::Unserialize(*this, obj); - return (*this); - } -}; - /** Writes data to an underlying source stream, while hashing the written data. */ template -class HashedSourceWriter : public CHashWriter +class HashedSourceWriter : public HashWriter { private: Source& m_source; public: - explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : CHashWriter{source.GetType(), source.GetVersion()}, m_source{source} {} + explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {} void write(Span src) { m_source.write(src); - CHashWriter::write(src); + HashWriter::write(src); } template diff --git a/src/index/disktxpos.h b/src/index/disktxpos.h index 7718755b789..1004f7ae87d 100644 --- a/src/index/disktxpos.h +++ b/src/index/disktxpos.h @@ -14,8 +14,7 @@ struct CDiskTxPos : public FlatFilePos SERIALIZE_METHODS(CDiskTxPos, obj) { - READWRITEAS(FlatFilePos, obj); - READWRITE(VARINT(obj.nTxOffset)); + READWRITE(AsBase(obj), VARINT(obj.nTxOffset)); } CDiskTxPos(const FlatFilePos &blockIn, unsigned int nTxOffsetIn) : FlatFilePos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) { diff --git a/src/net.cpp b/src/net.cpp index e66c0ec7f8b..4addca09822 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -202,7 +202,8 @@ static std::vector ConvertSeeds(const std::vector &vSeedsIn) const auto one_week{7 * 24h}; std::vector vSeedsOut; FastRandomContext rng; - CDataStream s(vSeedsIn, SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); + DataStream underlying_stream{vSeedsIn}; + ParamsStream s{CAddress::V2_NETWORK, underlying_stream}; while (!s.eof()) { CService endpoint; s >> endpoint; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index c5a22f258a4..6b415b3a1ea 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1415,8 +1415,8 @@ void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer) const bool tx_relay{!RejectIncomingTxs(pnode)}; m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, my_services, nTime, - your_services, addr_you, // Together the pre-version-31402 serialization of CAddress "addrYou" (without nTime) - my_services, CService(), // Together the pre-version-31402 serialization of CAddress "addrMe" (without nTime) + your_services, WithParams(CNetAddr::V1, addr_you), // Together the pre-version-31402 serialization of CAddress "addrYou" (without nTime) + my_services, WithParams(CNetAddr::V1, CService{}), // Together the pre-version-31402 serialization of CAddress "addrMe" (without nTime) nonce, strSubVersion, nNodeStartingHeight, tx_relay)); if (fLogIPs) { @@ -3293,7 +3293,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, nTime = 0; } vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer - vRecv >> addrMe; + vRecv >> WithParams(CNetAddr::V1, addrMe); if (!pfrom.IsInboundConn()) { m_addrman.SetServices(pfrom.addr, nServices); @@ -3672,17 +3672,17 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, } if (msg_type == NetMsgType::ADDR || msg_type == NetMsgType::ADDRV2) { - int stream_version = vRecv.GetVersion(); - if (msg_type == NetMsgType::ADDRV2) { - // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress + const auto ser_params{ + msg_type == NetMsgType::ADDRV2 ? + // Set V2 param so that the CNetAddr and CAddress // unserialize methods know that an address in v2 format is coming. - stream_version |= ADDRV2_FORMAT; - } + CAddress::V2_NETWORK : + CAddress::V1_NETWORK, + }; - OverrideStream s(&vRecv, vRecv.GetType(), stream_version); std::vector vAddr; - s >> vAddr; + vRecv >> WithParams(ser_params, vAddr); if (!SetupAddressRelay(pfrom, *peer)) { LogPrint(BCLog::NET, "ignoring %s message from %s peer=%d\n", msg_type, pfrom.ConnectionTypeAsString(), pfrom.GetId()); @@ -5289,15 +5289,15 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros if (peer.m_addrs_to_send.empty()) return; const char* msg_type; - int make_flags; + CNetAddr::Encoding ser_enc; if (peer.m_wants_addrv2) { msg_type = NetMsgType::ADDRV2; - make_flags = ADDRV2_FORMAT; + ser_enc = CNetAddr::Encoding::V2; } else { msg_type = NetMsgType::ADDR; - make_flags = 0; + ser_enc = CNetAddr::Encoding::V1; } - m_connman.PushMessage(&node, CNetMsgMaker(node.GetCommonVersion()).Make(make_flags, msg_type, peer.m_addrs_to_send)); + m_connman.PushMessage(&node, CNetMsgMaker(node.GetCommonVersion()).Make(msg_type, WithParams(CAddress::SerParams{{ser_enc}, CAddress::Format::Network}, peer.m_addrs_to_send))); peer.m_addrs_to_send.clear(); // we only send the big addr message once diff --git a/src/netaddress.h b/src/netaddress.h index 7cba6c00d09..a0944c886fc 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -24,14 +24,6 @@ #include #include -/** - * A flag that is ORed into the protocol version to designate that addresses - * should be serialized in (unserialized from) v2 format (BIP155). - * Make sure that this does not collide with any of the values in `version.h` - * or with `SERIALIZE_TRANSACTION_NO_WITNESS`. - */ -static constexpr int ADDRV2_FORMAT = 0x20000000; - /** * A network type. * @note An address may belong to more than one network, for example `10.0.0.1` @@ -220,13 +212,23 @@ public: return IsIPv4() || IsIPv6() || IsTor() || IsI2P() || IsCJDNS(); } + enum class Encoding { + V1, + V2, //!< BIP155 encoding + }; + struct SerParams { + const Encoding enc; + }; + static constexpr SerParams V1{Encoding::V1}; + static constexpr SerParams V2{Encoding::V2}; + /** * Serialize to a stream. */ template void Serialize(Stream& s) const { - if (s.GetVersion() & ADDRV2_FORMAT) { + if (s.GetParams().enc == Encoding::V2) { SerializeV2Stream(s); } else { SerializeV1Stream(s); @@ -239,7 +241,7 @@ public: template void Unserialize(Stream& s) { - if (s.GetVersion() & ADDRV2_FORMAT) { + if (s.GetParams().enc == Encoding::V2) { UnserializeV2Stream(s); } else { UnserializeV1Stream(s); @@ -540,8 +542,7 @@ public: SERIALIZE_METHODS(CService, obj) { - READWRITEAS(CNetAddr, obj); - READWRITE(Using>(obj.port)); + READWRITE(AsBase(obj), Using>(obj.port)); } friend class CServiceHash; diff --git a/src/primitives/block.h b/src/primitives/block.h index bd11279a6e0..861d362414b 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -87,8 +87,7 @@ public: SERIALIZE_METHODS(CBlock, obj) { - READWRITEAS(CBlockHeader, obj); - READWRITE(obj.vtx); + READWRITE(AsBase(obj), obj.vtx); } void SetNull() diff --git a/src/protocol.h b/src/protocol.h index ac4545c3111..a7ca0c6f3e3 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -390,35 +390,43 @@ public: CAddress(CService ipIn, ServiceFlags nServicesIn) : CService{ipIn}, nServices{nServicesIn} {}; CAddress(CService ipIn, ServiceFlags nServicesIn, NodeSeconds time) : CService{ipIn}, nTime{time}, nServices{nServicesIn} {}; - SERIALIZE_METHODS(CAddress, obj) + enum class Format { + Disk, + Network, + }; + struct SerParams : CNetAddr::SerParams { + const Format fmt; + }; + static constexpr SerParams V1_NETWORK{{CNetAddr::Encoding::V1}, Format::Network}; + static constexpr SerParams V2_NETWORK{{CNetAddr::Encoding::V2}, Format::Network}; + static constexpr SerParams V1_DISK{{CNetAddr::Encoding::V1}, Format::Disk}; + static constexpr SerParams V2_DISK{{CNetAddr::Encoding::V2}, Format::Disk}; + + SERIALIZE_METHODS_PARAMS(CAddress, obj, SerParams, params) { - // CAddress has a distinct network serialization and a disk serialization, but it should never - // be hashed (except through CHashWriter in addrdb.cpp, which sets SER_DISK), and it's - // ambiguous what that would mean. Make sure no code relying on that is introduced: - assert(!(s.GetType() & SER_GETHASH)); bool use_v2; - if (s.GetType() & SER_DISK) { + if (params.fmt == Format::Disk) { // In the disk serialization format, the encoding (v1 or v2) is determined by a flag version // that's part of the serialization itself. ADDRV2_FORMAT in the stream version only determines // whether V2 is chosen/permitted at all. uint32_t stored_format_version = DISK_VERSION_INIT; - if (s.GetVersion() & ADDRV2_FORMAT) stored_format_version |= DISK_VERSION_ADDRV2; + if (params.enc == Encoding::V2) stored_format_version |= DISK_VERSION_ADDRV2; READWRITE(stored_format_version); stored_format_version &= ~DISK_VERSION_IGNORE_MASK; // ignore low bits if (stored_format_version == 0) { use_v2 = false; - } else if (stored_format_version == DISK_VERSION_ADDRV2 && (s.GetVersion() & ADDRV2_FORMAT)) { - // Only support v2 deserialization if ADDRV2_FORMAT is set. + } else if (stored_format_version == DISK_VERSION_ADDRV2 && params.enc == Encoding::V2) { + // Only support v2 deserialization if V2 is set. use_v2 = true; } else { throw std::ios_base::failure("Unsupported CAddress disk format version"); } } else { + assert(params.fmt == Format::Network); // In the network serialization format, the encoding (v1 or v2) is determined directly by - // the value of ADDRV2_FORMAT in the stream version, as no explicitly encoded version + // the value of enc in the stream params, as no explicitly encoded version // exists in the stream. - assert(s.GetType() & SER_NETWORK); - use_v2 = s.GetVersion() & ADDRV2_FORMAT; + use_v2 = params.enc == Encoding::V2; } READWRITE(Using>(obj.nTime)); @@ -432,8 +440,8 @@ public: READWRITE(Using>(obj.nServices)); } // Invoke V1/V2 serializer for CService parent object. - OverrideStream os(&s, s.GetType(), use_v2 ? ADDRV2_FORMAT : 0); - SerReadWriteMany(os, ser_action, ReadWriteAsHelper(obj)); + const auto ser_params{use_v2 ? CNetAddr::V2 : CNetAddr::V1}; + READWRITE(WithParams(ser_params, AsBase(obj))); } //! Always included in serialization. The behavior is unspecified if the value is not representable as uint32_t. diff --git a/src/script/script.h b/src/script/script.h index 902f756afcf..c329a2afd6e 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -434,7 +434,7 @@ public: CScript(std::vector::const_iterator pbegin, std::vector::const_iterator pend) : CScriptBase(pbegin, pend) { } CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { } - SERIALIZE_METHODS(CScript, obj) { READWRITEAS(CScriptBase, obj); } + SERIALIZE_METHODS(CScript, obj) { READWRITE(AsBase(obj)); } explicit CScript(int64_t b) { operator<<(b); } explicit CScript(opcodetype b) { operator<<(b); } diff --git a/src/serialize.h b/src/serialize.h index 39f2c0f3ae7..2d790190a0b 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_SERIALIZE_H #define BITCOIN_SERIALIZE_H +#include #include #include @@ -133,12 +134,40 @@ enum SER_GETHASH = (1 << 2), }; -//! Convert the reference base type to X, without changing constness or reference type. -template X& ReadWriteAsHelper(X& x) { return x; } -template const X& ReadWriteAsHelper(const X& x) { return x; } +/** + * Convert any argument to a reference to X, maintaining constness. + * + * This can be used in serialization code to invoke a base class's + * serialization routines. + * + * Example use: + * class Base { ... }; + * class Child : public Base { + * int m_data; + * public: + * SERIALIZE_METHODS(Child, obj) { + * READWRITE(AsBase(obj), obj.m_data); + * } + * }; + * + * static_cast cannot easily be used here, as the type of Obj will be const Child& + * during serialization and Child& during deserialization. AsBase will convert to + * const Base& and Base& appropriately. + */ +template +Out& AsBase(In& x) +{ + static_assert(std::is_base_of_v); + return x; +} +template +const Out& AsBase(const In& x) +{ + static_assert(std::is_base_of_v); + return x; +} #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) -#define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper(obj))) #define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const::type& obj) { code; }) #define SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream& s, const Type& obj) { code; }) @@ -160,11 +189,66 @@ template const X& ReadWriteAsHelper(const X& x) { return x; } */ #define FORMATTER_METHODS(cls, obj) \ template \ - static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \ + static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, ActionSerialize{}); } \ template \ - static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \ + static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, ActionUnserialize{}); } \ template \ - static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \ + static void SerializationOps(Type& obj, Stream& s, Operation ser_action) + +/** + * Variant of FORMATTER_METHODS that supports a declared parameter type. + * + * If a formatter has a declared parameter type, it must be invoked directly or + * indirectly with a parameter of that type. This permits making serialization + * depend on run-time context in a type-safe way. + * + * Example use: + * struct BarParameter { bool fancy; ... }; + * struct Bar { ... }; + * struct FooFormatter { + * FORMATTER_METHODS(Bar, obj, BarParameter, param) { + * if (param.fancy) { + * READWRITE(VARINT(obj.value)); + * } else { + * READWRITE(obj.value); + * } + * } + * }; + * which would then be invoked as + * READWRITE(WithParams(BarParameter{...}, Using(obj.foo))) + * + * WithParams(parameter, obj) can be invoked anywhere in the call stack; it is + * passed down recursively into all serialization code, until another + * WithParams overrides it. + * + * Parameters will be implicitly converted where appropriate. This means that + * "parent" serialization code can use a parameter that derives from, or is + * convertible to, a "child" formatter's parameter type. + * + * Compilation will fail in any context where serialization is invoked but + * no parameter of a type convertible to BarParameter is provided. + */ +#define FORMATTER_METHODS_PARAMS(cls, obj, paramcls, paramobj) \ + template \ + static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, ActionSerialize{}, s.GetParams()); } \ + template \ + static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, ActionUnserialize{}, s.GetParams()); } \ + template \ + static void SerializationOps(Type& obj, Stream& s, Operation ser_action, const paramcls& paramobj) + +#define BASE_SERIALIZE_METHODS(cls) \ + template \ + void Serialize(Stream& s) const \ + { \ + static_assert(std::is_same::value, "Serialize type mismatch"); \ + Ser(s, *this); \ + } \ + template \ + void Unserialize(Stream& s) \ + { \ + static_assert(std::is_same::value, "Unserialize type mismatch"); \ + Unser(s, *this); \ + } /** * Implement the Serialize and Unserialize methods by delegating to a single templated @@ -173,21 +257,19 @@ template const X& ReadWriteAsHelper(const X& x) { return x; } * 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 \ - void Serialize(Stream& s) const \ - { \ - static_assert(std::is_same::value, "Serialize type mismatch"); \ - Ser(s, *this); \ - } \ - template \ - void Unserialize(Stream& s) \ - { \ - static_assert(std::is_same::value, "Unserialize type mismatch"); \ - Unser(s, *this); \ - } \ +#define SERIALIZE_METHODS(cls, obj) \ + BASE_SERIALIZE_METHODS(cls) \ FORMATTER_METHODS(cls, obj) +/** + * Variant of SERIALIZE_METHODS that supports a declared parameter type. + * + * See FORMATTER_METHODS_PARAMS for more information on parameters. + */ +#define SERIALIZE_METHODS_PARAMS(cls, obj, paramcls, paramobj) \ + BASE_SERIALIZE_METHODS(cls) \ + FORMATTER_METHODS_PARAMS(cls, obj, paramcls, paramobj) + // clang-format off #ifndef CHAR_EQUALS_INT8 template void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t @@ -925,26 +1007,17 @@ void Unserialize(Stream& is, std::shared_ptr& p) } - /** - * Support for SERIALIZE_METHODS and READWRITE macro. + * Support for all macros providing or using the ser_action parameter of the SerializationOps method. */ -struct CSerActionSerialize -{ +struct ActionSerialize { constexpr bool ForRead() const { return false; } }; -struct CSerActionUnserialize -{ +struct ActionUnserialize { constexpr bool ForRead() const { return true; } }; - - - - - - /* ::GetSerializeSize implementations * * Computing the serialized size of objects is done through a special stream @@ -1003,36 +1076,36 @@ inline void UnserializeMany(Stream& s, Args&&... args) } template -inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args) +inline void SerReadWriteMany(Stream& s, ActionSerialize ser_action, const Args&... args) { ::SerializeMany(s, args...); } template -inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args) +inline void SerReadWriteMany(Stream& s, ActionUnserialize ser_action, Args&&... args) { ::UnserializeMany(s, args...); } template -inline void SerRead(Stream& s, CSerActionSerialize ser_action, Type&&, Fn&&) +inline void SerRead(Stream& s, ActionSerialize ser_action, Type&&, Fn&&) { } template -inline void SerRead(Stream& s, CSerActionUnserialize ser_action, Type&& obj, Fn&& fn) +inline void SerRead(Stream& s, ActionUnserialize ser_action, Type&& obj, Fn&& fn) { fn(s, std::forward(obj)); } template -inline void SerWrite(Stream& s, CSerActionSerialize ser_action, Type&& obj, Fn&& fn) +inline void SerWrite(Stream& s, ActionSerialize ser_action, Type&& obj, Fn&& fn) { fn(s, std::forward(obj)); } template -inline void SerWrite(Stream& s, CSerActionUnserialize ser_action, Type&&, Fn&&) +inline void SerWrite(Stream& s, ActionUnserialize ser_action, Type&&, Fn&&) { } @@ -1061,4 +1134,61 @@ size_t GetSerializeSizeMany(int nVersion, const T&... t) return sc.size(); } +/** Wrapper that overrides the GetParams() function of a stream (and hides GetVersion/GetType). */ +template +class ParamsStream +{ + const Params& m_params; + SubStream& m_substream; // private to avoid leaking version/type into serialization code that shouldn't see it + +public: + ParamsStream(const Params& params LIFETIMEBOUND, SubStream& substream LIFETIMEBOUND) : m_params{params}, m_substream{substream} {} + template ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; } + template ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; } + void write(Span src) { m_substream.write(src); } + void read(Span dst) { m_substream.read(dst); } + void ignore(size_t num) { m_substream.ignore(num); } + bool eof() const { return m_substream.eof(); } + size_t size() const { return m_substream.size(); } + const Params& GetParams() const { return m_params; } + int GetVersion() = delete; // Deprecated with Params usage + int GetType() = delete; // Deprecated with Params usage +}; + +/** Wrapper that serializes objects with the specified parameters. */ +template +class ParamsWrapper +{ + static_assert(std::is_lvalue_reference::value, "ParamsWrapper needs an lvalue reference type T"); + const Params& m_params; + T m_object; + +public: + explicit ParamsWrapper(const Params& params, T obj) : m_params{params}, m_object{obj} {} + + template + void Serialize(Stream& s) const + { + ParamsStream ss{m_params, s}; + ::Serialize(ss, m_object); + } + template + void Unserialize(Stream& s) + { + ParamsStream ss{m_params, s}; + ::Unserialize(ss, m_object); + } +}; + +/** + * Return a wrapper around t that (de)serializes it with specified parameter params. + * + * See FORMATTER_METHODS_PARAMS for more information on serialization parameters. + */ +template +static auto WithParams(const Params& params, T&& t) +{ + return ParamsWrapper{params, t}; +} + #endif // BITCOIN_SERIALIZE_H diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 329b89554da..941018a8206 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -697,7 +697,7 @@ BOOST_AUTO_TEST_CASE(addrman_serialization) auto addrman_asmap1_dup = std::make_unique(netgroupman, DETERMINISTIC, ratio); auto addrman_noasmap = std::make_unique(EMPTY_NETGROUPMAN, DETERMINISTIC, ratio); - CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); + DataStream stream{}; CAddress addr = CAddress(ResolveService("250.1.1.1"), NODE_NONE); CNetAddr default_source; @@ -757,7 +757,7 @@ BOOST_AUTO_TEST_CASE(remove_invalid) // Confirm that invalid addresses are ignored in unserialization. auto addrman = std::make_unique(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node)); - CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); + DataStream stream{}; const CAddress new1{ResolveService("5.5.5.5"), NODE_NONE}; const CAddress new2{ResolveService("6.6.6.6"), NODE_NONE}; @@ -940,9 +940,9 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks) BOOST_CHECK(!addr_pos36.tried); } -static CDataStream AddrmanToStream(const AddrMan& addrman) +static auto AddrmanToStream(const AddrMan& addrman) { - CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION); + DataStream ssPeersIn{}; ssPeersIn << Params().MessageStart(); ssPeersIn << addrman; return ssPeersIn; @@ -972,7 +972,7 @@ BOOST_AUTO_TEST_CASE(load_addrman) BOOST_CHECK(addrman.Size() == 3); // Test that the de-serialization does not throw an exception. - CDataStream ssPeers1 = AddrmanToStream(addrman); + auto ssPeers1{AddrmanToStream(addrman)}; bool exceptionThrown = false; AddrMan addrman1{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)}; @@ -989,7 +989,7 @@ BOOST_AUTO_TEST_CASE(load_addrman) BOOST_CHECK(exceptionThrown == false); // Test that ReadFromStream creates an addrman with the correct number of addrs. - CDataStream ssPeers2 = AddrmanToStream(addrman); + DataStream ssPeers2 = AddrmanToStream(addrman); AddrMan addrman2{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)}; BOOST_CHECK(addrman2.Size() == 0); @@ -998,9 +998,9 @@ BOOST_AUTO_TEST_CASE(load_addrman) } // Produce a corrupt peers.dat that claims 20 addrs when it only has one addr. -static CDataStream MakeCorruptPeersDat() +static auto MakeCorruptPeersDat() { - CDataStream s(SER_DISK, CLIENT_VERSION); + DataStream s{}; s << ::Params().MessageStart(); unsigned char nVersion = 1; @@ -1019,7 +1019,7 @@ static CDataStream MakeCorruptPeersDat() std::optional resolved{LookupHost("252.2.2.2", false)}; BOOST_REQUIRE(resolved.has_value()); AddrInfo info = AddrInfo(addr, resolved.value()); - s << info; + s << WithParams(CAddress::V1_DISK, info); return s; } @@ -1027,7 +1027,7 @@ static CDataStream MakeCorruptPeersDat() BOOST_AUTO_TEST_CASE(load_addrman_corrupted) { // Test that the de-serialization of corrupted peers.dat throws an exception. - CDataStream ssPeers1 = MakeCorruptPeersDat(); + auto ssPeers1{MakeCorruptPeersDat()}; bool exceptionThrown = false; AddrMan addrman1{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)}; BOOST_CHECK(addrman1.Size() == 0); @@ -1041,7 +1041,7 @@ BOOST_AUTO_TEST_CASE(load_addrman_corrupted) BOOST_CHECK(exceptionThrown); // Test that ReadFromStream fails if peers.dat is corrupt - CDataStream ssPeers2 = MakeCorruptPeersDat(); + auto ssPeers2{MakeCorruptPeersDat()}; AddrMan addrman2{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)}; BOOST_CHECK(addrman2.Size() == 0); diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index 02df4590de3..9611a872ec4 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -49,7 +49,7 @@ void initialize_addrman() FUZZ_TARGET(data_stream_addr_man, .init = initialize_addrman) { FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; - CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider); + DataStream data_stream = ConsumeDataStream(fuzzed_data_provider); NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)}; AddrMan addr_man(netgroupman, /*deterministic=*/false, GetCheckRatio()); try { @@ -78,12 +78,12 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f net = 6; } - CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); + DataStream s{}; s << net; s << fast_random_context.randbytes(net_len_map.at(net)); - s >> addr; + s >> WithParams(CAddress::V2_NETWORK, addr); } // Return a dummy IPv4 5.5.5.5 if we generated an invalid address. @@ -241,9 +241,7 @@ FUZZ_TARGET(addrman, .init = initialize_addrman) auto addr_man_ptr = std::make_unique(netgroupman, fuzzed_data_provider); if (fuzzed_data_provider.ConsumeBool()) { const std::vector serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - CDataStream ds(serialized_data, SER_DISK, INIT_PROTO_VERSION); - const auto ser_version{fuzzed_data_provider.ConsumeIntegral()}; - ds.SetVersion(ser_version); + DataStream ds{serialized_data}; try { ds >> *addr_man_ptr; } catch (const std::ios_base::failure&) { @@ -295,7 +293,7 @@ FUZZ_TARGET(addrman, .init = initialize_addrman) in_new = fuzzed_data_provider.ConsumeBool(); } (void)const_addr_man.Size(network, in_new); - CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION); + DataStream data_stream{}; data_stream << const_addr_man; } @@ -309,10 +307,10 @@ FUZZ_TARGET(addrman_serdeser, .init = initialize_addrman) AddrManDeterministic addr_man1{netgroupman, fuzzed_data_provider}; AddrManDeterministic addr_man2{netgroupman, fuzzed_data_provider}; - CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION); + DataStream data_stream{}; FillAddrman(addr_man1, fuzzed_data_provider); data_stream << addr_man1; data_stream >> addr_man2; assert(addr_man1 == addr_man2); -} \ No newline at end of file +} diff --git a/src/test/fuzz/deserialize.cpp b/src/test/fuzz/deserialize.cpp index 09402233bdb..100a6b4ee45 100644 --- a/src/test/fuzz/deserialize.cpp +++ b/src/test/fuzz/deserialize.cpp @@ -24,6 +24,8 @@ #include #include