Skip to content

Commit

Permalink
Drop unused GetType() from CSizeComputer
Browse files Browse the repository at this point in the history
Based on btc@da74db0940720407fafaf3582bbaf9c81a4d3b4d
  • Loading branch information
furszy committed Jul 3, 2021
1 parent 5c36b3d commit f05e692
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ bool CCoinsViewCache::PruneInvalidEntries()
return Flush();
}

static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_SIZE_CURRENT / ::GetSerializeSize(CTxOut(), SER_NETWORK, PROTOCOL_VERSION); // TODO: merge with similar definition in undo.h.
static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_SIZE_CURRENT / ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION); // TODO: merge with similar definition in undo.h.

const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
entry.pushKV("txid", tx.GetHash().GetHex());
entry.pushKV("version", tx.nVersion);
entry.pushKV("type", tx.nType);
entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
entry.pushKV("locktime", (int64_t)tx.nLockTime);

UniValue vin(UniValue::VARR);
Expand Down
2 changes: 1 addition & 1 deletion src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ class CDBTransaction {
template <typename V>
void Write(const CDataStream& ssKey, const V& v)
{
auto valueMemoryUsage = ::GetSerializeSize(v, SER_DISK, CLIENT_VERSION);
auto valueMemoryUsage = ::GetSerializeSize(v, CLIENT_VERSION);
if (deletes.erase(ssKey)) {
memoryUsage -= ssKey.size();
}
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)
IncrementExtraNonce(pblock, pindexPrev->nHeight + 1, nExtraNonce);

LogPrintf("Running PIVXMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
::GetSerializeSize(*pblock, PROTOCOL_VERSION));

//
// Search
Expand Down
2 changes: 1 addition & 1 deletion src/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
if (txout.scriptPubKey.IsUnspendable())
return 0;

size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
size_t nSize = GetSerializeSize(txout, 0);
nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
return dustRelayFeeIn.GetFee(nSize);
}
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
// Providing any more cleanup incentive than making additional inputs free would
// risk encouraging people to create junk outputs to redeem later.
if (nTxSize == 0)
nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
nTxSize = ::GetSerializeSize(*this, PROTOCOL_VERSION);
for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
{
unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
Expand All @@ -264,7 +264,7 @@ unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const

unsigned int CTransaction::GetTotalSize() const
{
return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
return ::GetSerializeSize(*this, PROTOCOL_VERSION);
}

std::string CTransaction::ToString() const
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodeltransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CTransactionRef& WalletModelTransaction::getTransaction()

unsigned int WalletModelTransaction::getTransactionSize()
{
return (!walletTransaction ? 0 : (::GetSerializeSize(*walletTransaction, SER_NETWORK, PROTOCOL_VERSION)));
return (!walletTransaction ? 0 : (::GetSerializeSize(*walletTransaction, PROTOCOL_VERSION)));
}

CAmount WalletModelTransaction::getTransactionFee()
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
if (chainActive.Contains(blockindex))
confirmations = chainActive.Height() - blockindex->nHeight + 1;
result.pushKV("confirmations", confirmations);
result.pushKV("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION));
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
result.pushKV("height", blockindex->nHeight);
result.pushKV("version", block.nVersion);
result.pushKV("merkleroot", block.hashMerkleRoot.GetHex());
Expand Down Expand Up @@ -1361,7 +1361,7 @@ UniValue getblockindexstats(const JSONRPCRequest& request) {
continue;

// Transaction size
nBytes += GetSerializeSize(tx, SER_NETWORK, CLIENT_VERSION);
nBytes += GetSerializeSize(tx, CLIENT_VERSION);

// Transparent inputs
for (unsigned int j = 0; j < tx.vin.size(); j++) {
Expand Down
2 changes: 1 addition & 1 deletion src/sapling/sapling_operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ OperationResult CheckTransactionSize(std::vector<SendManyRecipient>& recipients,
}
}
CTransaction tx(mtx);
size_t txsize = GetSerializeSize(tx, SER_NETWORK, tx.nVersion) + CTXOUT_REGULAR_SIZE * nTransparentOuts;
size_t txsize = tx.GetTotalSize() + CTXOUT_REGULAR_SIZE * nTransparentOuts;
if (fromTaddr) {
txsize += CTXIN_SPEND_DUST_SIZE;
txsize += CTXOUT_REGULAR_SIZE; // There will probably be taddr change
Expand Down
2 changes: 1 addition & 1 deletion src/script/bitcoinconsensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned i
CTransaction tx(deserialize, stream);
if (nIn >= tx.vin.size())
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);

// Regardless of the verification result, the tx did not error.
Expand Down
47 changes: 17 additions & 30 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is
* optional
*/
template<typename T> unsigned int GetSerializeSize(const Optional<T> &item);
template<typename T> unsigned int GetSerializeSizeNetwork(const Optional<T> &item);
template<typename Stream, typename T> void Serialize(Stream& os, const Optional<T>& item);
template<typename Stream, typename T> void Unserialize(Stream& is, Optional<T>& item);

Expand All @@ -620,29 +619,6 @@ unsigned int GetSerializeSize(const std::array<T, N> &item)
return size;
}

/**
* optional
*/
template<typename T>
unsigned int GetSerializeSize(const Optional<T> &item)
{
if (item) {
return 1 + GetSerializeSize(*item);
} else {
return 1;
}
}

template<typename T>
unsigned int GetSerializeSizeNetwork(const Optional<T> &item)
{
if (item) {
return 1 + GetSerializeSize(*item, SER_NETWORK, 0);
} else {
return 1;
}
}

template<typename Stream, typename T>
void Serialize(Stream& os, const Optional<T>& item)
{
Expand Down Expand Up @@ -1074,11 +1050,10 @@ class CSizeComputer
protected:
size_t nSize;

const int nType;
const int nVersion;

public:
CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {}

void write(const char* psz, size_t _nSize)
{
Expand All @@ -1104,7 +1079,6 @@ class CSizeComputer
}

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

template<typename Stream>
Expand Down Expand Up @@ -1155,15 +1129,28 @@ inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
}

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

template <typename S, typename T>
size_t GetSerializeSize(const S& s, const T& t)
{
return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
return (CSizeComputer(s.GetVersion()) << t).size();
}

/**
* optional
*/
template<typename T>
unsigned int GetSerializeSize(const Optional<T> &item)
{
if (item) {
return 1 + GetSerializeSize(*item);
} else {
return 1;
}
}

#endif // PIVX_SERIALIZE_H
8 changes: 4 additions & 4 deletions src/test/arith_uint256_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
BOOST_CHECK(R1L.GetLow64() == R1LLow64);
BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32);
BOOST_CHECK(GetSerializeSize(ZeroL, 0,PROTOCOL_VERSION) == 32);
BOOST_CHECK(GetSerializeSize(R1L, PROTOCOL_VERSION) == 32);
BOOST_CHECK(GetSerializeSize(ZeroL, PROTOCOL_VERSION) == 32);

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

ss << R1S;
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
Expand Down
10 changes: 5 additions & 5 deletions src/test/mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
tx2.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
tx2.vout[0].nValue = 2 * COIN;
pool.addUnchecked(tx2.GetHash(), entry.Fee(20000LL).Priority(9.0).FromTx(tx2));
uint64_t tx2Size = ::GetSerializeSize(tx2, SER_NETWORK, PROTOCOL_VERSION);
uint64_t tx2Size = ::GetSerializeSize(tx2, PROTOCOL_VERSION);

/* lowest fee */
CMutableTransaction tx3 = CMutableTransaction();
Expand Down Expand Up @@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
tx6.vout.resize(1);
tx6.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
tx6.vout[0].nValue = 20 * COIN;
uint64_t tx6Size = ::GetSerializeSize(tx6, SER_NETWORK, PROTOCOL_VERSION);
uint64_t tx6Size = ::GetSerializeSize(tx6, PROTOCOL_VERSION);

pool.addUnchecked(tx6.GetHash(), entry.Fee(0LL).FromTx(tx6));
BOOST_CHECK_EQUAL(pool.size(), 6);
Expand All @@ -403,7 +403,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
tx7.vout.resize(1);
tx7.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
tx7.vout[0].nValue = 10 * COIN;
uint64_t tx7Size = ::GetSerializeSize(tx7, SER_NETWORK, PROTOCOL_VERSION);
uint64_t tx7Size = ::GetSerializeSize(tx7, PROTOCOL_VERSION);

/* set the fee to just below tx2's feerate when including ancestor */
CAmount fee = (20000/tx2Size)*(tx7Size + tx6Size) - 1;
Expand Down Expand Up @@ -475,12 +475,12 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
BOOST_CHECK(pool.exists(tx2.GetHash()));
BOOST_CHECK(pool.exists(tx3.GetHash()));

pool.TrimToSize(::GetSerializeSize(CTransaction(tx1), SER_NETWORK, PROTOCOL_VERSION)); // mempool is limited to tx1's size in memory usage, so nothing fits
pool.TrimToSize(::GetSerializeSize(CTransaction(tx1), PROTOCOL_VERSION)); // mempool is limited to tx1's size in memory usage, so nothing fits
BOOST_CHECK(!pool.exists(tx1.GetHash()));
BOOST_CHECK(!pool.exists(tx2.GetHash()));
BOOST_CHECK(!pool.exists(tx3.GetHash()));

CFeeRate maxFeeRateRemoved(25000, ::GetSerializeSize(CTransaction(tx3), SER_NETWORK, PROTOCOL_VERSION) + ::GetSerializeSize(CTransaction(tx2), SER_NETWORK, PROTOCOL_VERSION));
CFeeRate maxFeeRateRemoved(25000, ::GetSerializeSize(CTransaction(tx3), PROTOCOL_VERSION) + ::GetSerializeSize(CTransaction(tx2), PROTOCOL_VERSION));
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);

