Skip to content

Commit 2fe7cd4

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 89bd618 commit 2fe7cd4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

simln-lib/src/sim_node.rs

+14-10
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
};
@@ -8,7 +9,7 @@ use bitcoin::{Network, ScriptBuf, TxOut};
89
use lightning::ln::chan_utils::make_funding_redeemscript;
910
use std::collections::{hash_map::Entry, HashMap};
1011
use std::sync::Arc;
11-
use std::time::{SystemTime, UNIX_EPOCH};
12+
use std::time::UNIX_EPOCH;
1213
use tokio_util::task::TaskTracker;
1314

1415
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
@@ -729,8 +730,9 @@ pub async fn ln_node_from_graph(
729730
/// announcements, which has the effect of adding the nodes in each channel to the graph, because LDK does not export
730731
/// all of the fields required to apply node announcements. This means that we will not have node-level information
731732
/// (such as features) available in the routing graph.
732-
pub fn populate_network_graph<'a>(
733+
pub fn populate_network_graph<'a, C: Clock>(
733734
channels: Vec<SimulatedChannel>,
735+
clock: C,
734736
) -> Result<NetworkGraph<&'a WrappedLog>, LdkError> {
735737
let graph = NetworkGraph::new(Network::Regtest, &WrappedLog {});
736738

@@ -763,16 +765,16 @@ pub fn populate_network_graph<'a>(
763765

764766
graph.update_channel_from_unsigned_announcement(&announcement, &Some(&utxo_validator))?;
765767

766-
// The least significant bit of the channel flag field represents the direction that the channel update
767-
// applies to. This value is interpreted as node_1 if it is zero, and node_2 otherwise.
768+
// LDK only allows channel announcements up to 24h in the future. Use a fixed timestamp so that even if we've
769+
// sped up our clock dramatically, we won't hit that limit.
770+
let now = clock.now().duration_since(UNIX_EPOCH).unwrap().as_secs() as u32;
768771
for (i, node) in [channel.node_1, channel.node_2].iter().enumerate() {
769772
let update = UnsignedChannelUpdate {
770773
chain_hash,
771774
short_channel_id: channel.short_channel_id.into(),
772-
timestamp: SystemTime::now()
773-
.duration_since(UNIX_EPOCH)
774-
.unwrap()
775-
.as_secs() as u32,
775+
timestamp: now,
776+
// The least significant bit of the channel flag field represents the direction that the channel update
777+
// applies to. This value is interpreted as node_1 if it is zero, and node_2 otherwise.
776778
flags: i as u8,
777779
cltv_expiry_delta: node.policy.cltv_expiry_delta as u16,
778780
htlc_minimum_msat: node.policy.min_htlc_size_msat,
@@ -1089,6 +1091,7 @@ impl UtxoLookup for UtxoValidator {
10891091
#[cfg(test)]
10901092
mod tests {
10911093
use super::*;
1094+
use crate::clock::SystemClock;
10921095
use crate::test_utils::get_random_keypair;
10931096
use bitcoin::secp256k1::PublicKey;
10941097
use lightning::routing::router::Route;
@@ -1480,7 +1483,7 @@ mod tests {
14801483
let mock = MockNetwork::new();
14811484
let sim_network = Arc::new(Mutex::new(mock));
14821485
let channels = create_simulated_channels(5, 300000000);
1483-
let graph = populate_network_graph(channels.clone()).unwrap();
1486+
let graph = populate_network_graph(channels.clone(), SystemClock {}).unwrap();
14841487

14851488
// Create a simulated node for the first channel in our network.
14861489
let pk = channels[0].node_1.policy.pubkey;
@@ -1572,7 +1575,8 @@ mod tests {
15721575
async fn new(capacity: u64) -> Self {
15731576
let (shutdown, _listener) = triggered::trigger();
15741577
let channels = create_simulated_channels(3, capacity);
1575-
let routing_graph = Arc::new(populate_network_graph(channels.clone()).unwrap());
1578+
let routing_graph =
1579+
Arc::new(populate_network_graph(channels.clone(), SystemClock {}).unwrap());
15761580

15771581
let scorer = ProbabilisticScorer::new(
15781582
ProbabilisticScoringDecayParameters::default(),

0 commit comments

Comments
 (0)