From a615dacf6470e3aec4bed063236914f672d01e37 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Mon, 13 Jan 2020 16:54:40 +0000 Subject: [PATCH] Defined Ord (+ required traits) for NodeScore --- validator/src/validator/health_check/score.rs | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/validator/src/validator/health_check/score.rs b/validator/src/validator/health_check/score.rs index 6f64c74647..ecb7211047 100644 --- a/validator/src/validator/health_check/score.rs +++ b/validator/src/validator/health_check/score.rs @@ -1,10 +1,11 @@ use log::error; use sphinx::route::NodeAddressBytes; +use std::cmp::Ordering; use std::fmt::Formatter; use std::net::SocketAddr; use topology::{MixNode, MixProviderNode}; -#[derive(Debug)] +#[derive(Debug, Eq)] pub(crate) struct NodeScore { pub_key: NodeAddressBytes, addresses: Vec, @@ -14,6 +15,56 @@ pub(crate) struct NodeScore { packets_received: u64, } +impl Ord for NodeScore { + // order by: version, layer, sent, received, pubkey; ignore addresses + fn cmp(&self, other: &Self) -> Ordering { + if self.version > other.version { + return Ordering::Greater; + } else if self.version < other.version { + return Ordering::Less; + } + if self.layer > other.layer { + return Ordering::Greater; + } else if self.layer < other.layer { + return Ordering::Less; + } + if self.packets_sent > other.packets_sent { + return Ordering::Greater; + } else if self.packets_sent < other.packets_sent { + return Ordering::Less; + } + if self.packets_received > other.packets_received { + return Ordering::Greater; + } else if self.packets_received < other.packets_received { + return Ordering::Less; + } + if self.pub_key > other.pub_key { + return Ordering::Greater; + } else if self.pub_key < other.pub_key { + return Ordering::Less; + } + + Ordering::Equal + } +} + +impl PartialOrd for NodeScore { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for NodeScore { + fn eq(&self, other: &Self) -> bool { + self.pub_key == other.pub_key + && self.addresses == other.addresses + && self.version == other.version + && self.layer == other.layer + && self.packets_sent == other.packets_sent + && self.packets_received == other.packets_received + } +} + impl NodeScore { pub(crate) fn from_mixnode(node: MixNode) -> Self { NodeScore {