Skip to content

Commit caf1e12

Browse files
authored
Merge pull request #202 from carlaKC/157-simcfg
multi: add cfg struct to simulation
2 parents 582f1bc + eec8247 commit caf1e12

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

sim-cli/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bitcoin::secp256k1::PublicKey;
2+
use sim_lib::SimulationCfg;
23
use std::collections::HashMap;
34
use std::path::PathBuf;
45
use std::sync::Arc;
@@ -211,13 +212,15 @@ async fn main() -> anyhow::Result<()> {
211212
};
212213

213214
let sim = Simulation::new(
215+
SimulationCfg::new(
216+
cli.total_time,
217+
cli.expected_pmt_amt,
218+
cli.capacity_multiplier,
219+
write_results,
220+
cli.fix_seed,
221+
),
214222
clients,
215223
validated_activities,
216-
cli.total_time,
217-
cli.expected_pmt_amt,
218-
cli.capacity_multiplier,
219-
write_results,
220-
cli.fix_seed,
221224
);
222225
let sim2 = sim.clone();
223226

sim-lib/src/lib.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,9 @@ impl MutRng {
474474
}
475475
}
476476

477+
/// Contains the configuration options for our simulation.
477478
#[derive(Clone)]
478-
pub struct Simulation {
479-
/// The lightning node that is being simulated.
480-
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
481-
/// The activity that are to be executed on the node.
482-
activity: Vec<ActivityDefinition>,
483-
/// High level triggers used to manage simulation tasks and shutdown.
484-
shutdown_trigger: Trigger,
485-
shutdown_listener: Listener,
479+
pub struct SimulationCfg {
486480
/// Total simulation time. The simulation will run forever if undefined.
487481
total_time: Option<time::Duration>,
488482
/// The expected payment size for the network.
@@ -498,6 +492,38 @@ pub struct Simulation {
498492
results: Arc<Mutex<PaymentResultLogger>>,
499493
}
500494

495+
impl SimulationCfg {
496+
pub fn new(
497+
total_time: Option<u32>,
498+
expected_payment_msat: u64,
499+
activity_multiplier: f64,
500+
write_results: Option<WriteResults>,
501+
seed: Option<u64>,
502+
) -> Self {
503+
Self {
504+
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
505+
expected_payment_msat,
506+
activity_multiplier,
507+
write_results,
508+
seeded_rng: MutRng::new(seed),
509+
results: Arc::new(Mutex::new(PaymentResultLogger::new())),
510+
}
511+
}
512+
}
513+
514+
#[derive(Clone)]
515+
pub struct Simulation {
516+
/// Config for the simulation itself.
517+
cfg: SimulationCfg,
518+
/// The lightning node that is being simulated.
519+
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
520+
/// The activity that are to be executed on the node.
521+
activity: Vec<ActivityDefinition>,
522+
/// High level triggers used to manage simulation tasks and shutdown.
523+
shutdown_trigger: Trigger,
524+
shutdown_listener: Listener,
525+
}
526+
501527
#[derive(Clone)]
502528
pub struct WriteResults {
503529
/// Data directory where CSV result files are written.
@@ -518,26 +544,17 @@ struct ExecutorKit {
518544

519545
impl Simulation {
520546
pub fn new(
547+
cfg: SimulationCfg,
521548
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
522549
activity: Vec<ActivityDefinition>,
523-
total_time: Option<u32>,
524-
expected_payment_msat: u64,
525-
activity_multiplier: f64,
526-
write_results: Option<WriteResults>,
527-
seed: Option<u64>,
528550
) -> Self {
529551
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
530552
Self {
553+
cfg,
531554
nodes,
532555
activity,
533556
shutdown_trigger,
534557
shutdown_listener,
535-
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
536-
expected_payment_msat,
537-
activity_multiplier,
538-
write_results,
539-
seeded_rng: MutRng::new(seed),
540-
results: Arc::new(Mutex::new(PaymentResultLogger::new())),
541558
}
542559
}
543560

@@ -623,7 +640,7 @@ impl Simulation {
623640
}
624641

625642
pub async fn run(&self) -> Result<(), SimulationError> {
626-
if let Some(total_time) = self.total_time {
643+
if let Some(total_time) = self.cfg.total_time {
627644
log::info!("Running the simulation for {}s.", total_time.as_secs());
628645
} else {
629646
log::info!("Running the simulation forever.");
@@ -709,7 +726,7 @@ impl Simulation {
709726
});
710727

711728
// Start a task that will shutdown the simulation if the total_time is met.
712-
if let Some(total_time) = self.total_time {
729+
if let Some(total_time) = self.cfg.total_time {
713730
let t = self.shutdown_trigger.clone();
714731
let l = self.shutdown_listener.clone();
715732

@@ -743,11 +760,11 @@ impl Simulation {
743760
}
744761

745762
pub async fn get_total_payments(&self) -> u64 {
746-
self.results.lock().await.total_attempts()
763+
self.cfg.results.lock().await.total_attempts()
747764
}
748765

749766
pub async fn get_success_rate(&self) -> f64 {
750-
self.results.lock().await.success_rate()
767+
self.cfg.results.lock().await.success_rate()
751768
}
752769

753770
/// 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 {
781798
}
782799
});
783800

784-
let result_logger = self.results.clone();
801+
let result_logger = self.cfg.results.clone();
785802

786803
let result_logger_clone = result_logger.clone();
787804
let result_logger_listener = listener.clone();
@@ -797,7 +814,7 @@ impl Simulation {
797814
});
798815

799816
// csr: consume simulation results
800-
let csr_write_results = self.write_results.clone();
817+
let csr_write_results = self.cfg.write_results.clone();
801818
tasks.spawn(async move {
802819
log::debug!("Starting simulation results consumer.");
803820
if let Err(e) = consume_simulation_results(
@@ -864,9 +881,10 @@ impl Simulation {
864881
for (pk, node) in self.nodes.iter() {
865882
let chan_capacity = node.lock().await.list_channels().await?.iter().sum::<u64>();
866883

867-
if let Err(e) =
868-
RandomPaymentActivity::validate_capacity(chan_capacity, self.expected_payment_msat)
869-
{
884+
if let Err(e) = RandomPaymentActivity::validate_capacity(
885+
chan_capacity,
886+
self.cfg.expected_payment_msat,
887+
) {
870888
log::warn!("Node: {} not eligible for activity generation: {e}.", *pk);
871889
continue;
872890
}
@@ -881,7 +899,7 @@ impl Simulation {
881899
let network_generator = Arc::new(Mutex::new(
882900
NetworkGraphView::new(
883901
active_nodes.values().cloned().collect(),
884-
self.seeded_rng.clone(),
902+
self.cfg.seeded_rng.clone(),
885903
)
886904
.map_err(SimulationError::RandomActivityError)?,
887905
));
@@ -898,9 +916,9 @@ impl Simulation {
898916
payment_generator: Box::new(
899917
RandomPaymentActivity::new(
900918
*capacity,
901-
self.expected_payment_msat,
902-
self.activity_multiplier,
903-
self.seeded_rng.clone(),
919+
self.cfg.expected_payment_msat,
920+
self.cfg.activity_multiplier,
921+
self.cfg.seeded_rng.clone(),
904922
)
905923
.map_err(SimulationError::RandomActivityError)?,
906924
),

0 commit comments

Comments
 (0)