Simulation conf polish (#181)

* polish Duration ser/deser and support step_time in conf file
This commit is contained in:
Al Liu 2023-06-15 17:42:42 +08:00 committed by GitHub
parent b6789b994e
commit 9afd6c007c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 36 deletions

View File

@ -21,6 +21,7 @@ crossbeam = { version = "0.8.2", features = ["crossbeam-channel"] }
consensus-engine = { path = "../consensus-engine" } consensus-engine = { path = "../consensus-engine" }
fixed-slice-deque = "0.1.0-beta2" fixed-slice-deque = "0.1.0-beta2"
futures = "0.3" futures = "0.3"
humantime = "2.1"
humantime-serde = "1" humantime-serde = "1"
nomos-core = { path = "../nomos-core" } nomos-core = { path = "../nomos-core" }
nomos-consensus = { path = "../nomos-services/consensus" } nomos-consensus = { path = "../nomos-services/consensus" }

View File

@ -1,42 +1,15 @@
{ {
"network_settings": { "network_settings": {
"network_behaviors": { "network_behaviors": {
"north america:north america": { "north america:north america": "1s",
"secs": 1, "north america:europe": "1s",
"nanos": 0 "north america:asia": "1s",
}, "europe:europe": "1s",
"north america:europe": { "europe:asia": "1s",
"secs": 1, "europe:north america": "1s",
"nanos": 0 "asia:north america": "1s",
}, "asia:europe": "1s",
"north america:asia": { "asia:asia": "1s"
"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
}
}, },
"regions": { "regions": {
"north america": 0.4, "north america": 0.4,

View File

@ -56,12 +56,51 @@ impl<'de> Deserialize<'de> for NetworkBehaviourKey {
#[derive(Clone, Debug, Serialize, Deserialize, Default)] #[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct NetworkSettings { pub struct NetworkSettings {
#[serde(with = "network_behaviors_serde")]
pub network_behaviors: HashMap<NetworkBehaviourKey, Duration>, pub network_behaviors: HashMap<NetworkBehaviourKey, Duration>,
/// Represents node distribution in the simulated regions. /// Represents node distribution in the simulated regions.
/// The sum of distributions should be 1. /// The sum of distributions should be 1.
pub regions: HashMap<regions::Region, f32>, pub regions: HashMap<regions::Region, f32>,
} }
/// Ser/Deser `HashMap<NetworkBehaviourKey, Duration>` 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<S>(
vals: &HashMap<NetworkBehaviourKey, Duration>,
serializer: S,
) -> Result<S::Ok, S::Error>
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<HashMap<NetworkBehaviourKey, Duration>, D::Error>
where
D: serde::Deserializer<'de>,
{
let map = HashMap::<NetworkBehaviourKey, String>::deserialize(deserializer)?;
map.into_iter()
.map(|(k, v)| {
let v = humantime::parse_duration(&v).map_err(serde::de::Error::custom)?;
Ok((k, v))
})
.collect::<Result<HashMap<_, _>, _>>()
}
}
pub struct Network<M> { pub struct Network<M> {
pub regions: regions::RegionsData, pub regions: regions::RegionsData,
network_time: NetworkTime, network_time: NetworkTime,

View File

@ -148,6 +148,7 @@ where
views_count: _, views_count: _,
leaders_count: _, leaders_count: _,
network_settings: _, network_settings: _,
step_time: _,
} = settings; } = settings;
Ok(Self { Ok(Self {
runner_settings, runner_settings,

View File

@ -42,6 +42,8 @@ pub struct SimulationSettings {
#[serde(default)] #[serde(default)]
pub runner_settings: RunnerSettings, pub runner_settings: RunnerSettings,
pub stream_settings: StreamSettings, pub stream_settings: StreamSettings,
#[serde(with = "humantime_serde")]
pub step_time: std::time::Duration,
pub node_count: usize, pub node_count: usize,
pub views_count: usize, pub views_count: usize,
pub leaders_count: usize, pub leaders_count: usize,