take or leave suggestion: instead of copying and pasting, make the delay computation re-usable?
0diff --git a/src/node/txdownloadman_impl.cpp b/src/node/txdownloadman_impl.cpp
1index 41d1badadf..d6e44c321b 100644
2--- a/src/node/txdownloadman_impl.cpp
3+++ b/src/node/txdownloadman_impl.cpp
4@@ -171,10 +171,28 @@ void TxDownloadManagerImpl::DisconnectedPeer(NodeId nodeid)
5 m_peer_info.erase(it);
6 }
7
8 }
9
10+std::chrono::seconds TxDownloadManagerImpl::GetTxRequestDelay(NodeId peer, const TxDownloadConnectionInfo& info, const GenTxid& gtxid)
11+{
12+ // Decide the TxRequestTracker parameters for this announcement:
13+ // - "preferred": if fPreferredDownload is set (= outbound, or NetPermissionFlags::NoBan permission)
14+ // - "reqtime": current time plus delays for:
15+ // - NONPREF_PEER_TX_DELAY for announcements from non-preferred connections
16+ // - TXID_RELAY_DELAY for txid announcements while wtxid peers are available
17+ // - OVERLOADED_PEER_TX_DELAY for announcements from peers which have at least
18+ // MAX_PEER_TX_REQUEST_IN_FLIGHT requests in flight (and don't have NetPermissionFlags::Relay).
19+ auto delay{0s};
20+ if (!info.m_preferred) delay += NONPREF_PEER_TX_DELAY;
21+ if (!gtxid.IsWtxid() && m_num_wtxid_peers > 0) delay += TXID_RELAY_DELAY;
22+ const bool overloaded = !info.m_relay_permissions && m_txrequest.CountInFlight(peer) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
23+ if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
24+
25+ return delay;
26+}
27+
28 bool TxDownloadManagerImpl::AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now, bool p2p_inv)
29 {
30 // If this is an orphan we are trying to resolve, consider this peer as a orphan resolution candidate instead.
31 // - received as an p2p inv
32 // - is wtxid matching something in orphanage
33@@ -206,24 +224,12 @@ bool TxDownloadManagerImpl::AddTxAnnouncement(NodeId peer, const GenTxid& gtxid,
34 const auto& info = it->second.m_connection_info;
35 if (!info.m_relay_permissions && m_txrequest.Count(peer) >= MAX_PEER_TX_ANNOUNCEMENTS) {
36 // Too many queued announcements for this peer
37 return false;
38 }
39- // Decide the TxRequestTracker parameters for this announcement:
40- // - "preferred": if fPreferredDownload is set (= outbound, or NetPermissionFlags::NoBan permission)
41- // - "reqtime": current time plus delays for:
42- // - NONPREF_PEER_TX_DELAY for announcements from non-preferred connections
43- // - TXID_RELAY_DELAY for txid announcements while wtxid peers are available
44- // - OVERLOADED_PEER_TX_DELAY for announcements from peers which have at least
45- // MAX_PEER_TX_REQUEST_IN_FLIGHT requests in flight (and don't have NetPermissionFlags::Relay).
46- auto delay{0us};
47- if (!info.m_preferred) delay += NONPREF_PEER_TX_DELAY;
48- if (!gtxid.IsWtxid() && m_num_wtxid_peers > 0) delay += TXID_RELAY_DELAY;
49- const bool overloaded = !info.m_relay_permissions && m_txrequest.CountInFlight(peer) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
50- if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
51
52- m_txrequest.ReceivedInv(peer, gtxid, info.m_preferred, now + delay);
53+ m_txrequest.ReceivedInv(peer, gtxid, info.m_preferred, now + GetTxRequestDelay(peer, info, gtxid));
54
55 return false;
56 }
57
58 std::optional<std::chrono::seconds> TxDownloadManagerImpl::OrphanResolutionCandidate(NodeId nodeid, const Wtxid& orphan_wtxid)
59@@ -240,21 +246,12 @@ std::optional<std::chrono::seconds> TxDownloadManagerImpl::OrphanResolutionCandi
60 // orphan resolution involves at least 1 transaction request which may or may not be
61 // currently tracked in m_txrequest, so we include that in the count.
62 if (m_txrequest.Count(nodeid) >= MAX_PEER_TX_ANNOUNCEMENTS) return std::nullopt;
63 }
64
65- std::chrono::seconds delay{0s};
66- if (!info.m_preferred) delay += NONPREF_PEER_TX_DELAY;
67- // The orphan wtxid is used, but resolution entails requesting the parents by txid. Sometimes
68- // parent and child are announced and thus requested around the same time, and we happen to
69- // receive child sooner. Waiting a few seconds may allow us to cancel the orphan resolution
70- // request if the parent arrives in that time.
71- if (m_num_wtxid_peers > 0) delay += TXID_RELAY_DELAY;
72- const bool overloaded = !info.m_relay_permissions && m_txrequest.CountInFlight(nodeid) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
73- if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
74-
75- return delay;
76+ // We're fetching parents via txid; so pass in as a txid
77+ return GetTxRequestDelay(nodeid, info, GenTxid::Txid(orphan_wtxid));
78 }
79
80 std::vector<GenTxid> TxDownloadManagerImpl::GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
81 {
82 std::vector<GenTxid> requests;
83diff --git a/src/node/txdownloadman_impl.h b/src/node/txdownloadman_impl.h
84index 560802ab5d..0bd673e956 100644
85--- a/src/node/txdownloadman_impl.h
86+++ b/src/node/txdownloadman_impl.h
87@@ -158,10 +158,12 @@ public:
88 bool AlreadyHaveTx(const GenTxid& gtxid, bool include_reconsiderable);
89
90 void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo& info);
91 void DisconnectedPeer(NodeId nodeid);
92
93+ std::chrono::seconds GetTxRequestDelay(NodeId peer, const TxDownloadConnectionInfo& info, const GenTxid& gtxid);
94+
95 /** Consider adding this tx hash to txrequest. Should be called whenever a new inv has been received.
96 * Also called internally when a transaction is missing parents so that we can request them.
97 */
98 bool AddTxAnnouncement(NodeId peer, const GenTxid& gtxid, std::chrono::microseconds now, bool p2p_inv);