mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 10:31:21 +00:00
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 https://github.com/libp2p/rust-libp2p/pull/3327#issuecomment-1469870307. Pull-Request: #3652.
This commit is contained in:
@ -222,17 +222,19 @@ where
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#[allow(deprecated)]
|
|
||||||
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
||||||
peer_id: peer,
|
peer_id: peer,
|
||||||
connection_id: ConnectionId::DUMMY,
|
connection_id: ConnectionId::new_unchecked(0),
|
||||||
endpoint: &endpoint,
|
endpoint: &endpoint,
|
||||||
failed_addresses: &[],
|
failed_addresses: &[],
|
||||||
other_established: 0, // first connection
|
other_established: 0, // first connection
|
||||||
}));
|
}));
|
||||||
if let Some(kind) = kind {
|
if let Some(kind) = kind {
|
||||||
#[allow(deprecated)]
|
gs.on_connection_handler_event(
|
||||||
gs.on_connection_handler_event(peer, ConnectionId::DUMMY, HandlerEvent::PeerKind(kind));
|
peer,
|
||||||
|
ConnectionId::new_unchecked(0),
|
||||||
|
HandlerEvent::PeerKind(kind),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if explicit {
|
if explicit {
|
||||||
gs.add_explicit_peer(&peer);
|
gs.add_explicit_peer(&peer);
|
||||||
@ -582,10 +584,9 @@ fn test_join() {
|
|||||||
for _ in 0..3 {
|
for _ in 0..3 {
|
||||||
let random_peer = PeerId::random();
|
let random_peer = PeerId::random();
|
||||||
// inform the behaviour of a new peer
|
// inform the behaviour of a new peer
|
||||||
#[allow(deprecated)]
|
|
||||||
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
||||||
peer_id: random_peer,
|
peer_id: random_peer,
|
||||||
connection_id: ConnectionId::DUMMY,
|
connection_id: ConnectionId::new_unchecked(0),
|
||||||
endpoint: &ConnectedPoint::Dialer {
|
endpoint: &ConnectedPoint::Dialer {
|
||||||
address: "/ip4/127.0.0.1".parse::<Multiaddr>().unwrap(),
|
address: "/ip4/127.0.0.1".parse::<Multiaddr>().unwrap(),
|
||||||
role_override: Endpoint::Dialer,
|
role_override: Endpoint::Dialer,
|
||||||
@ -965,10 +966,7 @@ fn test_get_random_peers() {
|
|||||||
*p,
|
*p,
|
||||||
PeerConnections {
|
PeerConnections {
|
||||||
kind: PeerKind::Gossipsubv1_1,
|
kind: PeerKind::Gossipsubv1_1,
|
||||||
connections: vec![
|
connections: vec![ConnectionId::new_unchecked(0)],
|
||||||
#[allow(deprecated)]
|
|
||||||
ConnectionId::DUMMY,
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -2998,8 +2996,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() {
|
|||||||
//receive from p1
|
//receive from p1
|
||||||
gs.on_connection_handler_event(
|
gs.on_connection_handler_event(
|
||||||
p1,
|
p1,
|
||||||
#[allow(deprecated)]
|
ConnectionId::new_unchecked(0),
|
||||||
ConnectionId::DUMMY,
|
|
||||||
HandlerEvent::Message {
|
HandlerEvent::Message {
|
||||||
rpc: Rpc {
|
rpc: Rpc {
|
||||||
messages: vec![raw_message1],
|
messages: vec![raw_message1],
|
||||||
@ -3025,8 +3022,7 @@ fn test_ignore_rpc_from_peers_below_graylist_threshold() {
|
|||||||
//receive from p2
|
//receive from p2
|
||||||
gs.on_connection_handler_event(
|
gs.on_connection_handler_event(
|
||||||
p2,
|
p2,
|
||||||
#[allow(deprecated)]
|
ConnectionId::new_unchecked(0),
|
||||||
ConnectionId::DUMMY,
|
|
||||||
HandlerEvent::Message {
|
HandlerEvent::Message {
|
||||||
rpc: Rpc {
|
rpc: Rpc {
|
||||||
messages: vec![raw_message3],
|
messages: vec![raw_message3],
|
||||||
@ -3632,8 +3628,7 @@ fn test_scoring_p4_invalid_signature() {
|
|||||||
|
|
||||||
gs.on_connection_handler_event(
|
gs.on_connection_handler_event(
|
||||||
peers[0],
|
peers[0],
|
||||||
#[allow(deprecated)]
|
ConnectionId::new_unchecked(0),
|
||||||
ConnectionId::DUMMY,
|
|
||||||
HandlerEvent::Message {
|
HandlerEvent::Message {
|
||||||
rpc: Rpc {
|
rpc: Rpc {
|
||||||
messages: vec![],
|
messages: vec![],
|
||||||
@ -4114,11 +4109,10 @@ fn test_scoring_p6() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//add additional connection for 3 others with addr
|
//add additional connection for 3 others with addr
|
||||||
#[allow(deprecated)]
|
|
||||||
for id in others.iter().take(3) {
|
for id in others.iter().take(3) {
|
||||||
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
||||||
peer_id: *id,
|
peer_id: *id,
|
||||||
connection_id: ConnectionId::DUMMY,
|
connection_id: ConnectionId::new_unchecked(0),
|
||||||
endpoint: &ConnectedPoint::Dialer {
|
endpoint: &ConnectedPoint::Dialer {
|
||||||
address: addr.clone(),
|
address: addr.clone(),
|
||||||
role_override: Endpoint::Dialer,
|
role_override: Endpoint::Dialer,
|
||||||
@ -4137,10 +4131,9 @@ fn test_scoring_p6() {
|
|||||||
|
|
||||||
//add additional connection for 3 of the peers to addr2
|
//add additional connection for 3 of the peers to addr2
|
||||||
for peer in peers.iter().take(3) {
|
for peer in peers.iter().take(3) {
|
||||||
#[allow(deprecated)]
|
|
||||||
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
||||||
peer_id: *peer,
|
peer_id: *peer,
|
||||||
connection_id: ConnectionId::DUMMY,
|
connection_id: ConnectionId::new_unchecked(0),
|
||||||
endpoint: &ConnectedPoint::Dialer {
|
endpoint: &ConnectedPoint::Dialer {
|
||||||
address: addr2.clone(),
|
address: addr2.clone(),
|
||||||
role_override: Endpoint::Dialer,
|
role_override: Endpoint::Dialer,
|
||||||
@ -4168,10 +4161,9 @@ fn test_scoring_p6() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
//two times same ip doesn't count twice
|
//two times same ip doesn't count twice
|
||||||
#[allow(deprecated)]
|
|
||||||
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
gs.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
|
||||||
peer_id: peers[0],
|
peer_id: peers[0],
|
||||||
connection_id: ConnectionId::DUMMY,
|
connection_id: ConnectionId::new_unchecked(0),
|
||||||
endpoint: &ConnectedPoint::Dialer {
|
endpoint: &ConnectedPoint::Dialer {
|
||||||
address: addr,
|
address: addr,
|
||||||
role_override: Endpoint::Dialer,
|
role_override: Endpoint::Dialer,
|
||||||
@ -5179,8 +5171,7 @@ fn test_subscribe_and_graft_with_negative_score() {
|
|||||||
|
|
||||||
let (mut gs2, _, _) = inject_nodes1().create_network();
|
let (mut gs2, _, _) = inject_nodes1().create_network();
|
||||||
|
|
||||||
#[allow(deprecated)]
|
let connection_id = ConnectionId::new_unchecked(0);
|
||||||
let connection_id = ConnectionId::DUMMY;
|
|
||||||
|
|
||||||
let topic = Topic::new("test");
|
let topic = Topic::new("test");
|
||||||
|
|
||||||
|
@ -1295,8 +1295,7 @@ fn network_behaviour_on_address_change() {
|
|||||||
let local_peer_id = PeerId::random();
|
let local_peer_id = PeerId::random();
|
||||||
|
|
||||||
let remote_peer_id = PeerId::random();
|
let remote_peer_id = PeerId::random();
|
||||||
#[allow(deprecated)]
|
let connection_id = ConnectionId::new_unchecked(0);
|
||||||
let connection_id = ConnectionId::DUMMY;
|
|
||||||
let old_address: Multiaddr = Protocol::Memory(1).into();
|
let old_address: Multiaddr = Protocol::Memory(1).into();
|
||||||
let new_address: Multiaddr = Protocol::Memory(2).into();
|
let new_address: Multiaddr = Protocol::Memory(2).into();
|
||||||
|
|
||||||
|
@ -3,7 +3,11 @@
|
|||||||
- Deprecate `ConnectionLimits` in favor of `libp2p::connection_limits`.
|
- Deprecate `ConnectionLimits` in favor of `libp2p::connection_limits`.
|
||||||
See [PR 3386].
|
See [PR 3386].
|
||||||
|
|
||||||
|
- Introduce `ConnectionId::new_unchecked` to allow for more sophisticated, manual tests of `NetworkBehaviour`.
|
||||||
|
See [PR 3652].
|
||||||
|
|
||||||
[PR 3386]: https://github.com/libp2p/rust-libp2p/pull/3386
|
[PR 3386]: https://github.com/libp2p/rust-libp2p/pull/3386
|
||||||
|
[PR 3652]: https://github.com/libp2p/rust-libp2p/pull/3652
|
||||||
|
|
||||||
# 0.42.0
|
# 0.42.0
|
||||||
|
|
||||||
|
@ -67,6 +67,16 @@ impl ConnectionId {
|
|||||||
)]
|
)]
|
||||||
pub const DUMMY: ConnectionId = ConnectionId(0);
|
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`].
|
/// Returns the next available [`ConnectionId`].
|
||||||
pub(crate) fn next() -> Self {
|
pub(crate) fn next() -> Self {
|
||||||
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst))
|
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst))
|
||||||
|
Reference in New Issue
Block a user