Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/net/common/src/ban_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ impl BanList {
Self { banned_ips, banned_peers }
}

/// Removes all peers that are no longer banned.
pub fn evict_peers(&mut self, now: Instant) -> Vec<PeerId> {
let mut evicted = Vec::new();
self.banned_peers.retain(|peer, until| {
if let Some(until) = until {
if now > *until {
evicted.push(*peer);
return false
}
}
true
});
evicted
}

/// Removes all entries that should no longer be banned.
pub fn evict(&mut self, now: Instant) {
self.banned_ips.retain(|_, until| {
Expand Down
11 changes: 8 additions & 3 deletions crates/net/network/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ pub use client::FetchClient;
///
/// This type is hooked into the staged sync pipeline and delegates download request to available
/// peers and sends the response once ready.
///
/// This type maintains a list of connected peers that are available for requests.
pub struct StateFetcher {
/// Currently active [`GetBlockHeaders`] requests
inflight_headers_requests:
HashMap<PeerId, Request<HeadersRequest, PeerRequestResult<Vec<Header>>>>,
/// Currently active [`GetBlockBodies`] requests
inflight_bodies_requests:
HashMap<PeerId, Request<Vec<H256>, PeerRequestResult<Vec<BlockBody>>>>,
/// The list of available peers for requests.
/// The list of _available_ peers for requests.
peers: HashMap<PeerId, Peer>,
/// The handle to the peers manager
peers_handle: PeersHandle,
Expand Down Expand Up @@ -63,6 +65,9 @@ impl StateFetcher {
self.peers.insert(peer_id, Peer { state: PeerState::Idle, best_hash, best_number });
}

/// Removes the peer from the peer list, after which it is no longer available for future
/// requests.
///
/// Invoked when an active session was closed.
///
/// This cancels als inflight request and sends an error to the receiver.
Expand Down Expand Up @@ -97,7 +102,7 @@ impl StateFetcher {
}
}

/// Returns the next idle peer that's ready to accept a request
/// Returns the _next_ idle peer that's ready to accept a request.
fn next_peer(&mut self) -> Option<(&PeerId, &mut Peer)> {
self.peers.iter_mut().find(|(_, peer)| peer.state.is_idle())
}
Expand Down Expand Up @@ -181,7 +186,7 @@ impl StateFetcher {

/// Returns a new followup request for the peer.
///
/// Caution: this expects that the peer is _not_ closed
/// Caution: this expects that the peer is _not_ closed.
fn followup_request(&mut self, peer_id: PeerId) -> Option<BlockResponseOutcome> {
let req = self.queued_requests.pop_front()?;
let req = self.prepare_block_request(peer_id, req);
Expand Down
6 changes: 3 additions & 3 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,9 @@ where
?error,
"Outgoing pending session failed"
);
let swarm = this.swarm.state_mut().peers_mut();
swarm.on_closed_outgoing_pending_session();
swarm.apply_reputation_change(&peer_id, ReputationChangeKind::FailedToConnect);
let peers = this.swarm.state_mut().peers_mut();
peers.on_closed_outgoing_pending_session(&peer_id);
peers.apply_reputation_change(&peer_id, ReputationChangeKind::FailedToConnect);

if error.map(|err| err.merits_discovery_ban()).unwrap_or_default() {
this.swarm.state_mut().ban_discovery(peer_id, remote_addr.ip());
Expand Down
Loading