swarm/: Patch reporting on banned peer connections (#2350)

Don't report events of a connection to the `NetworkBehaviour`, if connection has
been established while the remote peer was banned. Among other guarantees this
upholds that `NetworkBehaviour::inject_event` is never called without a previous
`NetworkBehaviour::inject_connection_established` for said connection.

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Divma
2021-11-26 10:48:12 -05:00
committed by GitHub
parent f0000d56cd
commit fd417517ca
49 changed files with 437 additions and 193 deletions

View File

@ -42,10 +42,32 @@
# `libp2p` facade crate # `libp2p` facade crate
## Version 0.41.1 [unreleased] ## Version 0.42.0 [unreleased]
- Update individual crates. - Update individual crates.
- `libp2p-core`
- `libp2p-deflate`
- `libp2p-dns`
- `libp2p-floodsub`
- `libp2p-gossipsub`
- `libp2p-identify`
- `libp2p-kad`
- `libp2p-mdns`
- `libp2p-metrics`
- `libp2p-mplex`
- `libp2p-noise`
- `libp2p-ping`
- `libp2p-plaintext`
- `libp2p-relay`
- `libp2p-rendezvous`
- `libp2p-request-response`
- `libp2p-swarm-derive` - `libp2p-swarm-derive`
- `libp2p-swarm`
- `libp2p-tcp`
- `libp2p-uds`
- `libp2p-wasm-ext`
- `libp2p-websocket`
- `libp2p-yamux`
- `libp2p-mdns` (breaking compatibility with previous versions) - `libp2p-mdns` (breaking compatibility with previous versions)
## Version 0.41.0 [2021-11-16] ## Version 0.41.0 [2021-11-16]

View File

@ -2,7 +2,7 @@
name = "libp2p" name = "libp2p"
edition = "2018" edition = "2018"
description = "Peer-to-peer networking library" description = "Peer-to-peer networking library"
version = "0.41.1" version = "0.42.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -71,25 +71,25 @@ futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` featu
getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
lazy_static = "1.2" lazy_static = "1.2"
libp2p-core = { version = "0.30.0", path = "core", default-features = false } libp2p-core = { version = "0.31.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.32.0", path = "protocols/floodsub", optional = true } libp2p-floodsub = { version = "0.33.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.35.0", path = "./protocols/gossipsub", optional = true } libp2p-gossipsub = { version = "0.35.0", path = "./protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.32.0", path = "protocols/identify", optional = true } libp2p-identify = { version = "0.33.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.33.0", path = "protocols/kad", optional = true } libp2p-kad = { version = "0.34.0", path = "protocols/kad", optional = true }
libp2p-metrics = { version = "0.3.0", path = "misc/metrics", optional = true } libp2p-metrics = { version = "0.3.0", path = "misc/metrics", optional = true }
libp2p-mplex = { version = "0.30.1", path = "muxers/mplex", optional = true } libp2p-mplex = { version = "0.31.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true } libp2p-noise = { version = "0.34.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.32.0", path = "protocols/ping", optional = true } libp2p-ping = { version = "0.33.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true } libp2p-plaintext = { version = "0.31.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true } libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true }
libp2p-relay = { version = "0.5.0", path = "protocols/relay", optional = true } libp2p-relay = { version = "0.6.0", path = "protocols/relay", optional = true }
libp2p-rendezvous = { version = "0.2.0", path = "protocols/rendezvous", optional = true } libp2p-rendezvous = { version = "0.3.0", path = "protocols/rendezvous", optional = true }
libp2p-request-response = { version = "0.14.0", path = "protocols/request-response", optional = true } libp2p-request-response = { version = "0.15.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.32.0", path = "swarm" } libp2p-swarm = { version = "0.33.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.26.1", path = "swarm-derive" } libp2p-swarm-derive = { version = "0.26.1", path = "swarm-derive" }
libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true } libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.30.0", path = "transports/wasm-ext", default-features = false, optional = true } libp2p-wasm-ext = { version = "0.31.0", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.34.0", path = "muxers/yamux", optional = true } libp2p-yamux = { version = "0.35.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.13.0" } multiaddr = { version = "0.13.0" }
parking_lot = "0.11.0" parking_lot = "0.11.0"
pin-project = "1.0.0" pin-project = "1.0.0"
@ -97,11 +97,11 @@ rand = "0.7.3" # Explicit dependency to be used in `wasm-bindgen` feature
smallvec = "1.6.1" smallvec = "1.6.1"
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies] [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.30.0", path = "transports/deflate", optional = true } libp2p-deflate = { version = "0.31.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.30.0", path = "transports/dns", optional = true, default-features = false } libp2p-dns = { version = "0.31.0", path = "transports/dns", optional = true, default-features = false }
libp2p-mdns = { version = "0.34.0", path = "protocols/mdns", optional = true } libp2p-mdns = { version = "0.34.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = false, optional = true } libp2p-tcp = { version = "0.31.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.32.0", path = "transports/websocket", optional = true } libp2p-websocket = { version = "0.33.0", path = "transports/websocket", optional = true }
[dev-dependencies] [dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] } async-std = { version = "1.6.2", features = ["attributes"] }

View File

@ -1,3 +1,10 @@
# 0.31.0 [unreleased]
- Report concrete connection IDs in `NetworkEvent::ConnectionEstablished` and
`NetworkEvent::ConnectionClosed` (see [PR 2350]).
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350/
# 0.30.1 [2021-11-16] # 0.30.1 [2021-11-16]
- Use `instant` instead of `wasm-timer` (see [PR 2245]). - Use `instant` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-core" name = "libp2p-core"
edition = "2018" edition = "2018"
description = "Core traits and structs of libp2p" description = "Core traits and structs of libp2p"
version = "0.30.1" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"

View File

@ -45,7 +45,7 @@ use std::{
collections::{hash_map, HashMap}, collections::{hash_map, HashMap},
convert::TryFrom as _, convert::TryFrom as _,
fmt, fmt,
num::{NonZeroU32, NonZeroU8}, num::NonZeroU8,
pin::Pin, pin::Pin,
task::Context, task::Context,
task::Poll, task::Poll,
@ -156,7 +156,10 @@ where
/// A new connection has been established. /// A new connection has been established.
ConnectionEstablished { ConnectionEstablished {
connection: EstablishedConnection<'a, THandlerInEvent<THandler>>, connection: EstablishedConnection<'a, THandlerInEvent<THandler>>,
num_established: NonZeroU32, /// List of other connections to the same peer.
///
/// Note: Does not include the connection reported through this event.
other_established_connection_ids: Vec<ConnectionId>,
/// [`Some`] when the new connection is an outgoing connection. /// [`Some`] when the new connection is an outgoing connection.
/// Addresses are dialed in parallel. Contains the addresses and errors /// Addresses are dialed in parallel. Contains the addresses and errors
/// of dial attempts that failed before the one successful dial. /// of dial attempts that failed before the one successful dial.
@ -183,8 +186,8 @@ where
error: Option<ConnectionError<THandlerError<THandler>>>, error: Option<ConnectionError<THandlerError<THandler>>>,
/// A reference to the pool that used to manage the connection. /// A reference to the pool that used to manage the connection.
pool: &'a mut Pool<THandler, TTrans>, pool: &'a mut Pool<THandler, TTrans>,
/// The remaining number of established connections to the same peer. /// The remaining established connections to the same peer.
num_established: u32, remaining_established_connection_ids: Vec<ConnectionId>,
handler: THandler::Handler, handler: THandler::Handler,
}, },
@ -693,15 +696,16 @@ where
let EstablishedConnectionInfo { endpoint, .. } = let EstablishedConnectionInfo { endpoint, .. } =
connections.remove(&id).expect("Connection to be present"); connections.remove(&id).expect("Connection to be present");
self.counters.dec_established(&endpoint); self.counters.dec_established(&endpoint);
let num_established = u32::try_from(connections.len()).unwrap(); let remaining_established_connection_ids: Vec<ConnectionId> =
if num_established == 0 { connections.keys().cloned().collect();
if remaining_established_connection_ids.is_empty() {
self.established.remove(&peer_id); self.established.remove(&peer_id);
} }
return Poll::Ready(PoolEvent::ConnectionClosed { return Poll::Ready(PoolEvent::ConnectionClosed {
id, id,
connected: Connected { endpoint, peer_id }, connected: Connected { endpoint, peer_id },
error, error,
num_established, remaining_established_connection_ids,
pool: self, pool: self,
handler, handler,
}); });
@ -849,8 +853,7 @@ where
// Add the connection to the pool. // Add the connection to the pool.
let conns = self.established.entry(peer_id).or_default(); let conns = self.established.entry(peer_id).or_default();
let num_established = NonZeroU32::new(u32::try_from(conns.len() + 1).unwrap()) let other_established_connection_ids = conns.keys().cloned().collect();
.expect("n + 1 is always non-zero; qed");
self.counters.inc_established(&endpoint); self.counters.inc_established(&endpoint);
let (command_sender, command_receiver) = let (command_sender, command_receiver) =
@ -883,7 +886,7 @@ where
Some(PoolConnection::Established(connection)) => { Some(PoolConnection::Established(connection)) => {
return Poll::Ready(PoolEvent::ConnectionEstablished { return Poll::Ready(PoolEvent::ConnectionEstablished {
connection, connection,
num_established, other_established_connection_ids,
concurrent_dial_errors, concurrent_dial_errors,
}) })
} }

View File

@ -396,11 +396,11 @@ where
Poll::Pending => return Poll::Pending, Poll::Pending => return Poll::Pending,
Poll::Ready(PoolEvent::ConnectionEstablished { Poll::Ready(PoolEvent::ConnectionEstablished {
connection, connection,
num_established, other_established_connection_ids,
concurrent_dial_errors, concurrent_dial_errors,
}) => NetworkEvent::ConnectionEstablished { }) => NetworkEvent::ConnectionEstablished {
connection, connection,
num_established, other_established_connection_ids,
concurrent_dial_errors, concurrent_dial_errors,
}, },
Poll::Ready(PoolEvent::PendingOutboundConnectionError { Poll::Ready(PoolEvent::PendingOutboundConnectionError {
@ -435,13 +435,13 @@ where
id, id,
connected, connected,
error, error,
num_established, remaining_established_connection_ids,
handler, handler,
.. ..
}) => NetworkEvent::ConnectionClosed { }) => NetworkEvent::ConnectionClosed {
id, id,
connected, connected,
num_established, remaining_established_connection_ids,
error, error,
handler, handler,
}, },

View File

@ -29,7 +29,7 @@ use crate::{
transport::{Transport, TransportError}, transport::{Transport, TransportError},
Multiaddr, PeerId, Multiaddr, PeerId,
}; };
use std::{fmt, num::NonZeroU32}; use std::fmt;
/// Event that can happen on the `Network`. /// Event that can happen on the `Network`.
pub enum NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler> pub enum NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler>
@ -100,9 +100,10 @@ where
ConnectionEstablished { ConnectionEstablished {
/// The newly established connection. /// The newly established connection.
connection: EstablishedConnection<'a, TInEvent>, connection: EstablishedConnection<'a, TInEvent>,
/// The total number of established connections to the same peer, /// List of other connections to the same peer.
/// including the one that has just been opened. ///
num_established: NonZeroU32, /// Note: Does not include the connection reported through this event.
other_established_connection_ids: Vec<ConnectionId>,
/// [`Some`] when the new connection is an outgoing connection. /// [`Some`] when the new connection is an outgoing connection.
/// Addresses are dialed in parallel. Contains the addresses and errors /// Addresses are dialed in parallel. Contains the addresses and errors
/// of dial attempts that failed before the one successful dial. /// of dial attempts that failed before the one successful dial.
@ -128,8 +129,8 @@ where
connected: Connected, connected: Connected,
/// The error that occurred. /// The error that occurred.
error: Option<ConnectionError<<THandler::Handler as ConnectionHandler>::Error>>, error: Option<ConnectionError<<THandler::Handler as ConnectionHandler>::Error>>,
/// The remaining number of established connections to the same peer. /// List of remaining established connections to the same peer.
num_established: u32, remaining_established_connection_ids: Vec<ConnectionId>,
handler: THandler::Handler, handler: THandler::Handler,
}, },

View File

@ -16,12 +16,12 @@ kad = ["libp2p-kad"]
ping = ["libp2p-ping"] ping = ["libp2p-ping"]
[dependencies] [dependencies]
libp2p-core = { version = "0.30.0", path = "../../core" } libp2p-core = { version = "0.31.0", path = "../../core" }
libp2p-gossipsub = { version = "0.35.0", path = "../../protocols/gossipsub", optional = true } libp2p-gossipsub = { version = "0.35.0", path = "../../protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.32.0", path = "../../protocols/identify", optional = true } libp2p-identify = { version = "0.33.0", path = "../../protocols/identify", optional = true }
libp2p-kad = { version = "0.33.0", path = "../../protocols/kad", optional = true } libp2p-kad = { version = "0.34.0", path = "../../protocols/kad", optional = true }
libp2p-ping = { version = "0.32.0", path = "../../protocols/ping", optional = true } libp2p-ping = { version = "0.33.0", path = "../../protocols/ping", optional = true }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
open-metrics-client = "0.13.0" open-metrics-client = "0.13.0"
[dev-dependencies] [dev-dependencies]

View File

@ -11,5 +11,5 @@ categories = ["network-programming", "asynchronous"]
publish = false publish = false
[dependencies] [dependencies]
libp2p-core = { path = "../../core", default-features = false } libp2p-core = { path = "../../core", default-features = false, version = "0.31.0"}
num_cpus = "1.8" num_cpus = "1.8"

View File

@ -1,4 +1,6 @@
# 0.30.1 [unreleased] # 0.31.0 [unreleased]
- Update dependencies.
- Add `fn set_protocol_name(&mut self, protocol_name: &'static [u8])` to MplexConfig - Add `fn set_protocol_name(&mut self, protocol_name: &'static [u8])` to MplexConfig

View File

@ -2,7 +2,7 @@
name = "libp2p-mplex" name = "libp2p-mplex"
edition = "2018" edition = "2018"
description = "Mplex multiplexing protocol for libp2p" description = "Mplex multiplexing protocol for libp2p"
version = "0.30.1" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"]
bytes = "1" bytes = "1"
futures = "0.3.1" futures = "0.3.1"
asynchronous-codec = "0.6" asynchronous-codec = "0.6"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4" log = "0.4"
nohash-hasher = "0.2" nohash-hasher = "0.2"
parking_lot = "0.11" parking_lot = "0.11"

View File

@ -1,3 +1,7 @@
# 0.35.0 [unreleased]
- Update dependencies.
# 0.34.0 [2021-11-01] # 0.34.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -2,7 +2,7 @@
name = "libp2p-yamux" name = "libp2p-yamux"
edition = "2018" edition = "2018"
description = "Yamux multiplexing protocol for libp2p" description = "Yamux multiplexing protocol for libp2p"
version = "0.34.0" version = "0.35.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
futures = "0.3.1" futures = "0.3.1"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
parking_lot = "0.11" parking_lot = "0.11"
thiserror = "1.0" thiserror = "1.0"
yamux = "0.9.0" yamux = "0.9.0"

View File

@ -1,3 +1,7 @@
# 0.33.0 [unreleased]
- Update dependencies.
# 0.32.0 [2021-11-16] # 0.32.0 [2021-11-16]
- Update dependencies. - Update dependencies.

View File

@ -2,7 +2,7 @@
name = "libp2p-floodsub" name = "libp2p-floodsub"
edition = "2018" edition = "2018"
description = "Floodsub protocol for libp2p" description = "Floodsub protocol for libp2p"
version = "0.32.0" version = "0.33.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -13,8 +13,8 @@ categories = ["network-programming", "asynchronous"]
cuckoofilter = "0.5.0" cuckoofilter = "0.5.0"
fnv = "1.0" fnv = "1.0"
futures = "0.3.1" futures = "0.3.1"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4" log = "0.4"
prost = "0.9" prost = "0.9"
rand = "0.7" rand = "0.7"

View File

@ -10,8 +10,8 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"] categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
bytes = "1.0" bytes = "1.0"
byteorder = "1.3.4" byteorder = "1.3.4"
fnv = "1.0.7" fnv = "1.0.7"

View File

@ -1,3 +1,7 @@
# 0.33.0 [unreleased]
- Update dependencies.
# 0.32.0 [2021-11-16] # 0.32.0 [2021-11-16]
- Use `futures-timer` instead of `wasm-timer` (see [PR 2245]). - Use `futures-timer` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-identify" name = "libp2p-identify"
edition = "2018" edition = "2018"
description = "Nodes identifcation protocol for libp2p" description = "Nodes identifcation protocol for libp2p"
version = "0.32.0" version = "0.33.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -12,8 +12,8 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
futures = "0.3.1" futures = "0.3.1"
futures-timer = "3.0.2" futures-timer = "3.0.2"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4.1" log = "0.4.1"
lru = "0.6" lru = "0.6"
prost = "0.9" prost = "0.9"

View File

@ -1,3 +1,7 @@
# 0.34.0 [unreleased]
- Update dependencies.
# 0.33.0 [2021-11-16] # 0.33.0 [2021-11-16]
- Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]). - Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-kad" name = "libp2p-kad"
edition = "2018" edition = "2018"
description = "Kademlia protocol for libp2p" description = "Kademlia protocol for libp2p"
version = "0.33.0" version = "0.34.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -17,8 +17,8 @@ fnv = "1.0"
asynchronous-codec = "0.6" asynchronous-codec = "0.6"
futures = "0.3.1" futures = "0.3.1"
log = "0.4" log = "0.4"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
prost = "0.9" prost = "0.9"
rand = "0.7.2" rand = "0.7.2"
sha2 = "0.9.1" sha2 = "0.9.1"

