Analyze method in node trait

This commit is contained in:
Gusto 2024-11-07 05:52:15 +02:00
parent 29f78e026e
commit c634bd4232
No known key found for this signature in database
5 changed files with 33 additions and 13 deletions

View File

@ -27,5 +27,10 @@
}, },
"node_count": 3, "node_count": 3,
"seed": 0, "seed": 0,
"record_settings": {} "record_settings": {},
"wards": [
{
"max": 10
}
]
} }

View File

@ -3,7 +3,10 @@ pub mod state;
mod stream_wrapper; mod stream_wrapper;
use super::{Node, NodeId}; use super::{Node, NodeId};
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize}; use crate::{
network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize},
warding::Ward,
};
use crossbeam::channel; use crossbeam::channel;
use futures::Stream; use futures::Stream;
use multiaddr::Multiaddr; use multiaddr::Multiaddr;
@ -209,4 +212,10 @@ impl Node for MixNode {
self.state.step_id += 1; self.state.step_id += 1;
self.state.mock_counter += 1; self.state.mock_counter += 1;
} }
fn analyze(&self, ward: Ward) -> bool {
match ward {
Ward::Max(condition) => self.state.mock_counter >= condition.max_count,
}
}
} }

View File

@ -11,6 +11,8 @@ use std::{
// crates // crates
use parking_lot::RwLock; use parking_lot::RwLock;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::warding::Ward;
// internal // internal
#[serde_with::serde_as] #[serde_with::serde_as]
@ -89,6 +91,7 @@ pub trait Node {
fn id(&self) -> NodeId; fn id(&self) -> NodeId;
fn state(&self) -> &Self::State; fn state(&self) -> &Self::State;
fn step(&mut self, elapsed: Duration); fn step(&mut self, elapsed: Duration);
fn analyze(&self, ward: Ward) -> bool;
} }
#[derive( #[derive(
@ -160,6 +163,10 @@ impl Node for usize {
use std::ops::AddAssign; use std::ops::AddAssign;
self.add_assign(1); self.add_assign(1);
} }
fn analyze(&self, _: Ward) -> bool {
todo!()
}
} }
pub trait NodeIdExt { pub trait NodeIdExt {

View File

@ -33,7 +33,7 @@ pub trait SimulationWard<S, T> {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum Ward { pub enum Ward {
MaxView(ttf::MaxViewWard), Max(ttf::MaxWard),
} }
impl Ward { impl Ward {
@ -41,7 +41,7 @@ impl Ward {
&mut self, &mut self,
) -> &mut dyn SimulationWard<S, T, SimulationState = SimulationState<S, T>> { ) -> &mut dyn SimulationWard<S, T, SimulationState = SimulationState<S, T>> {
match self { match self {
Ward::MaxView(ward) => ward, Ward::Max(ward) => ward,
} }
} }
} }

View File

@ -1,3 +1,4 @@
use super::Ward::Max;
use crate::warding::{SimulationState, SimulationWard}; use crate::warding::{SimulationState, SimulationWard};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -5,29 +6,27 @@ use serde::{Deserialize, Serialize};
/// the set threshold. /// the set threshold.
#[derive(Debug, Serialize, Deserialize, Copy, Clone)] #[derive(Debug, Serialize, Deserialize, Copy, Clone)]
#[serde(transparent)] #[serde(transparent)]
pub struct MaxViewWard { pub struct MaxWard {
max_count: usize, pub max_count: usize,
} }
impl<S, T> SimulationWard<S, T> for MaxViewWard { impl<S, T> SimulationWard<S, T> for MaxWard {
type SimulationState = SimulationState<S, T>; type SimulationState = SimulationState<S, T>;
fn analyze(&mut self, _state: &Self::SimulationState) -> bool { fn analyze(&mut self, state: &Self::SimulationState) -> bool {
// state.nodes.read().iter(); state.nodes.read().iter().all(|n| n.analyze(Max(*self)))
//.all(|n| n.current_view() >= self.max_count)
todo!()
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::warding::ttf::MaxViewWard; use crate::warding::ttf::MaxWard;
use crate::warding::{SimulationState, SimulationWard}; use crate::warding::{SimulationState, SimulationWard};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::sync::Arc; use std::sync::Arc;
#[test] #[test]
fn rebase_threshold() { fn rebase_threshold() {
let mut ttf = MaxViewWard { max_count: 10 }; let mut ttf = MaxWard { max_count: 10 };
let node = 11; let node = 11;
let state = SimulationState { let state = SimulationState {