Skip to content

Commit

Permalink
fix: filter out disconnected nodes on Node response
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML committed Nov 26, 2023
1 parent dd1b670 commit c8ebd0e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion portalnet/src/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,19 @@ where
.collect()
}

/// Returns a vector of all the ENRs of nodes currently contained in the routing table which are connected.
fn table_entries_enr_connected(&self) -> Vec<Enr> {
self.kbuckets
.write()
.iter()
.filter(|entry| {
// Filter out disconnected nodes.
entry.status.is_connected()
})
.map(|entry| entry.node.value.enr())
.collect()
}

/// Returns a vector of the ENRs of the closest nodes by the given log2 distances.
fn nodes_by_distance(&self, mut log2_distances: Vec<u64>) -> Vec<SszEnr> {
let mut nodes_to_send = Vec::new();
Expand All @@ -2387,6 +2400,10 @@ where
for node in kbuckets
.nodes_by_distances(log2_distances, FIND_NODES_MAX_NODES)
.into_iter()
.filter(|entry| {
// Filter out disconnected nodes.
entry.status.is_connected()
})
.map(|entry| entry.node.value.clone())
{
nodes_to_send.push(SszEnr::new(node.enr()));
Expand All @@ -2403,7 +2420,7 @@ where
let content_id = content_key.content_id();

let mut nodes_with_distance: Vec<(Distance, Enr)> = self
.table_entries_enr()
.table_entries_enr_connected()
.into_iter()
.map(|enr| (TMetric::distance(&content_id, &enr.node_id().raw()), enr))
.collect();
Expand Down

0 comments on commit c8ebd0e

Please # to comment.