From 479b36035210e63734cba216a84638eae3d89b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?= Date: Thu, 24 Oct 2024 10:28:16 +0200 Subject: [PATCH] fix relayed dial not performed if local peer listen on same relay --- swarm/src/lib.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 12280e99f072..ec061c044fa0 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -120,6 +120,7 @@ pub use handler::{ ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerSelect, OneShotHandler, OneShotHandlerConfig, StreamUpgradeError, SubstreamProtocol, }; +use libp2p_core::multiaddr::Protocol; #[cfg(feature = "macros")] pub use libp2p_swarm_derive::NetworkBehaviour; pub use listen_opts::ListenOpts; @@ -509,10 +510,22 @@ where } } - let mut unique_addresses = HashSet::new(); + let mut unique_addresses: HashSet<_> = HashSet::new(); addresses_from_opts.retain(|addr| { - !self.listened_addrs.values().flatten().any(|a| a == addr) - && unique_addresses.insert(addr.clone()) + if !unique_addresses.insert(addr.clone()) { + // Address already added, don't dial it twice. + false + } else { + if addr.iter().any(|p| p == Protocol::P2pCircuit) { + // Address is relayed. It's ok to dial a peer that listens + // on the same relay that the local peer does. + true + } else { + // Address is not relayed. Prevent dial on any addresses + // the local peer listens on. + !self.listened_addrs.values().flatten().any(|a| a == addr) + } + } }); if addresses_from_opts.is_empty() {