Skip to content

Commit f0775f8

Browse files
authored
Merge pull request #1735 from naumenkogs/2022-09-prune-channels-if-either-not-upd
Prune channels if either not updated + track pruning time
2 parents a6609d6 + 080c70f commit f0775f8

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lightning/src/routing/gossip.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16531653
if info.two_to_one.is_some() && info.two_to_one.as_ref().unwrap().last_update < min_time_unix {
16541654
info.two_to_one = None;
16551655
}
1656-
if info.one_to_two.is_none() && info.two_to_one.is_none() {
1656+
if info.one_to_two.is_none() || info.two_to_one.is_none() {
16571657
// We check the announcement_received_time here to ensure we don't drop
16581658
// announcements that we just received and are just waiting for our peer to send a
16591659
// channel_update for.
@@ -1667,6 +1667,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16671667
for scid in scids_to_remove {
16681668
let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
16691669
Self::remove_channel_in_nodes(&mut nodes, &info, scid);
1670+
self.removed_channels.lock().unwrap().insert(scid, Some(current_time_unix));
16701671
}
16711672
}
16721673

@@ -2532,32 +2533,57 @@ mod tests {
25322533
assert!(network_graph.update_channel_from_announcement(&valid_channel_announcement, &chain_source).is_ok());
25332534
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
25342535

2536+
// Submit two channel updates for each channel direction (update.flags bit).
25352537
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
25362538
assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok());
25372539
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
25382540

2541+
let valid_channel_update_2 = get_signed_channel_update(|update| {update.flags |=1;}, node_2_privkey, &secp_ctx);
2542+
gossip_sync.handle_channel_update(&valid_channel_update_2).unwrap();
2543+
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().two_to_one.is_some());
2544+
25392545
network_graph.remove_stale_channels_and_tracking_with_time(100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
25402546
assert_eq!(network_graph.read_only().channels().len(), 1);
25412547
assert_eq!(network_graph.read_only().nodes().len(), 2);
25422548

25432549
network_graph.remove_stale_channels_and_tracking_with_time(101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
2550+
#[cfg(not(feature = "std"))] {
2551+
// Make sure removed channels are tracked.
2552+
assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
2553+
}
2554+
network_graph.remove_stale_channels_and_tracking_with_time(101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS +
2555+
REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
2556+
25442557
#[cfg(feature = "std")]
25452558
{
25462559
// In std mode, a further check is performed before fully removing the channel -
25472560
// the channel_announcement must have been received at least two weeks ago. We
2548-
// fudge that here by indicating the time has jumped two weeks. Note that the
2549-
// directional channel information will have been removed already..
2561+
// fudge that here by indicating the time has jumped two weeks.
25502562
assert_eq!(network_graph.read_only().channels().len(), 1);
25512563
assert_eq!(network_graph.read_only().nodes().len(), 2);
2552-
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
25532564

2565+
// Note that the directional channel information will have been removed already..
2566+
// We want to check that this will work even if *one* of the channel updates is recent,
2567+
// so we should add it with a recent timestamp.
2568+
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
25542569
use std::time::{SystemTime, UNIX_EPOCH};
25552570
let announcement_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs();
2571+
let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| {
2572+
unsigned_channel_update.timestamp = (announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS) as u32;
2573+
}, node_1_privkey, &secp_ctx);
2574+
assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok());
2575+
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
25562576
network_graph.remove_stale_channels_and_tracking_with_time(announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
2577+
// Make sure removed channels are tracked.
2578+
assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
2579+
// Provide a later time so that sufficient time has passed
2580+
network_graph.remove_stale_channels_and_tracking_with_time(announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS +
2581+
REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
25572582
}
25582583

25592584
assert_eq!(network_graph.read_only().channels().len(), 0);
25602585
assert_eq!(network_graph.read_only().nodes().len(), 0);
2586+
assert!(network_graph.removed_channels.lock().unwrap().is_empty());
25612587

25622588
#[cfg(feature = "std")]
25632589
{

0 commit comments

Comments
 (0)