View File

@ -1,5 +1,7 @@
# 0.34.0 [unreleased] # 0.34.0 [unreleased]
- Update dependencies.
- Use a random alphanumeric string instead of the local peer ID for mDNS peer - Use a random alphanumeric string instead of the local peer ID for mDNS peer
name (see [PR 2311]). name (see [PR 2311]).

View File

@ -16,8 +16,8 @@ dns-parser = "0.8.0"
futures = "0.3.13" futures = "0.3.13"
if-watch = "0.2.0" if-watch = "0.2.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4.14" log = "0.4.14"
rand = "0.8.3" rand = "0.8.3"
smallvec = "1.6.1" smallvec = "1.6.1"

View File

@ -1,3 +1,7 @@
# 0.33.0 [unreleased]
- Update dependencies.
# 0.32.0 [2021-11-16] # 0.32.0 [2021-11-16]
- Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]). - Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-ping" name = "libp2p-ping"
edition = "2018" edition = "2018"
description = "Ping protocol for libp2p" description = "Ping protocol for libp2p"
version = "0.32.0" version = "0.33.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -13,8 +13,8 @@ categories = ["network-programming", "asynchronous"]
futures = "0.3.1" futures = "0.3.1"
futures-timer = "3.0.2" futures-timer = "3.0.2"
instant = "0.1.11" instant = "0.1.11"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4.1" log = "0.4.1"
rand = "0.7.2" rand = "0.7.2"
void = "1.0" void = "1.0"

