diff --git a/simulations/Cargo.toml b/simulations/Cargo.toml index 86ef6f5f..e33ecbcc 100644 --- a/simulations/Cargo.toml +++ b/simulations/Cargo.toml @@ -21,6 +21,7 @@ crossbeam = { version = "0.8.2", features = ["crossbeam-channel"] } consensus-engine = { path = "../consensus-engine" } fixed-slice-deque = "0.1.0-beta2" futures = "0.3" +humantime = "2.1" humantime-serde = "1" nomos-core = { path = "../nomos-core" } nomos-consensus = { path = "../nomos-services/consensus" } diff --git a/simulations/config/carnot.json b/simulations/config/carnot.json index da92b5cf..483f4a01 100644 --- a/simulations/config/carnot.json +++ b/simulations/config/carnot.json @@ -1,42 +1,15 @@ { "network_settings": { "network_behaviors": { - "north america:north america": { - "secs": 1, - "nanos": 0 - }, - "north america:europe": { - "secs": 1, - "nanos": 0 - }, - "north america:asia": { - "secs": 1, - "nanos": 0 - }, - "europe:europe": { - "secs": 1, - "nanos": 0 - }, - "europe:asia": { - "secs": 1, - "nanos": 0 - }, - "europe:north america": { - "secs": 1, - "nanos": 0 - }, - "asia:north america": { - "secs": 1, - "nanos": 0 - }, - "asia:europe": { - "secs": 1, - "nanos": 0 - }, - "asia:asia": { - "secs": 1, - "nanos": 0 - } + "north america:north america": "1s", + "north america:europe": "1s", + "north america:asia": "1s", + "europe:europe": "1s", + "europe:asia": "1s", + "europe:north america": "1s", + "asia:north america": "1s", + "asia:europe": "1s", + "asia:asia": "1s" }, "regions": { "north america": 0.4, diff --git a/simulations/src/network/mod.rs b/simulations/src/network/mod.rs index 52e8e163..347c4257 100644 --- a/simulations/src/network/mod.rs +++ b/simulations/src/network/mod.rs @@ -56,12 +56,51 @@ impl<'de> Deserialize<'de> for NetworkBehaviourKey { #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NetworkSettings { + #[serde(with = "network_behaviors_serde")] pub network_behaviors: HashMap, /// Represents node distribution in the simulated regions. /// The sum of distributions should be 1. pub regions: HashMap, } +/// Ser/Deser `HashMap` to humantime format. +mod network_behaviors_serde { + use super::{Deserialize, Duration, HashMap, NetworkBehaviourKey}; + + /// Have to implement this manually because of the `serde_json` will panic if the key of map + /// is not a string. + pub fn serialize( + vals: &HashMap, + serializer: S, + ) -> Result + where + S: serde::Serializer, + { + use serde::ser::SerializeMap; + let mut ser = serializer.serialize_map(Some(vals.len()))?; + for (k, v) in vals { + ser.serialize_key(&k)?; + ser.serialize_value(&humantime::format_duration(*v).to_string())?; + } + ser.end() + } + + pub fn deserialize<'de, D>( + deserializer: D, + ) -> Result, D::Error> + where + D: serde::Deserializer<'de>, + { + let map = HashMap::::deserialize(deserializer)?; + map.into_iter() + .map(|(k, v)| { + let v = humantime::parse_duration(&v).map_err(serde::de::Error::custom)?; + Ok((k, v)) + }) + .collect::, _>>() + } +} + pub struct Network { pub regions: regions::RegionsData, network_time: NetworkTime, diff --git a/simulations/src/runner/mod.rs b/simulations/src/runner/mod.rs index 5d265f16..c65a4c91 100644 --- a/simulations/src/runner/mod.rs +++ b/simulations/src/runner/mod.rs @@ -148,6 +148,7 @@ where views_count: _, leaders_count: _, network_settings: _, + step_time: _, } = settings; Ok(Self { runner_settings, diff --git a/simulations/src/settings.rs b/simulations/src/settings.rs index 6157b330..f30cba0f 100644 --- a/simulations/src/settings.rs +++ b/simulations/src/settings.rs @@ -42,6 +42,8 @@ pub struct SimulationSettings { #[serde(default)] pub runner_settings: RunnerSettings, pub stream_settings: StreamSettings, + #[serde(with = "humantime_serde")] + pub step_time: std::time::Duration, pub node_count: usize, pub views_count: usize, pub leaders_count: usize,