Skip to content

Commit 3fa10be

Browse files
feat(swarm): introduce ConnectionId::new_unchecked constructor
In earlier iterations of the design for generic connection management, we removed the `ConnectionId::new` constructor because it would have allowed users to create `ConnectionId`s that are already taken, thus breaking invariants that `NetworkBehaviour`s rely on. Later, we incorporated the creation of `ConnectionId` in `DialOpts` which mitigates this risk altogether. Thus, it is reasonably safe to introduce a public, non-deprecated constructor for `ConnectionId` that can be used for tests. Related #3327 (comment). Pull-Request: #3652.
1 parent ab9555c commit 3fa10be

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

protocols/gossipsub/src/behaviour/tests.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,19 @@ where
222222
}
223223
};
224224

225-
#[allow(deprecated)]
226225
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
227226
peer_id: peer,
228-
connection_id: ConnectionId::DUMMY,
227+
connection_id: ConnectionId::new_unchecked(0),
229228
endpoint: &endpoint,
230229
failed_addresses: &[],
231230
other_established: 0, // first connection
232231
}));
233232
if let Some(kind) = kind {
234-
#[allow(deprecated)]
235-
gs.on_connection_handler_event(peer, ConnectionId::DUMMY, HandlerEvent::PeerKind(kind));
233+
gs.on_connection_handler_event(
234+
peer,
235+
ConnectionId::new_unchecked(0),
236+
HandlerEvent::PeerKind(kind),
237+
);
236238
}
237239
if explicit {
238240
gs.add_explicit_peer(&peer);
@@ -582,10 +584,9 @@ fn test_join() {
582584
for _ in 0..3 {
583585
let random_peer = PeerId::random();
584586
// inform the behaviour of a new peer
585-
#[allow(deprecated)]
586587
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
587588
peer_id: random_peer,
588-
connection_id: ConnectionId::DUMMY,
589+
connection_id: ConnectionId::new_unchecked(0),
589590
endpoint: &ConnectedPoint::Dialer {
590591
address: "/ip4/127.0.0.1".parse::<Multiaddr>().unwrap(),
591592
role_override: Endpoint::Dialer,
@@ -965,10 +966,7 @@ fn test_get_random_peers() {
965966
*p,
966967
PeerConnections {
967968
kind: PeerKind::Gossipsubv1_1,
968-
connections: vec![
969-
#[allow(deprecated)]
970-
ConnectionId::DUMMY,
971-
],
969+
connections: vec![ConnectionId::new_unchecked(0)],
972970
},
973971
)
974972
})
@@ -2998,8 +2996,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() {
29982996
//receive from p1
29992997
gs.on_connection_handler_event(
30002998
p1,
3001-
#[allow(deprecated)]
3002-
ConnectionId::DUMMY,
2999+
ConnectionId::new_unchecked(0),
30033000
HandlerEvent::Message {
30043001
rpc: Rpc {
30053002
messages: vec![raw_message1],
@@ -3025,8 +3022,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() {
30253022
//receive from p2
30263023
gs.on_connection_handler_event(
30273024
p2,
3028-
#[allow(deprecated)]
3029-
ConnectionId::DUMMY,
3025+
ConnectionId::new_unchecked(0),
30303026
HandlerEvent::Message {
30313027
rpc: Rpc {
30323028
messages: vec![raw_message3],
@@ -3632,8 +3628,7 @@ fn test_scoring_p4_invalid_signature() {
36323628

36333629
gs.on_connection_handler_event(
36343630
peers[0],
3635-
#[allow(deprecated)]
3636-
ConnectionId::DUMMY,
3631+
ConnectionId::new_unchecked(0),
36373632
HandlerEvent::Message {
36383633
rpc: Rpc {
36393634
messages: vec![],
@@ -4114,11 +4109,10 @@ fn test_scoring_p6() {
41144109
}
41154110

41164111
//add additional connection for 3 others with addr
4117-
#[allow(deprecated)]
41184112
for id in others.iter().take(3) {
41194113
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
41204114
peer_id: *id,
4121-
connection_id: ConnectionId::DUMMY,
4115+
connection_id: ConnectionId::new_unchecked(0),
41224116
endpoint: &ConnectedPoint::Dialer {
41234117
address: addr.clone(),
41244118
role_override: Endpoint::Dialer,
@@ -4137,10 +4131,9 @@ fn test_scoring_p6() {
41374131

41384132
//add additional connection for 3 of the peers to addr2
41394133
for peer in peers.iter().take(3) {
4140-
#[allow(deprecated)]
41414134
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
41424135
peer_id: *peer,
4143-
connection_id: ConnectionId::DUMMY,
4136+
connection_id: ConnectionId::new_unchecked(0),
41444137
endpoint: &ConnectedPoint::Dialer {
41454138
address: addr2.clone(),
41464139
role_override: Endpoint::Dialer,
@@ -4168,10 +4161,9 @@ fn test_scoring_p6() {
41684161
);
41694162

41704163
//two times same ip doesn't count twice
4171-
#[allow(deprecated)]
41724164
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
41734165
peer_id: peers[0],
4174-
connection_id: ConnectionId::DUMMY,
4166+
connection_id: ConnectionId::new_unchecked(0),
41754167
endpoint: &ConnectedPoint::Dialer {
41764168
address: addr,
41774169
role_override: Endpoint::Dialer,
@@ -5179,8 +5171,7 @@ fn test_subscribe_and_graft_with_negative_score() {
51795171

51805172
let (mut gs2, _, _) = inject_nodes1().create_network();
51815173

5182-
#[allow(deprecated)]
5183-
let connection_id = ConnectionId::DUMMY;
5174+
let connection_id = ConnectionId::new_unchecked(0);
51845175

51855176
let topic = Topic::new("test");
51865177

protocols/kad/src/behaviour/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,8 +1295,7 @@ fn network_behaviour_on_address_change() {
12951295
let local_peer_id = PeerId::random();
12961296

12971297
let remote_peer_id = PeerId::random();
1298-
#[allow(deprecated)]
1299-
let connection_id = ConnectionId::DUMMY;
1298+
let connection_id = ConnectionId::new_unchecked(0);
13001299
let old_address: Multiaddr = Protocol::Memory(1).into();
13011300
let new_address: Multiaddr = Protocol::Memory(2).into();
13021301

swarm/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
- Deprecate `ConnectionLimits` in favor of `libp2p::connection_limits`.
44
See [PR 3386].
55

6+
- Introduce `ConnectionId::new_unchecked` to allow for more sophisticated, manual tests of `NetworkBehaviour`.
7+
See [PR 3652].
8+
69
[PR 3386]: https://github.com/libp2p/rust-libp2p/pull/3386
10+
[PR 3652]: https://github.com/libp2p/rust-libp2p/pull/3652
711

812
# 0.42.0
913

swarm/src/connection.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ impl ConnectionId {
6767
)]
6868
pub const DUMMY: ConnectionId = ConnectionId(0);
6969

70+
/// Creates an _unchecked_ [`ConnectionId`].
71+
///
72+
/// [`Swarm`](crate::Swarm) enforces that [`ConnectionId`]s are unique and not reused.
73+
/// This constructor does not, hence the _unchecked_.
74+
///
75+
/// It is primarily meant for allowing manual tests of [`NetworkBehaviour`](crate::NetworkBehaviour)s.
76+
pub fn new_unchecked(id: usize) -> Self {
77+
Self(id)
78+
}
79+
7080
/// Returns the next available [`ConnectionId`].
7181
pub(crate) fn next() -> Self {
7282
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst))

0 commit comments

Comments
 (0)