View File

@ -1,3 +1,7 @@
# 0.6.0 [unreleased]
- Update dependencies.
# 0.5.0 [2021-11-16] # 0.5.0 [2021-11-16]
- Use `instant` instead of `wasm-timer` (see [PR 2245]). - Use `instant` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-relay" name = "libp2p-relay"
edition = "2018" edition = "2018"
description = "Communications relaying for libp2p" description = "Communications relaying for libp2p"
version = "0.5.0" version = "0.6.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -15,8 +15,8 @@ bytes = "1"
futures = "0.3.1" futures = "0.3.1"
futures-timer = "3" futures-timer = "3"
instant = "0.1.11" instant = "0.1.11"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4" log = "0.4"
pin-project = "1" pin-project = "1"
prost = "0.9" prost = "0.9"

View File

@ -1,3 +1,7 @@
# 0.3.0 [unreleased]
- Update dependencies.
# 0.2.0 [2021-11-16] # 0.2.0 [2021-11-16]
- Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]). - Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-rendezvous" name = "libp2p-rendezvous"
edition = "2018" edition = "2018"
description = "Rendezvous protocol for libp2p" description = "Rendezvous protocol for libp2p"
version = "0.2.0" version = "0.3.0"
authors = ["The COMIT guys <hello@comit.network>"] authors = ["The COMIT guys <hello@comit.network>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -11,8 +11,8 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
asynchronous-codec = "0.6" asynchronous-codec = "0.6"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
prost = "0.9" prost = "0.9"
void = "1" void = "1"
log = "0.4" log = "0.4"

View File

@ -1,3 +1,7 @@
# 0.15.0 [unreleased]
- Update dependencies.
# 0.14.0 [2021-11-16] # 0.14.0 [2021-11-16]
- Use `instant` instead of `wasm-timer` (see [PR 2245]). - Use `instant` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-request-response" name = "libp2p-request-response"
edition = "2018" edition = "2018"
description = "Generic Request/Response Protocols" description = "Generic Request/Response Protocols"
version = "0.14.0" version = "0.15.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -14,8 +14,8 @@ async-trait = "0.1"
bytes = "1" bytes = "1"
futures = "0.3.1" futures = "0.3.1"
instant = "0.1.11" instant = "0.1.11"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.32.0", path = "../../swarm" } libp2p-swarm = { version = "0.33.0", path = "../../swarm" }
log = "0.4.11" log = "0.4.11"
lru = "0.7" lru = "0.7"
rand = "0.7" rand = "0.7"

View File

@ -1,3 +1,11 @@
# 0.33.0 [unreleased]
- Patch reporting on banned peers and their non-banned and banned connections (see [PR 2350]).
- Update dependencies.
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350
# 0.32.0 [2021-11-16] # 0.32.0 [2021-11-16]
- Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]). - Use `instant` and `futures-timer` instead of `wasm-timer` (see [PR 2245]).