CMutableTransaction tx4 = CMutableTransaction();
Expand Down
2 changes: 1 addition & 1 deletion src/test/policyestimator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
tx.vin[0].scriptSig = garbage;
tx.vout.resize(1);
tx.vout[0].nValue=0LL;
CFeeRate baseRate(basefee, ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
CFeeRate baseRate(basefee, ::GetSerializeSize(tx, PROTOCOL_VERSION));

// Create a fake block
std::vector<CTransactionRef> block;
Expand Down
6 changes: 3 additions & 3 deletions src/test/serialize_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void check_ser_rep(T thing, std::vector<unsigned char> expected)
CDataStream ss(SER_DISK, 0);
ss << thing;

BOOST_CHECK(GetSerializeSize(thing, 0, 0) == ss.size());
BOOST_CHECK(GetSerializeSize(thing, 0) == ss.size());

std::vector<unsigned char> serialized_representation(ss.begin(), ss.end());

Expand Down Expand Up @@ -208,13 +208,13 @@ BOOST_AUTO_TEST_CASE(varints)
CDataStream::size_type size = 0;
for (int i = 0; i < 100000; i++) {
ss << VARINT(i, VarIntMode::NONNEGATIVE_SIGNED);
size += ::GetSerializeSize(VARINT(i, VarIntMode::NONNEGATIVE_SIGNED), 0, 0);
size += ::GetSerializeSize(VARINT(i, VarIntMode::NONNEGATIVE_SIGNED), 0);
BOOST_CHECK(size == ss.size());
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFe
bool _spendsCoinbaseOrCoinstake, unsigned int _sigOps) :
tx(MakeTransactionRef(_tx)), nFee(_nFee), nTime(_nTime), entryPriority(_entryPriority), entryHeight(_entryHeight), hadNoDependencies(poolHasNoInputsOf), inChainInputValue(_inChainInputValue), spendsCoinbaseOrCoinstake(_spendsCoinbaseOrCoinstake), sigOpCount(_sigOps)
{
nTxSize = ::GetSerializeSize(*_tx, SER_NETWORK, PROTOCOL_VERSION);
nTxSize = ::GetSerializeSize(*_tx, PROTOCOL_VERSION);
nModSize = _tx->CalculateModifiedSize(nTxSize);
nUsageSize = _tx->DynamicMemoryUsage();
hasZerocoins = _tx->ContainsZerocoins();
Expand Down
2 changes: 1 addition & 1 deletion src/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TxInUndoDeserializer
TxInUndoDeserializer(Coin* coin) : txout(coin) {}
};

static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_SIZE_CURRENT / ::GetSerializeSize(CTxIn(), SER_NETWORK, PROTOCOL_VERSION);
static const size_t MAX_INPUTS_PER_BLOCK = MAX_BLOCK_SIZE_CURRENT / ::GetSerializeSize(CTxIn(), PROTOCOL_VERSION);

/** Undo information for a CTransaction */
class CTxUndo
Expand Down
12 changes: 6 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
}

