log,net: avoid `ComputeTotalSize` when logging is disabled
What changed, and why it matters
This is a performance cleanup, not a security fix. A Bitcoin Core network function was doing unnecessary serialization work to calculate transaction byte sizes for a debug log line, even when debug logging was turned off. The change wraps that work in a check so it only runs when the relevant debug log category is actually enabled. There is no behavior change for users and no vulnerability is described or evident in the diff.
No security action required. Treat as a normal performance/refactoring commit. If reviewing for release notes, classify under 'performance' or 'resource usage' rather than security.
Security signals we found
No security signal present in commit message or diff
Performance optimization only
No input validation, authorization, or memory-safety changes
Evidence from the diff
In PeerManagerImpl::SendBlockTransactions(), the code previously called CTransaction::ComputeTotalSize() inside the main validation loop for every requested transaction, accumulating tx_requested_size regardless of whether BCLog::CMPCTBLOCK debug logging was active. The patch moves the size accumulation and the LogDebug() call behind LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug). Bounds checks and response construction remain on the hot path; only the debug-only serialization and logging are conditional. The variable type also changes from unsigned int to uint32_t, which is still adequate for serialized transaction sizes.
Changed components
src/net_processing.cppPeerManagerImpl::SendBlockTransactions()compact block (CMPCTBLOCK) debug loggingInspect captured patch +5 / −3
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 89f93781..f4c4ae28 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -2570,17 +2570,19 @@ uint32_t PeerManagerImpl::GetFetchFlags(const Peer& peer) const
void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req)
{
BlockTransactions resp(req);
- unsigned int tx_requested_size = 0;
for (size_t i = 0; i < req.indexes.size(); i++) {
if (req.indexes[i] >= block.vtx.size()) {
Misbehaving(peer, "getblocktxn with out-of-bounds tx indices");
return;
}
resp.txn[i] = block.vtx[req.indexes[i]];
- 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);
+ if (LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug)) {
+ uint32_t tx_requested_size{0};
+ for (const auto& tx : resp.txn) tx_requested_size += tx->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);
+ }
MakeAndPushMessage(pfrom, NetMsgType::BLOCKTXN, resp);
}
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.