From 228e9201efb5574b1b96bb924de1d2e8dd1317f3 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 8 May 2023 15:37:11 +1000 Subject: [PATCH 1/2] txmempool: have CompareDepthAndScore sort missing txs first We use CompareDepthAndScore to choose an order of txs to inv. Rather than sorting txs that have been evicted from the mempool at the end of the list, sort them at the beginning so they are removed from the queue immediately. --- src/txmempool.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/txmempool.cpp b/src/txmempool.cpp index da875c271e3..05f28520be4 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -755,11 +755,16 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid) { + /* Return `true` if hasha should be considered sooner than hashb. Namely when: + * a is not in the mempool, but b is + * both are in the mempool and a has fewer ancestors than b + * both are in the mempool and a has a higher score than b + */ LOCK(cs); - indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha); - if (i == mapTx.end()) return false; indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb); - if (j == mapTx.end()) return true; + if (j == mapTx.end()) return false; + indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha); + if (i == mapTx.end()) return true; uint64_t counta = i->GetCountWithAncestors(); uint64_t countb = j->GetCountWithAncestors(); if (counta == countb) { From 5b3406094f2679dfb3763de4414257268565b943 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 8 May 2023 17:14:53 +1000 Subject: [PATCH 2/2] net_processing: Boost inv trickle rate If transactions are being added to the mempool at a rate faster than 7tx/s (INVENTORY_BROADCAST_PER_SECOND) then peers' inventory_to_send queue can become relatively large. If this happens, increase the number of txids we include in an INV message (normally capped at 35) by 5 for each 1000 txids in the queue. This will tend to clear a temporary excess out reasonably quickly; an excess of 4000 invs to send will be cleared down to 1000 in about 30 minutes, while an excess of 20000 invs would be cleared down to 1000 in about 60 minutes. --- src/net_processing.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index f5552cfcd8a..bcbbddddaa2 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -5666,7 +5666,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto) // especially since we have many peers and some will draw much shorter delays. unsigned int nRelayedTransactions = 0; LOCK(tx_relay->m_bloom_filter_mutex); - while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) { + size_t broadcast_max{INVENTORY_BROADCAST_MAX + (tx_relay->m_tx_inventory_to_send.size()/1000)*5}; + broadcast_max = std::min(1000, broadcast_max); + while (!vInvTx.empty() && nRelayedTransactions < broadcast_max) { // Fetch the top element from the heap std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder); std::set::iterator it = vInvTx.back();