Skip to content

Commit b613883

Browse files
committed
Track the time a stale channel was pruned
1 parent 0880b4d commit b613883

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

lightning/src/routing/gossip.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
143143
/// failure so that we don't resync them from gossip. Each SCID is mapped to the instant when we
144144
/// removed it from the network graph so that we can forget we removed it after some time (by
145145
/// removing it from this map) and it can be resynced from gossip if it appears again.
146-
removed_channels: Mutex<HashMap<u64, Instant>>,
146+
removed_channels: Mutex<HashMap<u64, u64>>,
147147
#[cfg(feature = "std")]
148148
/// Keeps track of [`NodeId`]s we have explicitly removed due to permanent failure so that we
149149
/// don't resync them from gossip. Each [`NodeId`] is mapped to the instant when we removed it
@@ -1588,7 +1588,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15881588
if let Some(chan) = channels.remove(&short_channel_id) {
15891589
let mut nodes = self.nodes.write().unwrap();
15901590
#[cfg(feature = "std")]
1591-
self.removed_channels.lock().unwrap().insert(short_channel_id, Instant::now());
1591+
let cur_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs();
1592+
self.removed_channels.lock().unwrap().insert(short_channel_id, cur_time);
15921593
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
15931594
}
15941595
} else {
@@ -1616,8 +1617,6 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16161617
let mut removed_channels = self.removed_channels.lock().unwrap();
16171618
#[cfg(feature = "std")]
16181619
let mut removed_nodes = self.removed_nodes.lock().unwrap();
1619-
#[cfg(feature = "std")]
1620-
let time = Instant::now();
16211620

16221621
if let Some(node) = nodes.remove(&node_id) {
16231622
for scid in node.channels.iter() {
@@ -1632,11 +1631,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16321631
}
16331632
}
16341633
#[cfg(feature = "std")]
1635-
removed_channels.insert(*scid, time);
1634+
removed_channels.insert(*scid, SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("Time must be > 1970").as_secs());
16361635
}
16371636
}
16381637
#[cfg(feature = "std")]
1639-
removed_nodes.insert(node_id, time);
1638+
removed_nodes.insert(node_id, Instant::now());
16401639
}
16411640
}
16421641

@@ -1702,12 +1701,20 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17021701
for scid in scids_to_remove {
17031702
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
17041703
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
1704+
self.removed_channels.lock().unwrap().insert(scid, current_time_unix);
17051705
}
17061706
}
17071707

1708+
self.removed_channels.lock().unwrap().retain(|_, time| {
1709+
if current_time_unix > *time {
1710+
return current_time_unix - *time < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS;
1711+
} else {
1712+
return true;
1713+
}
1714+
});
1715+
17081716
#[cfg(feature = "std")]
17091717
{
1710-
self.removed_channels.lock().unwrap().retain(|_, time| time.elapsed().as_secs() < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
17111718
self.removed_nodes.lock().unwrap().retain(|_, time| time.elapsed().as_secs() < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
17121719
}
17131720
}
@@ -2586,9 +2593,6 @@ use super::STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS;
25862593

25872594
#[cfg(feature = "std")]
25882595
{
2589-
use std::time::Duration;
2590-
use util::time::tests::SinceEpoch;
2591-
25922596
// Clear tracked nodes and channels for clean slate
25932597
network_graph.removed_channels.lock().unwrap().clear();
25942598
network_graph.removed_nodes.lock().unwrap().clear();
@@ -2601,15 +2605,15 @@ use super::STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS;
26012605
// Mark the channel as permanently failed. This will also remove the two nodes
26022606
// and all of the entries will be tracked as removed.
26032607
network_graph.channel_failed(short_channel_id, true);
2608+
use std::time::{SystemTime, UNIX_EPOCH};
2609+
let failure_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs();
26042610

26052611
// Should not remove from tracking if insufficient time has passed
2606-
SinceEpoch::advance(Duration::from_secs(REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS - 1));
2607-
network_graph.remove_stale_channels_and_tracking();
2612+
network_graph.remove_stale_channels_and_tracking_with_time(failure_time);
26082613
assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
26092614

26102615
// Advance mock time so the entries are removed on next call
2611-
SinceEpoch::advance(Duration::from_secs(1));
2612-
network_graph.remove_stale_channels_and_tracking();
2616+
network_graph.remove_stale_channels_and_tracking_with_time(failure_time + REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS + 1);
26132617
assert!(network_graph.removed_channels.lock().unwrap().is_empty());
26142618
assert!(network_graph.removed_nodes.lock().unwrap().is_empty());
26152619
}

0 commit comments

Comments
 (0)