From 62869923921f772e02c6a65db52506ef5913fd70 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 13 Oct 2026 12:25:36 -0500 Subject: [PATCH 1/5] refactor: use HashVerifier in the evo snapshot decoder SnapshotBoundedInput and ReadMinedQuorumCommitment are the last Dash-only users of the legacy CHashVerifier, which bitcoin#25284 removes along with its remaining upstream users. Both only need the hash of the bytes they consume, so the version-free HashVerifier is enough. SnapshotBoundedInput still exposes GetType()/GetVersion() for the CDeterministicMN serializers, now taken from the underlying stream instead of the verifier. Both verifiers compute the same double-SHA256 over the same bytes, so the canonical-encoding checks are unchanged. Co-Authored-By: Claude Opus 5.5 --- src/evo/snapshot.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/evo/snapshot.h b/src/evo/snapshot.h index b0944838f90a..ca39e51daf14 100644 --- a/src/evo/snapshot.h +++ b/src/evo/snapshot.h @@ -167,15 +167,16 @@ template class SnapshotBoundedInput { private: - CHashVerifier m_stream; + Stream& m_source; + HashVerifier m_stream; uint64_t m_compact_budget; public: SnapshotBoundedInput(Stream& stream, uint64_t compact_budget) : - m_stream{&stream}, m_compact_budget{compact_budget} {} + m_source{stream}, m_stream{stream}, m_compact_budget{compact_budget} {} - int GetType() const { return m_stream.GetType(); } - int GetVersion() const { return m_stream.GetVersion(); } + int GetType() const { return m_source.GetType(); } + int GetVersion() const { return m_source.GetVersion(); } void read(Span dst) { m_stream.read(dst); } void ignore(size_t size) { m_stream.ignore(size); } @@ -431,7 +432,7 @@ MinedQuorumCommitment ReadMinedQuorumCommitment(Stream& s) ReadFixedBitSet(s, commitment.validMembers, valid_members_size); const bool legacy{commitment.nVersion == llmq::CFinalCommitment::LEGACY_BLS_NON_INDEXED_QUORUM_VERSION || commitment.nVersion == llmq::CFinalCommitment::LEGACY_BLS_INDEXED_QUORUM_VERSION}; - CHashVerifier bls_input{&s}; + HashVerifier bls_input{s}; bls_input >> CBLSPublicKeyVersionWrapper(commitment.quorumPublicKey, legacy) >> commitment.quorumVvecHash >> CBLSSignatureVersionWrapper(commitment.quorumSig, legacy) >> CBLSSignatureVersionWrapper(commitment.membersSig, legacy); From 1f76e265da492279fbe55540c3bb0005780f4028 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 3 Oct 2026 20:14:33 -0500 Subject: [PATCH 2/5] Merge bitcoin/bitcoin#25284: net: Use serialization parameters for CAddress serialization fa626af3edbe8d98b2de91dd71729ceef90389fb Remove unused legacy CHashVerifier (MarcoFalke) fafa3fc5a62702da72991497e3270034eb9159c0 test: add tests that exercise WithParams() (MarcoFalke) fac81affb527132945773a5315bd27fec61ec52f Use serialization parameters for CAddress serialization (MarcoFalke) faec591d64e40ba7ec7656cbfdda1a05953bde13 Support for serialization parameters (MarcoFalke) fac42e9d35f6ba046999b2e3a757ab720c51b6bb Rename CSerAction* to Action* (MarcoFalke) aaaa3fa9477eef9ea72e4a501d130c57b47b470a Replace READWRITEAS macro with AsBase wrapping function (MarcoFalke) Pull request description: It seems confusing that picking a wrong value for `ADDRV2_FORMAT` could have effects on consensus. (See the docstring of `ADDRV2_FORMAT`). Fix this by implementing https://github.com/bitcoin/bitcoin/issues/19477#issuecomment-1147421608 . This may also help with libbitcoinkernel, see https://github.com/bitcoin/bitcoin/pull/28327 ACKs for top commit: TheCharlatan: ACK fa626af3edbe8d98b2de91dd71729ceef90389fb ajtowns: ACK fa626af3edbe8d98b2de91dd71729ceef90389fb Tree-SHA512: 229d379da27308890de212b1fd2b85dac13f3f768413cb56a4b0c2da708f28344d04356ffd75bfcbaa4cabf0b6cc363c4f812a8f1648cff9e436811498278318 Dash adaptations: - src/net_processing.cpp: PushNodeVersion keeps Dash's nProtocolVersion (-pushversion), mnauthChallenge and m_masternode_connection fields; only addr_you/CService{} are wrapped in WithParams(CNetAddr::V1, ...) as upstream does - src/addrdb.cpp: DumpPeerAddresses/LoadAddrman keep Dash's gArgs.GetDataDirNet() (Dash's existing difference from upstream's args.) and drop the CLIENT_VERSION argument as upstream does - src/serialize.h: Dash's HAVE_CONFIG_H/config include kept; upstream's new #include added after it - src/version.h: upstream edits a comment in Bitcoin's version list that Dash does not have; Dash's own line 'Make sure that none of the values above collide with `ADDRV2_FORMAT`' is removed because ADDRV2_FORMAT no longer exists; all Dash protocol-version constants kept - src/test/netbase_tests.cpp: the caddress_(un)serialize_v1/v2 tests are at a different place in Dash's file; upstream's edits (DataStream + WithParams(CAddress::V1_NETWORK/V2_NETWORK, ...)) applied to Dash's existing copies; Dash's LookupSubNet(...).IsValid() checks kept - src/test/serialize_tests.cpp: upstream's new params tests (Base/Derived/WithParams) appended after Dash's LIMITED_VECTOR tests; upstream's added #include left out because Dash's include block already has it - src/test/fuzz/deserialize.cpp: Dash's '*/' that ends the commented-out block_filter_deserialize target kept before upstream's new addr_info_deserialize FUZZ_TARGET; Dash's script/sign.h and util/system.h includes kept - src/evo/netinfo.h (Dash-only): NetInfoEntry used OverrideStream with |ADDRV2_FORMAT, which upstream removed; it now uses WithParams(CNetAddr::V2, service). MnNetInfo (de)serialized CService on streams without ADDRV2_FORMAT and now uses WithParams(CNetAddr::V1, ...), including the CSizeComputer size override. Wire and disk bytes are unchanged - src/evo/deterministicmns.h (Dash-only): GetUniquePropertyHash switched from ser_version |= ADDRV2_FORMAT to WithParams(IsAddrV1Compatible() ? CNetAddr::V1 : CNetAddr::V2, v), with non-CService types in an else branch of the if constexpr; the hash input bytes are identical, so the unique-property map stays compatible. CSerAction{Serialize,Unserialize} renamed to Action{Serialize,Unserialize} (upstream rename) in the immer-map SerReadWrite overloads and CDeterministicMNList - src/llmq/snapshot.h (Dash-only): CSerAction{Serialize,Unserialize} -> Action{Serialize,Unserialize} (upstream rename) in CQuorumSnapshot and CQuorumRotationInfo - src/evo/snapshot.h, src/evo/snapshot.cpp, src/test/evo_snapshot_tests.cpp (Dash-only): with NetInfoEntry no longer wrapping its stream in OverrideStream, the snapshot-local ReadCompactSize(OverrideStream&) overload has no callers; removed, along with the comment mentioning it, and its budgeted-string test now reads through SnapshotBoundedInput directly, which is the path DomainPort strings take. The budget accounting is unchanged: addrv2 address lengths are read through CompactSizeFormatter, which never reached that overload - src/netfulfilledman.h (Dash-only): the netfulfilled.dat store serializes std::map keys; it now uses READWRITE(WithParams(CNetAddr::V1, ...)), which matches the old no-ADDRV2_FORMAT stream, so the on-disk format is unchanged Co-Authored-By: Claude Opus 5.5 --- src/addrdb.cpp | 25 ++-- src/addrdb.h | 7 +- src/addrman.cpp | 25 ++-- src/addrman_impl.h | 3 +- src/evo/deterministicmns.h | 17 ++- src/evo/netinfo.h | 18 ++- src/evo/snapshot.cpp | 4 +- src/evo/snapshot.h | 11 -- src/hash.h | 39 +----- src/index/disktxpos.h | 3 +- src/llmq/snapshot.h | 8 +- src/net.cpp | 3 +- src/net_processing.cpp | 28 ++--- src/netaddress.h | 24 ++-- src/netfulfilledman.h | 2 +- src/primitives/block.h | 3 +- src/protocol.h | 36 +++--- src/script/script.h | 2 +- src/serialize.h | 206 ++++++++++++++++++++++++++------ src/test/addrman_tests.cpp | 22 ++-- src/test/evo_netinfo_tests.cpp | 13 +- src/test/evo_snapshot_tests.cpp | 3 +- src/test/fuzz/addrman.cpp | 14 +-- src/test/fuzz/deserialize.cpp | 151 +++++++++++++---------- src/test/fuzz/net.cpp | 2 +- src/test/fuzz/script_sign.cpp | 3 +- src/test/fuzz/util.h | 21 +++- src/test/fuzz/util/net.cpp | 21 ++++ src/test/net_tests.cpp | 81 ++++++------- src/test/netbase_tests.cpp | 16 +-- src/test/serialize_tests.cpp | 142 ++++++++++++++++++++++ src/test/streams_tests.cpp | 4 +- src/test/util/net.cpp | 4 +- src/version.h | 2 - 34 files changed, 621 insertions(+), 342 deletions(-) diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 8d80ba8fce26..08d1ebaea881 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -52,16 +52,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); @@ -94,9 +94,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; @@ -119,11 +119,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{}; } @@ -183,10 +182,10 @@ bool CBanDB::Read(banmap_t& banSet) bool DumpPeerAddresses(const ArgsManager& args, const AddrMan& addr) { const auto pathAddr = gArgs.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); } @@ -199,7 +198,7 @@ util::Result> LoadAddrman(const NetGroupManager& netgro const auto start{SteadyClock::now()}; const auto path_addr{gArgs.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 @@ -233,14 +232,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 4a68d7b2e3d4..164191633abb 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -18,13 +18,14 @@ class ArgsManager; class AddrMan; class CAddress; -class CDataStream; +class DataStream; class NetGroupManager; struct bilingual_str; -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 329c80ac8466..ab7992da10f3 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -175,8 +175,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); @@ -240,14 +239,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; @@ -1310,12 +1303,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 28a6f9fc57f2..a0b46605f7d6 100644 --- a/src/addrman_impl.h +++ b/src/addrman_impl.h @@ -65,8 +65,7 @@ class AddrInfo : public CAddress 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/evo/deterministicmns.h b/src/evo/deterministicmns.h index efda69005f6c..1d77a06c862c 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -118,13 +118,13 @@ void UnserializeImmerMap(Stream& is, immer::map& m) // For some reason the compiler is not able to choose the correct Serialize/Deserialize methods without a specialized // version of SerReadWrite. It otherwise always chooses the version that calls a.Serialize() template -inline void SerReadWrite(Stream& s, const immer::map& m, CSerActionSerialize ser_action) +inline void SerReadWrite(Stream& s, const immer::map& m, ActionSerialize ser_action) { ::SerializeImmerMap(s, m); } template -inline void SerReadWrite(Stream& s, immer::map& obj, CSerActionUnserialize ser_action) +inline void SerReadWrite(Stream& s, immer::map& obj, ActionUnserialize ser_action) { ::UnserializeImmerMap(s, obj); } @@ -234,7 +234,7 @@ class CDeterministicMNList template void Serialize(Stream& s) const { - const_cast(this)->SerializationOpBase(s, CSerActionSerialize()); + const_cast(this)->SerializationOpBase(s, ActionSerialize()); // Serialize the map as a vector WriteCompactSize(s, mnMap.size()); @@ -248,7 +248,7 @@ class CDeterministicMNList { Clear(); - SerializationOpBase(s, CSerActionUnserialize()); + SerializationOpBase(s, ActionUnserialize()); for (size_t to_read = ReadCompactSize(s); to_read > 0; --to_read) { AddMN(std::make_shared(deserialize, s), /*fBumpTotalCount=*/false); @@ -534,17 +534,16 @@ class CDeterministicMNList DMNL_NO_TEMPLATE(NetInfoInterface); DMNL_NO_TEMPLATE(std::shared_ptr); #undef DMNL_NO_TEMPLATE - int ser_version{PROTOCOL_VERSION}; if constexpr (std::is_same_v, CService>) { // Special handling is required if we're using addresses that can only be (de)serialized using // ADDRv2. Without this step, the address gets truncated, the hashmap gets contaminated with // an invalid entry and subsequent attempts at registering ADDRv2 entries get blocked. We cannot // apply this treatment ADDRv1 compatible addresses for backwards compatibility with the existing map. - if (!v.IsAddrV1Compatible()) { - ser_version |= ADDRV2_FORMAT; - } + const auto& ser_params{v.IsAddrV1Compatible() ? CNetAddr::V1 : CNetAddr::V2}; + return ::SerializeHash(WithParams(ser_params, v), /*nType=*/SER_GETHASH, /*nVersion=*/PROTOCOL_VERSION); + } else { + return ::SerializeHash(v, /*nType=*/SER_GETHASH, /*nVersion=*/PROTOCOL_VERSION); } - return ::SerializeHash(v, /*nType=*/SER_GETHASH, /*nVersion=*/ser_version); } template [[nodiscard]] bool AddUniqueProperty(const CDeterministicMN& dmn, const T& v) diff --git a/src/evo/netinfo.h b/src/evo/netinfo.h index ead025ca6629..11047a7268c3 100644 --- a/src/evo/netinfo.h +++ b/src/evo/netinfo.h @@ -225,12 +225,11 @@ class NetInfoEntry bool operator!=(const NetInfoEntry& rhs) const { return !(*this == rhs); } template - void Serialize(Stream& s_) const + void Serialize(Stream& s) const { - OverrideStream s(&s_, /*nType=*/0, s_.GetVersion() | ADDRV2_FORMAT); if (const auto* data_ptr_service{std::get_if(&m_data)}; m_type == NetInfoType::Service && data_ptr_service && data_ptr_service->IsValid()) { - s << m_type << *data_ptr_service; + s << m_type << WithParams(CNetAddr::V2, *data_ptr_service); } else if (const auto* data_ptr_domain{std::get_if(&m_data)}; m_type == NetInfoType::Domain && data_ptr_domain && data_ptr_domain->IsValid()) { s << m_type << *data_ptr_domain; @@ -240,14 +239,13 @@ class NetInfoEntry } template - void Unserialize(Stream& s_) + void Unserialize(Stream& s) { - OverrideStream s(&s_, /*nType=*/0, s_.GetVersion() | ADDRV2_FORMAT); s >> m_type; if (m_type == NetInfoType::Service) { try { auto& service{m_data.emplace()}; - s >> service; + s >> WithParams(CNetAddr::V2, service); if (!service.IsValid()) { Clear(); } // Invalid CService, mark as invalid } catch (const std::ios_base::failure&) { Clear(); } // Deser failed, mark as invalid } else if (m_type == NetInfoType::Domain) { @@ -327,23 +325,23 @@ class MnNetInfo final : public NetInfoInterface void Serialize(Stream& s) const { if (const auto service_opt{m_addr.GetAddrPort()}) { - s << *service_opt; + s << WithParams(CNetAddr::V1, *service_opt); } else { - s << CService{}; + s << WithParams(CNetAddr::V1, CService{}); } } // cppcheck-suppress functionStatic void Serialize(CSizeComputer& s) const { - s.seek(::GetSerializeSize(CService{}, s.GetVersion())); + s.seek(::GetSerializeSize(WithParams(CNetAddr::V1, CService{}), s.GetVersion())); } template void Unserialize(Stream& s) { CService service; - s >> service; + s >> WithParams(CNetAddr::V1, service); m_addr = NetInfoEntry{service}; } diff --git a/src/evo/snapshot.cpp b/src/evo/snapshot.cpp index b5c9bb2406e8..04cffdba85e8 100644 --- a/src/evo/snapshot.cpp +++ b/src/evo/snapshot.cpp @@ -92,8 +92,8 @@ void ValidateHashPrefixRuns(std::vector& prefixes) template void ValidateMNEncoding(const T& obj) { - // Use the decoder's actual accounting, including version-dependent fields - // and OverrideStream wrappers, rather than maintaining a second size formula. + // Use the decoder's actual accounting, including version-dependent fields, + // rather than maintaining a second size formula. CDataStream encoded{SER_DISK, CLIENT_VERSION}; encoded << obj; SnapshotBoundedInput bounded{encoded, EVO_SNAPSHOT_MAX_MN_COMPACT_ITEMS}; diff --git a/src/evo/snapshot.h b/src/evo/snapshot.h index ca39e51daf14..00d97aff56d0 100644 --- a/src/evo/snapshot.h +++ b/src/evo/snapshot.h @@ -215,17 +215,6 @@ uint64_t ReadCompactSize(SnapshotBoundedInput& stream) return stream.ReadBudgetedCompactSize(); } -/** - * NetInfoEntry overrides the stream version while decoding its payload. Keep - * the snapshot-local CompactSize budget visible through that transparent - * wrapper so strings are rejected before their deserializer resizes them. - */ -template -uint64_t ReadCompactSize(OverrideStream>& stream) -{ - return stream.GetStream().ReadBudgetedCompactSize(); -} - /** * Snapshot-local canonical deterministic-MN encoding. * diff --git a/src/hash.h b/src/hash.h index 0abe93aa2793..b7bd4ad2ea68 100644 --- a/src/hash.h +++ b/src/hash.h @@ -200,53 +200,20 @@ class HashVerifier : public HashWriter } }; -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 7718755b7899..1004f7ae87df 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/llmq/snapshot.h b/src/llmq/snapshot.h index 43370849c461..725072ad84e6 100644 --- a/src/llmq/snapshot.h +++ b/src/llmq/snapshot.h @@ -60,7 +60,7 @@ class CQuorumSnapshot template void Serialize(Stream& s) const { - const_cast(this)->SerializationOpBase(s, CSerActionSerialize()); + const_cast(this)->SerializationOpBase(s, ActionSerialize()); WriteCompactSize(s, activeQuorumMembers.size()); WriteFixedBitSet(s, activeQuorumMembers, activeQuorumMembers.size()); @@ -73,7 +73,7 @@ class CQuorumSnapshot template void Unserialize(Stream& s) { - SerializationOpBase(s, CSerActionUnserialize()); + SerializationOpBase(s, ActionUnserialize()); size_t cnt = ReadCompactSize(s); ReadFixedBitSet(s, activeQuorumMembers, cnt); @@ -158,7 +158,7 @@ class CQuorumRotationInfo template void Serialize(Stream& s) const { - const_cast(this)->SerializationOpBase(s, CSerActionSerialize()); + const_cast(this)->SerializationOpBase(s, ActionSerialize()); if (extraShare) { // Needed to maintain compatibility with existing on-disk format @@ -185,7 +185,7 @@ class CQuorumRotationInfo template void Unserialize(Stream& s) { - SerializationOpBase(s, CSerActionUnserialize()); + SerializationOpBase(s, ActionUnserialize()); if (extraShare) { CycleData val{}; diff --git a/src/net.cpp b/src/net.cpp index 4e9d3457cb7f..086ad8042653 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -213,7 +213,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 53e4834bdbcf..015855f0006d 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1581,8 +1581,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, nProtocolVersion, 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, mnauthChallenge, pnode.m_masternode_connection.load())); if (fLogIPs) { @@ -3952,7 +3952,7 @@ void PeerManagerImpl::ProcessMessage( nTime = 0; } vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer - vRecv >> addrMe; + vRecv >> WithParams(CNetAddr::V1, addrMe); if (!pfrom.IsInboundConn()) { // Overwrites potentially existing services. In contrast to this, @@ -4386,17 +4386,17 @@ void PeerManagerImpl::ProcessMessage( } 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()); @@ -6234,15 +6234,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 173179309add..3fb2e527eca3 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -22,13 +22,6 @@ extern bool fAllowPrivateNet; -/** - * 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` - */ -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` @@ -236,13 +229,23 @@ class CNetAddr 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); @@ -255,7 +258,7 @@ class CNetAddr template void Unserialize(Stream& s) { - if (s.GetVersion() & ADDRV2_FORMAT) { + if (s.GetParams().enc == Encoding::V2) { UnserializeV2Stream(s); } else { UnserializeV1Stream(s); @@ -562,8 +565,7 @@ class CService : public CNetAddr SERIALIZE_METHODS(CService, obj) { - READWRITEAS(CNetAddr, obj); - READWRITE(Using>(obj.port)); + READWRITE(AsBase(obj), Using>(obj.port)); } friend class CServiceHash; diff --git a/src/netfulfilledman.h b/src/netfulfilledman.h index 1e2367679c48..cbcc1d681ded 100644 --- a/src/netfulfilledman.h +++ b/src/netfulfilledman.h @@ -30,7 +30,7 @@ class NetFulfilledRequestStore SERIALIZE_METHODS(NetFulfilledRequestStore, obj) { LOCK(obj.cs_mapFulfilledRequests); - READWRITE(obj.mapFulfilledRequests); + READWRITE(WithParams(CNetAddr::V1, obj.mapFulfilledRequests)); } void Clear(); diff --git a/src/primitives/block.h b/src/primitives/block.h index 4505ee4db3ff..8789a21cbe37 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -212,8 +212,7 @@ class CBlock : public CBlockHeader 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 880d4bf43190..e2506594cb8e 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -429,35 +429,43 @@ class CAddress : public CService explicit 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)); @@ -471,8 +479,8 @@ class CAddress : public CService 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 c74a91c7d8a2..0e42e7080f5d 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -417,7 +417,7 @@ class CScript : public CScriptBase 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 8f5f1b228566..673eec91da12 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -10,6 +10,7 @@ #include #endif +#include #include #include @@ -144,12 +145,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; }) @@ -171,11 +200,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 @@ -184,21 +268,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 // Typically int8_t and char are distinct types, but some systems may define int8_t @@ -1394,26 +1476,17 @@ void Unserialize(Stream& is, std::atomic& a) } - /** - * 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 @@ -1472,36 +1545,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&&) { } @@ -1530,4 +1603,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 fd6c5a36e862..c654bbd32f30 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -714,7 +714,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; @@ -774,7 +774,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}; @@ -957,9 +957,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; @@ -989,7 +989,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)}; @@ -1006,7 +1006,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); @@ -1015,9 +1015,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; @@ -1036,7 +1036,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; } @@ -1044,7 +1044,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); @@ -1058,7 +1058,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/evo_netinfo_tests.cpp b/src/test/evo_netinfo_tests.cpp index 81d395f15c20..562bff5890b8 100644 --- a/src/test/evo_netinfo_tests.cpp +++ b/src/test/evo_netinfo_tests.cpp @@ -328,7 +328,7 @@ BOOST_AUTO_TEST_CASE(netinfo_ser) // Reading an invalid CService should fail trivial validation and return an empty object CDataStream ds(SER_DISK, CLIENT_VERSION); NetInfoEntry entry{}; - ds << NetInfoEntry::NetInfoType::Service << CService{}; + ds << NetInfoEntry::NetInfoType::Service << WithParams(CNetAddr::V1, CService{}); ds >> entry; BOOST_CHECK(entry.IsEmpty() && !entry.IsTriviallyValid()); } @@ -344,11 +344,11 @@ BOOST_AUTO_TEST_CASE(netinfo_ser) { // A valid CService should be constructable, readable and pass validation - CDataStream ds(SER_DISK, CLIENT_VERSION | ADDRV2_FORMAT); + CDataStream ds(SER_DISK, CLIENT_VERSION); CService service{LookupNumeric("1.1.1.1", Params().GetDefaultPort())}; BOOST_CHECK(service.IsValid()); NetInfoEntry entry{service}, entry2{}; - ds << NetInfoEntry::NetInfoType::Service << service; + ds << NetInfoEntry::NetInfoType::Service << WithParams(CNetAddr::V2, service); ds >> entry2; BOOST_CHECK(entry == entry2); BOOST_CHECK(!entry.IsEmpty() && entry.IsTriviallyValid()); @@ -361,9 +361,8 @@ BOOST_AUTO_TEST_CASE(netinfo_ser) service.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"); BOOST_CHECK(service.IsValid() && service.IsTor()); - CDataStream ds(SER_DISK, CLIENT_VERSION | ADDRV2_FORMAT); - ds << NetInfoEntry::NetInfoType::Service << service; - ds.SetVersion(CLIENT_VERSION); // Drop the explicit format flag + CDataStream ds(SER_DISK, CLIENT_VERSION); + ds << NetInfoEntry::NetInfoType::Service << WithParams(CNetAddr::V2, service); NetInfoEntry entry{}; ds >> entry; @@ -412,7 +411,7 @@ BOOST_AUTO_TEST_CASE(netinfo_retvals) bool CheckIfSerSame(const CService& lhs, const MnNetInfo& rhs) { CHashWriter ss_lhs(SER_GETHASH, 0), ss_rhs(SER_GETHASH, 0); - ss_lhs << lhs; + ss_lhs << WithParams(CNetAddr::V1, lhs); ss_rhs << rhs; return ss_lhs.GetSHA256() == ss_rhs.GetSHA256(); } diff --git a/src/test/evo_snapshot_tests.cpp b/src/test/evo_snapshot_tests.cpp index 2d3f97647c16..62b37a8d9048 100644 --- a/src/test/evo_snapshot_tests.cpp +++ b/src/test/evo_snapshot_tests.cpp @@ -884,9 +884,8 @@ BOOST_FIXTURE_TEST_CASE(bounded_readers_reject_claimed_sizes_first, BasicTesting WriteCompactSize(wrapped_string, evo::EVO_SNAPSHOT_MAX_MN_COMPACT_ITEMS + 1); wrapped_string << uint8_t{0x42}; evo::SnapshotBoundedInput bounded_string{wrapped_string, evo::EVO_SNAPSHOT_MAX_MN_COMPACT_ITEMS}; - OverrideStream bounded_override{&bounded_string, SER_DISK, CLIENT_VERSION}; std::string decoded_string; - BOOST_CHECK_EXCEPTION(bounded_override >> decoded_string, std::ios_base::failure, + BOOST_CHECK_EXCEPTION(bounded_string >> decoded_string, std::ios_base::failure, [](const auto& e) { return std::string{e.what()}.find("CompactSize budget exceeded") != std::string::npos; }); BOOST_CHECK(decoded_string.empty()); BOOST_REQUIRE_EQUAL(wrapped_string.size(), 1U); diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index eddd958094bb..033f11dd4409 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -48,7 +48,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 { @@ -77,12 +77,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. @@ -240,9 +240,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,7 +307,7 @@ 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; diff --git a/src/test/fuzz/deserialize.cpp b/src/test/fuzz/deserialize.cpp index d34cbb25fea3..afe5ad5d2af2 100644 --- a/src/test/fuzz/deserialize.cpp +++ b/src/test/fuzz/deserialize.cpp @@ -22,6 +22,8 @@ #include #include