Skip to content

Commit

Permalink
review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-gi committed Feb 16, 2024
1 parent 4dc5455 commit 67f810e
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 264 deletions.
25 changes: 5 additions & 20 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use rand::Rng;
use sim_lib::{
cln::ClnNode,
lnd::LndNode,
sim_node::{
ln_node_from_graph, populate_network_graph, ChannelPolicy, SimGraph, SimulatedChannel,
},
sim_node::{ChannelPolicy, SimulatedChannel},
ActivityDefinition, LightningError, LightningNode, NodeConnection, NodeId, SimParams,
Simulation, WriteResults,
};
Expand Down Expand Up @@ -204,28 +202,15 @@ async fn main() -> anyhow::Result<()> {
None
};

let (shutdown_trigger, shutdown_listener) = triggered::trigger();

let channels = generate_sim_nodes();
let graph = match SimGraph::new(channels.clone(), shutdown_trigger.clone()) {
Ok(graph) => Arc::new(Mutex::new(graph)),
Err(e) => anyhow::bail!("failed: {:?}", e),
};

let routing_graph = match populate_network_graph(channels) {
Ok(r) => r,
Err(e) => anyhow::bail!("failed: {:?}", e),
};

let sim = Simulation::new(
ln_node_from_graph(graph.clone(), Arc::new(routing_graph)).await,
let (sim, graph) = Simulation::from_sim_channels(
generate_sim_nodes(),
validated_activities,
cli.total_time,
cli.expected_pmt_amt,
cli.capacity_multiplier,
write_results,
(shutdown_trigger, shutdown_listener),
);
)
.await?;
let sim2 = sim.clone();

ctrlc::set_handler(move || {
Expand Down
43 changes: 40 additions & 3 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lightning::ln::features::NodeFeatures;
use lightning::ln::PaymentHash;
use random_activity::RandomActivityError;
use serde::{Deserialize, Serialize};
use sim_node::{SimGraph, SimulatedChannel};
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::marker::Send;
Expand All @@ -21,6 +22,7 @@ use triggered::{Listener, Trigger};

use self::defined_activity::DefinedPaymentActivity;
use self::random_activity::{NetworkGraphView, RandomPaymentActivity};
use self::sim_node::{ln_node_from_graph, populate_network_graph};

pub mod cln;
mod defined_activity;
Expand Down Expand Up @@ -134,6 +136,8 @@ pub enum SimulationError {
FileError,
#[error("{0}")]
RandomActivityError(RandomActivityError),
#[error("{0}")]
RandomGraphError(String),
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -368,20 +372,53 @@ impl Simulation {
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
shutdown: (Trigger, Listener),
) -> Self {
let (shutdown_trigger, shutdown_listener) = triggered::trigger();

Self {
nodes,
activity,
shutdown_trigger: shutdown.0,
shutdown_listener: shutdown.1,
shutdown_trigger,
shutdown_listener,
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
expected_payment_msat,
activity_multiplier,
write_results,
}
}

pub async fn from_sim_channels(
channels: Vec<SimulatedChannel>,
activity: Vec<ActivityDefinition>,
total_time: Option<u32>,
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
) -> Result<(Self, Arc<Mutex<SimGraph>>), SimulationError> {
let (shutdown_trigger, shutdown_listener) = triggered::trigger();

let sim_graph = SimGraph::new(channels.clone(), shutdown_trigger.clone())
.map(|graph| Arc::new(Mutex::new(graph)))
.map_err(|e| SimulationError::RandomGraphError(e.err))?;

let routing_graph = populate_network_graph(channels)
.map_err(|e| SimulationError::RandomGraphError(e.err))?;

Ok((
Self {
nodes: ln_node_from_graph(sim_graph.clone(), Arc::new(routing_graph)).await,
activity,
shutdown_trigger,
shutdown_listener,
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
expected_payment_msat,
activity_multiplier,
write_results,
},
sim_graph.clone(),
))
}

/// validate_activity validates that the user-provided activity description is achievable for the network that
/// we're working with. If no activity description is provided, then it ensures that we have configured a network
/// that is suitable for random activity generation.
Expand Down
Loading

0 comments on commit 67f810e

Please sign in to comment.