View File

@ -2,7 +2,7 @@
name = "libp2p-swarm" name = "libp2p-swarm"
edition = "2018" edition = "2018"
description = "The libp2p swarm" description = "The libp2p swarm"
version = "0.32.0" version = "0.33.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -12,7 +12,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
either = "1.6.0" either = "1.6.0"
futures = "0.3.1" futures = "0.3.1"
libp2p-core = { version = "0.30.0", path = "../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../core", default-features = false }
log = "0.4" log = "0.4"
rand = "0.7" rand = "0.7"
smallvec = "1.6.1" smallvec = "1.6.1"

View File

@ -97,6 +97,7 @@ use smallvec::SmallVec;
use std::collections::HashSet; use std::collections::HashSet;
use std::num::{NonZeroU32, NonZeroU8, NonZeroUsize}; use std::num::{NonZeroU32, NonZeroU8, NonZeroUsize};
use std::{ use std::{
convert::TryFrom,
error, fmt, io, error, fmt, io,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
@ -275,6 +276,12 @@ where
/// List of nodes for which we deny any incoming connection. /// List of nodes for which we deny any incoming connection.
banned_peers: HashSet<PeerId>, banned_peers: HashSet<PeerId>,
/// Connections for which we withhold any reporting. These belong to banned peers.
///
/// Note: Connections to a peer that are established at the time of banning that peer
/// are not added here. Instead they are simply closed.
banned_peer_connections: HashSet<ConnectionId>,
/// Pending event to be delivered to connection handlers /// Pending event to be delivered to connection handlers
/// (or dropped if the peer disconnected) before the `behaviour` /// (or dropped if the peer disconnected) before the `behaviour`
/// can be polled again. /// can be polled again.
@ -540,6 +547,10 @@ where
pub fn ban_peer_id(&mut self, peer_id: PeerId) { pub fn ban_peer_id(&mut self, peer_id: PeerId) {
if self.banned_peers.insert(peer_id) { if self.banned_peers.insert(peer_id) {
if let Some(peer) = self.network.peer(peer_id).into_connected() { if let Some(peer) = self.network.peer(peer_id).into_connected() {
// Note that established connections to the now banned peer are closed but not
// added to [`Swarm::banned_peer_connections`]. They have been previously reported
// as open to the behaviour and need be reported as closed once closing the
// connection finishes.
peer.disconnect(); peer.disconnect();
} }
} }
@ -603,8 +614,12 @@ where
Poll::Pending => network_not_ready = true, Poll::Pending => network_not_ready = true,
Poll::Ready(NetworkEvent::ConnectionEvent { connection, event }) => { Poll::Ready(NetworkEvent::ConnectionEvent { connection, event }) => {
let peer = connection.peer_id(); let peer = connection.peer_id();
let connection = connection.id(); let conn_id = connection.id();
this.behaviour.inject_event(peer, connection, event); if this.banned_peer_connections.contains(&conn_id) {
log::debug!("Ignoring event from banned peer: {} {:?}.", peer, conn_id);
} else {
this.behaviour.inject_event(peer, conn_id, event);
}
} }
Poll::Ready(NetworkEvent::AddressChange { Poll::Ready(NetworkEvent::AddressChange {
connection, connection,
@ -612,22 +627,27 @@ where
old_endpoint, old_endpoint,
}) => { }) => {
let peer = connection.peer_id(); let peer = connection.peer_id();
let connection = connection.id(); let conn_id = connection.id();
this.behaviour.inject_address_change( if !this.banned_peer_connections.contains(&conn_id) {
&peer, this.behaviour.inject_address_change(
&connection, &peer,
&old_endpoint, &conn_id,
&new_endpoint, &old_endpoint,
); &new_endpoint,
);
}
} }
Poll::Ready(NetworkEvent::ConnectionEstablished { Poll::Ready(NetworkEvent::ConnectionEstablished {
connection, connection,
num_established, other_established_connection_ids,
concurrent_dial_errors, concurrent_dial_errors,
}) => { }) => {
let peer_id = connection.peer_id(); let peer_id = connection.peer_id();
let endpoint = connection.endpoint().clone(); let endpoint = connection.endpoint().clone();
if this.banned_peers.contains(&peer_id) { if this.banned_peers.contains(&peer_id) {
// Mark the connection for the banned peer as banned, thus withholding any
// future events from the connection to the behaviour.
this.banned_peer_connections.insert(connection.id());
this.network this.network
.peer(peer_id) .peer(peer_id)
.into_connected() .into_connected()
@ -635,6 +655,11 @@ where
.disconnect(); .disconnect();
return Poll::Ready(SwarmEvent::BannedPeer { peer_id, endpoint }); return Poll::Ready(SwarmEvent::BannedPeer { peer_id, endpoint });
} else { } else {
let num_established = NonZeroU32::new(
u32::try_from(other_established_connection_ids.len() + 1).unwrap(),
)
.expect("n + 1 is always non-zero; qed");
log::debug!( log::debug!(
"Connection established: {:?} {:?}; Total (peer): {}.", "Connection established: {:?} {:?}; Total (peer): {}.",
connection.peer_id(), connection.peer_id(),
@ -651,7 +676,13 @@ where
&endpoint, &endpoint,
failed_addresses.as_ref(), failed_addresses.as_ref(),
); );
if num_established.get() == 1 { // The peer is not banned, but there could be previous banned connections
// if the peer was just unbanned. Check if this is the first non-banned
// connection.
let first_non_banned = other_established_connection_ids
.into_iter()
.all(|conn_id| this.banned_peer_connections.contains(&conn_id));
if first_non_banned {
this.behaviour.inject_connected(&peer_id); this.behaviour.inject_connected(&peer_id);
} }
return Poll::Ready(SwarmEvent::ConnectionEstablished { return Poll::Ready(SwarmEvent::ConnectionEstablished {
@ -666,7 +697,7 @@ where
id, id,
connected, connected,
error, error,
num_established, remaining_established_connection_ids,
handler, handler,
}) => { }) => {
if let Some(error) = error.as_ref() { if let Some(error) = error.as_ref() {
@ -676,14 +707,26 @@ where
} }
let peer_id = connected.peer_id; let peer_id = connected.peer_id;
let endpoint = connected.endpoint; let endpoint = connected.endpoint;
this.behaviour.inject_connection_closed( let num_established =
&peer_id, u32::try_from(remaining_established_connection_ids.len()).unwrap();
&id, let conn_was_reported = !this.banned_peer_connections.remove(&id);
&endpoint, if conn_was_reported {
handler.into_protocols_handler(), this.behaviour.inject_connection_closed(
); &peer_id,
if num_established == 0 { &id,
this.behaviour.inject_disconnected(&peer_id); &endpoint,
handler.into_protocols_handler(),
);
// This connection was reported as open to the behaviour. Check if this is
// the last non-banned connection for the peer.
let last_non_banned = remaining_established_connection_ids
.into_iter()
.all(|conn_id| this.banned_peer_connections.contains(&conn_id));
if last_non_banned {
this.behaviour.inject_disconnected(&peer_id)
}
} }
return Poll::Ready(SwarmEvent::ConnectionClosed { return Poll::Ready(SwarmEvent::ConnectionClosed {
peer_id, peer_id,
@ -1253,6 +1296,7 @@ where
listened_addrs: SmallVec::new(), listened_addrs: SmallVec::new(),
external_addrs: Addresses::default(), external_addrs: Addresses::default(),
banned_peers: HashSet::new(), banned_peers: HashSet::new(),
banned_peer_connections: HashSet::new(),
pending_event: None, pending_event: None,
substream_upgrade_protocol_override: self.substream_upgrade_protocol_override, substream_upgrade_protocol_override: self.substream_upgrade_protocol_override,
} }
@ -1450,15 +1494,6 @@ mod tests {
TBehaviour: NetworkBehaviour, TBehaviour: NetworkBehaviour,
<<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone, <<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone,
{ {
for s in &[swarm1, swarm2] {
if s.behaviour.inject_connection_established.len() > 0 {
assert_eq!(s.behaviour.inject_connected.len(), 1);
} else {
assert_eq!(s.behaviour.inject_connected.len(), 0);
}
assert!(s.behaviour.inject_connection_closed.is_empty());
assert!(s.behaviour.inject_disconnected.is_empty());
}
[swarm1, swarm2] [swarm1, swarm2]
.iter() .iter()
.all(|s| s.behaviour.inject_connection_established.len() == num_connections) .all(|s| s.behaviour.inject_connection_established.len() == num_connections)
@ -1473,10 +1508,6 @@ mod tests {
TBehaviour: NetworkBehaviour, TBehaviour: NetworkBehaviour,
<<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone <<TBehaviour::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Clone
{ {
for s in &[swarm1, swarm2] {
assert_eq!(s.behaviour.inject_connection_established.len(), 0);
assert_eq!(s.behaviour.inject_connected.len(), 0);
}
[swarm1, swarm2] [swarm1, swarm2]
.iter() .iter()
.all(|s| s.behaviour.inject_connection_closed.len() == num_connections) .all(|s| s.behaviour.inject_connection_closed.len() == num_connections)
@ -1490,7 +1521,12 @@ mod tests {
/// ///
/// The test expects both behaviours to be notified via pairs of /// The test expects both behaviours to be notified via pairs of
/// inject_connected / inject_disconnected as well as /// inject_connected / inject_disconnected as well as
/// inject_connection_established / inject_connection_closed calls. /// inject_connection_established / inject_connection_closed calls
/// while unbanned.
///
/// While the ban is in effect, further dials occur. For these connections no
/// `inject_connected`, `inject_connection_established`, `inject_disconnected`,
/// `inject_connection_closed` calls should be registered.
#[test] #[test]
fn test_connect_disconnect_ban() { fn test_connect_disconnect_ban() {
// Since the test does not try to open any substreams, we can // Since the test does not try to open any substreams, we can
@ -1505,59 +1541,103 @@ mod tests {
let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
swarm1.listen_on(addr1.clone().into()).unwrap(); swarm1.listen_on(addr1.clone()).unwrap();
swarm2.listen_on(addr2.clone().into()).unwrap(); swarm2.listen_on(addr2.clone()).unwrap();
let swarm1_id = *swarm1.local_peer_id(); let swarm1_id = *swarm1.local_peer_id();
let mut banned = false; enum Stage {
let mut unbanned = false; /// Waiting for the peers to connect. Banning has not occurred.
Connecting,
/// Ban occurred.
Banned,
// Ban is in place and a dial is ongoing.
BannedDial,
// Mid-ban dial was registered and the peer was unbanned.
Unbanned,
// There are dial attempts ongoing for the no longer banned peers.
Reconnecting,
}
let num_connections = 10; let num_connections = 10;
for _ in 0..num_connections { for _ in 0..num_connections {
swarm1.dial(addr2.clone()).unwrap(); swarm1.dial(addr2.clone()).unwrap();
} }
let mut state = State::Connecting;
executor::block_on(future::poll_fn(move |cx| { let mut s1_expected_conns = num_connections;
loop { let mut s2_expected_conns = num_connections;
let poll1 = Swarm::poll_next_event(Pin::new(&mut swarm1), cx);
let poll2 = Swarm::poll_next_event(Pin::new(&mut swarm2), cx); let mut stage = Stage::Connecting;
match state {
State::Connecting => { executor::block_on(future::poll_fn(move |cx| loop {
if swarms_connected(&swarm1, &swarm2, num_connections) { let poll1 = Swarm::poll_next_event(Pin::new(&mut swarm1), cx);
if banned { let poll2 = Swarm::poll_next_event(Pin::new(&mut swarm2), cx);
return Poll::Ready(()); match stage {
} Stage::Connecting => {
swarm2.ban_peer_id(swarm1_id.clone()); if swarm1.behaviour.assert_connected(s1_expected_conns, 1)
swarm1.behaviour.reset(); && swarm2.behaviour.assert_connected(s2_expected_conns, 1)
swarm2.behaviour.reset(); {
banned = true; // Setup to test that already established connections are correctly closed
state = State::Disconnecting; // and reported as such after the peer is banned.
} swarm2.ban_peer_id(swarm1_id);
} stage = Stage::Banned;
State::Disconnecting => {
if swarms_disconnected(&swarm1, &swarm2, num_connections) {
if unbanned {
return Poll::Ready(());
}
// Unban the first peer and reconnect.
swarm2.unban_peer_id(swarm1_id.clone());
swarm1.behaviour.reset();
swarm2.behaviour.reset();
unbanned = true;
for _ in 0..num_connections {
swarm2.dial(addr1.clone()).unwrap();
}
state = State::Connecting;
}
} }
} }
Stage::Banned => {
if poll1.is_pending() && poll2.is_pending() { if swarm1.behaviour.assert_disconnected(s1_expected_conns, 1)
return Poll::Pending; && swarm2.behaviour.assert_disconnected(s2_expected_conns, 1)
{
// Setup to test that new connections of banned peers are not reported.
swarm1.dial(addr2.clone()).unwrap();
s1_expected_conns += 1;
stage = Stage::BannedDial;
}
} }
Stage::BannedDial => {
if swarm2.network_info().num_peers() == 1 {
// The banned connection was established. Check that it was not reported to
// the behaviour of the banning swarm.
assert_eq!(
swarm2.behaviour.inject_connection_established.len(), s2_expected_conns,
"No additional closed connections should be reported for the banned peer"
);
// Setup to test that the banned connection is not reported upon closing
// even if the peer is unbanned.
swarm2.unban_peer_id(swarm1_id);
stage = Stage::Unbanned;
}
}
Stage::Unbanned => {
if swarm2.network_info().num_peers() == 0 {
// The banned connection has closed. Check that it was not reported.
assert_eq!(
swarm2.behaviour.inject_connection_closed.len(), s2_expected_conns,
"No additional closed connections should be reported for the banned peer"
);
assert!(swarm2.banned_peer_connections.is_empty());
// Setup to test that a ban lifted does not affect future connections.
for _ in 0..num_connections {
swarm1.dial(addr2.clone()).unwrap();
}
s1_expected_conns += num_connections;
s2_expected_conns += num_connections;
stage = Stage::Reconnecting;
}
}
Stage::Reconnecting => {
if swarm1.behaviour.inject_connection_established.len() == s1_expected_conns
&& swarm2.behaviour.assert_connected(s2_expected_conns, 2)
{
return Poll::Ready(());
}
}
}
if poll1.is_pending() && poll2.is_pending() {
return Poll::Pending;
} }
})) }))
} }
@ -1582,8 +1662,8 @@ mod tests {
let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
swarm1.listen_on(addr1.clone().into()).unwrap(); swarm1.listen_on(addr1.clone()).unwrap();
swarm2.listen_on(addr2.clone().into()).unwrap(); swarm2.listen_on(addr2.clone()).unwrap();
let swarm1_id = *swarm1.local_peer_id(); let swarm1_id = *swarm1.local_peer_id();
@ -1605,10 +1685,8 @@ mod tests {
return Poll::Ready(()); return Poll::Ready(());
} }
swarm2 swarm2
.disconnect_peer_id(swarm1_id.clone()) .disconnect_peer_id(swarm1_id)
.expect("Error disconnecting"); .expect("Error disconnecting");
swarm1.behaviour.reset();
swarm2.behaviour.reset();
state = State::Disconnecting; state = State::Disconnecting;
} }
} }
@ -1618,8 +1696,6 @@ mod tests {
return Poll::Ready(()); return Poll::Ready(());
} }
reconnected = true; reconnected = true;
swarm1.behaviour.reset();
swarm2.behaviour.reset();
for _ in 0..num_connections { for _ in 0..num_connections {
swarm2.dial(addr1.clone()).unwrap(); swarm2.dial(addr1.clone()).unwrap();
} }
@ -1655,8 +1731,8 @@ mod tests {
let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
swarm1.listen_on(addr1.clone().into()).unwrap(); swarm1.listen_on(addr1.clone()).unwrap();
swarm2.listen_on(addr2.clone().into()).unwrap(); swarm2.listen_on(addr2.clone()).unwrap();
let swarm1_id = *swarm1.local_peer_id(); let swarm1_id = *swarm1.local_peer_id();
@ -1679,12 +1755,10 @@ mod tests {
} }
swarm2.behaviour.inner().next_action.replace( swarm2.behaviour.inner().next_action.replace(
NetworkBehaviourAction::CloseConnection { NetworkBehaviourAction::CloseConnection {
peer_id: swarm1_id.clone(), peer_id: swarm1_id,
connection: CloseConnection::All, connection: CloseConnection::All,
}, },
); );
swarm1.behaviour.reset();
swarm2.behaviour.reset();
state = State::Disconnecting; state = State::Disconnecting;
} }
} }
@ -1694,8 +1768,6 @@ mod tests {
return Poll::Ready(()); return Poll::Ready(());
} }
reconnected = true; reconnected = true;
swarm1.behaviour.reset();
swarm2.behaviour.reset();
for _ in 0..num_connections { for _ in 0..num_connections {
swarm2.dial(addr1.clone()).unwrap(); swarm2.dial(addr1.clone()).unwrap();
} }
@ -1731,8 +1803,8 @@ mod tests {
let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into(); let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
swarm1.listen_on(addr1.clone().into()).unwrap(); swarm1.listen_on(addr1.clone()).unwrap();
swarm2.listen_on(addr2.clone().into()).unwrap(); swarm2.listen_on(addr2.clone()).unwrap();
let swarm1_id = *swarm1.local_peer_id(); let swarm1_id = *swarm1.local_peer_id();
@ -1756,22 +1828,23 @@ mod tests {
.1; .1;
swarm2.behaviour.inner().next_action.replace( swarm2.behaviour.inner().next_action.replace(
NetworkBehaviourAction::CloseConnection { NetworkBehaviourAction::CloseConnection {
peer_id: swarm1_id.clone(), peer_id: swarm1_id,
connection: CloseConnection::One(conn_id), connection: CloseConnection::One(conn_id),
}, },
); );
Some(conn_id) Some(conn_id)
}; };
swarm1.behaviour.reset();
swarm2.behaviour.reset();
state = State::Disconnecting; state = State::Disconnecting;
} }
} }
State::Disconnecting => { State::Disconnecting => {
for s in &[&swarm1, &swarm2] { for s in &[&swarm1, &swarm2] {
assert_eq!(s.behaviour.inject_disconnected.len(), 0); assert_eq!(s.behaviour.inject_disconnected.len(), 0);
assert_eq!(s.behaviour.inject_connection_established.len(), 0); assert_eq!(
assert_eq!(s.behaviour.inject_connected.len(), 0); s.behaviour.inject_connection_established.len(),
num_connections
);
assert_eq!(s.behaviour.inject_connected.len(), 1);
} }
if [&swarm1, &swarm2] if [&swarm1, &swarm2]
.iter() .iter()

View File

@ -91,7 +91,7 @@ where
/// A `CallTraceBehaviour` is a `NetworkBehaviour` that tracks /// A `CallTraceBehaviour` is a `NetworkBehaviour` that tracks
/// invocations of callback methods and their arguments, wrapping /// invocations of callback methods and their arguments, wrapping
/// around an inner behaviour. /// around an inner behaviour. It ensures certain invariants are met.
pub struct CallTraceBehaviour<TInner> pub struct CallTraceBehaviour<TInner>
where where
TInner: NetworkBehaviour, TInner: NetworkBehaviour,
@ -144,6 +144,7 @@ where
} }
} }
#[allow(dead_code)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.addresses_of_peer = Vec::new(); self.addresses_of_peer = Vec::new();
self.inject_connected = Vec::new(); self.inject_connected = Vec::new();
@ -163,6 +164,40 @@ where
pub fn inner(&mut self) -> &mut TInner { pub fn inner(&mut self) -> &mut TInner {
&mut self.inner &mut self.inner
} }
/// Checks that when the expected number of closed connection notifications are received, a
/// given number of expected disconnections have been received as well.
///
/// Returns if the first condition is met.
pub fn assert_disconnected(
&self,
expected_closed_connections: usize,
expected_disconnections: usize,
) -> bool {
if self.inject_connection_closed.len() == expected_closed_connections {
assert_eq!(self.inject_disconnected.len(), expected_disconnections);
return true;
}
false
}
/// Checks that when the expected number of established connection notifications are received,
/// a given number of expected connections have been received as well.
///
/// Returns if the first condition is met.
pub fn assert_connected(
&self,
expected_established_connections: usize,
expected_connections: usize,
) -> bool {
if self.inject_connection_established.len() == expected_established_connections {
assert_eq!(self.inject_connected.len(), expected_connections);
return true;
}
false
}
} }
impl<TInner> NetworkBehaviour for CallTraceBehaviour<TInner> impl<TInner> NetworkBehaviour for CallTraceBehaviour<TInner>
@ -184,6 +219,12 @@ where
} }
fn inject_connected(&mut self, peer: &PeerId) { fn inject_connected(&mut self, peer: &PeerId) {
assert!(
self.inject_connection_established
.iter()
.any(|(peer_id, _, _)| peer_id == peer),
"`inject_connected` is called after at least one `inject_connection_established`."
);
self.inject_connected.push(peer.clone()); self.inject_connected.push(peer.clone());
self.inner.inject_connected(peer); self.inner.inject_connected(peer);
} }
@ -201,7 +242,13 @@ where
} }
fn inject_disconnected(&mut self, peer: &PeerId) { fn inject_disconnected(&mut self, peer: &PeerId) {
self.inject_disconnected.push(peer.clone()); assert!(
self.inject_connection_closed
.iter()
.any(|(peer_id, _, _)| peer_id == peer),
"`inject_disconnected` is called after at least one `inject_connection_closed`."
);
self.inject_disconnected.push(*peer);
self.inner.inject_disconnected(peer); self.inner.inject_disconnected(peer);
} }
@ -212,8 +259,13 @@ where
e: &ConnectedPoint, e: &ConnectedPoint,
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler, handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
) { ) {
self.inject_connection_closed let connection = (p.clone(), c.clone(), e.clone());
.push((p.clone(), c.clone(), e.clone())); assert!(
self.inject_connection_established.contains(&connection),
"`inject_connection_closed` is called only for connections for \
which `inject_connection_established` was called first."
);
self.inject_connection_closed.push(connection);
self.inner.inject_connection_closed(p, c, e, handler); self.inner.inject_connection_closed(p, c, e, handler);
} }
@ -223,6 +275,20 @@ where
c: ConnectionId, c: ConnectionId,
e: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent, e: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
) { ) {
assert!(
self.inject_connection_established
.iter()
.any(|(peer_id, conn_id, _)| *peer_id == p && c == *conn_id),
"`inject_event` is called for reported connections."
);
assert!(
!self
.inject_connection_closed
.iter()
.any(|(peer_id, conn_id, _)| *peer_id == p && c == *conn_id),
"`inject_event` is never called for closed connections."
);
self.inject_event.push((p.clone(), c.clone(), e.clone())); self.inject_event.push((p.clone(), c.clone(), e.clone()));
self.inner.inject_event(p, c, e); self.inner.inject_event(p, c, e);
} }

