Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(hole-punch): don't explicitly connect to the relay #4746

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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