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
This commit is contained in:
Daniel Sanchez 2024-11-08 07:31:51 +01:00 committed by GitHub
parent 8faafcfb6f
commit c220497ffb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 76 additions and 92 deletions

View File

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

View File

@ -35,5 +35,17 @@
{
"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
}

View File

@ -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<NodeId> = (0..simulation_settings.node_count)
let mut node_ids: Vec<NodeId> = (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(())
}
}

View File

@ -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<Duration, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
humantime::parse_duration(&s).map_err(serde::de::Error::custom)
}

View File

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