From ae16e8583bba0b65891be4a7c2db37c2ac544fa0 Mon Sep 17 00:00:00 2001 From: Al Liu Date: Mon, 27 Mar 2023 18:45:50 +0800 Subject: [PATCH] impl ward condition for max view ward (#106) * impl ward condition for max view ward --- simulations/src/warding/ttf.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/simulations/src/warding/ttf.rs b/simulations/src/warding/ttf.rs index 512cbed1..827e98fb 100644 --- a/simulations/src/warding/ttf.rs +++ b/simulations/src/warding/ttf.rs @@ -6,13 +6,18 @@ use serde::Deserialize; /// the set threshold. #[derive(Debug, Deserialize, Copy, Clone)] pub struct MaxViewWard { - _max_view: usize, + max_view: usize, } impl SimulationWard for MaxViewWard { type SimulationState = SimulationState; - fn analyze(&mut self, _state: &Self::SimulationState) -> bool { - true // TODO: implement using simulation state + fn analyze(&mut self, state: &Self::SimulationState) -> bool { + !state + .nodes + .read() + .expect("simulations: MaxViewWard panic when requiring a read lock") + .iter() + .any(|n| n.current_view() >= self.max_view) } } @@ -31,7 +36,7 @@ mod test { type Settings = (); type State = Self; - fn new(rng: &mut R, id: NodeId, settings: Self::Settings) -> Self { + fn new(_rng: &mut R, id: NodeId, _settings: Self::Settings) -> Self { id.inner() } @@ -51,13 +56,15 @@ mod test { self.add_assign(1); } } - let mut ttf = MaxViewWard { _max_view: 10 }; - let mut cond = false; + let mut ttf = MaxViewWard { max_view: 10 }; - let node = 11; - let mut state = SimulationState { + let node = 9; + let state = SimulationState { nodes: Arc::new(RwLock::new(vec![node])), }; assert!(ttf.analyze(&state)); + + state.nodes.write().unwrap().push(11); + assert!(!ttf.analyze(&state)); } }