Skip to content

Commit

Permalink
Remove unreliable peers
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Oct 24, 2024
1 parent 6643752 commit 9c9387e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/subcoin-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
9 changes: 5 additions & 4 deletions crates/subcoin-network/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
23 changes: 7 additions & 16 deletions crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9c9387e

Please sign in to comment.