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

feat(relay): emit external address on reservation #4809

Merged
merged 26 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4a9932a
feat: Implement external address on successful reservation
dariusc93 Nov 2, 2023
b8aa098
chore: Emit event before reservation accepted event
dariusc93 Nov 2, 2023
02aff86
fix: remove extra protocol
dariusc93 Nov 2, 2023
441fccf
chore: Update CHANGELOG.md
dariusc93 Nov 2, 2023
f21b7a6
Merge remote-tracking branch 'origin/master' into feat/relay-external…
dariusc93 Nov 3, 2023
82b7e7a
Merge branch 'master' into feat/relay-external-addr
dariusc93 Nov 6, 2023
cbecab9
chore: Update test
dariusc93 Nov 6, 2023
c3042f9
Merge branch 'master' into feat/relay-external-addr
dariusc93 Nov 9, 2023
66c4fe7
chore: Check external addr
dariusc93 Nov 9, 2023
b6c7347
chore: Use event to get relay address provided
dariusc93 Nov 9, 2023
5f91043
chore: Update dcutr test
dariusc93 Nov 9, 2023
b775b9c
chore: Format code
dariusc93 Nov 9, 2023
7b15110
chore: Update Cargo.toml
dariusc93 Nov 9, 2023
84f59c9
chore: Check client address
dariusc93 Nov 9, 2023
604bba7
chore: Emit event is reservation isnt renewed
dariusc93 Nov 9, 2023
deb3182
chore: Check renewal in test
dariusc93 Nov 9, 2023
8982465
chore: Check renewal
dariusc93 Nov 9, 2023
e9372d8
Update protocols/relay/CHANGELOG.md
dariusc93 Nov 9, 2023
8ac345c
Merge branch 'master' into feat/relay-external-addr
dariusc93 Nov 13, 2023
b121cd6
Merge branch 'feat/relay-external-addr' of github.com:dariusc93/rust-…
dariusc93 Nov 13, 2023
17e7a8e
chore: check if reservation exist prior to emitting event
dariusc93 Nov 13, 2023
b781678
fix: Correct package version
dariusc93 Nov 13, 2023
9e18f6b
chore: use a single map to track reservation
dariusc93 Nov 14, 2023
cc0c26b
chore: Correct naming
dariusc93 Nov 14, 2023
f9f2ee6
Don't use `matches!` if not necessary
thomaseizinger Nov 14, 2023
396fb8a
Merge branch 'master' into feat/relay-external-addr
mergify[bot] Nov 14, 2023
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
6 changes: 5 additions & 1 deletion protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.17.0

## 0.17.1 - unreleased
- Emit an external address on making reservation.
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved
See [PR 4809](https://github.com/libp2p/rust-lib2pp/pulls/4809).

## 0.17.0
- Don't close connections on protocol failures within the relay-server.
To achieve this, error handling was restructured:
- `libp2p::relay::outbound::stop::FatalUpgradeError` has been removed.
Expand Down
25 changes: 23 additions & 2 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use futures::future::{BoxFuture, FutureExt};
use futures::io::{AsyncRead, AsyncWrite};
use futures::ready;
use futures::stream::StreamExt;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
Expand Down Expand Up @@ -79,6 +80,9 @@ pub struct Behaviour {
/// connection.
directly_connected_peers: HashMap<PeerId, Vec<ConnectionId>>,

/// Direct connection address
direct_connection_addrs: HashMap<ConnectionId, Multiaddr>,

/// Queue of actions to return when polled.
queued_actions: VecDeque<ToSwarm<Event, Either<handler::In, Void>>>,

Expand All @@ -92,6 +96,7 @@ pub fn new(local_peer_id: PeerId) -> (Transport, Behaviour) {
local_peer_id,
from_transport,
directly_connected_peers: Default::default(),
direct_connection_addrs: Default::default(),
queued_actions: Default::default(),
pending_handler_commands: Default::default(),
};
Expand Down Expand Up @@ -126,6 +131,7 @@ impl Behaviour {
unreachable!("`on_connection_closed` for unconnected peer.")
}
};
self.direct_connection_addrs.remove(&connection_id);
}
}
}
Expand Down Expand Up @@ -186,6 +192,10 @@ impl NetworkBehaviour for Behaviour {
.entry(peer_id)
.or_default()
.push(connection_id);

let addr = endpoint.get_remote_address().clone();
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

self.direct_connection_addrs.insert(connection_id, addr);
}

if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
Expand All @@ -209,7 +219,7 @@ impl NetworkBehaviour for Behaviour {
fn on_connection_handler_event(
&mut self,
event_source: PeerId,
_connection: ConnectionId,
connection: ConnectionId,
handler_event: THandlerOutEvent<Self>,
) {
let handler_event = match handler_event {
Expand All @@ -219,6 +229,17 @@ impl NetworkBehaviour for Behaviour {

let event = match handler_event {
handler::Event::ReservationReqAccepted { renewal, limit } => {
let addr = self
.direct_connection_addrs
.get(&connection)
.cloned()
.expect("Connection to be direct")
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(self.local_peer_id));
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

self.queued_actions
.push_back(ToSwarm::ExternalAddrConfirmed(addr));
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

Event::ReservationReqAccepted {
relay_peer_id: event_source,
renewal,
Expand All @@ -236,7 +257,7 @@ impl NetworkBehaviour for Behaviour {
}
};

self.queued_actions.push_back(ToSwarm::GenerateEvent(event))
self.queued_actions.push_back(ToSwarm::GenerateEvent(event));
}

fn poll(
Expand Down
2 changes: 2 additions & 0 deletions protocols/relay/tests/lib.rs
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn new_reservation_to_same_relay_replaces_old() {
break;
}
}
SwarmEvent::ExternalAddrConfirmed { .. } => {}
SwarmEvent::Behaviour(ClientEvent::Ping(_)) => {}
e => panic!("{e:?}"),
}
Expand Down Expand Up @@ -521,6 +522,7 @@ async fn wait_for_reservation(

loop {
match client.select_next_some().await {
SwarmEvent::ExternalAddrConfirmed { .. } => {}
SwarmEvent::Behaviour(ClientEvent::Relay(
relay::client::Event::ReservationReqAccepted {
relay_peer_id: peer_id,
Expand Down