View File

@ -1,3 +1,7 @@
# 0.31.0 [unreleased]
- Update dependencies.
# 0.30.0 [2021-11-01] # 0.30.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -2,7 +2,7 @@
name = "libp2p-deflate" name = "libp2p-deflate"
edition = "2018" edition = "2018"
description = "Deflate encryption protocol for libp2p" description = "Deflate encryption protocol for libp2p"
version = "0.30.0" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
futures = "0.3.1" futures = "0.3.1"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
flate2 = "1.0" flate2 = "1.0"
[dev-dependencies] [dev-dependencies]

View File

@ -1,3 +1,7 @@
# 0.31.0 [unreleased]
- Update dependencies.
# 0.30.0 [2021-11-01] # 0.30.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -2,7 +2,7 @@
name = "libp2p-dns" name = "libp2p-dns"
edition = "2018" edition = "2018"
description = "DNS transport implementation for libp2p" description = "DNS transport implementation for libp2p"
version = "0.30.0" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -10,7 +10,7 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"] categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4.1" log = "0.4.1"
futures = "0.3.1" futures = "0.3.1"
trust-dns-resolver = { version = "0.20", default-features = false, features = ["system-config"] } trust-dns-resolver = { version = "0.20", default-features = false, features = ["system-config"] }

View File

