Skip to content

Commit

Permalink
fix(relay/client): only try to forward handler events once
Browse files Browse the repository at this point in the history
We currently try to forward pending handler events in the relay client behavior twice: in `handle_established_{in, out}bound_connection` and in `on_swarm_event()` (introduced in #3328).
This is redundant because by the second time we try to forward it there won't be any pending event anymore.
This PR removes the duplicated logic from `on_swarm_event`. Forwarding it in `handle_established_{in, out}bound_connection` is more convenient as we still have direct access to the handler there and don't need to go through the swarm.

However, it requires that in `handle_established_{in, out}bound_connection` the event is also removed when the connection is relayed, in which case the event is simply discarded because the commands are only valid on direct connections.

Pull-Request: #5765.
  • Loading branch information
elenaf9 authored Jan 27, 2025
1 parent 0b44564 commit 68ea5b7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 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 @@ -94,7 +94,7 @@ libp2p-ping = { version = "0.46.0", path = "protocols/ping" }
libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.26.0", path = "transports/pnet" }
libp2p-quic = { version = "0.12.0", path = "transports/quic" }
libp2p-relay = { version = "0.19.0", path = "protocols/relay" }
libp2p-relay = { version = "0.19.1", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.16.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.28.0", path = "protocols/request-response" }
libp2p-server = { version = "0.12.6", path = "misc/server" }
Expand Down
4 changes: 4 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.19.1

- Remove duplicated forwarding of pending events to connection handler.

## 0.19.0

- Deprecate `void` crate.
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-relay"
edition = "2021"
rust-version = { workspace = true }
description = "Communications relaying for libp2p"
version = "0.19.0"
version = "0.19.1"
authors = ["Parity Technologies <[email protected]>", "Max Inden <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
28 changes: 11 additions & 17 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ impl NetworkBehaviour for Behaviour {
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
let pending_handler_command = self.pending_handler_commands.remove(&connection_id);

if local_addr.is_relayed() {
return Ok(Either::Right(dummy::ConnectionHandler));
}
let mut handler = Handler::new(self.local_peer_id, peer, remote_addr.clone());

if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
if let Some(event) = pending_handler_command {
handler.on_behaviour_event(event)
}

Expand All @@ -188,13 +190,15 @@ impl NetworkBehaviour for Behaviour {
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
let pending_handler_command = self.pending_handler_commands.remove(&connection_id);

if addr.is_relayed() {
return Ok(Either::Right(dummy::ConnectionHandler));
}

let mut handler = Handler::new(self.local_peer_id, peer, addr.clone());

if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
if let Some(event) = pending_handler_command {
handler.on_behaviour_event(event)
}

Expand All @@ -208,21 +212,11 @@ impl NetworkBehaviour for Behaviour {
connection_id,
endpoint,
..
}) => {
if !endpoint.is_relayed() {
self.directly_connected_peers
.entry(peer_id)
.or_default()
.push(connection_id);
}

if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
self.queued_actions.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: Either::Left(event),
})
}
}) if !endpoint.is_relayed() => {
self.directly_connected_peers
.entry(peer_id)
.or_default()
.push(connection_id);
}
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
Expand Down

0 comments on commit 68ea5b7

Please sign in to comment.