Skip to content

Commit

Permalink
Merge pull request #2244 from subspace/restore-removal-of-outdated-ad…
Browse files Browse the repository at this point in the history
…dresses

Restore removal of old addresses
  • Loading branch information
nazar-pc authored Nov 21, 2023
2 parents 7a42947 + 1e478b2 commit 85129f8
Showing 1 changed file with 58 additions and 40 deletions.
98 changes: 58 additions & 40 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,48 +731,55 @@ where
});

if full_kademlia_support {
//TODO: Consider restoring obsolete address removal
// let old_addresses = kademlia
// .kbucket(peer_id)
// .and_then(|peers| {
// let key = peer_id.into();
// peers.iter().find_map(|peer| {
// (peer.node.key == &key).then_some(
// peer.node
// .value
// .iter()
// .filter(|address| info.listen_addrs.contains(address))
// .cloned()
// .collect::<Vec<_>>(),
// )
// })
// })
// .unwrap_or_default();

// for old_address in old_addresses {
// trace!(
// %local_peer_id,
// %peer_id,
// %old_address,
// "Removing old self-reported address from Kademlia DHT",
// );
//
// kademlia.remove_address(&peer_id, &old_address);
// }
let received_addresses = info
.listen_addrs
.into_iter()
.filter(|address| {
if self.allow_non_global_addresses_in_dht
|| is_global_address_or_dns(address)
{
true
} else {
trace!(
%local_peer_id,
%peer_id,
%address,
"Ignoring self-reported non-global address",
);

for address in info.listen_addrs {
if !self.allow_non_global_addresses_in_dht
&& !is_global_address_or_dns(&address)
{
trace!(
%local_peer_id,
%peer_id,
%address,
"Ignoring self-reported non-global address",
);
continue;
}
false
}
})
.collect::<Vec<_>>();
let received_address_strings = received_addresses
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
let old_addresses = kademlia
.kbucket(peer_id)
.and_then(|peers| {
let key = peer_id.into();
peers.iter().find_map(|peer| {
(peer.node.key == &key).then_some(
peer.node
.value
.iter()
.filter(|existing_address| {
let existing_address = existing_address.to_string();

!received_address_strings.iter().any(|received_address| {
received_address.starts_with(&existing_address)
|| existing_address.starts_with(received_address)
})
})
.cloned()
.collect::<Vec<_>>(),
)
})
})
.unwrap_or_default();

for address in received_addresses {
debug!(
%local_peer_id,
%peer_id,
Expand All @@ -783,6 +790,17 @@ where

kademlia.add_address(&peer_id, address);
}

for old_address in old_addresses {
trace!(
%local_peer_id,
%peer_id,
%old_address,
"Removing old self-reported address from Kademlia DHT",
);

kademlia.remove_address(&peer_id, &old_address);
}
} else {
debug!(
%local_peer_id,
Expand Down

0 comments on commit 85129f8

Please sign in to comment.