From 7f6f366e51ecb589471740fba3e212d320b0f184 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Thu, 9 Jan 2020 18:01:28 +0000 Subject: [PATCH] Logic for calculating health --- validator/src/validator/health_check/mod.rs | 66 ++++++++++++++++----- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/validator/src/validator/health_check/mod.rs b/validator/src/validator/health_check/mod.rs index cdf20c057a..7c1908c6f0 100644 --- a/validator/src/validator/health_check/mod.rs +++ b/validator/src/validator/health_check/mod.rs @@ -14,6 +14,7 @@ use topology::{MixNode, MixProviderNode, NymTopology, NymTopologyError}; pub enum HealthCheckerError { FailedToObtainTopologyError, InvalidTopologyError, + UnknownError, } impl From for HealthCheckerError { @@ -55,8 +56,54 @@ impl HealthCheckResult { HealthCheckResult(health) } - fn calculate(topology: T) -> Self { - HealthCheckResult(Vec::new()) + fn check_path(path: Vec) -> bool { + trace!("Checking path: {:?}", path); + // TODO: + false + } + + pub fn calculate(topology: T) -> Self { + let all_paths = match current_topology.all_paths() { + Ok(paths) => paths, + Err(_) => return Self::zero_score(topology), + }; + + // create entries for all nodes + let mut score_map = HashMap::new(); + current_topology + .get_mix_nodes() + .into_iter() + .for_each(|node| { + score_map.insert( + NodeAddressBytes::from_b64_string(node.pub_key.clone()).0, + NodeScore::from_mixnode(node), + ); + }); + + current_topology + .get_mix_provider_nodes() + .into_iter() + .for_each(|node| { + score_map.insert( + NodeAddressBytes::from_b64_string(node.pub_key.clone()).0, + NodeScore::from_provider(node), + ); + }); + + for path in all_paths { + let path_status = HealthCheckResult::check_path(path); + for node in path { + // if value doesn't exist, something extremely weird must have happened + let current_score = score_map.get_mut(&node.pub_key.0); + if current_score.is_none() { + return Self::zero_score(topology); + } + let current_score = current_score.unwrap(); + current_score.increase_packet_count(path_status); + } + } + + HealthCheckResult(score_map.values().collect()) } } @@ -90,9 +137,9 @@ impl NodeScore { } } - fn test_packet(&mut self, was_successful: bool) { + fn increase_packet_count(&mut self, was_delivered: bool) { self.packets_sent += 1; - if was_successful { + if was_delivered { self.packets_received += 1; } } @@ -149,24 +196,15 @@ impl HealthChecker { } } - fn check_path(path: Vec) -> bool { - false - } - fn do_check(&self) -> Result { trace!("going to perform a healthcheck!"); let current_topology = match self.directory_client.presence_topology.get() { Ok(topology) => topology, Err(_) => return Err(HealthCheckerError::FailedToObtainTopologyError), }; - trace!("current topology: {:?}", current_topology); - let all_paths = match current_topology.all_paths() { - Ok(paths) => paths, - Err(_) => return Ok(HealthCheckResult::zero_score(current_topology)), - }; - Ok(HealthCheckResult::zero_score(current_topology)) + Ok(HealthCheckResult::calculate(current_topology)) } pub async fn run(self) -> Result<(), HealthCheckerError> {