From eec8247a5de933879ff200d2618c9dfd3e6bb900 Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Wed, 10 Jul 2024 09:14:05 -0400 Subject: [PATCH] multi: add cfg struct to simulation Move configuration parameters into their own struct. Clients and activities are not included here, because they're separately parsed out of a simulation file which describes the network. --- sim-cli/src/main.rs | 13 ++++--- sim-lib/src/lib.rs | 82 +++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/sim-cli/src/main.rs b/sim-cli/src/main.rs index a75d9743..6375ba9d 100644 --- a/sim-cli/src/main.rs +++ b/sim-cli/src/main.rs @@ -1,4 +1,5 @@ use bitcoin::secp256k1::PublicKey; +use sim_lib::SimulationCfg; use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; @@ -211,13 +212,15 @@ async fn main() -> anyhow::Result<()> { }; let sim = Simulation::new( + SimulationCfg::new( + cli.total_time, + cli.expected_pmt_amt, + cli.capacity_multiplier, + write_results, + cli.fix_seed, + ), clients, validated_activities, - cli.total_time, - cli.expected_pmt_amt, - cli.capacity_multiplier, - write_results, - cli.fix_seed, ); let sim2 = sim.clone(); diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index b08000f4..3cccf8c2 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -474,15 +474,9 @@ impl MutRng { } } +/// Contains the configuration options for our simulation. #[derive(Clone)] -pub struct Simulation { - /// The lightning node that is being simulated. - nodes: HashMap>>, - /// The activity that are to be executed on the node. - activity: Vec, - /// High level triggers used to manage simulation tasks and shutdown. - shutdown_trigger: Trigger, - shutdown_listener: Listener, +pub struct SimulationCfg { /// Total simulation time. The simulation will run forever if undefined. total_time: Option, /// The expected payment size for the network. @@ -498,6 +492,38 @@ pub struct Simulation { results: Arc>, } +impl SimulationCfg { + pub fn new( + total_time: Option, + expected_payment_msat: u64, + activity_multiplier: f64, + write_results: Option, + seed: Option, + ) -> Self { + Self { + total_time: total_time.map(|x| Duration::from_secs(x as u64)), + expected_payment_msat, + activity_multiplier, + write_results, + seeded_rng: MutRng::new(seed), + results: Arc::new(Mutex::new(PaymentResultLogger::new())), + } + } +} + +#[derive(Clone)] +pub struct Simulation { + /// Config for the simulation itself. + cfg: SimulationCfg, + /// The lightning node that is being simulated. + nodes: HashMap>>, + /// The activity that are to be executed on the node. + activity: Vec, + /// High level triggers used to manage simulation tasks and shutdown. + shutdown_trigger: Trigger, + shutdown_listener: Listener, +} + #[derive(Clone)] pub struct WriteResults { /// Data directory where CSV result files are written. @@ -518,26 +544,17 @@ struct ExecutorKit { impl Simulation { pub fn new( + cfg: SimulationCfg, nodes: HashMap>>, activity: Vec, - total_time: Option, - expected_payment_msat: u64, - activity_multiplier: f64, - write_results: Option, - seed: Option, ) -> Self { let (shutdown_trigger, shutdown_listener) = triggered::trigger(); Self { + cfg, nodes, activity, shutdown_trigger, shutdown_listener, - total_time: total_time.map(|x| Duration::from_secs(x as u64)), - expected_payment_msat, - activity_multiplier, - write_results, - seeded_rng: MutRng::new(seed), - results: Arc::new(Mutex::new(PaymentResultLogger::new())), } } @@ -623,7 +640,7 @@ impl Simulation { } pub async fn run(&self) -> Result<(), SimulationError> { - if let Some(total_time) = self.total_time { + if let Some(total_time) = self.cfg.total_time { log::info!("Running the simulation for {}s.", total_time.as_secs()); } else { log::info!("Running the simulation forever."); @@ -709,7 +726,7 @@ impl Simulation { }); // Start a task that will shutdown the simulation if the total_time is met. - if let Some(total_time) = self.total_time { + if let Some(total_time) = self.cfg.total_time { let t = self.shutdown_trigger.clone(); let l = self.shutdown_listener.clone(); @@ -743,11 +760,11 @@ impl Simulation { } pub async fn get_total_payments(&self) -> u64 { - self.results.lock().await.total_attempts() + self.cfg.results.lock().await.total_attempts() } pub async fn get_success_rate(&self) -> f64 { - self.results.lock().await.success_rate() + self.cfg.results.lock().await.success_rate() } /// run_data_collection starts the tasks required for the simulation to report of the results of the activity that @@ -781,7 +798,7 @@ impl Simulation { } }); - let result_logger = self.results.clone(); + let result_logger = self.cfg.results.clone(); let result_logger_clone = result_logger.clone(); let result_logger_listener = listener.clone(); @@ -797,7 +814,7 @@ impl Simulation { }); // csr: consume simulation results - let csr_write_results = self.write_results.clone(); + let csr_write_results = self.cfg.write_results.clone(); tasks.spawn(async move { log::debug!("Starting simulation results consumer."); if let Err(e) = consume_simulation_results( @@ -864,9 +881,10 @@ impl Simulation { for (pk, node) in self.nodes.iter() { let chan_capacity = node.lock().await.list_channels().await?.iter().sum::(); - if let Err(e) = - RandomPaymentActivity::validate_capacity(chan_capacity, self.expected_payment_msat) - { + if let Err(e) = RandomPaymentActivity::validate_capacity( + chan_capacity, + self.cfg.expected_payment_msat, + ) { log::warn!("Node: {} not eligible for activity generation: {e}.", *pk); continue; } @@ -881,7 +899,7 @@ impl Simulation { let network_generator = Arc::new(Mutex::new( NetworkGraphView::new( active_nodes.values().cloned().collect(), - self.seeded_rng.clone(), + self.cfg.seeded_rng.clone(), ) .map_err(SimulationError::RandomActivityError)?, )); @@ -898,9 +916,9 @@ impl Simulation { payment_generator: Box::new( RandomPaymentActivity::new( *capacity, - self.expected_payment_msat, - self.activity_multiplier, - self.seeded_rng.clone(), + self.cfg.expected_payment_msat, + self.cfg.activity_multiplier, + self.cfg.seeded_rng.clone(), ) .map_err(SimulationError::RandomActivityError)?, ),