From 810ec02ee98f42cdbca87d6a6f767fa72199d9a1 Mon Sep 17 00:00:00 2001 From: gusto Date: Thu, 7 Nov 2024 06:49:06 +0200 Subject: [PATCH] Netrunner alanyzable node (#27) * Analyze method in node trait * Sum ward condition --- network-runner/config/mixnode.json | 10 ++++++- network-runner/src/node/mix/mod.rs | 15 ++++++++++- network-runner/src/node/mod.rs | 7 +++++ network-runner/src/warding/mod.rs | 11 ++++++-- network-runner/src/warding/ttf.rs | 42 ++++++++++++++++++++++++------ 5 files changed, 73 insertions(+), 12 deletions(-) diff --git a/network-runner/config/mixnode.json b/network-runner/config/mixnode.json index 957af74..ff47f3d 100644 --- a/network-runner/config/mixnode.json +++ b/network-runner/config/mixnode.json @@ -27,5 +27,13 @@ }, "node_count": 3, "seed": 0, - "record_settings": {} + "record_settings": {}, + "wards": [ + { + "max": 10 + }, + { + "sum": 10 + } + ] } diff --git a/network-runner/src/node/mix/mod.rs b/network-runner/src/node/mix/mod.rs index 1b7b129..694378e 100644 --- a/network-runner/src/node/mix/mod.rs +++ b/network-runner/src/node/mix/mod.rs @@ -5,7 +5,10 @@ pub mod state; mod stream_wrapper; use super::{Node, NodeId}; -use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize}; +use crate::{ + network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize}, + warding::WardCondition, +}; use crossbeam::channel; use futures::Stream; use lottery::StakeLottery; @@ -261,4 +264,14 @@ impl Node for MixNode { self.state.step_id += 1; self.state.mock_counter += 1; } + + fn analyze(&self, ward: &mut WardCondition) -> bool { + match ward { + WardCondition::Max(condition) => self.state.mock_counter >= condition.max_count, + WardCondition::Sum(condition) => { + *condition.step_result.borrow_mut() += self.state.mock_counter; + false + } + } + } } diff --git a/network-runner/src/node/mod.rs b/network-runner/src/node/mod.rs index aab90c9..83f652f 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::WardCondition; // 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: &mut WardCondition) -> bool; } #[derive( @@ -160,6 +163,10 @@ impl Node for usize { use std::ops::AddAssign; self.add_assign(1); } + + fn analyze(&self, _: &mut WardCondition) -> bool { + todo!() + } } pub trait NodeIdExt { diff --git a/network-runner/src/warding/mod.rs b/network-runner/src/warding/mod.rs index 4241425..a91409b 100644 --- a/network-runner/src/warding/mod.rs +++ b/network-runner/src/warding/mod.rs @@ -33,7 +33,13 @@ pub trait SimulationWard { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Ward { - MaxView(ttf::MaxViewWard), + Max(ttf::MaxWard), + Sum(ttf::SumWard), +} + +pub enum WardCondition<'a> { + Max(&'a ttf::MaxWard), + Sum(&'a ttf::SumWardCondition), } impl Ward { @@ -41,7 +47,8 @@ impl Ward { &mut self, ) -> &mut dyn SimulationWard> { match self { - Ward::MaxView(ward) => ward, + Ward::Max(ward) => ward, + Ward::Sum(ward) => ward, } } } diff --git a/network-runner/src/warding/ttf.rs b/network-runner/src/warding/ttf.rs index ac2f7d9..750126c 100644 --- a/network-runner/src/warding/ttf.rs +++ b/network-runner/src/warding/ttf.rs @@ -1,3 +1,6 @@ +use std::cell::RefCell; + +use super::WardCondition::{self, Max}; use crate::warding::{SimulationState, SimulationWard}; use serde::{Deserialize, Serialize}; @@ -5,29 +8,52 @@ use serde::{Deserialize, Serialize}; /// the set threshold. #[derive(Debug, Serialize, Deserialize, Copy, Clone)] #[serde(transparent)] -pub struct MaxViewWard { +pub struct MaxWard { + pub max_count: usize, +} + +impl SimulationWard for MaxWard { + type SimulationState = SimulationState; + fn analyze(&mut self, state: &Self::SimulationState) -> bool { + state.nodes.read().iter().all(|n| n.analyze(&mut Max(self))) + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(transparent)] +pub struct SumWard { max_count: usize, } -impl SimulationWard for MaxViewWard { +pub struct SumWardCondition { + pub step_result: RefCell, +} + +impl SimulationWard for SumWard { 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 { + let nodes = state.nodes.read(); + let condition = SumWardCondition { + step_result: RefCell::new(0), + }; + for node in nodes.iter() { + node.analyze(&mut WardCondition::Sum(&condition)); + } + let result = condition.step_result.borrow(); + *result > self.max_count } } #[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 {