Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.22.0

- Avoid panic when dst has connections without reservations.
See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX).

- Raise MSRV to 1.88.0.
See [PR 6273](https://github.com/libp2p/rust-libp2p/pull/6273).

Expand Down
6 changes: 2 additions & 4 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,11 @@ impl NetworkBehaviour for Behaviour {
status: proto::Status::ResourceLimitExceeded,
}),
}
} else if let Some((dst_conn, status)) = self
} else if let Some((dst_conn, _)) = self
.connections
.get(&inbound_circuit_req.dst())
.and_then(|cs| cs.iter().next())
.and_then(|cs| cs.iter().find(|(_, status)| status.is_active()))
{
assert_eq!(*status, Reservation::Active);

// Accept circuit request if reservation present.
let circuit_id = self.circuits.insert(Circuit {
status: CircuitStatus::Accepting,
Expand Down
85 changes: 85 additions & 0 deletions protocols/relay/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,91 @@
));
}

#[tokio::test]
async fn deny_circuit_to_connected_peer_without_reservation() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();

let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::<u64>()));
let mut relay = build_relay();
let relay_peer_id = *relay.local_peer_id();

relay.listen_on(relay_addr.clone()).unwrap();
relay.add_external_address(relay_addr.clone());

let mut dst = build_client();
let dst_peer_id = *dst.local_peer_id();
dst.dial(relay_addr.clone()).unwrap();

let mut relay_saw_dst = false;
let mut dst_connected = false;
while !(relay_saw_dst && dst_connected) {
tokio::select! {
event = relay.select_next_some() => {
if let SwarmEvent::ConnectionEstablished { peer_id, .. } = event {

Check failure on line 500 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (1.93.1)

this `if` statement can be collapsed

Check failure on line 500 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

this `if` statement can be collapsed
if peer_id == dst_peer_id {
relay_saw_dst = true;
}
}
}
event = dst.select_next_some() => {
if let SwarmEvent::ConnectionEstablished { peer_id, .. } = event {

Check failure on line 507 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (1.93.1)

this `if` statement can be collapsed

Check failure on line 507 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

this `if` statement can be collapsed
if peer_id == relay_peer_id {
dst_connected = true;
}
}
}
}
}

let mut src = build_client();
let dst_addr = relay_addr
.with(Protocol::P2p(relay_peer_id))
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(dst_peer_id));

let opts = DialOpts::from(dst_addr.clone());
let circuit_connection_id = opts.connection_id();

src.dial(opts).unwrap();

let (failed_address, error) = loop {
tokio::select! {
_ = relay.select_next_some() => {}
_ = dst.select_next_some() => {}
event = src.select_next_some() => {
if let SwarmEvent::OutgoingConnectionError {

Check failure on line 532 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (1.93.1)

this `if` statement can be collapsed

Check failure on line 532 in protocols/relay/tests/lib.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

this `if` statement can be collapsed
connection_id,
error: DialError::Transport(mut errors),
..
} = event
{
if connection_id == circuit_connection_id {
assert_eq!(errors.len(), 1);
break errors.remove(0);
}
}
}
}
};

// This is a bit wonky but we need to get the source error
let error = error
.source()
.unwrap()
.source()
.unwrap()
.downcast_ref::<relay::outbound::hop::ConnectError>()
.unwrap();

assert_eq!(failed_address, dst_addr);
assert!(matches!(
error,
relay::outbound::hop::ConnectError::NoReservation
));
}

#[tokio::test]
async fn reuse_connection() {
let _ = tracing_subscriber::fmt()
Expand Down
Loading