vPos.emplace_back(tx.GetHash(), pos);
pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
pos.nTxOffset += ::GetSerializeSize(tx, CLIENT_VERSION);
}

// Push new tree anchor
Expand Down Expand Up @@ -1752,7 +1752,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
if (pindex->GetUndoPos().IsNull()) {
FlatFilePos diskPosBlock;
if (!FindUndoPos(state, pindex->nFile, diskPosBlock, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
if (!FindUndoPos(state, pindex->nFile, diskPosBlock, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40))
return error("ConnectBlock() : FindUndoPos failed");
if (!UndoWriteToDisk(blockundo, diskPosBlock, pindex->pprev->GetBlockHash()))
return AbortNode(state, "Failed to write undo data");
Expand Down Expand Up @@ -2778,7 +2778,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo

// Size limits
unsigned int nMaxBlockSize = MAX_BLOCK_SIZE_CURRENT;
const unsigned int nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
const unsigned int nBlockSize = ::GetSerializeSize(block, PROTOCOL_VERSION);
if (block.vtx.empty() || block.vtx.size() > nMaxBlockSize || nBlockSize > nMaxBlockSize)
return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");

Expand Down Expand Up @@ -3345,7 +3345,7 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockInde

// Write block to history file
try {
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
FlatFilePos blockPos;
if (dbp != nullptr)
blockPos = *dbp;
Expand Down Expand Up @@ -3401,7 +3401,7 @@ bool ProcessNewBlock(CValidationState& state, const std::shared_ptr<const CBlock
}

LogPrintf("%s : ACCEPTED Block %ld in %ld milliseconds with size=%d\n", __func__, newHeight, GetTimeMillis() - nStartTime,
GetSerializeSize(*pblock, SER_DISK, CLIENT_VERSION));
GetSerializeSize(*pblock, CLIENT_VERSION));

return true;
}
Expand Down Expand Up @@ -3890,7 +3890,7 @@ bool LoadGenesisBlock()
try {
CBlock& block = const_cast<CBlock&>(Params().GenesisBlock());
// Start new block file
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
FlatFilePos blockPos;
CValidationState state;
if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, block.GetBlockTime()))
Expand Down
Loading

0 comments on commit f05e692

Please sign in to comment.