Skip to content

Commit c54c0f2

Browse files
feat(swarm): add Option<PeerId> to IncomingConnectionError
Fixes #6025 Supercedes #6027 Pull-Request: #6032.
1 parent cd215dc commit c54c0f2

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

misc/connection-limits/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 0.5.1
22

3-
- Allow setting Peer IDs for bypassing limit check.
3+
- Allow setting Peer IDs for bypassing limit check.
44
Connections to the specified peers won't be counted toward limits.
55
See [PR 5720](https://github.com/libp2p/rust-libp2p/pull/5720).
66

misc/connection-limits/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,37 @@ mod tests {
658658
)
659659
}
660660

661+
#[tokio::test]
662+
async fn incoming_connection_error_carries_peer_id() {
663+
let mut swarm1 = Swarm::new_ephemeral_tokio(|_| {
664+
// Set max incoming connections to 0 to immediately deny all connections
665+
Behaviour::new(ConnectionLimits::default().with_max_established_incoming(Some(0)))
666+
});
667+
let mut swarm2 =
668+
Swarm::new_ephemeral_tokio(|_| Behaviour::new(ConnectionLimits::default()));
669+
670+
let dialer_peer_id = *swarm2.local_peer_id();
671+
672+
let (listen_addr, _) = swarm1.listen().with_memory_addr_external().await;
673+
674+
swarm2.dial(listen_addr).unwrap();
675+
676+
tokio::spawn(swarm2.loop_on_next());
677+
678+
let peer_id = swarm1
679+
.wait(|event| match event {
680+
SwarmEvent::IncomingConnectionError {
681+
error: ListenError::Denied { .. },
682+
peer_id,
683+
..
684+
} => peer_id,
685+
_ => None,
686+
})
687+
.await;
688+
689+
assert_eq!(Some(dialer_peer_id), Some(peer_id));
690+
}
691+
661692
#[derive(libp2p_swarm_derive::NetworkBehaviour)]
662693
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
663694
struct Behaviour {

swarm/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
certain upgrade.
1010
See [PR 5860](https://github.com/libp2p/rust-libp2p/pull/5860).
1111

12+
- Expose `Option<PeerId>` in `SwarmEvent::IncomingConnectionError`.
13+
This enables insight to which `PeerId` errored.
14+
See [PR 6032](https://github.com/libp2p/rust-libp2p/pull/6032)
15+
1216
## 0.46.0
1317

1418
- Don't report `NewExternalAddrCandidate` for confirmed external addresses.

swarm/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ pub enum SwarmEvent<TBehaviourOutEvent> {
221221
send_back_addr: Multiaddr,
222222
/// The error that happened.
223223
error: ListenError,
224+
/// If known, [`PeerId`] of the peer that tried to connect to us.
225+
peer_id: Option<PeerId>,
224226
},
225227
/// An error happened on an outbound connection.
226228
OutgoingConnectionError {
@@ -756,6 +758,7 @@ where
756758
send_back_addr,
757759
local_addr,
758760
error: listen_error,
761+
peer_id: Some(peer_id),
759762
},
760763
);
761764
return;
@@ -868,6 +871,7 @@ where
868871
local_addr,
869872
send_back_addr,
870873
error,
874+
peer_id: None,
871875
});
872876
}
873877
PoolEvent::ConnectionClosed {
@@ -974,6 +978,7 @@ where
974978
local_addr,
975979
send_back_addr,
976980
error: listen_error,
981+
peer_id: None,
977982
});
978983
return;
979984
}

0 commit comments

Comments
 (0)