Skip to content

Commit

Permalink
add confirmed external address
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 14, 2024
1 parent 8674d6b commit f93973b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
20 changes: 15 additions & 5 deletions network/core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ where
},
SwarmEvent::ExternalAddrConfirmed { address } => {
info!(target: LOG_TARGET, "🌍️ External address confirmed: {}", address);
self.swarm
.behaviour_mut()
.peer_sync
.add_known_local_public_addresses([address]);
},
SwarmEvent::ExternalAddrExpired { address } => {
info!(target: LOG_TARGET, "🌍️ External address expired: {}", address);
if is_relayed_address(&address) {
self.attempt_relay_reservation();
}
},
SwarmEvent::Dialing { peer_id, connection_id } => {
if let Some(peer_id) = peer_id {
Expand Down Expand Up @@ -980,7 +990,7 @@ where
self.swarm
.behaviour_mut()
.peer_sync
.add_known_local_public_addresses(vec![public_address]);
.add_known_local_public_addresses([public_address]);
}

self.autonat_status_sender.send_if_modified(|prev| {
Expand Down Expand Up @@ -1159,7 +1169,7 @@ where
.map(|conns| {
conns
.iter()
.any(|c| c.endpoint.is_dialer() && is_through_relay_address(c.endpoint.get_remote_address()))
.any(|c| c.endpoint.is_dialer() && is_relayed_address(c.endpoint.get_remote_address()))
})
.unwrap_or(false);

Expand All @@ -1174,7 +1184,7 @@ where
let _ignore = self
.swarm
.dial(DialOpts::peer_id(peer_id).addresses(vec![address.clone()]).build());
} else if is_relay && !is_through_relay_address(&address) {
} else if is_relay && !is_relayed_address(&address) {
// Otherwise, if the peer advertises as a relay we'll add them
info!(target: LOG_TARGET, "📡 Adding peer {peer_id} {address} as a relay");
self.relays.add_possible_relay(peer_id, address.clone());
Expand Down Expand Up @@ -1279,7 +1289,7 @@ where
self.swarm
.behaviour_mut()
.peer_sync
.add_known_local_public_addresses(vec![circuit_addr]);
.add_known_local_public_addresses([circuit_addr]);
info!(target: LOG_TARGET, "🌍️ Peer {peer_id} is a relay. Listening (id={id:?}) for circuit connections");
let Some(relay_mut) = self.relays.selected_relay_mut() else {
// unreachable
Expand Down Expand Up @@ -1376,7 +1386,7 @@ fn is_p2p_address(address: &Multiaddr) -> bool {
address.iter().any(|p| matches!(p, Protocol::P2p(_)))
}

fn is_through_relay_address(address: &Multiaddr) -> bool {
fn is_relayed_address(address: &Multiaddr) -> bool {
let mut found_p2p_circuit = false;
for protocol in address {
if !found_p2p_circuit {
Expand Down
13 changes: 2 additions & 11 deletions network/libp2p-peersync/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,8 @@ where TPeerStore: PeerStore
&self.local_peer_record
}

pub fn add_known_local_public_addresses(&mut self, addrs: Vec<Multiaddr>) {
if addrs.is_empty() {
return;
}

let mut is_any_new = false;
for addr in addrs {
is_any_new |= self.local_peer_record.add_address(addr.clone());
}

if is_any_new {
pub fn add_known_local_public_addresses<I: IntoIterator<Item = Multiaddr>>(&mut self, addrs: I) {
if self.local_peer_record.add_addresses(addrs) {
self.handle_update_local_record();
}
}
Expand Down
13 changes: 13 additions & 0 deletions network/libp2p-peersync/src/peer_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ impl LocalPeerRecord {
false
}

pub fn add_addresses<I: IntoIterator<Item = Multiaddr>>(&mut self, addresses: I) -> bool {
let mut any_changed = false;
self.addresses.reserve(addresses.len());
for addr in addresses {
any_changed |= self.addresses.insert(addr);
}
if any_changed {
// Sign only if any addresses were added
self.sign();
}
any_changed
}

pub fn remove_address(&mut self, address: &Multiaddr) -> bool {
if self.addresses.remove(address) {
self.sign();
Expand Down

0 comments on commit f93973b

Please sign in to comment.