From b85d70401455fdf4b00866f95e6fb5703486fd2a Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 11:54:27 +0100 Subject: [PATCH 1/4] Introduce `ConnectionId::new_unchecked` constructor --- protocols/gossipsub/src/behaviour/tests.rs | 39 +++++++++------------- protocols/kad/src/behaviour/test.rs | 3 +- swarm/CHANGELOG.md | 5 +++ swarm/Cargo.toml | 2 +- swarm/src/connection.rs | 10 ++++++ 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index 5413f6dbb89..ab1deb32f77 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -222,17 +222,19 @@ where } }; - #[allow(deprecated)] gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id: peer, - connection_id: ConnectionId::DUMMY, + connection_id: ConnectionId::new_unchecked(0), endpoint: &endpoint, failed_addresses: &[], other_established: 0, // first connection })); if let Some(kind) = kind { - #[allow(deprecated)] - gs.on_connection_handler_event(peer, ConnectionId::DUMMY, HandlerEvent::PeerKind(kind)); + gs.on_connection_handler_event( + peer, + ConnectionId::new_unchecked(0), + HandlerEvent::PeerKind(kind), + ); } if explicit { gs.add_explicit_peer(&peer); @@ -582,10 +584,9 @@ fn test_join() { for _ in 0..3 { let random_peer = PeerId::random(); // inform the behaviour of a new peer - #[allow(deprecated)] gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id: random_peer, - connection_id: ConnectionId::DUMMY, + connection_id: ConnectionId::new_unchecked(0), endpoint: &ConnectedPoint::Dialer { address: "/ip4/127.0.0.1".parse::().unwrap(), role_override: Endpoint::Dialer, @@ -965,10 +966,7 @@ fn test_get_random_peers() { *p, PeerConnections { kind: PeerKind::Gossipsubv1_1, - connections: vec![ - #[allow(deprecated)] - ConnectionId::DUMMY, - ], + connections: vec![ConnectionId::new_unchecked(0)], }, ) }) @@ -2998,8 +2996,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() { //receive from p1 gs.on_connection_handler_event( p1, - #[allow(deprecated)] - ConnectionId::DUMMY, + ConnectionId::new_unchecked(0), HandlerEvent::Message { rpc: Rpc { messages: vec![raw_message1], @@ -3025,8 +3022,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() { //receive from p2 gs.on_connection_handler_event( p2, - #[allow(deprecated)] - ConnectionId::DUMMY, + ConnectionId::new_unchecked(0), HandlerEvent::Message { rpc: Rpc { messages: vec![raw_message3], @@ -3632,8 +3628,7 @@ fn test_scoring_p4_invalid_signature() { gs.on_connection_handler_event( peers[0], - #[allow(deprecated)] - ConnectionId::DUMMY, + ConnectionId::new_unchecked(0), HandlerEvent::Message { rpc: Rpc { messages: vec![], @@ -4114,11 +4109,10 @@ fn test_scoring_p6() { } //add additional connection for 3 others with addr - #[allow(deprecated)] for id in others.iter().take(3) { gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id: *id, - connection_id: ConnectionId::DUMMY, + connection_id: ConnectionId::new_unchecked(0), endpoint: &ConnectedPoint::Dialer { address: addr.clone(), role_override: Endpoint::Dialer, @@ -4137,10 +4131,9 @@ fn test_scoring_p6() { //add additional connection for 3 of the peers to addr2 for peer in peers.iter().take(3) { - #[allow(deprecated)] gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id: *peer, - connection_id: ConnectionId::DUMMY, + connection_id: ConnectionId::new_unchecked(0), endpoint: &ConnectedPoint::Dialer { address: addr2.clone(), role_override: Endpoint::Dialer, @@ -4168,10 +4161,9 @@ fn test_scoring_p6() { ); //two times same ip doesn't count twice - #[allow(deprecated)] gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id: peers[0], - connection_id: ConnectionId::DUMMY, + connection_id: ConnectionId::new_unchecked(0), endpoint: &ConnectedPoint::Dialer { address: addr, role_override: Endpoint::Dialer, @@ -5179,8 +5171,7 @@ fn test_subscribe_and_graft_with_negative_score() { let (mut gs2, _, _) = inject_nodes1().create_network(); - #[allow(deprecated)] - let connection_id = ConnectionId::DUMMY; + let connection_id = ConnectionId::new_unchecked(0); let topic = Topic::new("test"); diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 33cd9dbe7d6..f848812df10 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -1295,8 +1295,7 @@ fn network_behaviour_on_address_change() { let local_peer_id = PeerId::random(); let remote_peer_id = PeerId::random(); - #[allow(deprecated)] - let connection_id = ConnectionId::DUMMY; + let connection_id = ConnectionId::new_unchecked(0); let old_address: Multiaddr = Protocol::Memory(1).into(); let new_address: Multiaddr = Protocol::Memory(2).into(); diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index df04507376e..c7e5047644b 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.42.1 - unreleased + +- Introduce `ConnectionId::new_unchecked` to allow for more sophisticated, manual tests of `NetworkBehaviour`. + See [PR] + # 0.42.0 - Allow `NetworkBehaviour`s to manage connections. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index ed1d03f31a9..4ce7e70b814 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = "1.62.0" description = "The libp2p swarm" -version = "0.42.0" +version = "0.42.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index b42bdde9620..ea91d64e2a9 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -67,6 +67,16 @@ impl ConnectionId { )] pub const DUMMY: ConnectionId = ConnectionId(0); + /// Creates an _unchecked_ [`ConnectionId`]. + /// + /// [`Swarm`](crate::Swarm) enforces that [`ConnectionId`]s are unique and not reused. + /// This constructor does not, hence the _unchecked_. + /// + /// It is primarily meant for allowing manual tests of [`NetworkBehaviour`](crate::NetworkBehaviour)s. + pub fn new_unchecked(id: usize) -> Self { + Self(id) + } + /// Returns the next available [`ConnectionId`]. pub(crate) fn next() -> Self { Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst)) From 76d072dae1fba13015d0feb2d0b8cc355abf455e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 12:10:33 +0100 Subject: [PATCH 2/4] Add PR link --- swarm/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index c7e5047644b..e42653349a8 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,7 +1,9 @@ # 0.42.1 - unreleased - Introduce `ConnectionId::new_unchecked` to allow for more sophisticated, manual tests of `NetworkBehaviour`. - See [PR] + See [PR 3652]. + +[PR 3652]: https://github.com/libp2p/rust-libp2p/pull/3652 # 0.42.0 From a31d0cb288777f4e03c9a377d4bb47cbea37f75b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 15:00:38 +0100 Subject: [PATCH 3/4] Update cargo lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index fb2a020a34e..6b9d70eca85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2752,7 +2752,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.42.0" +version = "0.42.1" dependencies = [ "async-std", "either", From 1d91f0c07b7dbcfab0a35b79b77367fe485bdff4 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 15:02:44 +0100 Subject: [PATCH 4/4] Prepare for release --- swarm/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index e42653349a8..d415fac736a 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.42.1 - unreleased +# 0.42.1 - Introduce `ConnectionId::new_unchecked` to allow for more sophisticated, manual tests of `NetworkBehaviour`. See [PR 3652].