@ -1,3 +1,7 @@
# 0.34.0 [unreleased]
- Update dependencies.
# 0.33.0 [2021-11-01] # 0.33.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -1,7 +1,7 @@
[package] [package]
name = "libp2p-noise" name = "libp2p-noise"
description = "Cryptographic handshake protocol using the noise framework." description = "Cryptographic handshake protocol using the noise framework."
version = "0.33.0" version = "0.34.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -12,7 +12,7 @@ bytes = "1"
curve25519-dalek = "3.0.0" curve25519-dalek = "3.0.0"
futures = "0.3.1" futures = "0.3.1"
lazy_static = "1.2" lazy_static = "1.2"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4" log = "0.4"
prost = "0.9" prost = "0.9"
rand = "0.8.3" rand = "0.8.3"

View File

@ -1,3 +1,7 @@
# 0.31.0 [unreleased]
- Update dependencies.
# 0.30.0 [2021-11-01] # 0.30.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -2,7 +2,7 @@
name = "libp2p-plaintext" name = "libp2p-plaintext"
edition = "2018" edition = "2018"
description = "Plaintext encryption dummy protocol for libp2p" description = "Plaintext encryption dummy protocol for libp2p"
version = "0.30.0" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"]
bytes = "1" bytes = "1"
futures = "0.3.1" futures = "0.3.1"
asynchronous-codec = "0.6" asynchronous-codec = "0.6"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4.8" log = "0.4.8"
prost = "0.9" prost = "0.9"
unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] } unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }

