From 9c9387eda9ee2c658f0301db3b6e32081678ca04 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Thu, 24 Oct 2024 21:31:51 +0800 Subject: [PATCH] Remove unreliable peers --- crates/subcoin-network/src/lib.rs | 4 ++-- crates/subcoin-network/src/peer_manager.rs | 9 +++++---- crates/subcoin-network/src/worker.rs | 23 +++++++--------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/crates/subcoin-network/src/lib.rs b/crates/subcoin-network/src/lib.rs index 4db9a43..2e9e456 100644 --- a/crates/subcoin-network/src/lib.rs +++ b/crates/subcoin-network/src/lib.rs @@ -116,8 +116,8 @@ pub enum Error { PingTimeout, #[error("Ping latency exceeds the threshold")] PingLatencyTooHigh, - #[error("Peer is deprioritized and has encountered multiple syncing failures")] - UnreliableSyncPeer, + #[error("Peer is deprioritized for syncing and has encountered multiple failures")] + UnreliablePeer, #[error("Peer's latency ({0} ms) is too high")] SlowPeer(Latency), #[error("Unexpected pong message")] diff --git a/crates/subcoin-network/src/peer_manager.rs b/crates/subcoin-network/src/peer_manager.rs index 5301b58..75dca49 100644 --- a/crates/subcoin-network/src/peer_manager.rs +++ b/crates/subcoin-network/src/peer_manager.rs @@ -266,7 +266,7 @@ impl PeerInfo { #[derive(Debug)] pub(crate) struct SlowPeer { pub(crate) peer_id: PeerId, - pub(crate) peer_latency: Latency, + pub(crate) latency: Latency, } /// Manages the peers in the network. @@ -392,9 +392,9 @@ where } }) .max_by_key(|(_peer_id, avg_latency)| *avg_latency) - .map(|(peer_id, peer_latency)| SlowPeer { + .map(|(peer_id, latency)| SlowPeer { peer_id: *peer_id, - peer_latency, + latency, }) } else { None @@ -479,7 +479,8 @@ where }); } - pub(crate) fn update_last_eviction(&mut self) { + pub(crate) fn evict(&mut self, peer_id: PeerId, reason: Error) { + self.peer_manager.disconnect(peer_id, reason); self.last_eviction = Instant::now(); } diff --git a/crates/subcoin-network/src/worker.rs b/crates/subcoin-network/src/worker.rs index c6bbe5c..aab1f04 100644 --- a/crates/subcoin-network/src/worker.rs +++ b/crates/subcoin-network/src/worker.rs @@ -215,29 +215,20 @@ where let sync_action = self.chain_sync.on_tick(); self.do_sync_action(sync_action); - let unreliable_peers = self.chain_sync.unreliable_peers(); - for peer in unreliable_peers { - self.peer_manager - .disconnect(peer, Error::UnreliableSyncPeer); + for peer in self.chain_sync.unreliable_peers() { + self.peer_manager.disconnect(peer, Error::UnreliablePeer); self.chain_sync.remove_peer(peer); self.peer_store.remove_peer(peer); } - if let Some(SlowPeer { - peer_id, - peer_latency, - }) = self.peer_manager.on_tick() - { - self.peer_manager - .disconnect(peer_id, Error::SlowPeer(peer_latency)); - self.peer_manager.update_last_eviction(); + if let Some(SlowPeer { peer_id, latency }) = self.peer_manager.on_tick() { + self.peer_manager.evict(peer_id, Error::SlowPeer(latency)); self.chain_sync.remove_peer(peer_id); } - for (peer, txids) in self - .transaction_manager - .on_tick(self.peer_manager.connected_peers()) - { + let connected_peers = self.peer_manager.connected_peers(); + + for (peer, txids) in self.transaction_manager.on_tick(connected_peers) { tracing::debug!("Broadcasting transaction IDs {txids:?} to {peer:?}"); let msg = NetworkMessage::Inv(txids.into_iter().map(Inventory::Transaction).collect()); if let Err(err) = self.send(peer, msg) {