Skip to content

Commit 32c4089

Browse files
committed
sim-ln/feat: Track SimulationClock in SimNode
1 parent d3618b5 commit 32c4089

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

sim-cli/src/parsing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub async fn create_simulation_with_network(
301301
.map_err(|e| SimulationError::SimulatedNetworkError(format!("{:?}", e)))?,
302302
);
303303

304-
let mut nodes = ln_node_from_graph(simulation_graph.clone(), routing_graph).await;
304+
let mut nodes = ln_node_from_graph(simulation_graph.clone(), routing_graph).await?;
305305
for pk in exclude {
306306
nodes.remove(pk);
307307
}

simln-lib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ pub enum LightningError {
263263
/// Error that occurred while getting graph.
264264
#[error("Get graph error: {0}")]
265265
GetGraphError(String),
266+
/// Error that occured when getting clock info.
267+
#[error("SystemTime conversion error: {0}")]
268+
SystemTimeConversionError(#[from] SystemTimeError),
266269
}
267270

268271
/// Information about a Lightning Network node.

simln-lib/src/sim_node.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clock::Clock;
1+
use crate::clock::{Clock, SimulationClock};
22
use crate::{
33
Graph, LightningError, LightningNode, NodeInfo, PaymentOutcome, PaymentResult, SimulationError,
44
};
@@ -513,6 +513,8 @@ pub struct SimNode<T: SimNetwork> {
513513
/// Probabilistic scorer used to rank paths through the network for routing. This is reused across
514514
/// multiple payments to maintain scoring state.
515515
scorer: ProbabilisticScorer<Arc<LdkNetworkGraph>, Arc<WrappedLog>>,
516+
/// Clock for tracking simulation time.
517+
clock: SimulationClock,
516518
}
517519

518520
impl<T: SimNetwork> SimNode<T> {
@@ -522,7 +524,7 @@ impl<T: SimNetwork> SimNode<T> {
522524
info: NodeInfo,
523525
payment_network: Arc<Mutex<T>>,
524526
pathfinding_graph: Arc<LdkNetworkGraph>,
525-
) -> Self {
527+
) -> Result<Self, LightningError> {
526528
// Initialize the probabilistic scorer with default parameters for learning from payment
527529
// history. These parameters control how much successful/failed payments affect routing
528530
// scores and how quickly these scores decay over time.
@@ -532,13 +534,24 @@ impl<T: SimNetwork> SimNode<T> {
532534
Arc::new(WrappedLog {}),
533535
);
534536

535-
SimNode {
537+
let clock = match SimulationClock::new(1) {
538+
Ok(c) => c,
539+
Err(e) => {
540+
return Err(LightningError::SendPaymentError(format!(
541+
"Error creating simulation clock: {}",
542+
e
543+
)));
544+
},
545+
};
546+
547+
Ok(SimNode {
536548
info,
537549
network: payment_network,
538550
in_flight: HashMap::new(),
539551
pathfinding_graph,
540552
scorer,
541-
}
553+
clock,
554+
})
542555
}
543556

544557
/// Dispatches a payment to a specified route. If `custom_records` is `Some`, they will be attached to the outgoing
@@ -1044,7 +1057,7 @@ impl SimGraph {
10441057
pub async fn ln_node_from_graph(
10451058
graph: Arc<Mutex<SimGraph>>,
10461059
routing_graph: Arc<LdkNetworkGraph>,
1047-
) -> HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>> {
1060+
) -> Result<HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>>, LightningError> {
10481061
let mut nodes: HashMap<PublicKey, Arc<Mutex<SimNode<SimGraph>>>> = HashMap::new();
10491062

10501063
for node in graph.lock().await.nodes.iter() {
@@ -1054,11 +1067,11 @@ pub async fn ln_node_from_graph(
10541067
node.1 .0.clone(),
10551068
graph.clone(),
10561069
routing_graph.clone(),
1057-
))),
1070+
)?)),
10581071
);
10591072
}
10601073

1061-
nodes
1074+
Ok(nodes)
10621075
}
10631076

10641077
/// Populates a network graph based on the set of simulated channels provided. This function *only* applies channel
@@ -1939,7 +1952,8 @@ mod tests {
19391952
node_info(pk, String::default()),
19401953
sim_network.clone(),
19411954
Arc::new(graph),
1942-
);
1955+
)
1956+
.unwrap();
19431957

19441958
// Prime mock to return node info from lookup and assert that we get the pubkey we're expecting.
19451959
let lookup_pk = channels[3].node_1.policy.pubkey;
@@ -2333,7 +2347,8 @@ mod tests {
23332347
node_info(test_kit.nodes[0], String::default()),
23342348
Arc::new(Mutex::new(test_kit.graph)),
23352349
test_kit.routing_graph.clone(),
2336-
);
2350+
)
2351+
.unwrap();
23372352

23382353
let route = build_route_from_hops(
23392354
&test_kit.nodes[0],

0 commit comments

Comments
 (0)