From eebda00bd28aae70813c644ff2b63925cc934ced Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Tue, 8 Aug 2023 14:47:41 +0200 Subject: [PATCH] fix: peer connection to stale nodes (#5579) Description --- This fixes nodes continuously selecting bad stale nodes as connections and attempting to connect to them. Motivation and Context --- If nodes have bad nodes in their peer database they should not keep trying to connect to them. This will attempt to connect to a node once, and if the node was never online and it tried to connect to the node once, it will ignore the node till the node tries to connect to it first. This stops the problem with if a node has too many stale connections in its peer database, it will keep selecting stale nodes and try to connect to them. If the amount of stale nodes are too much, it can cause a constant read/write on the peer database of the node. How Has This Been Tested? --- Manually verified with database with mostly stale nodes. Node will try them once, then ignore them. --- comms/core/src/peer_manager/peer.rs | 17 +++++++++++++++++ comms/dht/src/connectivity/mod.rs | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/comms/core/src/peer_manager/peer.rs b/comms/core/src/peer_manager/peer.rs index 933a5627e0..b21fb14e23 100644 --- a/comms/core/src/peer_manager/peer.rs +++ b/comms/core/src/peer_manager/peer.rs @@ -158,6 +158,23 @@ impl Peer { &self.supported_protocols } + pub fn last_connect_attempt(&self) -> Option { + let mut last_connected_attempt = None; + for address in self.addresses.addresses() { + if let Some(address_time) = address.last_attempted { + match last_connected_attempt { + Some(last_time) => { + if last_time < address_time { + last_connected_attempt = address.last_attempted + } + }, + None => last_connected_attempt = address.last_attempted, + } + } + } + last_connected_attempt + } + /// Returns true if the peer is marked as offline pub fn is_offline(&self) -> bool { self.addresses.offline_at().is_some() diff --git a/comms/dht/src/connectivity/mod.rs b/comms/dht/src/connectivity/mod.rs index 9eaf9b2677..37ab6f5bf8 100644 --- a/comms/dht/src/connectivity/mod.rs +++ b/comms/dht/src/connectivity/mod.rs @@ -793,6 +793,10 @@ impl DhtConnectivity { { return false; } + // we have tried to connect to this peer, and we have never made a successful attempt at connection + if peer.offline_since().is_none() && peer.last_connect_attempt().is_some() { + return false; + } let is_excluded = excluded.contains(&peer.node_id); if is_excluded {