Skip to content

Commit cff1b4b

Browse files
committed
Prune stale channels from network graph after RGS sync
1 parent 8fcbe64 commit cff1b4b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,8 @@ mod tests {
14291429
];
14301430
$nodes[0].rapid_gossip_sync.update_network_graph_no_std(&initialization_input[..], Some(1642291930)).unwrap();
14311431

1432-
// this should have added two channels
1433-
assert_eq!($nodes[0].network_graph.read_only().channels().len(), 3);
1432+
// this should have added two channels and pruned the previous one.
1433+
assert_eq!($nodes[0].network_graph.read_only().channels().len(), 2);
14341434

14351435
$receive.expect("Network graph not pruned within deadline");
14361436

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
237237
}
238238

239239
self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
240+
241+
if let Some(time) = current_time_unix {
242+
self.network_graph.remove_stale_channels_and_tracking_with_time(time)
243+
}
244+
240245
self.is_initial_sync_complete.store(true, Ordering::Release);
241246
log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
242247
Ok(latest_seen_timestamp)
@@ -554,6 +559,34 @@ mod tests {
554559
assert_eq!(network_graph.read_only().channels().len(), 2);
555560
}
556561

562+
#[test]
563+
fn prunes_after_update() {
564+
// this is the timestamp encoded in the binary data of valid_input below
565+
let logger = TestLogger::new();
566+
567+
let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
568+
569+
{
570+
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
571+
assert_eq!(network_graph.read_only().channels().len(), 0);
572+
573+
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
574+
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time));
575+
assert!(update_result.is_ok());
576+
assert_eq!(network_graph.read_only().channels().len(), 2);
577+
}
578+
579+
{
580+
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
581+
assert_eq!(network_graph.read_only().channels().len(), 0);
582+
583+
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
584+
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
585+
assert!(update_result.is_ok());
586+
assert_eq!(network_graph.read_only().channels().len(), 0);
587+
}
588+
}
589+
557590
#[test]
558591
fn timestamp_edge_cases_are_handled_correctly() {
559592
// this is the timestamp encoded in the binary data of valid_input below
@@ -569,7 +602,7 @@ mod tests {
569602
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
570603
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
571604
assert!(update_result.is_ok());
572-
assert_eq!(network_graph.read_only().channels().len(), 2);
605+
assert_eq!(network_graph.read_only().channels().len(), 0);
573606
}
574607

575608
{

0 commit comments

Comments
 (0)