mirror of
https://github.com/logos-co/nomos-simulations.git
synced 2025-01-11 03:06:31 +00:00
Sum ward condition
This commit is contained in:
parent
c634bd4232
commit
8cf691ad22
@ -31,6 +31,9 @@
|
||||
"wards": [
|
||||
{
|
||||
"max": 10
|
||||
},
|
||||
{
|
||||
"sum": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ mod stream_wrapper;
|
||||
use super::{Node, NodeId};
|
||||
use crate::{
|
||||
network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize},
|
||||
warding::Ward,
|
||||
warding::WardCondition,
|
||||
};
|
||||
use crossbeam::channel;
|
||||
use futures::Stream;
|
||||
@ -213,9 +213,13 @@ impl Node for MixNode {
|
||||
self.state.mock_counter += 1;
|
||||
}
|
||||
|
||||
fn analyze(&self, ward: Ward) -> bool {
|
||||
fn analyze(&self, ward: &mut WardCondition) -> bool {
|
||||
match ward {
|
||||
Ward::Max(condition) => self.state.mock_counter >= condition.max_count,
|
||||
WardCondition::Max(condition) => self.state.mock_counter >= condition.max_count,
|
||||
WardCondition::Sum(condition) => {
|
||||
*condition.step_result.borrow_mut() += self.state.mock_counter;
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use std::{
|
||||
use parking_lot::RwLock;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::warding::Ward;
|
||||
use crate::warding::WardCondition;
|
||||
// internal
|
||||
|
||||
#[serde_with::serde_as]
|
||||
@ -91,7 +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;
|
||||
fn analyze(&self, ward: &mut WardCondition) -> bool;
|
||||
}
|
||||
|
||||
#[derive(
|
||||
@ -164,7 +164,7 @@ impl Node for usize {
|
||||
self.add_assign(1);
|
||||
}
|
||||
|
||||
fn analyze(&self, _: Ward) -> bool {
|
||||
fn analyze(&self, _: &mut WardCondition) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,12 @@ pub trait SimulationWard<S, T> {
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Ward {
|
||||
Max(ttf::MaxWard),
|
||||
Sum(ttf::SumWard),
|
||||
}
|
||||
|
||||
pub enum WardCondition<'a> {
|
||||
Max(&'a ttf::MaxWard),
|
||||
Sum(&'a ttf::SumWardCondition),
|
||||
}
|
||||
|
||||
impl Ward {
|
||||
@ -42,6 +48,7 @@ impl Ward {
|
||||
) -> &mut dyn SimulationWard<S, T, SimulationState = SimulationState<S, T>> {
|
||||
match self {
|
||||
Ward::Max(ward) => ward,
|
||||
Ward::Sum(ward) => ward,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
use super::Ward::Max;
|
||||
use std::cell::RefCell;
|
||||
|
||||
use super::WardCondition::{self, Max};
|
||||
use crate::warding::{SimulationState, SimulationWard};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -13,7 +15,32 @@ pub struct MaxWard {
|
||||
impl<S, T> SimulationWard<S, T> for MaxWard {
|
||||
type SimulationState = SimulationState<S, T>;
|
||||
fn analyze(&mut self, state: &Self::SimulationState) -> bool {
|
||||
state.nodes.read().iter().all(|n| n.analyze(Max(*self)))
|
||||
state.nodes.read().iter().all(|n| n.analyze(&mut Max(self)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(transparent)]
|
||||
pub struct SumWard {
|
||||
max_count: usize,
|
||||
}
|
||||
|
||||
pub struct SumWardCondition {
|
||||
pub step_result: RefCell<usize>,
|
||||
}
|
||||
|
||||
impl<S, T> SimulationWard<S, T> for SumWard {
|
||||
type SimulationState = SimulationState<S, T>;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user