From b75862010aca90faee8687d969aa10b4076cb947 Mon Sep 17 00:00:00 2001 From: int88 <106391185+int88@users.noreply.github.com> Date: Mon, 4 Mar 2024 02:29:55 +0800 Subject: [PATCH] fix(tx-fetcher): only remove peer from active_peers when inflight_count <= 0 (#6928) Signed-off-by: int88 --- crates/net/network/src/transactions/fetcher.rs | 4 ++-- crates/net/network/src/transactions/mod.rs | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/net/network/src/transactions/fetcher.rs b/crates/net/network/src/transactions/fetcher.rs index 403c8b03ee..5e261eca7f 100644 --- a/crates/net/network/src/transactions/fetcher.rs +++ b/crates/net/network/src/transactions/fetcher.rs @@ -90,10 +90,10 @@ impl TransactionFetcher { fn decrement_inflight_request_count_for(&mut self, peer_id: &PeerId) { let remove = || -> bool { if let Some(inflight_count) = self.active_peers.get(peer_id) { - if *inflight_count <= DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER { + *inflight_count -= 1; + if *inflight_count == 0 { return true } - *inflight_count -= 1; } false }(); diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index 8efcb24d19..5980182d6e 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -2053,6 +2053,7 @@ mod tests { // peer_1 is idle assert!(tx_fetcher.is_idle(&peer_id_1)); + assert_eq!(tx_fetcher.active_peers.len(), 0); // sends request for buffered hashes to peer_1 tx_fetcher.on_fetch_pending_hashes(&tx_manager.peers, |_| true, || ()); @@ -2062,6 +2063,7 @@ mod tests { assert!(tx_fetcher.hashes_pending_fetch.is_empty()); // as long as request is in inflight peer_1 is not idle assert!(!tx_fetcher.is_idle(&peer_id_1)); + assert_eq!(tx_fetcher.active_peers.len(), 1); // mock session of peer_1 receives request let req = to_mock_session_rx @@ -2085,6 +2087,7 @@ mod tests { // request has resolved, peer_1 is idle again assert!(tx_fetcher.is_idle(&peer_id)); + assert_eq!(tx_fetcher.active_peers.len(), 0); // failing peer_1's request buffers requested hashes for retry assert_eq!(tx_fetcher.hashes_pending_fetch.len(), 2); @@ -2098,6 +2101,9 @@ mod tests { let tx_fetcher = &mut tx_manager.transaction_fetcher; + // peer_2 should be in active_peers. + assert_eq!(tx_fetcher.active_peers.len(), 1); + // since hashes are already seen, no changes to length of unknown hashes assert_eq!(tx_fetcher.hashes_fetch_inflight_and_pending_fetch.len(), 2); // but hashes are taken out of buffer and packed into request to peer_2 @@ -2119,5 +2125,6 @@ mod tests { // `MAX_REQUEST_RETRIES_PER_TX_HASH`, 2, for hashes reached so this time won't be buffered // for retry assert!(tx_fetcher.hashes_pending_fetch.is_empty()); + assert_eq!(tx_fetcher.active_peers.len(), 0); } }