refactor: rename CTransaction::GetTotalSize to signal that it's not cached

Transaction hashes are cached, it may not be intuitive that their sizes are actually recalculated every time.
This is done before the other refactors to clarify why we want to avoid calling this method;

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
This commit is contained in:
Lőrincandmaflcko committed 2026-01-19 20:20:13 +01:00
1 parent 898e8d3a2d
commit 1658b8f82b
10 files changed
+12 -12

No files matched your search

+1 -1
View File
@@ -58,7 +58,7 @@ static void DuplicateInputs(benchmark::Bench& bench)
naughtyTx.vout[0].nValue = 0; naughtyTx.vout[0].nValue = 0;
naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB; naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100; uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).ComputeTotalSize() + CTransaction(naughtyTx).ComputeTotalSize())) / 41) - 100;
for (uint64_t x = 0; x < (n_inputs - 1); ++x) { for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0); naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0);
} }
+1 -1
View File
@@ -203,7 +203,7 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
if (vtx_missing.size() <= tx_missing_offset) if (vtx_missing.size() <= tx_missing_offset)
return READ_STATUS_INVALID; return READ_STATUS_INVALID;
block.vtx[i] = vtx_missing[tx_missing_offset++]; block.vtx[i] = vtx_missing[tx_missing_offset++];
tx_missing_size += block.vtx[i]->GetTotalSize(); tx_missing_size += block.vtx[i]->ComputeTotalSize();
} else } else
block.vtx[i] = std::move(txn_available[i]); block.vtx[i] = std::move(txn_available[i]);
} }
+1 -1
View File
@@ -432,7 +432,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
entry.pushKV("txid", tx.GetHash().GetHex()); entry.pushKV("txid", tx.GetHash().GetHex());
entry.pushKV("hash", tx.GetWitnessHash().GetHex()); entry.pushKV("hash", tx.GetWitnessHash().GetHex());
entry.pushKV("version", tx.version); entry.pushKV("version", tx.version);
entry.pushKV("size", tx.GetTotalSize()); entry.pushKV("size", tx.ComputeTotalSize());
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR); entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
entry.pushKV("weight", GetTransactionWeight(tx)); entry.pushKV("weight", GetTransactionWeight(tx));
entry.pushKV("locktime", (int64_t)tx.nLockTime); entry.pushKV("locktime", (int64_t)tx.nLockTime);
+1 -1
View File
@@ -2577,7 +2577,7 @@ void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlo
return; return;
} }
resp.txn[i] = block.vtx[req.indexes[i]]; resp.txn[i] = block.vtx[req.indexes[i]];
tx_requested_size += resp.txn[i]->GetTotalSize(); tx_requested_size += resp.txn[i]->ComputeTotalSize();
} }
LogDebug(BCLog::CMPCTBLOCK, "Peer %d sent us a GETBLOCKTXN for block %s, sending a BLOCKTXN with %u txns. (%u bytes)\n", pfrom.GetId(), block.GetHash().ToString(), resp.txn.size(), tx_requested_size); LogDebug(BCLog::CMPCTBLOCK, "Peer %d sent us a GETBLOCKTXN for block %s, sending a BLOCKTXN with %u txns. (%u bytes)\n", pfrom.GetId(), block.GetHash().ToString(), resp.txn.size(), tx_requested_size);
+1 -1
View File
@@ -107,7 +107,7 @@ CAmount CTransaction::GetValueOut() const
return nValueOut; return nValueOut;
} }
unsigned int CTransaction::GetTotalSize() const unsigned int CTransaction::ComputeTotalSize() const
{ {
return ::GetSerializeSize(TX_WITH_WITNESS(*this)); return ::GetSerializeSize(TX_WITH_WITNESS(*this));
} }
+2 -2
View File
@@ -332,11 +332,11 @@ public:
CAmount GetValueOut() const; CAmount GetValueOut() const;
/** /**
* Get the total transaction size in bytes, including witness data. * Calculate the total transaction size in bytes, including witness data.
* "Total Size" defined in BIP141 and BIP144. * "Total Size" defined in BIP141 and BIP144.
* @return Total transaction size in bytes * @return Total transaction size in bytes
*/ */
unsigned int GetTotalSize() const; unsigned int ComputeTotalSize() const;
bool IsCoinBase() const bool IsCoinBase() const
{ {
+1 -1
View File
@@ -279,7 +279,7 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["comment"], true) + "<br>"; strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["comment"], true) + "<br>";
strHTML += "<b>" + tr("Transaction ID") + ":</b> " + rec->getTxHash() + "<br>"; strHTML += "<b>" + tr("Transaction ID") + ":</b> " + rec->getTxHash() + "<br>";
strHTML += "<b>" + tr("Transaction total size") + ":</b> " + QString::number(wtx.tx->GetTotalSize()) + " bytes<br>"; strHTML += "<b>" + tr("Transaction total size") + ":</b> " + QString::number(wtx.tx->ComputeTotalSize()) + " bytes<br>";
strHTML += "<b>" + tr("Transaction virtual size") + ":</b> " + QString::number(GetVirtualTransactionSize(*wtx.tx)) + " bytes<br>"; strHTML += "<b>" + tr("Transaction virtual size") + ":</b> " + QString::number(GetVirtualTransactionSize(*wtx.tx)) + " bytes<br>";
strHTML += "<b>" + tr("Output index") + ":</b> " + QString::number(rec->getOutputIndex()) + "<br>"; strHTML += "<b>" + tr("Output index") + ":</b> " + QString::number(rec->getOutputIndex()) + "<br>";
+1 -1
View File
@@ -2053,7 +2053,7 @@ static RPCHelpMan getblockstats()
int64_t tx_size = 0; int64_t tx_size = 0;
if (do_calculate_size) { if (do_calculate_size) {
tx_size = tx->GetTotalSize(); tx_size = tx->ComputeTotalSize();
if (do_mediantxsize) { if (do_mediantxsize) {
txsize_array.push_back(tx_size); txsize_array.push_back(tx_size);
} }
+1 -1
View File
@@ -1031,7 +1031,7 @@ static UniValue OrphanToJSON(const node::TxOrphanage::OrphanInfo& orphan)
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
o.pushKV("txid", orphan.tx->GetHash().ToString()); o.pushKV("txid", orphan.tx->GetHash().ToString());
o.pushKV("wtxid", orphan.tx->GetWitnessHash().ToString()); o.pushKV("wtxid", orphan.tx->GetWitnessHash().ToString());
o.pushKV("bytes", orphan.tx->GetTotalSize()); o.pushKV("bytes", orphan.tx->ComputeTotalSize());
o.pushKV("vsize", GetVirtualTransactionSize(*orphan.tx)); o.pushKV("vsize", GetVirtualTransactionSize(*orphan.tx));
o.pushKV("weight", GetTransactionWeight(*orphan.tx)); o.pushKV("weight", GetTransactionWeight(*orphan.tx));
UniValue from(UniValue::VARR); UniValue from(UniValue::VARR);
+2 -2
View File
@@ -68,7 +68,7 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
} }
(void)tx.GetHash(); (void)tx.GetHash();
(void)tx.GetTotalSize(); (void)tx.ComputeTotalSize();
try { try {
(void)tx.GetValueOut(); (void)tx.GetValueOut();
} catch (const std::runtime_error&) { } catch (const std::runtime_error&) {
@@ -92,7 +92,7 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
(void)AreInputsStandard(tx, coins_view_cache); (void)AreInputsStandard(tx, coins_view_cache);
(void)IsWitnessStandard(tx, coins_view_cache); (void)IsWitnessStandard(tx, coins_view_cache);
if (tx.GetTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding if (tx.ComputeTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
{ {
UniValue u{UniValue::VOBJ}; UniValue u{UniValue::VOBJ};
TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u); TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);