From 1be6fac660b46ba6b8fb529a5b1f64719553ed01 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Sat, 18 Nov 2023 10:37:18 +0200 Subject: [PATCH 1/3] Restore removal of old addresses --- crates/subspace-networking/src/node_runner.rs | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/crates/subspace-networking/src/node_runner.rs b/crates/subspace-networking/src/node_runner.rs index 586406fedf..05795a32e4 100644 --- a/crates/subspace-networking/src/node_runner.rs +++ b/crates/subspace-networking/src/node_runner.rs @@ -733,34 +733,22 @@ 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::>(), - // ) - // }) - // }) - // .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 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::>(), + ) + }) + }) + .unwrap_or_default(); for address in info.listen_addrs { if !self.allow_non_global_addresses_in_dht @@ -785,6 +773,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, From 185767c8016565cb2a204d35f1d90e6d5b042090 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Sat, 18 Nov 2023 19:31:52 +0200 Subject: [PATCH 2/3] Fix removal of addresses from Kademlia to account for identity in multiaddress --- crates/subspace-networking/src/node_runner.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/subspace-networking/src/node_runner.rs b/crates/subspace-networking/src/node_runner.rs index 05795a32e4..862be18c1a 100644 --- a/crates/subspace-networking/src/node_runner.rs +++ b/crates/subspace-networking/src/node_runner.rs @@ -733,6 +733,11 @@ where }); if full_kademlia_support { + let received_address_strings = info + .listen_addrs + .iter() + .map(ToString::to_string) + .collect::>(); let old_addresses = kademlia .kbucket(peer_id) .and_then(|peers| { @@ -742,7 +747,14 @@ where peer.node .value .iter() - .filter(|address| !info.listen_addrs.contains(address)) + .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::>(), ) From 1e478b2f8642d077b31afc803b5d108502438516 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Sat, 18 Nov 2023 19:36:27 +0200 Subject: [PATCH 3/3] Filter private addresses early to do less work overall --- crates/subspace-networking/src/node_runner.rs | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/crates/subspace-networking/src/node_runner.rs b/crates/subspace-networking/src/node_runner.rs index 862be18c1a..a7f9858563 100644 --- a/crates/subspace-networking/src/node_runner.rs +++ b/crates/subspace-networking/src/node_runner.rs @@ -733,8 +733,27 @@ where }); if full_kademlia_support { - let received_address_strings = info + 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", + ); + + false + } + }) + .collect::>(); + let received_address_strings = received_addresses .iter() .map(ToString::to_string) .collect::>(); @@ -762,19 +781,7 @@ where }) .unwrap_or_default(); - 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; - } - + for address in received_addresses { debug!( %local_peer_id, %peer_id,