Skip to content

Commit 459c9d4

Browse files
refactor(hole-punch): don't explicitly connect to the relay
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: #4746.
1 parent 461209a commit 459c9d4

File tree

1 file changed

+0
-55
lines changed

1 file changed

+0
-55
lines changed

hole-punching-tests/src/main.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
use anyhow::{Context, Result};
2222
use futures::stream::StreamExt;
23-
use libp2p::swarm::dial_opts::DialOpts;
24-
use libp2p::swarm::ConnectionId;
2523
use libp2p::{
2624
core::multiaddr::{Multiaddr, Protocol},
2725
dcutr, identify, noise, ping, relay,
@@ -85,10 +83,6 @@ async fn main() -> Result<()> {
8583
.build();
8684

8785
client_listen_on_transport(&mut swarm, transport).await?;
88-
let relay_conn_id = client_connect_to_relay(&mut swarm, relay_addr.clone())
89-
.await
90-
.context("Failed to connect to relay")?;
91-
9286
client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?;
9387

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

121-
// Closing the connection to the relay will implicitly close the relayed connection to the other peer.
122-
swarm.close_connection(relay_conn_id);
123115
hole_punched_peer_connection = Some(connection_id);
124116
}
125117
(
@@ -150,19 +142,6 @@ async fn main() -> Result<()> {
150142
(SwarmEvent::OutgoingConnectionError { error, .. }, _) => {
151143
anyhow::bail!(error)
152144
}
153-
(
154-
SwarmEvent::ConnectionClosed {
155-
connection_id,
156-
cause: Some(error),
157-
..
158-
},
159-
_,
160-
) if connection_id == relay_conn_id => {
161-
log::warn!("Connection to relay failed: {error}");
162-
163-
// TODO: Re-connecting is a bit of a hack, we should figure out why the connection sometimes fails.
164-
client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?;
165-
}
166145
_ => {}
167146
}
168147
}
@@ -194,40 +173,6 @@ where
194173
Ok(val)
195174
}
196175

197-
async fn client_connect_to_relay(
198-
swarm: &mut Swarm<Behaviour>,
199-
relay_addr: Multiaddr,
200-
) -> Result<ConnectionId> {
201-
let opts = DialOpts::from(relay_addr);
202-
let relay_connection_id = opts.connection_id();
203-
204-
// Connect to the relay server.
205-
swarm.dial(opts)?;
206-
207-
loop {
208-
match swarm.next().await.unwrap() {
209-
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
210-
info: identify::Info { observed_addr, .. },
211-
..
212-
})) => {
213-
log::info!("Relay told us our public address: {observed_addr}");
214-
break;
215-
}
216-
SwarmEvent::ConnectionEstablished { connection_id, .. }
217-
if connection_id == relay_connection_id =>
218-
{
219-
log::info!("Connected to the relay");
220-
}
221-
SwarmEvent::OutgoingConnectionError { error, .. } => {
222-
anyhow::bail!(error)
223-
}
224-
_ => {}
225-
}
226-
}
227-
228-
Ok(relay_connection_id)
229-
}
230-
231176
async fn client_listen_on_transport(
232177
swarm: &mut Swarm<Behaviour>,
233178
transport: TransportProtocol,

0 commit comments

Comments
 (0)