Skip to content

Commit

Permalink
refactor(hole-punch): don't explicitly connect to the relay
Browse files Browse the repository at this point in the history
Making a reservation or opening a circuit will automatically establish a connection to the relay, we don't need to do this manually. Plus, the relay in the test setup has a statically configured external address, meaning it doesn't need to learn its external address via identify.

Pull-Request: libp2p#4746.
  • Loading branch information
thomaseizinger authored Oct 27, 2023
1 parent 461209a commit 459c9d4
Showing 1 changed file with 0 additions and 55 deletions.
55 changes: 0 additions & 55 deletions hole-punching-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

use anyhow::{Context, Result};
use futures::stream::StreamExt;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::ConnectionId;
use libp2p::{
core::multiaddr::{Multiaddr, Protocol},
dcutr, identify, noise, ping, relay,
Expand Down Expand Up @@ -85,10 +83,6 @@ async fn main() -> Result<()> {
.build();

client_listen_on_transport(&mut swarm, transport).await?;
let relay_conn_id = client_connect_to_relay(&mut swarm, relay_addr.clone())
.await
.context("Failed to connect to relay")?;

client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?;

let mut hole_punched_peer_connection = None;
Expand Down Expand Up @@ -118,8 +112,6 @@ async fn main() -> Result<()> {
) => {
log::info!("Successfully hole-punched to {remote_peer_id}");

// Closing the connection to the relay will implicitly close the relayed connection to the other peer.
swarm.close_connection(relay_conn_id);
hole_punched_peer_connection = Some(connection_id);
}
(
Expand Down Expand Up @@ -150,19 +142,6 @@ async fn main() -> Result<()> {
(SwarmEvent::OutgoingConnectionError { error, .. }, _) => {
anyhow::bail!(error)
}
(
SwarmEvent::ConnectionClosed {
connection_id,
cause: Some(error),
..
},
_,
) if connection_id == relay_conn_id => {
log::warn!("Connection to relay failed: {error}");

// TODO: Re-connecting is a bit of a hack, we should figure out why the connection sometimes fails.
client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?;
}
_ => {}
}
}
Expand Down Expand Up @@ -194,40 +173,6 @@ where
Ok(val)
}

async fn client_connect_to_relay(
swarm: &mut Swarm<Behaviour>,
relay_addr: Multiaddr,
) -> Result<ConnectionId> {
let opts = DialOpts::from(relay_addr);
let relay_connection_id = opts.connection_id();

// Connect to the relay server.
swarm.dial(opts)?;

loop {
match swarm.next().await.unwrap() {
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
})) => {
log::info!("Relay told us our public address: {observed_addr}");
break;
}
SwarmEvent::ConnectionEstablished { connection_id, .. }
if connection_id == relay_connection_id =>
{
log::info!("Connected to the relay");
}
SwarmEvent::OutgoingConnectionError { error, .. } => {
anyhow::bail!(error)
}
_ => {}
}
}

Ok(relay_connection_id)
}

async fn client_listen_on_transport(
swarm: &mut Swarm<Behaviour>,
transport: TransportProtocol,
Expand Down

0 comments on commit 459c9d4

Please sign in to comment.