View File

@ -1,3 +1,7 @@
# 0.31.0 [unreleased]
- Update dependencies.
# 0.30.0 [2021-11-01] # 0.30.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -2,7 +2,7 @@
name = "libp2p-tcp" name = "libp2p-tcp"
edition = "2018" edition = "2018"
description = "TCP/IP transport protocol for libp2p" description = "TCP/IP transport protocol for libp2p"
version = "0.30.0" version = "0.31.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -17,7 +17,7 @@ if-watch = { version = "0.2.0", optional = true }
if-addrs = { version = "0.6.4", optional = true } if-addrs = { version = "0.6.4", optional = true }
ipnet = "2.0.0" ipnet = "2.0.0"
libc = "0.2.80" libc = "0.2.80"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4.11" log = "0.4.11"
socket2 = { version = "0.4.0", features = ["all"] } socket2 = { version = "0.4.0", features = ["all"] }
tokio-crate = { package = "tokio", version = "1.0.1", default-features = false, features = ["net"], optional = true } tokio-crate = { package = "tokio", version = "1.0.1", default-features = false, features = ["net"], optional = true }

View File

@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
[target.'cfg(all(unix, not(target_os = "emscripten")))'.dependencies] [target.'cfg(all(unix, not(target_os = "emscripten")))'.dependencies]
async-std = { version = "1.6.2", optional = true } async-std = { version = "1.6.2", optional = true }
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4.1" log = "0.4.1"
futures = "0.3.1" futures = "0.3.1"
tokio = { version = "1.0.1", default-features = false, features = ["net"], optional = true } tokio = { version = "1.0.1", default-features = false, features = ["net"], optional = true }

