From c634bd4232c1c7162bfc97d66a70a07232c5b960 Mon Sep 17 00:00:00 2001 From: Gusto Date: Thu, 7 Nov 2024 05:52:15 +0200 Subject: [PATCH] Analyze method in node trait --- network-runner/config/mixnode.json | 7 ++++++- network-runner/src/node/mix/mod.rs | 11 ++++++++++- network-runner/src/node/mod.rs | 7 +++++++ network-runner/src/warding/mod.rs | 4 ++-- network-runner/src/warding/ttf.rs | 17 ++++++++--------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/network-runner/config/mixnode.json b/network-runner/config/mixnode.json index 957af74..b7e91f6 100644 --- a/network-runner/config/mixnode.json +++ b/network-runner/config/mixnode.json @@ -27,5 +27,10 @@ }, "node_count": 3, "seed": 0, - "record_settings": {} + "record_settings": {}, + "wards": [ + { + "max": 10 + } + ] } diff --git a/network-runner/src/node/mix/mod.rs b/network-runner/src/node/mix/mod.rs index 9e70381..a56a1b4 100644 --- a/network-runner/src/node/mix/mod.rs +++ b/network-runner/src/node/mix/mod.rs @@ -3,7 +3,10 @@ pub mod state; mod stream_wrapper; use super::{Node, NodeId}; -use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize}; +use crate::{ + network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize}, + warding::Ward, +}; use crossbeam::channel; use futures::Stream; use multiaddr::Multiaddr; @@ -209,4 +212,10 @@ impl Node for MixNode { self.state.step_id += 1; self.state.mock_counter += 1; } + + fn analyze(&self, ward: Ward) -> bool { + match ward { + Ward::Max(condition) => self.state.mock_counter >= condition.max_count, + } + } } diff --git a/network-runner/src/node/mod.rs b/network-runner/src/node/mod.rs index aab90c9..822382c 100644 --- a/network-runner/src/node/mod.rs +++ b/network-runner/src/node/mod.rs @@ -11,6 +11,8 @@ use std::{ // crates use parking_lot::RwLock; use serde::{Deserialize, Serialize}; + +use crate::warding::Ward; // internal #[serde_with::serde_as] @@ -89,6 +91,7 @@ pub trait Node { fn id(&self) -> NodeId; fn state(&self) -> &Self::State; fn step(&mut self, elapsed: Duration); + fn analyze(&self, ward: Ward) -> bool; } #[derive( @@ -160,6 +163,10 @@ impl Node for usize { use std::ops::AddAssign; self.add_assign(1); } + + fn analyze(&self, _: Ward) -> bool { + todo!() + } } pub trait NodeIdExt { diff --git a/network-runner/src/warding/mod.rs b/network-runner/src/warding/mod.rs index 4241425..8d22ccf 100644 --- a/network-runner/src/warding/mod.rs +++ b/network-runner/src/warding/mod.rs @@ -33,7 +33,7 @@ pub trait SimulationWard { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Ward { - MaxView(ttf::MaxViewWard), + Max(ttf::MaxWard), } impl Ward { @@ -41,7 +41,7 @@ impl Ward { &mut self, ) -> &mut dyn SimulationWard> { match self { - Ward::MaxView(ward) => ward, + Ward::Max(ward) => ward, } } } diff --git a/network-runner/src/warding/ttf.rs b/network-runner/src/warding/ttf.rs index ac2f7d9..fb85c07 100644 --- a/network-runner/src/warding/ttf.rs +++ b/network-runner/src/warding/ttf.rs @@ -1,3 +1,4 @@ +use super::Ward::Max; use crate::warding::{SimulationState, SimulationWard}; use serde::{Deserialize, Serialize}; @@ -5,29 +6,27 @@ use serde::{Deserialize, Serialize}; /// the set threshold. #[derive(Debug, Serialize, Deserialize, Copy, Clone)] #[serde(transparent)] -pub struct MaxViewWard { - max_count: usize, +pub struct MaxWard { + pub max_count: usize, } -impl SimulationWard for MaxViewWard { +impl SimulationWard for MaxWard { type SimulationState = SimulationState; - fn analyze(&mut self, _state: &Self::SimulationState) -> bool { - // state.nodes.read().iter(); - //.all(|n| n.current_view() >= self.max_count) - todo!() + fn analyze(&mut self, state: &Self::SimulationState) -> bool { + state.nodes.read().iter().all(|n| n.analyze(Max(*self))) } } #[cfg(test)] mod test { - use crate::warding::ttf::MaxViewWard; + use crate::warding::ttf::MaxWard; use crate::warding::{SimulationState, SimulationWard}; use parking_lot::RwLock; use std::sync::Arc; #[test] fn rebase_threshold() { - let mut ttf = MaxViewWard { max_count: 10 }; + let mut ttf = MaxWard { max_count: 10 }; let node = 11; let state = SimulationState {