Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <logging.h>
#include <random.h>
#include <util/trace.h>
#include <version.h>

bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 0 additions & 1 deletion src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <univalue.h>
#include <util/string.h>
#include <util/strencodings.h>
#include <version.h>

#include <algorithm>
#include <string>
Expand Down
1 change: 0 additions & 1 deletion src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <prevector.h>
#include <serialize.h>
#include <uint256.h>
#include <version.h>

#include <string>
#include <vector>
Expand Down
4 changes: 2 additions & 2 deletions src/index/blockfilterindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <util/system.h>
#include <util/threadinterrupt.h>
#include <util/wpipe.h>
#include <version.h>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
#include <consensus/params.h>

#include <atomic>
Expand Down
4 changes: 2 additions & 2 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())) {
Expand Down
1 change: 0 additions & 1 deletion src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <tinyformat.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <version.h>

#include <cassert>
#include <stdexcept>
Expand Down
1 change: 1 addition & 0 deletions src/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <tinyformat.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <version.h>

#include <numeric>

Expand Down
4 changes: 3 additions & 1 deletion src/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ struct PSBTProprietary
template<typename Stream, typename... X>
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...);
}

Expand Down
1 change: 0 additions & 1 deletion src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <txmempool.h>
#include <util/check.h>
#include <validation.h>
#include <version.h>

#include <string>

Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
#include <version.h>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
#include <util/irange.h>

#include <chainlock/chainlock.h>
Expand Down
1 change: 0 additions & 1 deletion src/script/bitcoinconsensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <primitives/transaction.h>
#include <pubkey.h>
#include <script/interpreter.h>
#include <version.h>

namespace {

Expand Down
41 changes: 15 additions & 26 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
// i.e. anything that supports .read(Span<std::byte>) and .write(Span<const std::byte>)
//

class CSizeComputer;
class SizeComputer;

enum
{
Expand Down Expand Up @@ -250,7 +250,7 @@ inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
else return sizeof(unsigned char) + sizeof(uint64_t);
}

inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
inline void WriteCompactSize(SizeComputer& os, uint64_t nSize);

template<typename Stream>
void WriteCompactSize(Stream& os, uint64_t nSize)
Expand Down Expand Up @@ -376,7 +376,7 @@ inline unsigned int GetSizeOfVarInt(I n)
}

template<typename I>
inline void WriteVarInt(CSizeComputer& os, I n);
inline void WriteVarInt(SizeComputer& os, I n);

template<typename Stream, VarIntMode Mode, typename I>
void WriteVarInt(Stream& os, I n)
Expand Down Expand Up @@ -516,7 +516,7 @@ struct CFixedVarIntsBitSet
};

/* Forward declaration for WriteAutoBitSet */
template <typename T> size_t GetSerializeSize(const T& t, int nVersion = 0);
template <typename T> size_t GetSerializeSize(const T& t);

template<typename Stream>
void WriteAutoBitSet(Stream& s, const autobitset_t& item)
Expand All @@ -526,8 +526,8 @@ void WriteAutoBitSet(Stream& s, const autobitset_t& item)

assert(vec.size() == size);

size_t size1 = ::GetSerializeSize(CFixedBitSet(vec, size), s.GetVersion());
size_t size2 = ::GetSerializeSize(CFixedVarIntsBitSet(vec, size), s.GetVersion());
size_t size1 = ::GetSerializeSize(CFixedBitSet(vec, size));
size_t size2 = ::GetSerializeSize(CFixedVarIntsBitSet(vec, size));

assert(size1 == GetSizeOfFixedBitSet(size));

Expand Down Expand Up @@ -1336,22 +1336,21 @@ struct CSerActionUnserialize
/* ::GetSerializeSize implementations
*
* Computing the serialized size of objects is done through a special stream
* object of type CSizeComputer, which only records the number of bytes written
* object of type SizeComputer, which only records the number of bytes written
* to it.
*
* If your Serialize or SerializationOp method has non-trivial overhead for
* serialization, it may be worthwhile to implement a specialized version for
* CSizeComputer, which uses the s.seek() method to record bytes that would
* SizeComputer, which uses the s.seek() method to record bytes that would
* be written instead.
*/
class CSizeComputer
class SizeComputer
{
protected:
size_t nSize;

const int nVersion;
public:
explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {}
SizeComputer() : nSize(0) {}

void write(Span<const std::byte> src)
{
Expand All @@ -1365,7 +1364,7 @@ class CSizeComputer
}

template<typename T>
CSizeComputer& operator<<(const T& obj)
SizeComputer& operator<<(const T& obj)
{
::Serialize(*this, obj);
return (*this);
Expand All @@ -1374,8 +1373,6 @@ class CSizeComputer
size_t size() const {
return nSize;
}

int GetVersion() const { return nVersion; }
};

template <typename Stream, typename... Args>
Expand Down Expand Up @@ -1425,28 +1422,20 @@ inline void SerWrite(Stream& s, CSerActionUnserialize ser_action, Type&&, Fn&&)
}

