Skip to content

Commit ad7f901

Browse files
committed
sim-lib: use Clock trait to create network graph for sim-node
As is, channels were all imported with different timestamps. There's no use for this, and it runs the risk of exceeding LDK's 24h limit if we're using a very sped up clock. Using a single value fixes this.
1 parent 9fd58d2 commit ad7f901

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

simln-lib/src/sim_node.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::clock::Clock;
12
use crate::{
23
LightningError, LightningNode, NodeInfo, PaymentOutcome, PaymentResult, SimulationError,
34
};
@@ -9,7 +10,7 @@ use lightning::ln::chan_utils::make_funding_redeemscript;
910
use serde::{Deserialize, Serialize};
1011
use std::collections::{hash_map::Entry, HashMap};
1112
use std::sync::Arc;
12-
use std::time::{SystemTime, UNIX_EPOCH};
13+
use std::time::UNIX_EPOCH;
1314
use tokio_util::task::TaskTracker;
1415

1516
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
@@ -737,8 +738,9 @@ pub async fn ln_node_from_graph(
737738
/// announcements, which has the effect of adding the nodes in each channel to the graph, because LDK does not export
738739
/// all of the fields required to apply node announcements. This means that we will not have node-level information
739740
/// (such as features) available in the routing graph.
740-
pub fn populate_network_graph<'a>(
741+
pub fn populate_network_graph<'a, C: Clock>(
741742
channels: Vec<SimulatedChannel>,
743+
clock: Arc<C>,
742744
) -> Result<NetworkGraph<&'a WrappedLog>, LdkError> {
743745
let graph = NetworkGraph::new(Network::Regtest, &WrappedLog {});
744746

@@ -771,16 +773,16 @@ pub fn populate_network_graph<'a>(
771773

772774
graph.update_channel_from_unsigned_announcement(&announcement, &Some(&utxo_validator))?;
773775

774-
// The least significant bit of the channel flag field represents the direction that the channel update
775-
// applies to. This value is interpreted as node_1 if it is zero, and node_2 otherwise.
776+
// LDK only allows channel announcements up to 24h in the future. Use a fixed timestamp so that even if we've
777+
// sped up our clock dramatically, we won't hit that limit.
778+
let now = clock.now().duration_since(UNIX_EPOCH).unwrap().as_secs() as u32;
776779
for (i, node) in [channel.node_1, channel.node_2].iter().enumerate() {
777780
let update = UnsignedChannelUpdate {
778781
chain_hash,
779782
short_channel_id: channel.short_channel_id.into(),
780-
timestamp: SystemTime::now()
781-
.duration_since(UNIX_EPOCH)
782-
.unwrap()
783-
.as_secs() as u32,
783+
timestamp: now,
784+
// The least significant bit of the channel flag field represents the direction that the channel update
785+
// applies to. This value is interpreted as node_1 if it is zero, and node_2 otherwise.
784786
flags: i as u8,
785787
cltv_expiry_delta: node.policy.cltv_expiry_delta as u16,
786788
htlc_minimum_msat: node.policy.min_htlc_size_msat,
@@ -1097,6 +1099,7 @@ impl UtxoLookup for UtxoValidator {
10971099
#[cfg(test)]
10981100
mod tests {
10991101
use super::*;
1102+
use crate::clock::SystemClock;
11001103
use crate::test_utils::get_random_keypair;
11011104
use bitcoin::secp256k1::PublicKey;
11021105
use lightning::routing::router::Route;
@@ -1488,7 +1491,7 @@ mod tests {
14881491
let mock = MockNetwork::new();
14891492
let sim_network = Arc::new(Mutex::new(mock));
14901493
let channels = create_simulated_channels(5, 300000000);
1491-
let graph = populate_network_graph(channels.clone()).unwrap();
1494+
let graph = populate_network_graph(channels.clone(), Arc::new(SystemClock {})).unwrap();
14921495

14931496
// Create a simulated node for the first channel in our network.
14941497
let pk = channels[0].node_1.policy.pubkey;
@@ -1580,7 +1583,9 @@ mod tests {
15801583
async fn new(capacity: u64) -> Self {
15811584
let (shutdown, _listener) = triggered::trigger();
15821585
let channels = create_simulated_channels(3, capacity);
1583-
let routing_graph = Arc::new(populate_network_graph(channels.clone()).unwrap());
1586+
let routing_graph = Arc::new(
1587+
populate_network_graph(channels.clone(), Arc::new(SystemClock {})).unwrap(),
1588+
);
15841589

15851590
let scorer = ProbabilisticScorer::new(
15861591
ProbabilisticScoringDecayParameters::default(),

0 commit comments

Comments
 (0)