From: benthecarman Date: Thu, 6 Apr 2023 15:10:14 +0000 (-0500) Subject: Prune stale channels from network graph after RGS sync X-Git-Tag: v0.0.115~27^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=cff1b4bce349f5f80fd8093ef7f97ab3c2e76d71;p=rust-lightning Prune stale channels from network graph after RGS sync --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index a45dc6d6c..6f46c1f2b 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -1429,8 +1429,8 @@ mod tests { ]; $nodes[0].rapid_gossip_sync.update_network_graph_no_std(&initialization_input[..], Some(1642291930)).unwrap(); - // this should have added two channels - assert_eq!($nodes[0].network_graph.read_only().channels().len(), 3); + // this should have added two channels and pruned the previous one. + assert_eq!($nodes[0].network_graph.read_only().channels().len(), 2); $receive.expect("Network graph not pruned within deadline"); diff --git a/lightning-rapid-gossip-sync/src/processing.rs b/lightning-rapid-gossip-sync/src/processing.rs index 400fe1ccc..9e9a1ef86 100644 --- a/lightning-rapid-gossip-sync/src/processing.rs +++ b/lightning-rapid-gossip-sync/src/processing.rs @@ -237,6 +237,11 @@ impl>, L: Deref> RapidGossipSync where L } self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp); + + if let Some(time) = current_time_unix { + self.network_graph.remove_stale_channels_and_tracking_with_time(time) + } + self.is_initial_sync_complete.store(true, Ordering::Release); log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp); Ok(latest_seen_timestamp) @@ -554,6 +559,34 @@ mod tests { assert_eq!(network_graph.read_only().channels().len(), 2); } + #[test] + fn prunes_after_update() { + // this is the timestamp encoded in the binary data of valid_input below + let logger = TestLogger::new(); + + let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7; + + { + let network_graph = NetworkGraph::new(Network::Bitcoin, &logger); + assert_eq!(network_graph.read_only().channels().len(), 0); + + let rapid_sync = RapidGossipSync::new(&network_graph, &logger); + let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time)); + assert!(update_result.is_ok()); + assert_eq!(network_graph.read_only().channels().len(), 2); + } + + { + let network_graph = NetworkGraph::new(Network::Bitcoin, &logger); + assert_eq!(network_graph.read_only().channels().len(), 0); + + let rapid_sync = RapidGossipSync::new(&network_graph, &logger); + let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1)); + assert!(update_result.is_ok()); + assert_eq!(network_graph.read_only().channels().len(), 0); + } + } + #[test] fn timestamp_edge_cases_are_handled_correctly() { // this is the timestamp encoded in the binary data of valid_input below @@ -569,7 +602,7 @@ mod tests { let rapid_sync = RapidGossipSync::new(&network_graph, &logger); let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time)); assert!(update_result.is_ok()); - assert_eq!(network_graph.read_only().channels().len(), 2); + assert_eq!(network_graph.read_only().channels().len(), 0); } {