Added node type enum to NodeScore
This commit is contained in:
@@ -5,8 +5,26 @@ use std::fmt::Formatter;
|
||||
use std::net::SocketAddr;
|
||||
use topology::{MixNode, MixProviderNode};
|
||||
|
||||
// TODO: should 'nodetype' really be part of healthcheck::score
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub(crate) enum NodeType {
|
||||
Mix,
|
||||
MixProvider,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NodeType {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
||||
match self {
|
||||
NodeType::Mix => write!(f, "Mix"),
|
||||
NodeType::MixProvider => write!(f, "MixProvider"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq)]
|
||||
pub(crate) struct NodeScore {
|
||||
typ: NodeType,
|
||||
pub_key: NodeAddressBytes,
|
||||
addresses: Vec<SocketAddr>,
|
||||
version: String,
|
||||
@@ -18,6 +36,11 @@ pub(crate) struct NodeScore {
|
||||
impl Ord for NodeScore {
|
||||
// order by: version, layer, sent, received, pubkey; ignore addresses
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self.typ > other.typ {
|
||||
return Ordering::Greater;
|
||||
} else if self.typ < other.typ {
|
||||
return Ordering::Less;
|
||||
}
|
||||
if self.version > other.version {
|
||||
return Ordering::Greater;
|
||||
} else if self.version < other.version {
|
||||
@@ -56,7 +79,8 @@ impl PartialOrd for NodeScore {
|
||||
|
||||
impl PartialEq for NodeScore {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.pub_key == other.pub_key
|
||||
self.typ == other.typ
|
||||
&& self.pub_key == other.pub_key
|
||||
&& self.addresses == other.addresses
|
||||
&& self.version == other.version
|
||||
&& self.layer == other.layer
|
||||
@@ -68,6 +92,7 @@ impl PartialEq for NodeScore {
|
||||
impl NodeScore {
|
||||
pub(crate) fn from_mixnode(node: MixNode) -> Self {
|
||||
NodeScore {
|
||||
typ: NodeType::Mix,
|
||||
pub_key: NodeAddressBytes::from_b64_string(node.pub_key),
|
||||
addresses: vec![node.host],
|
||||
version: node.version,
|
||||
@@ -79,6 +104,7 @@ impl NodeScore {
|
||||
|
||||
pub(crate) fn from_provider(node: MixProviderNode) -> Self {
|
||||
NodeScore {
|
||||
typ: NodeType::MixProvider,
|
||||
pub_key: NodeAddressBytes::from_b64_string(node.pub_key),
|
||||
addresses: vec![node.mixnet_listener, node.client_listener],
|
||||
version: node.version,
|
||||
@@ -95,7 +121,10 @@ impl NodeScore {
|
||||
pub(crate) fn increase_received_packet_count(&mut self) {
|
||||
self.packets_received += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn typ(&self) -> NodeType {
|
||||
self.typ
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NodeScore {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
|
||||
@@ -110,14 +139,11 @@ impl std::fmt::Display for NodeScore {
|
||||
return Err(std::fmt::Error);
|
||||
}
|
||||
};
|
||||
let health_percentage = match self.packets_sent {
|
||||
0 => 0.0,
|
||||
_ => (self.packets_received as f64 / self.packets_sent as f64) * 100.0,
|
||||
};
|
||||
let stringified_key = self.pub_key.to_b64_string();
|
||||
write!(
|
||||
f,
|
||||
"{}/{}\t({}%)\t|| {}\tv{} <{}> - {}",
|
||||
"({})\t{}/{}\t({}%)\t|| {}\tv{} <{}> - {}",
|
||||
self.typ,
|
||||
self.packets_received,
|
||||
self.packets_sent,
|
||||
health_percentage,
|
||||
|
||||
Reference in New Issue
Block a user