Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
18 changes: 9 additions & 9 deletions client/network/src/protocol/generic_proto/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl GenericProto {
timer: _
} => {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);
let backoff_until = Some(if let Some(ban) = ban {
cmp::max(timer_deadline, Instant::now() + ban)
} else {
Expand All @@ -486,7 +486,7 @@ impl GenericProto {
// If relevant, the external API is instantly notified.
PeerState::Enabled { mut connections } => {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);

if connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_))) {
debug!(target: "sub-libp2p", "External API <= Closed({}, {:?})", peer_id, set_id);
Expand Down Expand Up @@ -942,7 +942,7 @@ impl GenericProto {
_ => {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})",
incoming.peer_id, incoming.set_id);
self.peerset.dropped(incoming.set_id, incoming.peer_id);
self.peerset.dropped(incoming.set_id, incoming.peer_id, sc_peerset::DropReason::Unknown);
},
}
return
Expand Down Expand Up @@ -1184,7 +1184,7 @@ impl NetworkBehaviour for GenericProto {

if connections.is_empty() {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);
*entry.get_mut() = PeerState::Backoff { timer, timer_deadline };

} else {
Expand Down Expand Up @@ -1324,7 +1324,7 @@ impl NetworkBehaviour for GenericProto {

if connections.is_empty() {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);
let ban_dur = Uniform::new(5, 10).sample(&mut rand::thread_rng());

let delay_id = self.next_delay_id;
Expand All @@ -1345,7 +1345,7 @@ impl NetworkBehaviour for GenericProto {
matches!(s, ConnectionState::Opening | ConnectionState::Open(_)))
{
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);

*entry.get_mut() = PeerState::Disabled {
connections,
Expand Down Expand Up @@ -1396,7 +1396,7 @@ impl NetworkBehaviour for GenericProto {
st @ PeerState::Requested |
st @ PeerState::PendingRequest { .. } => {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id.clone());
self.peerset.dropped(set_id, peer_id.clone(), sc_peerset::DropReason::Unknown);

let now = Instant::now();
let ban_duration = match st {
Expand Down Expand Up @@ -1682,7 +1682,7 @@ impl NetworkBehaviour for GenericProto {
// List of open connections wasn't empty before but now it is.
if !connections.iter().any(|(_, s)| matches!(s, ConnectionState::Opening)) {
debug!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", source, set_id);
self.peerset.dropped(set_id, source.clone());
self.peerset.dropped(set_id, source.clone(), sc_peerset::DropReason::Refused);
*entry.into_mut() = PeerState::Disabled {
connections, backoff_until: None
};
Expand Down Expand Up @@ -1846,7 +1846,7 @@ impl NetworkBehaviour for GenericProto {
matches!(s, ConnectionState::Opening | ConnectionState::Open(_)))
{
debug!(target: "sub-libp2p", "PSM <= Dropped({:?})", source);
self.peerset.dropped(set_id, source.clone());
self.peerset.dropped(set_id, source.clone(), sc_peerset::DropReason::Refused);

*entry.into_mut() = PeerState::Disabled {
connections,
Expand Down
17 changes: 16 additions & 1 deletion client/peerset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl Peerset {
///
/// Must only be called after the PSM has either generated a `Connect` message with this
/// `PeerId`, or accepted an incoming connection with this `PeerId`.
pub fn dropped(&mut self, set_id: SetId, peer_id: PeerId) {
pub fn dropped(&mut self, set_id: SetId, peer_id: PeerId, reason: DropReason) {
// We want reputations to be up-to-date before adjusting them.
self.update_time();

Expand All @@ -620,6 +620,10 @@ impl Peerset {
error!(target: "peerset", "Received dropped() for non-connected node"),
}

if let DropReason::Refused = reason {
self.on_remove_from_peers_set(set_id, peer_id);
}

self.alloc_slots();
}

Expand Down Expand Up @@ -704,6 +708,17 @@ impl Stream for Peerset {
}
}

/// Reason for calling [`Peerset::dropped`].
pub enum DropReason {
/// Substream or connection has been closed for an unknown reason.
Unknown,
/// Substream or connection has been explicitly refused by the target. In other words, the
/// peer doesn't actually belong to this set.
///
/// This has the side effect of calling [`PeersetHandle::remove_from_peers_set`].
Refused,
}

#[cfg(test)]
mod tests {
use libp2p::PeerId;
Expand Down
4 changes: 2 additions & 2 deletions client/peerset/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use futures::prelude::*;
use libp2p::PeerId;
use rand::distributions::{Distribution, Uniform, WeightedIndex};
use rand::seq::IteratorRandom;
use sc_peerset::{IncomingIndex, Message, Peerset, PeersetConfig, ReputationChange, SetConfig, SetId};
use sc_peerset::{DropReason, IncomingIndex, Message, Peerset, PeersetConfig, ReputationChange, SetConfig, SetId};
use std::{collections::HashMap, collections::HashSet, pin::Pin, task::Poll};

#[test]
Expand Down Expand Up @@ -130,7 +130,7 @@ fn test_once() {
3 => {
if let Some(id) = connected_nodes.iter().choose(&mut rng).cloned() {
connected_nodes.remove(&id);
peerset.dropped(SetId::from(0), id);
peerset.dropped(SetId::from(0), id, DropReason::Unknown);
}
}

Expand Down