Simulation App: Make step cost configurable (#102)

* WIP: make step cost configurable

* fix fmt
This commit is contained in:
Al Liu 2023-03-23 16:57:18 +08:00 committed by GitHub
parent 2d8c9f1099
commit ea589d018a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -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"] }

View File

@ -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<dyn std::error::Error>> {
<CarnotNode as Node>::Settings,
<FlatOverlay as Overlay<CarnotNode>>::Settings,
CarnotStep,
CarnotStepSolverType,
>,
>(std::fs::File::open(config)?)?;
#[allow(clippy::unit_arg)]

View File

@ -4,9 +4,10 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
pub struct Config<N, O, S>
pub struct Config<N, O, S, C>
where
S: core::str::FromStr,
C: core::str::FromStr,
{
pub network_behaviors: HashMap<(Region, Region), StepTime>,
pub regions: Vec<Region>,
@ -14,5 +15,5 @@ where
pub node_settings: N,
pub node_count: usize,
pub committee_size: usize,
pub steps: Vec<S>,
pub step_costs: Vec<(S, C)>,
}

View File

@ -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<Self, Self::Err> {
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::<u64>()
.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 {