Skip to content

Commit b4e724d

Browse files
feat(swarm): remove deprecated banning API
This is replaced by the `libp2p::allow_block_list` module. Related: #3647. Pull-Request: #3886.
1 parent b507fe2 commit b4e724d

File tree

5 files changed

+7
-239
lines changed

5 files changed

+7
-239
lines changed

misc/metrics/src/swarm.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub(crate) struct Metrics {
4141

4242
dial_attempt: Counter,
4343
outgoing_connection_error: Family<OutgoingConnectionErrorLabels, Counter>,
44-
connected_to_banned_peer: Family<AddressLabels, Counter>,
4544
}
4645

4746
impl Metrics {
@@ -104,13 +103,6 @@ impl Metrics {
104103
outgoing_connection_error.clone(),
105104
);
106105

107-
let connected_to_banned_peer = Family::default();
108-
sub_registry.register(
109-
"connected_to_banned_peer",
110-
"Number of connection attempts to banned peer",
111-
connected_to_banned_peer.clone(),
112-
);
113-
114106
let connections_established = Family::default();
115107
sub_registry.register(
116108
"connections_established",
@@ -145,7 +137,6 @@ impl Metrics {
145137
listener_error,
146138
dial_attempt,
147139
outgoing_connection_error,
148-
connected_to_banned_peer,
149140
connections_establishment_duration,
150141
}
151142
}
@@ -224,8 +215,6 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
224215
}
225216
}
226217
#[allow(deprecated)]
227-
libp2p_swarm::DialError::Banned => record(OutgoingConnectionError::Banned),
228-
#[allow(deprecated)]
229218
libp2p_swarm::DialError::ConnectionLimit(_) => {
230219
record(OutgoingConnectionError::ConnectionLimit)
231220
}
@@ -250,14 +239,6 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
250239
}
251240
};
252241
}
253-
#[allow(deprecated)]
254-
libp2p_swarm::SwarmEvent::BannedPeer { endpoint, .. } => {
255-
self.connected_to_banned_peer
256-
.get_or_create(&AddressLabels {
257-
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
258-
})
259-
.inc();
260-
}
261242
libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => {
262243
self.new_listen_addr
263244
.get_or_create(&AddressLabels {
@@ -339,7 +320,6 @@ enum PeerStatus {
339320

340321
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
341322
enum OutgoingConnectionError {
342-
Banned,
343323
ConnectionLimit,
344324
LocalPeerId,
345325
NoAddresses,

protocols/kad/src/behaviour.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,9 +1905,7 @@ where
19051905
};
19061906

19071907
match error {
1908-
#[allow(deprecated)]
1909-
DialError::Banned
1910-
| DialError::LocalPeerId { .. }
1908+
DialError::LocalPeerId { .. }
19111909
| DialError::InvalidPeerId { .. }
19121910
| DialError::WrongPeerId { .. }
19131911
| DialError::Aborted

swarm/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
- Return a bool from `ExternalAddresses::on_swarm_event` and `ListenAddresses::on_swarm_event` indicating whether any state was changed.
1111
See [PR 3865].
1212

13+
- Remove deprecated banning API from `Swarm`.
14+
Users should migrate to `libp2p::allow_block_list`.
15+
See [PR 3886].
16+
1317
- Remove `ConnectionHandlerUpgrErr::Timer` variant.
1418
This variant was never constructed and thus dead code.
1519
See [PR 3605].
@@ -19,6 +23,7 @@
1923
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
2024
[PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746
2125
[PR 3865]: https://github.com/libp2p/rust-libp2p/pull/3865
26+
[PR 3886]: https://github.com/libp2p/rust-libp2p/pull/3886
2227

2328
## 0.42.2
2429

swarm/src/lib.rs

Lines changed: 1 addition & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
223223
},
224224
/// A new connection arrived on a listener and is in the process of protocol negotiation.
225225
///
226-
/// A corresponding [`ConnectionEstablished`](SwarmEvent::ConnectionEstablished),
227-
/// [`BannedPeer`](SwarmEvent::BannedPeer), or
226+
/// A corresponding [`ConnectionEstablished`](SwarmEvent::ConnectionEstablished) or
228227
/// [`IncomingConnectionError`](SwarmEvent::IncomingConnectionError) event will later be
229228
/// generated for this connection.
230229
IncomingConnection {
@@ -256,14 +255,6 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
256255
/// Error that has been encountered.
257256
error: DialError,
258257
},
259-
/// We connected to a peer, but we immediately closed the connection because that peer is banned.
260-
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
261-
BannedPeer {
262-
/// Identity of the banned peer.
263-
peer_id: PeerId,
264-
/// Endpoint of the connection that has been closed.
265-
endpoint: ConnectedPoint,
266-
},
267258
/// One of our listeners has reported a new local listening address.
268259
NewListenAddr {
269260
/// The listener that is listening on the new address.
@@ -349,9 +340,6 @@ where
349340
/// similar mechanisms.
350341
external_addrs: Addresses,
351342

352-
/// List of nodes for which we deny any incoming connection.
353-
banned_peers: HashSet<PeerId>,
354-
355343
/// Pending event to be delivered to connection handlers
356344
/// (or dropped if the peer disconnected) before the `behaviour`
357345
/// can be polled again.
@@ -560,22 +548,6 @@ where
560548
return Err(e);
561549
}
562550

563-
if let Some(peer_id) = peer_id {
564-
// Check if peer is banned.
565-
if self.banned_peers.contains(&peer_id) {
566-
#[allow(deprecated)]
567-
let error = DialError::Banned;
568-
self.behaviour
569-
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
570-
peer_id: Some(peer_id),
571-
error: &error,
572-
connection_id,
573-
}));
574-
575-
return Err(error);
576-
}
577-
}
578-
579551
let addresses = {
580552
let mut addresses_from_opts = dial_opts.get_addresses();
581553

@@ -742,27 +714,6 @@ where
742714
}
743715
}
744716

745-
/// Bans a peer by its peer ID.
746-
///
747-
/// Any incoming connection and any dialing attempt will immediately be rejected.
748-
/// This function has no effect if the peer is already banned.
749-
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
750-
pub fn ban_peer_id(&mut self, peer_id: PeerId) {
751-
if self.banned_peers.insert(peer_id) {
752-
// Note that established connections to the now banned peer are closed but not
753-
// added to [`Swarm::banned_peer_connections`]. They have been previously reported
754-
// as open to the behaviour and need be reported as closed once closing the
755-
// connection finishes.
756-
self.pool.disconnect(peer_id);
757-
}
758-
}
759-
760-
/// Unbans a peer.
761-
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
762-
pub fn unban_peer_id(&mut self, peer_id: PeerId) {
763-
self.banned_peers.remove(&peer_id);
764-
}
765-
766717
/// Disconnects a peer by its peer ID, closing all connections to said peer.
767718
///
768719
/// Returns `Ok(())` if there was one or more established connections to the peer.
@@ -818,11 +769,6 @@ where
818769
concurrent_dial_errors,
819770
established_in,
820771
} => {
821-
if self.banned_peers.contains(&peer_id) {
822-
#[allow(deprecated)]
823-
return Some(SwarmEvent::BannedPeer { peer_id, endpoint });
824-
}
825-
826772
let handler = match endpoint.clone() {
827773
ConnectedPoint::Dialer {
828774
address,
@@ -1715,7 +1661,6 @@ where
17151661
supported_protocols: Default::default(),
17161662
listened_addrs: HashMap::new(),
17171663
external_addrs: Addresses::default(),
1718-
banned_peers: HashSet::new(),
17191664
pending_event: None,
17201665
}
17211666
}
@@ -1724,9 +1669,6 @@ where
17241669
/// Possible errors when trying to establish or upgrade an outbound connection.
17251670
#[derive(Debug)]
17261671
pub enum DialError {
1727-
/// The peer is currently banned.
1728-
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
1729-
Banned,
17301672
/// The configured limit for simultaneous outgoing connections
17311673
/// has been reached.
17321674
#[deprecated(
@@ -1786,8 +1728,6 @@ impl fmt::Display for DialError {
17861728
f,
17871729
"Dial error: tried to dial local peer id at {endpoint:?}."
17881730
),
1789-
#[allow(deprecated)]
1790-
DialError::Banned => write!(f, "Dial error: peer is banned."),
17911731
DialError::DialPeerConditionFalse(c) => {
17921732
write!(f, "Dial error: condition {c:?} for dialing peer was false.")
17931733
}
@@ -1838,8 +1778,6 @@ impl error::Error for DialError {
18381778
DialError::ConnectionLimit(err) => Some(err),
18391779
DialError::LocalPeerId { .. } => None,
18401780
DialError::NoAddresses => None,
1841-
#[allow(deprecated)]
1842-
DialError::Banned => None,
18431781
DialError::DialPeerConditionFalse(_) => None,
18441782
DialError::Aborted => None,
18451783
DialError::InvalidPeerId { .. } => None,
@@ -2121,136 +2059,6 @@ mod tests {
21212059
&& !swarm2.is_connected(swarm1.local_peer_id())
21222060
}
21232061

2124-
/// Establishes multiple connections between two peers,
2125-
/// after which one peer bans the other.
2126-
///
2127-
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
2128-
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
2129-
/// while unbanned.
2130-
///
2131-
/// While the ban is in effect, further dials occur. For these connections no
2132-
/// [`FromSwarm::ConnectionEstablished`], [`FromSwarm::ConnectionClosed`]
2133-
/// calls should be registered.
2134-
#[test]
2135-
#[allow(deprecated)]
2136-
fn test_connect_disconnect_ban() {
2137-
let _ = env_logger::try_init();
2138-
2139-
// Since the test does not try to open any substreams, we can
2140-
// use keep alive protocols handler.
2141-
let handler_proto = keep_alive::ConnectionHandler;
2142-
2143-
let mut swarm1 = new_test_swarm::<_, ()>(handler_proto.clone()).build();
2144-
let mut swarm2 = new_test_swarm::<_, ()>(handler_proto).build();
2145-
2146-
let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
2147-
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
2148-
2149-
swarm1.listen_on(addr1).unwrap();
2150-
swarm2.listen_on(addr2.clone()).unwrap();
2151-
2152-
let swarm1_id = *swarm1.local_peer_id();
2153-
2154-
#[derive(Debug)]
2155-
enum Stage {
2156-
/// Waiting for the peers to connect. Banning has not occurred.
2157-
Connecting,
2158-
/// Ban occurred.
2159-
Banned,
2160-
// Ban is in place and a dial is ongoing.
2161-
BannedDial,
2162-
// Mid-ban dial was registered and the peer was unbanned.
2163-
Unbanned,
2164-
// There are dial attempts ongoing for the no longer banned peers.
2165-
Reconnecting,
2166-
}
2167-
2168-
let num_connections = 10;
2169-
2170-
for _ in 0..num_connections {
2171-
swarm1.dial(addr2.clone()).unwrap();
2172-
}
2173-
2174-
let mut s1_expected_conns = num_connections;
2175-
let mut s2_expected_conns = num_connections;
2176-
2177-
let mut stage = Stage::Connecting;
2178-
2179-
executor::block_on(future::poll_fn(move |cx| loop {
2180-
let poll1 = Swarm::poll_next_event(Pin::new(&mut swarm1), cx);
2181-
let poll2 = Swarm::poll_next_event(Pin::new(&mut swarm2), cx);
2182-
match stage {
2183-
Stage::Connecting => {
2184-
if swarm1.behaviour.assert_connected(s1_expected_conns, 1)
2185-
&& swarm2.behaviour.assert_connected(s2_expected_conns, 1)
2186-
{
2187-
// Setup to test that already established connections are correctly closed
2188-
// and reported as such after the peer is banned.
2189-
swarm2.ban_peer_id(swarm1_id);
2190-
stage = Stage::Banned;
2191-
}
2192-
}
2193-
Stage::Banned => {
2194-
if swarm1.behaviour.assert_disconnected(s1_expected_conns, 1)
2195-
&& swarm2.behaviour.assert_disconnected(s2_expected_conns, 1)
2196-
{
2197-
// Setup to test that new connections of banned peers are not reported.
2198-
swarm1.dial(addr2.clone()).unwrap();
2199-
s1_expected_conns += 1;
2200-
stage = Stage::BannedDial;
2201-
}
2202-
}
2203-
Stage::BannedDial => {
2204-
if swarm1.behaviour.assert_disconnected(s1_expected_conns, 2) {
2205-
// The banned connection was established. Given the ban, swarm2 closed the
2206-
// connection. Check that it was not reported to the behaviour of the
2207-
// banning swarm.
2208-
assert_eq!(
2209-
swarm2.behaviour.on_connection_established.len(),
2210-
s2_expected_conns,
2211-
"No additional closed connections should be reported for the banned peer"
2212-
);
2213-
2214-
// Setup to test that the banned connection is not reported upon closing
2215-
// even if the peer is unbanned.
2216-
swarm2.unban_peer_id(swarm1_id);
2217-
stage = Stage::Unbanned;
2218-
}
2219-
}
2220-
Stage::Unbanned => {
2221-
if swarm1.network_info().num_peers() == 0
2222-
&& swarm2.network_info().num_peers() == 0
2223-
{
2224-
// The banned connection has closed. Check that it was not reported.
2225-
assert_eq!(
2226-
swarm2.behaviour.on_connection_closed.len(), s2_expected_conns,
2227-
"No additional closed connections should be reported for the banned peer"
2228-
);
2229-
2230-
// Setup to test that a ban lifted does not affect future connections.
2231-
for _ in 0..num_connections {
2232-
swarm1.dial(addr2.clone()).unwrap();
2233-
}
2234-
s1_expected_conns += num_connections;
2235-
s2_expected_conns += num_connections;
2236-
stage = Stage::Reconnecting;
2237-
}
2238-
}
2239-
Stage::Reconnecting => {
2240-
if swarm1.behaviour.on_connection_established.len() == s1_expected_conns
2241-
&& swarm2.behaviour.assert_connected(s2_expected_conns, 2)
2242-
{
2243-
return Poll::Ready(());
2244-
}
2245-
}
2246-
}
2247-
2248-
if poll1.is_pending() && poll2.is_pending() {
2249-
return Poll::Pending;
2250-
}
2251-
}))
2252-
}
2253-
22542062
/// Establishes multiple connections between two peers,
22552063
/// after which one peer disconnects the other using [`Swarm::disconnect_peer_id`].
22562064
///

swarm/src/test.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,6 @@ where
235235
.count()
236236
}
237237

238-
/// Checks that when the expected number of closed connection notifications are received, a
239-
/// given number of expected disconnections have been received as well.
240-
///
241-
/// Returns if the first condition is met.
242-
pub(crate) fn assert_disconnected(
243-
&self,
244-
expected_closed_connections: usize,
245-
expected_disconnections: usize,
246-
) -> bool {
247-
if self.on_connection_closed.len() == expected_closed_connections {
248-
assert_eq!(
249-
self.on_connection_closed
250-
.iter()
251-
.filter(|(.., remaining_established)| { *remaining_established == 0 })
252-
.count(),
253-
expected_disconnections
254-
);
255-
return true;
256-
}
257-
258-
false
259-
}
260-
261238
/// Checks that when the expected number of established connection notifications are received,
262239
/// a given number of expected connections have been received as well.
263240
///

0 commit comments

Comments
 (0)