Skip to content

Commit 9077600

Browse files
committed
sim-cli: add latency interceptor option in cli
1 parent bd0930d commit 9077600

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sim-cli/src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::sync::Arc;
2+
13
use clap::Parser;
24
use log::LevelFilter;
35
use sim_cli::parsing::{create_simulation, create_simulation_with_network, parse_sim_params, Cli};
6+
use simln_lib::{latency_interceptor::LatencyIntercepor, sim_node::Interceptor};
47
use simple_logger::SimpleLogger;
58
use tokio_util::task::TaskTracker;
69

@@ -29,7 +32,13 @@ async fn main() -> anyhow::Result<()> {
2932
let (sim, validated_activities) = if sim_params.sim_network.is_empty() {
3033
create_simulation(&cli, &sim_params, tasks.clone()).await?
3134
} else {
32-
create_simulation_with_network(&cli, &sim_params, tasks.clone()).await?
35+
let latency = cli.latency_ms.unwrap_or(0);
36+
let interceptors = if latency > 0 {
37+
vec![Arc::new(LatencyIntercepor::new_poisson(latency as f32)?) as Arc<dyn Interceptor>]
38+
} else {
39+
vec![]
40+
};
41+
create_simulation_with_network(&cli, &sim_params, tasks.clone(), interceptors).await?
3342
};
3443
let sim2 = sim.clone();
3544

sim-cli/src/parsing.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use log::LevelFilter;
55
use serde::{Deserialize, Serialize};
66
use simln_lib::clock::SimulationClock;
77
use simln_lib::sim_node::{
8-
ln_node_from_graph, populate_network_graph, ChannelPolicy, SimGraph, SimulatedChannel,
8+
ln_node_from_graph, populate_network_graph, ChannelPolicy, Interceptor, SimGraph,
9+
SimulatedChannel,
910
};
1011
use simln_lib::{
1112
cln, cln::ClnNode, eclair, eclair::EclairNode, lnd, lnd::LndNode, serializers,
@@ -87,6 +88,10 @@ pub struct Cli {
8788
/// simulated nodes.
8889
#[clap(long)]
8990
pub speedup_clock: Option<u16>,
91+
/// Latency to optionally introduce for payments in a simulated network expressed in
92+
/// milliseconds.
93+
#[clap(long)]
94+
pub latency_ms: Option<u32>,
9095
}
9196

9297
impl Cli {
@@ -112,6 +117,12 @@ impl Cli {
112117
));
113118
}
114119

120+
if !sim_params.nodes.is_empty() && self.latency_ms.is_some() {
121+
return Err(anyhow!(
122+
"Latency for payments is only allowed when running on a simulated network"
123+
));
124+
}
125+
115126
Ok(())
116127
}
117128
}
@@ -217,6 +228,7 @@ pub async fn create_simulation_with_network(
217228
cli: &Cli,
218229
sim_params: &SimParams,
219230
tasks: TaskTracker,
231+
interceptors: Vec<Arc<dyn Interceptor>>,
220232
) -> Result<(Simulation<SimulationClock>, Vec<ActivityDefinition>), anyhow::Error> {
221233
let cfg: SimulationCfg = SimulationCfg::try_from(cli)?;
222234
let SimParams {
@@ -246,7 +258,7 @@ pub async fn create_simulation_with_network(
246258
SimGraph::new(
247259
channels.clone(),
248260
tasks.clone(),
249-
vec![],
261+
interceptors,
250262
(shutdown_trigger.clone(), shutdown_listener.clone()),
251263
)
252264
.map_err(|e| SimulationError::SimulatedNetworkError(format!("{:?}", e)))?,

0 commit comments

Comments
 (0)