From c220497ffbc402a8ca98d829bd9902649c5f43a6 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez <3danimanimal@gmail.com> Date: Fri, 8 Nov 2024 07:31:51 +0100 Subject: [PATCH] Extract node configuration to simulation settings (#36) * Extract node configuration to simulation settings * Pipe settings and create mixnode setttings * Update config with human readable parsing --- simlib/mixnet-sims/Cargo.toml | 1 + .../config/mixnode.json | 26 +++++--- simlib/mixnet-sims/src/main.rs | 49 ++++++++------- simlib/mixnet-sims/src/settings.rs | 30 +++++++++ simlib/netrunner/config/carnot_dev.json | 62 ------------------- 5 files changed, 76 insertions(+), 92 deletions(-) rename simlib/{netrunner => mixnet-sims}/config/mixnode.json (63%) create mode 100644 simlib/mixnet-sims/src/settings.rs delete mode 100644 simlib/netrunner/config/carnot_dev.json diff --git a/simlib/mixnet-sims/Cargo.toml b/simlib/mixnet-sims/Cargo.toml index 33a6759..94cf492 100644 --- a/simlib/mixnet-sims/Cargo.toml +++ b/simlib/mixnet-sims/Cargo.toml @@ -8,6 +8,7 @@ anyhow = "1.0.93" clap = { version = "4.5.20", features = ["derive"] } crossbeam = "0.8.4" ctrlc = "3.4" +humantime = "2" parking_lot = "0.12.3" rand = "0.8" serde = { version = "1.0.214", features = ["derive"] } diff --git a/simlib/netrunner/config/mixnode.json b/simlib/mixnet-sims/config/mixnode.json similarity index 63% rename from simlib/netrunner/config/mixnode.json rename to simlib/mixnet-sims/config/mixnode.json index ff47f3d..817b5c8 100644 --- a/simlib/netrunner/config/mixnode.json +++ b/simlib/mixnet-sims/config/mixnode.json @@ -29,11 +29,23 @@ "seed": 0, "record_settings": {}, "wards": [ - { - "max": 10 - }, - { - "sum": 10 - } - ] + { + "max": 10 + }, + { + "sum": 10 + } + ], + "connected_peers_count": 3, + "data_message_lottery_interval": "20s", + "stake_proportion": 1.0, + "epoch_duration": "432000s", + "slot_duration": "20s", + "persistent_transmission": { + "max_emission_frequency": 1.0, + "drop_message_probability": 0.0 + }, + "number_of_mix_layers": 4, + "max_delay_seconds": 10, + "slots_per_epoch": 21600 } diff --git a/simlib/mixnet-sims/src/main.rs b/simlib/mixnet-sims/src/main.rs index ba384e0..3353c1b 100644 --- a/simlib/mixnet-sims/src/main.rs +++ b/simlib/mixnet-sims/src/main.rs @@ -20,20 +20,22 @@ use nomos_mix::cover_traffic::CoverTrafficSettings; use nomos_mix::message_blend::{ CryptographicProcessorSettings, MessageBlendSettings, TemporalSchedulerSettings, }; -use nomos_mix::persistent_transmission::PersistentTransmissionSettings; use parking_lot::Mutex; use rand::prelude::IteratorRandom; use rand::rngs::SmallRng; use rand::seq::SliceRandom; -use rand::SeedableRng; +use rand::{RngCore, SeedableRng}; use serde::de::DeserializeOwned; use serde::Serialize; // internal use crate::node::mix::MixNode; +use crate::settings::SimSettings; use netrunner::{runner::SimulationRunner, settings::SimulationSettings}; mod log; mod node; +mod settings; + /// Main simulation wrapper /// Pipes together the cli arguments with the execution #[derive(Parser)] @@ -63,22 +65,26 @@ impl SimulationApp { no_netcap, with_metrics: _, } = self; - let simulation_settings: SimulationSettings = load_json_from_file(&input_settings)?; + let settings: SimSettings = load_json_from_file(&input_settings)?; - let seed = simulation_settings.seed.unwrap_or_else(|| { + let seed = settings.simulation_settings.seed.unwrap_or_else(|| { SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Time went backwards") .as_secs() }); let mut rng = SmallRng::seed_from_u64(seed); - let mut node_ids: Vec = (0..simulation_settings.node_count) + let mut node_ids: Vec = (0..settings.simulation_settings.node_count) .map(NodeId::from_index) .collect(); node_ids.shuffle(&mut rng); - let regions = create_regions(&node_ids, &mut rng, &simulation_settings.network_settings); - let behaviours = create_behaviours(&simulation_settings.network_settings); + let regions = create_regions( + &node_ids, + &mut rng, + &settings.simulation_settings.network_settings, + ); + let behaviours = create_behaviours(&settings.simulation_settings.network_settings); let regions_data = RegionsData::new(regions, behaviours); let ids = node_ids.clone(); @@ -92,36 +98,33 @@ impl SimulationApp { create_boxed_mixnode( node_id, &mut network, - simulation_settings.clone(), + settings.simulation_settings.clone(), no_netcap, MixnodeSettings { connected_peers: ids .iter() .filter(|&id| id != &node_id) .copied() - .choose_multiple(&mut rng, 3), - data_message_lottery_interval: Duration::from_secs(20), - stake_proportion: 1.0 / node_ids.len() as f64, - seed: 0, - epoch_duration: Duration::from_secs(86400 * 5), // 5 days seconds - slot_duration: Duration::from_secs(20), - persistent_transmission: PersistentTransmissionSettings { - max_emission_frequency: 1.0, - drop_message_probability: 0.0, - }, + .choose_multiple(&mut rng, settings.connected_peers_count), + data_message_lottery_interval: settings.data_message_lottery_interval, + stake_proportion: settings.stake_proportion / node_ids.len() as f64, + seed: rng.next_u64(), + epoch_duration: settings.epoch_duration, // 5 days seconds + slot_duration: settings.slot_duration, + persistent_transmission: settings.persistent_transmission, message_blend: MessageBlendSettings { cryptographic_processor: CryptographicProcessorSettings { private_key: node_id.into(), - num_mix_layers: 4, + num_mix_layers: settings.number_of_mix_layers, }, temporal_processor: TemporalSchedulerSettings { - max_delay_seconds: 10, + max_delay_seconds: settings.max_delay_seconds, }, }, cover_traffic_settings: CoverTrafficSettings { node_id: node_id.0, - number_of_hops: 4, - slots_per_epoch: 21600, + number_of_hops: settings.number_of_mix_layers, + slots_per_epoch: settings.slots_per_epoch, network_size: node_ids.len(), }, membership: node_ids.iter().map(|&id| id.into()).collect(), @@ -132,7 +135,7 @@ impl SimulationApp { let network = Arc::try_unwrap(network) .expect("network is not used anywhere else") .into_inner(); - run::<_, _, _>(network, nodes, simulation_settings, stream_type)?; + run::<_, _, _>(network, nodes, settings.simulation_settings, stream_type)?; Ok(()) } } diff --git a/simlib/mixnet-sims/src/settings.rs b/simlib/mixnet-sims/src/settings.rs new file mode 100644 index 0000000..14e949c --- /dev/null +++ b/simlib/mixnet-sims/src/settings.rs @@ -0,0 +1,30 @@ +use netrunner::settings::SimulationSettings; +use nomos_mix::persistent_transmission::PersistentTransmissionSettings; +use serde::{Deserialize, Deserializer}; +use std::time::Duration; + +#[derive(Deserialize)] +pub struct SimSettings { + #[serde(flatten)] + pub simulation_settings: SimulationSettings, + pub connected_peers_count: usize, + #[serde(deserialize_with = "deserialize_duration_with_human_time")] + pub data_message_lottery_interval: Duration, + pub stake_proportion: f64, + #[serde(deserialize_with = "deserialize_duration_with_human_time")] + pub epoch_duration: Duration, + #[serde(deserialize_with = "deserialize_duration_with_human_time")] + pub slot_duration: Duration, + pub persistent_transmission: PersistentTransmissionSettings, + pub number_of_mix_layers: usize, + pub max_delay_seconds: u64, + pub slots_per_epoch: usize, +} + +fn deserialize_duration_with_human_time<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let s = String::deserialize(deserializer)?; + humantime::parse_duration(&s).map_err(serde::de::Error::custom) +} diff --git a/simlib/netrunner/config/carnot_dev.json b/simlib/netrunner/config/carnot_dev.json deleted file mode 100644 index b98ced2..0000000 --- a/simlib/netrunner/config/carnot_dev.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "network_settings": { - "network_behaviors": { - "north america:north america": "10ms", - "north america:europe": "150ms", - "north america:asia": "250ms", - "europe:europe": "10ms", - "europe:asia": "200ms", - "europe:north america": "150ms", - "asia:north america": "250ms", - "asia:europe": "200ms", - "asia:asia": "10ms" - }, - "regions": { - "north america": 0.4, - "europe": 0.3, - "asia": 0.3 - } - }, - "overlay_settings": { - "number_of_committees": 7 - }, - "node_settings": { - "network_capacity_kbps": 10000024, - "timeout": "10000ms" - }, - "step_time": "100ms", - "runner_settings": "Sync", - "stream_settings": { - "path": "tree_500_7_view_1_default.csv", - "format": "csv" - }, - "node_count": 500, - "views_count": 10, - "leaders_count": 1, - "seed": 0, - "wards": [ - { - "max_view": 1 - }, - { - "stalled_view": { - "consecutive_viewed_checkpoint": null, - "criterion": 0, - "threshold": 100 - } - } - ], - "record_settings": { - "current_view": true, - "highest_voted_view": true, - "local_high_qc": true, - "safe_blocks": false, - "last_view_timeout_qc": true, - "latest_committed_block": true, - "latest_committed_view": true, - "root_committee": false, - "parent_committee": false, - "child_committees": false, - "committed_blocks": false - } -} \ No newline at end of file