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

View File

@ -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,

View File

@ -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<NetworkBehaviourKey, Duration>,
/// Represents node distribution in the simulated regions.
/// The sum of distributions should be 1.
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 regions: regions::RegionsData,
network_time: NetworkTime,

View File

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

View File

@ -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,