Skip to content

Commit

Permalink
feat(swarm): add peer_id to ListenFailure
Browse files Browse the repository at this point in the history
It's possible in certain failure modes to know the peer_id of a failed incoming connection. For example when an inbound connection has been negotiated/upgraded but then rejected locally for a connection limit or similar reason. In these cases it makes sense to communicate the peer_id to behaviours in case they have created any internal state about the peer.

Related #4777

Pull-Request: #4818.
  • Loading branch information
nathanielc authored Jun 13, 2024
1 parent 87b9394 commit 43524e7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ libp2p-rendezvous = { version = "0.14.1", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.4", path = "protocols/request-response" }
libp2p-server = { version = "0.12.7", path = "misc/server" }
libp2p-stream = { version = "0.1.0-alpha.1", path = "protocols/stream" }
libp2p-swarm = { version = "0.44.3", path = "swarm" }
libp2p-swarm = { version = "0.45.0", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.2", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.41.1", path = "transports/tcp" }
Expand Down
5 changes: 4 additions & 1 deletion swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 0.44.3
## 0.45.0

- Add peer_id to `FromSwarm::ListenFailure`.
See [PR 4818](https://github.com/libp2p/rust-libp2p/pull/4818).

- Use `web-time` instead of `instant`.
See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347).
Expand Down
2 changes: 1 addition & 1 deletion swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm"
edition = "2021"
rust-version = { workspace = true }
description = "The libp2p swarm"
version = "0.44.3"
version = "0.45.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
9 changes: 8 additions & 1 deletion swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub trait NetworkBehaviour: 'static {
/// At this point, we have verified their [`PeerId`] and we know, which particular [`Multiaddr`] succeeded in the dial.
/// In order to actually use this connection, this function must return a [`ConnectionHandler`].
/// Returning an error will immediately close the connection.
///
/// Note when any composed behaviour returns an error the connection will be closed and a
/// [`FromSwarm::ListenFailure`] event will be emitted.
fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
Expand Down Expand Up @@ -184,6 +187,9 @@ pub trait NetworkBehaviour: 'static {
/// At this point, we have verified their [`PeerId`] and we know, which particular [`Multiaddr`] succeeded in the dial.
/// In order to actually use this connection, this function must return a [`ConnectionHandler`].
/// Returning an error will immediately close the connection.
///
/// Note when any composed behaviour returns an error the connection will be closed and a
/// [`FromSwarm::DialFailure`] event will be emitted.
fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
Expand Down Expand Up @@ -269,7 +275,7 @@ pub enum ToSwarm<TOutEvent, TInEvent> {
/// The emphasis on a **new** candidate is important.
/// Protocols MUST take care to only emit a candidate once per "source".
/// For example, the observed address of a TCP connection does not change throughout its lifetime.
/// Thus, only one candidate should be emitted per connection.
/// Thus, only one candidate should be emitted per connection.
///
/// This makes the report frequency of an address a meaningful data-point for consumers of this event.
/// This address will be shared with all [`NetworkBehaviour`]s via [`FromSwarm::NewExternalAddrCandidate`].
Expand Down Expand Up @@ -508,6 +514,7 @@ pub struct ListenFailure<'a> {
pub send_back_addr: &'a Multiaddr,
pub error: &'a ListenError,
pub connection_id: ConnectionId,
pub peer_id: Option<PeerId>,
}

/// [`FromSwarm`] variant that informs the behaviour that a new listener was created.
Expand Down
3 changes: 3 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ where
send_back_addr: &send_back_addr,
error: &listen_error,
connection_id: id,
peer_id: Some(peer_id),
},
));

Expand Down Expand Up @@ -867,6 +868,7 @@ where
send_back_addr: &send_back_addr,
error: &error,
connection_id: id,
peer_id: None,
}));
self.pending_swarm_events
.push_back(SwarmEvent::IncomingConnectionError {
Expand Down Expand Up @@ -970,6 +972,7 @@ where
send_back_addr: &send_back_addr,
error: &listen_error,
connection_id,
peer_id: None,
}));

self.pending_swarm_events
Expand Down

0 comments on commit 43524e7

Please sign in to comment.