Skip to content

Commit

Permalink
fix(network): remove all external addresses related to a relay server
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and jacderida committed Apr 24, 2024
1 parent 3e77a06 commit ea0b0bc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl NetworkBuilder {

let bootstrap = ContinuousBootstrap::new();
let replication_fetcher = ReplicationFetcher::new(peer_id, network_event_sender.clone());
let mut relay_manager = RelayManager::new(self.initial_peers);
let mut relay_manager = RelayManager::new(self.initial_peers, peer_id);
if !is_client {
relay_manager.enable_hole_punching(self.is_behind_home_network);
}
Expand Down
18 changes: 15 additions & 3 deletions sn_networking/src/relay_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const MAX_PEERS_IN_RT_DURING_NAT_CHECK: usize = 30;
/// To manager relayed connections.
#[derive(Debug)]
pub(crate) struct RelayManager {
self_peer_id: PeerId,
// states
enabled: bool,
candidates: VecDeque<(PeerId, Multiaddr)>,
Expand All @@ -30,7 +31,7 @@ pub(crate) struct RelayManager {
}

impl RelayManager {
pub(crate) fn new(initial_peers: Vec<Multiaddr>) -> Self {
pub(crate) fn new(initial_peers: Vec<Multiaddr>, self_peer_id: PeerId) -> Self {
let candidates = initial_peers
.into_iter()
.filter_map(|addr| {
Expand All @@ -45,6 +46,7 @@ impl RelayManager {
})
.collect();
Self {
self_peer_id,
enabled: false,
connected_relays: Default::default(),
waiting_for_reservation: Default::default(),
Expand Down Expand Up @@ -198,8 +200,18 @@ impl RelayManager {
};

if let Some(addr) = self.connected_relays.remove(&peer_id) {
info!("Removed peer form connected_relays and external address as the listener has been closed {peer_id:?}: {addr:?}");
info!("Removing connected relay server as the listener has been closed: {peer_id:?}");
info!("Removing external addr: {addr:?}");
swarm.remove_external_address(&addr);

// Even though we craft and store addrs in this format /ip4/198.51.100.0/tcp/55555/p2p/QmRelay/p2p-circuit/,
// sometimes our PeerId is added at the end by the swarm?, which we want to remove as well i.e.,
// /ip4/198.51.100.0/tcp/55555/p2p/QmRelay/p2p-circuit/p2p/QmSelf
let Ok(addr_with_self_peer_id) = addr.with_p2p(self.self_peer_id) else {
return;
};
info!("Removing external addr: {addr_with_self_peer_id:?}");
swarm.remove_external_address(&addr_with_self_peer_id);
} else if let Some(addr) = self.waiting_for_reservation.remove(&peer_id) {
info!("Removed peer form waiting_for_reservation as the listener has been closed {peer_id:?}: {addr:?}");
} else {
Expand All @@ -216,7 +228,7 @@ impl RelayManager {
false
}

// the listen addr should be something like `/ip4/198.51.100.0/tcp/55555/p2p/QmRelay/p2p-circuit/
/// The listen addr should be something like /ip4/198.51.100.0/tcp/55555/p2p/QmRelay/p2p-circuit/
fn craft_relay_address(addr: &Multiaddr, peer_id: Option<PeerId>) -> Option<Multiaddr> {
let mut output_addr = Multiaddr::empty();

Expand Down

0 comments on commit ea0b0bc

Please sign in to comment.