template<typename I>
inline void WriteVarInt(CSizeComputer &s, I n)
inline void WriteVarInt(SizeComputer &s, I n)
{
s.seek(GetSizeOfVarInt<I>(n));
}

inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
inline void WriteCompactSize(SizeComputer &s, uint64_t nSize)
{
s.seek(GetSizeOfCompactSize(nSize));
}

template <typename T>
size_t GetSerializeSize(const T& t, int nVersion)
{
return (CSizeComputer(nVersion) << t).size();
}

template <typename... T>
size_t GetSerializeSizeMany(int nVersion, const T&... t)
size_t GetSerializeSize(const T& t)
{
CSizeComputer sc(nVersion);
SerializeMany(sc, t...);
return sc.size();
return (SizeComputer() << t).size();
}

#endif // BITCOIN_SERIALIZE_H
2 changes: 1 addition & 1 deletion src/test/flatfile_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
"lost if a trusted third party is still required to prevent double-spending.");

size_t pos1 = 0;
size_t pos2 = pos1 + GetSerializeSize(line1, CLIENT_VERSION);
size_t pos2 = pos1 + GetSerializeSize(line1);

// Write first line to file.
{
Expand Down
46 changes: 24 additions & 22 deletions src/test/serialize_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,31 @@ class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle

BOOST_AUTO_TEST_CASE(sizes)
{
BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0, 0));
BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0));
BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0)));
BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0)));
BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0)));
BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0)));
BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0)));
BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0)));
BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0)));
BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0)));
// Bool is serialized as uint8_t
BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0));
BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0)));

// Sanity-check GetSerializeSize and c++ type matching
BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0, 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0), 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0), 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0), 0), 2U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0), 0), 2U);
BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0), 0), 4U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4U);
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0)), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0)), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0)), 2U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0)), 2U);
BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0)), 4U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0)), 4U);
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0)), 8U);
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0)), 8U);
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0)), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}), 1U);
BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}), 2U);
}

BOOST_AUTO_TEST_CASE(varints)
Expand All @@ -94,13 +96,13 @@ BOOST_AUTO_TEST_CASE(varints)
CDataStream::size_type size = 0;
for (int i = 0; i < 100000; i++) {
ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED), 0);
size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED));
BOOST_CHECK(size == ss.size());
}

for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
ss << VARINT(i);
size += ::GetSerializeSize(VARINT(i), 0);
size += ::GetSerializeSize(VARINT(i));
BOOST_CHECK(size == ss.size());
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/uint256_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
BOOST_CHECK(OneL.begin() + 32 == OneL.end());
BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
BOOST_CHECK(GetSerializeSize(R1L, PROTOCOL_VERSION) == 32);
BOOST_CHECK(GetSerializeSize(ZeroL, PROTOCOL_VERSION) == 32);
BOOST_CHECK(GetSerializeSize(R1L) == 32);
BOOST_CHECK(GetSerializeSize(ZeroL) == 32);

CDataStream ss(0, PROTOCOL_VERSION);
ss << R1L;
Expand Down Expand Up @@ -230,8 +230,8 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
BOOST_CHECK(OneS.begin() + 20 == OneS.end());
BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
BOOST_CHECK(GetSerializeSize(R1S, PROTOCOL_VERSION) == 20);
BOOST_CHECK(GetSerializeSize(ZeroS, PROTOCOL_VERSION) == 20);
BOOST_CHECK(GetSerializeSize(R1S) == 20);
BOOST_CHECK(GetSerializeSize(ZeroS) == 20);

ss << R1S;
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
Expand Down
1 change: 1 addition & 0 deletions src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <util/string.h>
#include <util/time.h>
#include <util/vector.h>
#include <version.h>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

#include <functional>
#include <stdexcept>
Expand Down
Loading
Loading