From 3e3af451652322c92e8e41cf918e69d608ec7c77 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Thu, 7 Sep 2023 14:12:18 +0000 Subject: [PATCH] Convert CDiskBlockIndex and CBlockLocator to SerParams --- src/chain.h | 18 +++++++++++++++--- src/dbwrapper.h | 4 ++-- src/index/base.cpp | 7 +++++-- src/net_processing.cpp | 9 ++++++--- src/node/blockstorage.cpp | 8 ++++++-- src/primitives/block.h | 16 +++++++++++++--- src/test/fuzz/util.h | 2 +- src/wallet/walletdb.cpp | 11 +++++++---- 8 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/chain.h b/src/chain.h index 2e1fb37bec07a..d3a2a754473cc 100644 --- a/src/chain.h +++ b/src/chain.h @@ -389,6 +389,17 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* class CDiskBlockIndex : public CBlockIndex { public: + + enum class hashing_type + { + no, + yes + }; + struct SerParams { + const hashing_type value; + bool is_hashing() const { return value == hashing_type::yes; } + }; + uint256 hashPrev; CDiskBlockIndex() @@ -401,11 +412,12 @@ class CDiskBlockIndex : public CBlockIndex hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); } - SERIALIZE_METHODS(CDiskBlockIndex, obj) + SERIALIZE_METHODS_PARAMS(CDiskBlockIndex, obj, SerParams, params) { LOCK(::cs_main); - int _nVersion = s.GetVersion(); - if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED)); + int _nVersion = 0; + if (!params.is_hashing()) + READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED)); READWRITE(VARINT_MODE(obj.nHeight, VarIntMode::NONNEGATIVE_SIGNED)); READWRITE(VARINT(obj.nStatus)); diff --git a/src/dbwrapper.h b/src/dbwrapper.h index eac9594aa10e1..44366d0033090 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -167,7 +167,7 @@ class CDBIterator template bool GetValue(V& value) { try { - CDataStream ssValue{GetValueImpl(), SER_DISK, CLIENT_VERSION}; + DataStream ssValue{GetValueImpl()}; ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue >> value; } catch (const std::exception&) { @@ -229,7 +229,7 @@ class CDBWrapper return false; } try { - CDataStream ssValue{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION}; + DataStream ssValue{MakeByteSpan(*strValue)}; ssValue.Xor(obfuscate_key); ssValue >> value; } catch (const std::exception&) { diff --git a/src/index/base.cpp b/src/index/base.cpp index f18205a76ffd0..314b2323922a6 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -56,7 +56,9 @@ BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const { - bool success = Read(DB_BEST_BLOCK, locator); + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + auto wrapper = WithParams(ser_params, locator); + bool success = Read(DB_BEST_BLOCK, wrapper); if (!success) { locator.SetNull(); } @@ -65,7 +67,8 @@ bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator) { - batch.Write(DB_BEST_BLOCK, locator); + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + batch.Write(DB_BEST_BLOCK, WithParams(ser_params, locator)); } BaseIndex::BaseIndex(std::unique_ptr chain, std::string name) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 6b415b3a1ea63..8fbeaf3b75902 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2627,7 +2627,8 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc // Only allow a new getheaders message to go out if we don't have a recent // one already in-flight if (current_time - peer.m_last_getheaders_timestamp > HEADERS_RESPONSE_TIME) { - m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, locator, uint256())); + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, WithParams(ser_params, locator), uint256())); peer.m_last_getheaders_timestamp = current_time; return true; } @@ -3886,7 +3887,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, if (msg_type == NetMsgType::GETBLOCKS) { CBlockLocator locator; uint256 hashStop; - vRecv >> locator >> hashStop; + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + vRecv >> WithParams(ser_params, locator) >> hashStop; if (locator.vHave.size() > MAX_LOCATOR_SZ) { LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId()); @@ -4001,7 +4003,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, if (msg_type == NetMsgType::GETHEADERS) { CBlockLocator locator; uint256 hashStop; - vRecv >> locator >> hashStop; + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + vRecv >> WithParams(ser_params, locator) >> hashStop; if (locator.vHave.size() > MAX_LOCATOR_SZ) { LogPrint(BCLog::NET, "getheaders locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId()); diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 70f11be58615e..3594912b45e76 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -69,8 +69,10 @@ bool BlockTreeDB::WriteBatchSync(const std::vectorGetBlockHash()), CDiskBlockIndex{bi}); + auto wrapper = WithParams(ser_params, CDiskBlockIndex{bi}); + batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), wrapper); } return WriteBatch(batch, true); } @@ -102,7 +104,9 @@ bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, s std::pair key; if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) { CDiskBlockIndex diskindex; - if (pcursor->GetValue(diskindex)) { + const CDiskBlockIndex::SerParams ser_params{CDiskBlockIndex::hashing_type::no}; + auto wrapper = WithParams(ser_params, diskindex); + if (pcursor->GetValue(wrapper)) { // Construct block index object CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash()); pindexNew->pprev = insertBlockIndex(diskindex.hashPrev); diff --git a/src/primitives/block.h b/src/primitives/block.h index 861d362414b75..f79ed4ac26962 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -118,16 +118,26 @@ class CBlock : public CBlockHeader */ struct CBlockLocator { + enum class hashing_type + { + no, + yes + }; + struct SerParams { + const hashing_type value; + bool is_hashing() const { return value == hashing_type::yes; } + }; + std::vector vHave; CBlockLocator() {} explicit CBlockLocator(std::vector&& have) : vHave(std::move(have)) {} - SERIALIZE_METHODS(CBlockLocator, obj) + SERIALIZE_METHODS_PARAMS(CBlockLocator, obj, SerParams, params) { - int nVersion = s.GetVersion(); - if (!(s.GetType() & SER_GETHASH)) + int nVersion = 0; + if (!params.is_hashing()) READWRITE(nVersion); READWRITE(obj.vHave); } diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index 8263cd4c08e27..773b01c651edb 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -118,7 +118,7 @@ template [[nodiscard]] inline std::optional ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional& max_length = std::nullopt) noexcept { const std::vector buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length); - CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION}; + DataStream ds{buffer}; T obj; try { ds >> obj; diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 92eca46f051ec..ec062e89080af 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -174,14 +174,17 @@ bool WalletBatch::EraseWatchOnly(const CScript &dest) bool WalletBatch::WriteBestBlock(const CBlockLocator& locator) { - WriteIC(DBKeys::BESTBLOCK, CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan - return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, locator); + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + WriteIC(DBKeys::BESTBLOCK, WithParams(ser_params, CBlockLocator())); // Write empty block locator so versions that require a merkle branch automatically rescan + return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, WithParams(ser_params, locator)); } bool WalletBatch::ReadBestBlock(CBlockLocator& locator) { - if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true; - return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator); + const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no}; + auto wrapper = WithParams(ser_params, locator); + if (m_batch->Read(DBKeys::BESTBLOCK, wrapper) && !locator.vHave.empty()) return true; + return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, wrapper); } bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)