From 8e5504aa6445a27fc141930e6eea566603786fbe Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 17 Nov 2023 10:37:47 +0000 Subject: [PATCH 1/3] Merge bitcoin/bitcoin#28878: Remove version field from GetSerializeSize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 83986f464c59a6517f790a960a72574e167f3f72 Include version.h in fewer places (Anthony Towns) c7b61fd61b199cbefda660c9d394bb4035a49528 Convert some CDataStream to DataStream (Anthony Towns) 1410d300df7e57a895f2697d9849a2201021c973 serialize: Drop useless version param from GetSerializeSize() (Anthony Towns) bf574a75016123309b894da895ab1c7a81731933 serialize: drop GetSerializeSizeMany (Anthony Towns) efa9eb6d7c8012fe4ed85699d81c8fe5dd18da1e serialize: Drop nVersion from [C]SizeComputer (Anthony Towns) Pull request description: Drops the version field from `GetSerializeSize()`, simplifying the code in various places. Also drop `GetSerializeSizeMany()` (as just removing the version parameter could result in silent bugs) and remove unnecessary instances of `#include `. ACKs for top commit: maflcko: ACK 83986f464c59a6517f790a960a72574e167f3f72 📒 theuni: ACK 83986f464c59a6517f790a960a72574e167f3f72. Tree-SHA512: 36617b6dfbb1b4b0afbf673e905525fc6d623d3f568d3f86e3b9d4f69820db97d099e83a88007bfff881f731ddca6755ebf1549e8d8a7762437dfadbf434c62e --- src/bench/checkblock.cpp | 4 +-- src/blockencodings.cpp | 2 +- src/coins.cpp | 3 +-- src/core_read.cpp | 1 - src/hash.h | 1 - src/index/blockfilterindex.cpp | 4 +-- src/node/blockstorage.cpp | 4 +-- src/primitives/transaction.cpp | 1 - src/psbt.cpp | 1 + src/psbt.h | 4 ++- src/rest.cpp | 1 - src/rpc/blockchain.cpp | 4 +-- src/script/bitcoinconsensus.cpp | 1 - src/serialize.h | 41 +++++++++++------------------ src/test/flatfile_tests.cpp | 2 +- src/test/serialize_tests.cpp | 46 +++++++++++++++++---------------- src/test/uint256_tests.cpp | 8 +++--- src/test/util/setup_common.h | 1 + src/undo.h | 1 - src/wallet/rpc/spend.cpp | 1 + src/wallet/spend.cpp | 2 +- src/zmq/zmqpublishnotifier.cpp | 2 ++ 22 files changed, 63 insertions(+), 72 deletions(-) diff --git a/src/bench/checkblock.cpp b/src/bench/checkblock.cpp index b4ff0ee3373f..47482e9c8ff1 100644 --- a/src/bench/checkblock.cpp +++ b/src/bench/checkblock.cpp @@ -17,7 +17,7 @@ static void DeserializeBlockTest(benchmark::Bench& bench) { - CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION); + DataStream stream(benchmark::data::block813851); std::byte a{0}; stream.write({&a, 1}); // Prevent compaction @@ -31,7 +31,7 @@ static void DeserializeBlockTest(benchmark::Bench& bench) static void DeserializeAndCheckBlockTest(benchmark::Bench& bench) { - CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION); + DataStream stream(benchmark::data::block813851); std::byte a{0}; stream.write({&a, 1}); // Prevent compaction diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index b66c98e8e80c..9f1757322066 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -164,7 +164,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c break; } - LogPrint(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, PROTOCOL_VERSION)); + LogPrint(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock)); return READ_STATUS_OK; } diff --git a/src/coins.cpp b/src/coins.cpp index 0aa3bf708484..6e37bd390e50 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -8,7 +8,6 @@ #include #include #include -#include bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; } uint256 CCoinsView::GetBestBlock() const { return uint256(); } @@ -339,7 +338,7 @@ void CCoinsViewCache::SanityCheck() const assert(recomputed_usage == cachedCoinsUsage); } -static const size_t MAX_OUTPUTS_PER_BLOCK = MaxBlockSize() / ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION); +static const size_t MAX_OUTPUTS_PER_BLOCK = MaxBlockSize() / ::GetSerializeSize(CTxOut()); const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid) { diff --git a/src/core_read.cpp b/src/core_read.cpp index 9f7c198d3f9e..86d8f6091d39 100644 --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include diff --git a/src/hash.h b/src/hash.h index 5dad7c72bcf7..a2ace4c9040e 100644 --- a/src/hash.h +++ b/src/hash.h @@ -14,7 +14,6 @@ #include #include #include -#include #include #include diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index f21b8afe4b43..33848d909a60 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -176,8 +176,8 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& assert(filter.GetFilterType() == GetFilterType()); size_t data_size = - GetSerializeSize(filter.GetBlockHash(), CLIENT_VERSION) + - GetSerializeSize(filter.GetEncodedFilter(), CLIENT_VERSION); + GetSerializeSize(filter.GetBlockHash()) + + GetSerializeSize(filter.GetEncodedFilter()); // If writing the filter would overflow the file, flush and move to the next one. if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) { diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 0a5ca022fa73..d8dbed4bc9a6 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -493,7 +493,7 @@ static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const } // Write index header - unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion()); + unsigned int nSize = GetSerializeSize(blockundo); fileout << messageStart << nSize; // Write undo data @@ -719,7 +719,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid // Write undo information to disk if (pindex->GetUndoPos().IsNull()) { FlatFilePos _pos; - if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) { + if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo) + 40)) { return error("ConnectBlock(): FindUndoPos failed"); } if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart())) { diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index a56f8c3677f4..3c7c4592fc20 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include diff --git a/src/psbt.cpp b/src/psbt.cpp index 90285c414ee8..c707e2d79ec8 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include diff --git a/src/psbt.h b/src/psbt.h index d3d778d8952a..8c2ef582baa7 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -77,7 +77,9 @@ struct PSBTProprietary template void SerializeToVector(Stream& s, const X&... args) { - WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...)); + SizeComputer sizecomp; + SerializeMany(sizecomp, args...); + WriteCompactSize(s, sizecomp.size()); SerializeMany(s, args...); } diff --git a/src/rest.cpp b/src/rest.cpp index e58a650689f6..b10fb6c8aefb 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 084849d6a8bd..b46834e772bc 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2085,7 +2085,7 @@ static RPCHelpMan getblockstats() for (const CTxOut& out : tx->vout) { tx_total_out += out.nValue; - size_t out_size = GetSerializeSize(out, PROTOCOL_VERSION) + PER_UTXO_OVERHEAD; + size_t out_size = GetSerializeSize(out) + PER_UTXO_OVERHEAD; utxo_size_inc += out_size; // The Genesis block and the repeated BIP30 block coinbases don't change the UTXO @@ -2125,7 +2125,7 @@ static RPCHelpMan getblockstats() const CTxOut& prevoutput = coin.out; tx_total_in += prevoutput.nValue; - size_t prevout_size = GetSerializeSize(prevoutput, PROTOCOL_VERSION) + PER_UTXO_OVERHEAD; + size_t prevout_size = GetSerializeSize(prevoutput) + PER_UTXO_OVERHEAD; utxo_size_inc -= prevout_size; utxo_size_inc_actual -= prevout_size; } diff --git a/src/script/bitcoinconsensus.cpp b/src/script/bitcoinconsensus.cpp index 8f979a0c26b0..7efea1f853ca 100644 --- a/src/script/bitcoinconsensus.cpp +++ b/src/script/bitcoinconsensus.cpp @@ -8,7 +8,6 @@ #include #include #include