Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
[client/network] remove peer entry from ephemeral_addresses (#13084)
Browse files Browse the repository at this point in the history
* [client/network] remove peer entry from `ephemeral_addresses`

if there are no addresses associated with that peer

* refactor as per @bkchr suggestion

#13084 (comment)

* add missing import

* fix error
  • Loading branch information
melekes authored and Ank4n committed Feb 28, 2023
1 parent 3525b7d commit 70cfde8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use sc_network_common::{config::ProtocolId, utils::LruHashSet};
use sp_core::hexdisplay::HexDisplay;
use std::{
cmp,
collections::{HashMap, HashSet, VecDeque},
collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
num::NonZeroUsize,
task::{Context, Poll},
time::Duration,
Expand Down Expand Up @@ -318,14 +318,16 @@ impl DiscoveryBehaviour {
/// If we didn't know this address before, also generates a `Discovered` event.
pub fn add_known_address(&mut self, peer_id: PeerId, addr: Multiaddr) {
let addrs_list = self.ephemeral_addresses.entry(peer_id).or_default();
if !addrs_list.iter().any(|a| *a == addr) {
if let Some(k) = self.kademlia.as_mut() {
k.add_address(&peer_id, addr.clone());
}
if addrs_list.contains(&addr) {
return
}

self.pending_events.push_back(DiscoveryOut::Discovered(peer_id));
addrs_list.push(addr);
if let Some(k) = self.kademlia.as_mut() {
k.add_address(&peer_id, addr.clone());
}

self.pending_events.push_back(DiscoveryOut::Discovered(peer_id));
addrs_list.push(addr);
}

/// Add a self-reported address of a remote peer to the k-buckets of the DHT
Expand Down Expand Up @@ -456,7 +458,7 @@ pub enum DiscoveryOut {

/// The DHT yielded results for the record request.
///
/// Returning the result grouped in (key, value) pairs as well as the request duration..
/// Returning the result grouped in (key, value) pairs as well as the request duration.
ValueFound(Vec<(record::Key, Vec<u8>)>, Duration),

/// The record requested was not found in the DHT.
Expand Down Expand Up @@ -535,9 +537,13 @@ impl NetworkBehaviour for DiscoveryBehaviour {
FromSwarm::DialFailure(e @ DialFailure { peer_id, error, .. }) => {
if let Some(peer_id) = peer_id {
if let DialError::Transport(errors) = error {
if let Some(list) = self.ephemeral_addresses.get_mut(&peer_id) {
if let Entry::Occupied(mut entry) = self.ephemeral_addresses.entry(peer_id)
{
for (addr, _error) in errors {
list.retain(|a| a != addr);
entry.get_mut().retain(|a| a != addr);
}
if entry.get().is_empty() {
entry.remove();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ where
self.network_service.behaviour().user_protocol().num_sync_requests()
}

/// Adds an address for a node.
/// Adds an address known to a node.
pub fn add_known_address(&mut self, peer_id: PeerId, addr: Multiaddr) {
self.network_service.behaviour_mut().add_known_address(peer_id, addr);
}
Expand Down

0 comments on commit 70cfde8

Please sign in to comment.