Skip to content

Commit 6f1c56f

Browse files
committed
Merge bitcoin#35670: net: optimize compact block extra tx iteration
1a3cbf1 net: optimize compact block extra tx iteration (Lőrinc) Pull request description: **Problem:** `vExtraTxnForCompact` gives compact block reconstruction one more source for recently removed transactions. Before this PR, the first insertion resized the cache to its configured capacity, so before the cache was full, `PartiallyDownloadedBlock::InitData()` also scanned default `{Wtxid::ZERO, nullptr}` entries that had never been added to the cache. Those unused entries could still participate in reconstruction short-id matching. **Fix:** Reserve the configured capacity and append entries until the cache is full. Once full, keep the same ring-buffer overwrite behavior, configured maximum, and replacement order. **Risk:** Triggering the affected duplicate-match branch requires a block-specific 6-byte short-id collision while the extra-txn cache still contains default null slots. With a 16-thread benchmark of the `CBlockHeaderAndShortTxIDs` nonce-grinding path, the 50% collision time was ~178 days on my machine. ACKs for top commit: davidgumberg: crACK bitcoin@1a3cbf1 darosior: utACK 1a3cbf1 w0xlt: ACK 1a3cbf1 sedited: ACK 1a3cbf1 Tree-SHA512: 9022a2ea5f3ff4279bdee7fa0316d6e5c922be3f7c566743b4d16708c49c0c8dc8a8dcbd60530a0128277db6f192355be88312419952213ae06353a0d937c507
2 parents e94fda8 + 1a3cbf1 commit 6f1c56f

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/net_processing.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,11 +1908,13 @@ std::vector<CTransactionRef> PeerManagerImpl::AbortPrivateBroadcast(const uint25
19081908

19091909
void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
19101910
{
1911-
if (m_opts.max_extra_txs <= 0)
1912-
return;
1913-
if (!vExtraTxnForCompact.size())
1914-
vExtraTxnForCompact.resize(m_opts.max_extra_txs);
1915-
vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
1911+
if (m_opts.max_extra_txs == 0) return;
1912+
if (vExtraTxnForCompact.size() < m_opts.max_extra_txs) {
1913+
if (vExtraTxnForCompact.empty()) vExtraTxnForCompact.reserve(m_opts.max_extra_txs);
1914+
vExtraTxnForCompact.emplace_back(tx->GetWitnessHash(), tx);
1915+
} else {
1916+
vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
1917+
}
19161918
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % m_opts.max_extra_txs;
19171919
}
19181920

0 commit comments

Comments
 (0)