Skip to content

Commit 67f810e

Browse files
committed
review suggestions
1 parent 4dc5455 commit 67f810e

File tree

3 files changed

+220
-264
lines changed

3 files changed

+220
-264
lines changed

sim-cli/src/main.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use rand::Rng;
88
use sim_lib::{
99
cln::ClnNode,
1010
lnd::LndNode,
11-
sim_node::{
12-
ln_node_from_graph, populate_network_graph, ChannelPolicy, SimGraph, SimulatedChannel,
13-
},
11+
sim_node::{ChannelPolicy, SimulatedChannel},
1412
ActivityDefinition, LightningError, LightningNode, NodeConnection, NodeId, SimParams,
1513
Simulation, WriteResults,
1614
};
@@ -204,28 +202,15 @@ async fn main() -> anyhow::Result<()> {
204202
None
205203
};
206204

207-
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
208-
209-
let channels = generate_sim_nodes();
210-
let graph = match SimGraph::new(channels.clone(), shutdown_trigger.clone()) {
211-
Ok(graph) => Arc::new(Mutex::new(graph)),
212-
Err(e) => anyhow::bail!("failed: {:?}", e),
213-
};
214-
215-
let routing_graph = match populate_network_graph(channels) {
216-
Ok(r) => r,
217-
Err(e) => anyhow::bail!("failed: {:?}", e),
218-
};
219-
220-
let sim = Simulation::new(
221-
ln_node_from_graph(graph.clone(), Arc::new(routing_graph)).await,
205+
let (sim, graph) = Simulation::from_sim_channels(
206+
generate_sim_nodes(),
222207
validated_activities,
223208
cli.total_time,
224209
cli.expected_pmt_amt,
225210
cli.capacity_multiplier,
226211
write_results,
227-
(shutdown_trigger, shutdown_listener),
228-
);
212+
)
213+
.await?;
229214
let sim2 = sim.clone();
230215

231216
ctrlc::set_handler(move || {

sim-lib/src/lib.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use lightning::ln::features::NodeFeatures;
66
use lightning::ln::PaymentHash;
77
use random_activity::RandomActivityError;
88
use serde::{Deserialize, Serialize};
9+
use sim_node::{SimGraph, SimulatedChannel};
910
use std::collections::HashSet;
1011
use std::fmt::{Display, Formatter};
1112
use std::marker::Send;
@@ -21,6 +22,7 @@ use triggered::{Listener, Trigger};
2122

2223
use self::defined_activity::DefinedPaymentActivity;
2324
use self::random_activity::{NetworkGraphView, RandomPaymentActivity};
25+
use self::sim_node::{ln_node_from_graph, populate_network_graph};
2426

2527
pub mod cln;
2628
mod defined_activity;
@@ -134,6 +136,8 @@ pub enum SimulationError {
134136
FileError,
135137
#[error("{0}")]
136138
RandomActivityError(RandomActivityError),
139+
#[error("{0}")]
140+
RandomGraphError(String),
137141
}
138142

139143
#[derive(Debug, Error)]
@@ -368,20 +372,53 @@ impl Simulation {
368372
expected_payment_msat: u64,
369373
activity_multiplier: f64,
370374
write_results: Option<WriteResults>,
371-
shutdown: (Trigger, Listener),
372375
) -> Self {
376+
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
377+
373378
Self {
374379
nodes,
375380
activity,
376-
shutdown_trigger: shutdown.0,
377-
shutdown_listener: shutdown.1,
381+
shutdown_trigger,
382+
shutdown_listener,
378383
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
379384
expected_payment_msat,
380385
activity_multiplier,
381386
write_results,
382387
}
383388
}
384389

390+
pub async fn from_sim_channels(
391+
channels: Vec<SimulatedChannel>,
392+
activity: Vec<ActivityDefinition>,
393+
total_time: Option<u32>,
394+
expected_payment_msat: u64,
395+
activity_multiplier: f64,
396+
write_results: Option<WriteResults>,
397+
) -> Result<(Self, Arc<Mutex<SimGraph>>), SimulationError> {
398+
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
399+
400+
let sim_graph = SimGraph::new(channels.clone(), shutdown_trigger.clone())
401+
.map(|graph| Arc::new(Mutex::new(graph)))
402+
.map_err(|e| SimulationError::RandomGraphError(e.err))?;
403+
404+
let routing_graph = populate_network_graph(channels)
405+
.map_err(|e| SimulationError::RandomGraphError(e.err))?;
406+
407+
Ok((
408+
Self {
409+
nodes: ln_node_from_graph(sim_graph.clone(), Arc::new(routing_graph)).await,
410+
activity,
411+
shutdown_trigger,
412+
shutdown_listener,
413+
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
414+
expected_payment_msat,
415+
activity_multiplier,
416+
write_results,
417+
},
418+
sim_graph.clone(),
419+
))
420+
}
421+
385422
/// validate_activity validates that the user-provided activity description is achievable for the network that
386423
/// we're working with. If no activity description is provided, then it ensures that we have configured a network
387424
/// that is suitable for random activity generation.

0 commit comments

Comments
 (0)