Skip to content

Commit

Permalink
feat(discv4): add soft_remove_node (paradigmxyz#11970)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 22, 2024
1 parent 22171d2 commit 8bfbd97
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/net/discv4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ serde = { workspace = true, optional = true }
[dev-dependencies]
assert_matches.workspace = true
rand.workspace = true
tokio = { workspace = true, features = ["macros"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
reth-tracing.workspace = true

[features]
Expand Down
26 changes: 19 additions & 7 deletions crates/net/discv4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,24 @@ impl Discv4Service {
/// table. Returns `true` if the node was in the table and `false` otherwise.
pub fn remove_node(&mut self, node_id: PeerId) -> bool {
let key = kad_key(node_id);
self.remove_key(node_id, key)
}

/// Removes a `node_id` from the routing table but only if there are enough other nodes in the
/// bucket (bucket must be at least half full)
///
/// Returns `true` if the node was removed
pub fn soft_remove_node(&mut self, node_id: PeerId) -> bool {
let key = kad_key(node_id);
let Some(bucket) = self.kbuckets.get_bucket(&key) else { return false };
if bucket.num_entries() < MAX_NODES_PER_BUCKET / 2 {
// skip half empty bucket
return false;
}
self.remove_key(node_id, key)
}

fn remove_key(&mut self, node_id: PeerId, key: discv5::Key<NodeKey>) -> bool {
let removed = self.kbuckets.remove(&key);
if removed {
trace!(target: "discv4", ?node_id, "removed node");
Expand Down Expand Up @@ -1491,13 +1509,7 @@ impl Discv4Service {
// the table, but only if there are enough other nodes in the bucket (bucket must be at
// least half full)
if failures > (self.config.max_find_node_failures as usize) {
if let Some(bucket) = self.kbuckets.get_bucket(&key) {
if bucket.num_entries() < MAX_NODES_PER_BUCKET / 2 {
// skip half empty bucket
continue
}
}
self.remove_node(node_id);
self.soft_remove_node(node_id);
}
}
}
Expand Down

0 comments on commit 8bfbd97

Please sign in to comment.