Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: add cfg struct to simulation #202

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bitcoin::secp256k1::PublicKey;
use sim_lib::SimulationCfg;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -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();

Expand Down
82 changes: 50 additions & 32 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicKey, Arc<Mutex<dyn LightningNode>>>,
/// The activity that are to be executed on the node.
activity: Vec<ActivityDefinition>,
/// 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<time::Duration>,
/// The expected payment size for the network.
Expand All @@ -498,6 +492,38 @@ pub struct Simulation {
results: Arc<Mutex<PaymentResultLogger>>,
}

impl SimulationCfg {
pub fn new(
total_time: Option<u32>,
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
seed: Option<u64>,
) -> 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())),
bjohnson5 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[derive(Clone)]
pub struct Simulation {
/// Config for the simulation itself.
cfg: SimulationCfg,
/// The lightning node that is being simulated.
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
/// The activity that are to be executed on the node.
activity: Vec<ActivityDefinition>,
/// 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.
Expand All @@ -518,26 +544,17 @@ struct ExecutorKit {

impl Simulation {
pub fn new(
cfg: SimulationCfg,
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
activity: Vec<ActivityDefinition>,
total_time: Option<u32>,
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
seed: Option<u64>,
) -> 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())),
}
}

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -864,9 +881,10 @@ impl Simulation {
for (pk, node) in self.nodes.iter() {
let chan_capacity = node.lock().await.list_channels().await?.iter().sum::<u64>();

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;
}
Expand All @@ -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)?,
));
Expand All @@ -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)?,
),
Expand Down