Skip to content

Commit 49d5ae4

Browse files
authored
Merge pull request #290 from elnosh/return-sim-nodes
return `SimNode`s when creating simulated network
2 parents 75f4201 + d97b155 commit 49d5ae4

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

sim-cli/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ async fn main() -> anyhow::Result<()> {
4343
vec![]
4444
};
4545
let sim_cfg: SimulationCfg = SimulationCfg::try_from(&cli)?;
46-
create_simulation_with_network(
46+
let (sim, validated_activities, _) = create_simulation_with_network(
4747
sim_cfg,
4848
&sim_params,
4949
cli.speedup_clock.unwrap_or(1),
5050
tasks.clone(),
5151
interceptors,
5252
CustomRecords::default(),
5353
)
54-
.await?
54+
.await?;
55+
(sim, validated_activities)
5556
};
5657
let sim2 = sim.clone();
5758

sim-cli/src/parsing.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
use simln_lib::clock::SimulationClock;
77
use simln_lib::sim_node::{
88
ln_node_from_graph, populate_network_graph, ChannelPolicy, CustomRecords, Interceptor,
9-
SimGraph, SimulatedChannel,
9+
SimGraph, SimNode, SimulatedChannel,
1010
};
1111
use simln_lib::{
1212
cln, cln::ClnNode, eclair, eclair::EclairNode, lnd, lnd::LndNode, serializers,
@@ -248,7 +248,14 @@ pub async fn create_simulation_with_network(
248248
tasks: TaskTracker,
249249
interceptors: Vec<Arc<dyn Interceptor>>,
250250
custom_records: CustomRecords,
251-
) -> Result<(Simulation<SimulationClock>, Vec<ActivityDefinition>), anyhow::Error> {
251+
) -> Result<
252+
(
253+
Simulation<SimulationClock>,
254+
Vec<ActivityDefinition>,
255+
HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>>,
256+
),
257+
anyhow::Error,
258+
> {
252259
let SimParams {
253260
nodes: _,
254261
sim_network,
@@ -299,19 +306,25 @@ pub async fn create_simulation_with_network(
299306
nodes.remove(pk);
300307
}
301308

309+
let nodes_dyn: HashMap<_, Arc<Mutex<dyn LightningNode>>> = nodes
310+
.iter()
311+
.map(|(pk, node)| (*pk, Arc::clone(node) as Arc<Mutex<dyn LightningNode>>))
312+
.collect();
313+
302314
let validated_activities =
303-
get_validated_activities(&nodes, nodes_info, sim_params.activity.clone()).await?;
315+
get_validated_activities(&nodes_dyn, nodes_info, sim_params.activity.clone()).await?;
304316

305317
Ok((
306318
Simulation::new(
307319
cfg,
308-
nodes,
320+
nodes_dyn,
309321
tasks,
310322
clock,
311323
shutdown_trigger,
312324
shutdown_listener,
313325
),
314326
validated_activities,
327+
nodes,
315328
))
316329
}
317330

simln-lib/src/sim_node.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -487,38 +487,40 @@ pub trait SimNetwork: Send + Sync {
487487
fn list_nodes(&self) -> Vec<NodeInfo>;
488488
}
489489

490+
type LdkNetworkGraph = NetworkGraph<Arc<WrappedLog>>;
491+
490492
/// A wrapper struct used to implement the LightningNode trait (can be thought of as "the" lightning node). Passes
491493
/// all functionality through to a coordinating simulation network. This implementation contains both the [`SimNetwork`]
492494
/// implementation that will allow us to dispatch payments and a read-only NetworkGraph that is used for pathfinding.
493495
/// While these two could be combined, we re-use the LDK-native struct to allow re-use of their pathfinding logic.
494-
pub struct SimNode<'a, T: SimNetwork> {
496+
pub struct SimNode<T: SimNetwork> {
495497
info: NodeInfo,
496498
/// The underlying execution network that will be responsible for dispatching payments.
497499
network: Arc<Mutex<T>>,
498500
/// Tracks the channel that will provide updates for payments by hash.
499501
in_flight: HashMap<PaymentHash, Receiver<Result<PaymentResult, LightningError>>>,
500502
/// A read-only graph used for pathfinding.
501-
pathfinding_graph: Arc<NetworkGraph<&'a WrappedLog>>,
503+
pathfinding_graph: Arc<LdkNetworkGraph>,
502504
/// Probabilistic scorer used to rank paths through the network for routing. This is reused across
503505
/// multiple payments to maintain scoring state.
504-
scorer: ProbabilisticScorer<Arc<NetworkGraph<&'a WrappedLog>>, &'a WrappedLog>,
506+
scorer: ProbabilisticScorer<Arc<LdkNetworkGraph>, Arc<WrappedLog>>,
505507
}
506508

507-
impl<'a, T: SimNetwork> SimNode<'a, T> {
509+
impl<T: SimNetwork> SimNode<T> {
508510
/// Creates a new simulation node that refers to the high level network coordinator provided to process payments
509511
/// on its behalf. The pathfinding graph is provided separately so that each node can handle its own pathfinding.
510512
pub fn new(
511513
info: NodeInfo,
512514
payment_network: Arc<Mutex<T>>,
513-
pathfinding_graph: Arc<NetworkGraph<&'a WrappedLog>>,
515+
pathfinding_graph: Arc<LdkNetworkGraph>,
514516
) -> Self {
515517
// Initialize the probabilistic scorer with default parameters for learning from payment
516518
// history. These parameters control how much successful/failed payments affect routing
517519
// scores and how quickly these scores decay over time.
518520
let scorer = ProbabilisticScorer::new(
519521
ProbabilisticScoringDecayParameters::default(),
520522
pathfinding_graph.clone(),
521-
&WrappedLog {},
523+
Arc::new(WrappedLog {}),
522524
);
523525

524526
SimNode {
@@ -584,12 +586,12 @@ fn node_info(pubkey: PublicKey, alias: String) -> NodeInfo {
584586

585587
/// Uses LDK's pathfinding algorithm with default parameters to find a path from source to destination, with no
586588
/// restrictions on fee budget.
587-
fn find_payment_route<'a>(
589+
fn find_payment_route(
588590
source: &PublicKey,
589591
dest: PublicKey,
590592
amount_msat: u64,
591-
pathfinding_graph: &NetworkGraph<&'a WrappedLog>,
592-
scorer: &ProbabilisticScorer<Arc<NetworkGraph<&'a WrappedLog>>, &'a WrappedLog>,
593+
pathfinding_graph: &LdkNetworkGraph,
594+
scorer: &ProbabilisticScorer<Arc<LdkNetworkGraph>, Arc<WrappedLog>>,
593595
) -> Result<Route, SimulationError> {
594596
find_route(
595597
source,
@@ -606,15 +608,15 @@ fn find_payment_route<'a>(
606608
pathfinding_graph,
607609
None,
608610
&WrappedLog {},
609-
scorer,
611+
&scorer,
610612
&Default::default(),
611613
&[0; 32],
612614
)
613615
.map_err(|e| SimulationError::SimulatedNetworkError(e.err))
614616
}
615617

616618
#[async_trait]
617-
impl<T: SimNetwork> LightningNode for SimNode<'_, T> {
619+
impl<T: SimNetwork> LightningNode for SimNode<T> {
618620
fn get_info(&self) -> &NodeInfo {
619621
&self.info
620622
}
@@ -1017,9 +1019,9 @@ impl SimGraph {
10171019
/// Produces a map of node public key to lightning node implementation to be used for simulations.
10181020
pub async fn ln_node_from_graph(
10191021
graph: Arc<Mutex<SimGraph>>,
1020-
routing_graph: Arc<NetworkGraph<&'_ WrappedLog>>,
1021-
) -> HashMap<PublicKey, Arc<Mutex<dyn LightningNode + '_>>> {
1022-
let mut nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>> = HashMap::new();
1022+
routing_graph: Arc<LdkNetworkGraph>,
1023+
) -> HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>> {
1024+
let mut nodes: HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>> = HashMap::new();
10231025

10241026
for node in graph.lock().await.nodes.iter() {
10251027
nodes.insert(
@@ -1039,11 +1041,11 @@ pub async fn ln_node_from_graph(
10391041
/// announcements, which has the effect of adding the nodes in each channel to the graph, because LDK does not export
10401042
/// all of the fields required to apply node announcements. This means that we will not have node-level information
10411043
/// (such as features) available in the routing graph.
1042-
pub fn populate_network_graph<'a, C: Clock>(
1044+
pub fn populate_network_graph<C: Clock>(
10431045
channels: Vec<SimulatedChannel>,
10441046
clock: Arc<C>,
1045-
) -> Result<NetworkGraph<&'a WrappedLog>, LdkError> {
1046-
let graph = NetworkGraph::new(Network::Regtest, &WrappedLog {});
1047+
) -> Result<LdkNetworkGraph, LdkError> {
1048+
let graph = NetworkGraph::new(Network::Regtest, Arc::new(WrappedLog {}));
10471049

10481050
let chain_hash = ChainHash::using_genesis_block(Network::Regtest);
10491051

@@ -2002,15 +2004,15 @@ mod tests {
20022004
}
20032005

20042006
/// Contains elements required to test dispatch_payment functionality.
2005-
struct DispatchPaymentTestKit<'a> {
2007+
struct DispatchPaymentTestKit {
20062008
graph: SimGraph,
20072009
nodes: Vec<PublicKey>,
2008-
routing_graph: Arc<NetworkGraph<&'a WrappedLog>>,
2009-
scorer: ProbabilisticScorer<Arc<NetworkGraph<&'a WrappedLog>>, &'a WrappedLog>,
2010+
routing_graph: Arc<LdkNetworkGraph>,
2011+
scorer: ProbabilisticScorer<Arc<LdkNetworkGraph>, Arc<WrappedLog>>,
20102012
shutdown: (Trigger, Listener),
20112013
}
20122014

2013-
impl DispatchPaymentTestKit<'_> {
2015+
impl DispatchPaymentTestKit {
20142016
/// Creates a test graph with a set of nodes connected by three channels, with all the capacity of the channel
20152017
/// on the side of the first node. For example, if called with capacity = 100 it will set up the following
20162018
/// network:
@@ -2031,7 +2033,7 @@ mod tests {
20312033
let scorer = ProbabilisticScorer::new(
20322034
ProbabilisticScoringDecayParameters::default(),
20332035
routing_graph.clone(),
2034-
&WrappedLog {},
2036+
Arc::new(WrappedLog {}),
20352037
);
20362038

20372039
// Collect pubkeys in-order, pushing the last node on separately because they don't have an outgoing

0 commit comments

Comments
 (0)