View File

@ -1,3 +1,7 @@
# 0.31.0 [unreleased]
- Update dependencies.
# 0.30.0 [2021-11-01] # 0.30.0 [2021-11-01]
- Make default features of `libp2p-core` optional. - Make default features of `libp2p-core` optional.

View File

@ -1,6 +1,6 @@
[package] [package]
name = "libp2p-wasm-ext" name = "libp2p-wasm-ext"
version = "0.30.0" version = "0.31.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
edition = "2018" edition = "2018"
description = "Allows passing in an external transport in a WASM environment" description = "Allows passing in an external transport in a WASM environment"
@ -12,7 +12,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
futures = "0.3.1" futures = "0.3.1"
js-sys = "0.3.50" js-sys = "0.3.50"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
parity-send-wrapper = "0.1.0" parity-send-wrapper = "0.1.0"
wasm-bindgen = "0.2.42" wasm-bindgen = "0.2.42"
wasm-bindgen-futures = "0.4.4" wasm-bindgen-futures = "0.4.4"

View File

@ -1,3 +1,7 @@
# 0.33.0 [unreleased]
- Update dependencies.
# v0.32.0 [2021-11-16] # v0.32.0 [2021-11-16]
- Handle websocket CLOSE with reason code (see [PR 2085]). - Handle websocket CLOSE with reason code (see [PR 2085]).

View File

@ -2,7 +2,7 @@
name = "libp2p-websocket" name = "libp2p-websocket"
edition = "2018" edition = "2018"
description = "WebSocket transport for libp2p" description = "WebSocket transport for libp2p"
version = "0.32.0" version = "0.33.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT" license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p" repository = "https://github.com/libp2p/rust-libp2p"
@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"]
futures-rustls = "0.22" futures-rustls = "0.22"
either = "1.5.3" either = "1.5.3"
futures = "0.3.1" futures = "0.3.1"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false } libp2p-core = { version = "0.31.0", path = "../../core", default-features = false }
log = "0.4.8" log = "0.4.8"
quicksink = "0.1" quicksink = "0.1"
rw-stream-sink = "0.2.0" rw-stream-sink = "0.2.0"