From ea589d018a1f355c4d937e9dba98a75211c6d00a Mon Sep 17 00:00:00 2001 From: Al Liu Date: Thu, 23 Mar 2023 16:57:18 +0800 Subject: [PATCH] Simulation App: Make step cost configurable (#102) * WIP: make step cost configurable * fix fmt --- simulations/Cargo.toml | 1 + simulations/src/bin/app.rs | 3 ++- simulations/src/config.rs | 5 +++-- simulations/src/node/carnot.rs | 23 ++++++++++++++++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/simulations/Cargo.toml b/simulations/Cargo.toml index ec78c7ca..bb56b945 100644 --- a/simulations/Cargo.toml +++ b/simulations/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] rand = { version = "0.8", features = ["small_rng"] } serde = { version = "1.0", features = ["derive", "rc"] } +serde_with = "2.3" serde_json = "1.0" clap = { version = "4", features = ["derive"] } diff --git a/simulations/src/bin/app.rs b/simulations/src/bin/app.rs index e28ae386..0b53f879 100644 --- a/simulations/src/bin/app.rs +++ b/simulations/src/bin/app.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use simulations::{ config::Config, node::{ - carnot::{CarnotNode, CarnotStep}, + carnot::{CarnotNode, CarnotStep, CarnotStepSolverType}, Node, StepTime, }, overlay::{flat::FlatOverlay, Overlay}, @@ -98,6 +98,7 @@ pub fn main() -> Result<(), Box> { ::Settings, >::Settings, CarnotStep, + CarnotStepSolverType, >, >(std::fs::File::open(config)?)?; #[allow(clippy::unit_arg)] diff --git a/simulations/src/config.rs b/simulations/src/config.rs index fa7f74d5..4d821243 100644 --- a/simulations/src/config.rs +++ b/simulations/src/config.rs @@ -4,9 +4,10 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[derive(Serialize, Deserialize)] -pub struct Config +pub struct Config where S: core::str::FromStr, + C: core::str::FromStr, { pub network_behaviors: HashMap<(Region, Region), StepTime>, pub regions: Vec, @@ -14,5 +15,5 @@ where pub node_settings: N, pub node_count: usize, pub committee_size: usize, - pub steps: Vec, + pub step_costs: Vec<(S, C)>, } diff --git a/simulations/src/node/carnot.rs b/simulations/src/node/carnot.rs index 54bd3b6c..01963c34 100644 --- a/simulations/src/node/carnot.rs +++ b/simulations/src/node/carnot.rs @@ -69,6 +69,7 @@ fn receive_commit( } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] pub enum CarnotStep { RootReceiveProposal, ReceiveProposal, @@ -102,15 +103,35 @@ pub enum CarnotStepSolver { RootCommitteeReceiverSolver(RootCommitteeReceiverSolver), } +#[serde_with::serde_as] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] pub enum CarnotStepSolverType { - Plain(StepTime), + Plain(#[serde_as(as = "serde_with::DurationMilliSeconds")] StepTime), ParentCommitteeReceiverSolver, ChildCommitteeReceiverSolver, LeaderToCommitteeReceiverSolver, RootCommitteeReceiverSolver, } +impl core::str::FromStr for CarnotStepSolverType { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.trim().replace(['_', '-'], "").to_lowercase().as_str() { + "plain" => Ok(Self::Plain(StepTime::from_millis(1))), + "parentcommitteereceiversolver" => Ok(Self::ParentCommitteeReceiverSolver), + "childcommitteereceiversolver" => Ok(Self::ChildCommitteeReceiverSolver), + x => { + let millis = x + .parse::() + .map_err(|_| format!("Unknown step solver type: {s}"))?; + Ok(Self::Plain(StepTime::from_millis(millis))) + } + } + } +} + impl CarnotStepSolverType { pub fn to_solver(self) -> CarnotStepSolver { match self {