diff --git a/util/network-devp2p/src/host.rs b/util/network-devp2p/src/host.rs index 629b6af8a..c23527c32 100644 --- a/util/network-devp2p/src/host.rs +++ b/util/network-devp2p/src/host.rs @@ -467,8 +467,8 @@ impl Host { }; if let Some(mut discovery) = discovery { - discovery.init_node_list(self.nodes.read().unordered_entries()); - discovery.add_node_list(self.nodes.read().unordered_entries()); + discovery.init_node_list(self.nodes.read().entries()); + discovery.add_node_list(self.nodes.read().entries()); *self.discovery.lock() = Some(discovery); io.register_stream(DISCOVERY)?; io.register_timer(DISCOVERY_REFRESH, DISCOVERY_REFRESH_TIMEOUT)?; diff --git a/util/network-devp2p/src/node_table.rs b/util/network-devp2p/src/node_table.rs index c3927c694..244f518f2 100644 --- a/util/network-devp2p/src/node_table.rs +++ b/util/network-devp2p/src/node_table.rs @@ -241,24 +241,33 @@ impl NodeTable { self.nodes.insert(node.id.clone(), node); } - /// Returns node ids sorted by failure percentage, for nodes with the same failure percentage the absolute number of - /// failures is considered. - pub fn nodes(&self, filter: IpFilter) -> Vec { + fn ordered_entries(&self) -> Vec<&Node> { let mut refs: Vec<&Node> = self.nodes.values() .filter(|n| !self.useless_nodes.contains(&n.id)) - .filter(|n| n.endpoint.is_allowed(&filter)) .collect(); + refs.sort_by(|a, b| { a.failure_percentage().cmp(&b.failure_percentage()) .then_with(|| a.failures.cmp(&b.failures)) .then_with(|| b.attempts.cmp(&a.attempts)) // we use reverse ordering for number of attempts }); - refs.into_iter().map(|n| n.id).collect() + + refs } - /// Unordered list of all entries - pub fn unordered_entries(&self) -> Vec { - self.nodes.values().map(|n| NodeEntry { + /// Returns node ids sorted by failure percentage, for nodes with the same failure percentage the absolute number of + /// failures is considered. + pub fn nodes(&self, filter: IpFilter) -> Vec { + self.ordered_entries().iter() + .filter(|n| n.endpoint.is_allowed(&filter)) + .map(|n| n.id) + .collect() + } + + /// Ordered list of all entries by failure percentage, for nodes with the same failure percentage the absolute + /// number of failures is considered. + pub fn entries(&self) -> Vec { + self.ordered_entries().iter().map(|n| NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone(), }).collect()