Skip to content

Commit 74328bd

Browse files
authored
Merge pull request #2161 from benthecarman/prune-stale-channels-rgs
Prune stale channels from network graph after RGS sync.
2 parents 186cd04 + b336b2e commit 74328bd

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

lightning-background-processor/src/lib.rs

+2-2
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/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
//! let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
5757
//! let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
5858
//! let snapshot_contents: &[u8] = &[0; 0];
59-
//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph(snapshot_contents);
59+
//! // In no-std you need to provide the current time in unix epoch seconds
60+
//! // otherwise you can use update_network_graph
61+
//! let current_time_unix = 0;
62+
//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph_no_std(snapshot_contents, Some(current_time_unix));
6063
//! ```
6164
6265
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
@@ -128,6 +131,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
128131
/// Returns the last sync timestamp to be used the next time rapid sync data is queried.
129132
///
130133
/// `update_data`: `&[u8]` binary stream that comprises the update data
134+
#[cfg(feature = "std")]
131135
pub fn update_network_graph(&self, update_data: &[u8]) -> Result<u32, GraphSyncError> {
132136
let mut read_cursor = io::Cursor::new(update_data);
133137
self.update_network_graph_from_byte_stream(&mut read_cursor)

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

+46-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000;
3838
const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
3939

4040
impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
41+
#[cfg(feature = "std")]
4142
pub(crate) fn update_network_graph_from_byte_stream<R: io::Read>(
4243
&self,
4344
read_cursor: &mut R,
4445
) -> Result<u32, GraphSyncError> {
4546
#[allow(unused_mut, unused_assignments)]
4647
let mut current_time_unix = None;
47-
#[cfg(all(feature = "std", not(test)))]
48+
#[cfg(not(test))]
4849
{
4950
// Note that many tests rely on being able to set arbitrarily old timestamps, thus we
5051
// disable this check during tests!
@@ -237,6 +238,11 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
237238
}
238239

239240
self.network_graph.set_last_rapid_gossip_sync_timestamp(latest_seen_timestamp);
241+
242+
if let Some(time) = current_time_unix {
243+
self.network_graph.remove_stale_channels_and_tracking_with_time(time)
244+
}
245+
240246
self.is_initial_sync_complete.store(true, Ordering::Release);
241247
log_trace!(self.logger, "Done processing RGS data from {}", latest_seen_timestamp);
242248
Ok(latest_seen_timestamp)
@@ -247,7 +253,9 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
247253
mod tests {
248254
use bitcoin::Network;
249255

256+
#[cfg(feature = "std")]
250257
use lightning::ln::msgs::DecodeError;
258+
251259
use lightning::routing::gossip::NetworkGraph;
252260
use lightning::util::test_utils::TestLogger;
253261

@@ -275,6 +283,7 @@ mod tests {
275283
const VALID_BINARY_TIMESTAMP: u64 = 1642291930;
276284

277285
#[test]
286+
#[cfg(feature = "std")]
278287
fn network_graph_fails_to_update_from_clipped_input() {
279288
let logger = TestLogger::new();
280289
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
@@ -306,6 +315,7 @@ mod tests {
306315
}
307316

308317
#[test]
318+
#[cfg(feature = "std")]
309319
fn incremental_only_update_ignores_missing_channel() {
310320
let incremental_update_input = vec![
311321
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -326,6 +336,7 @@ mod tests {
326336
}
327337

328338
#[test]
339+
#[cfg(feature = "std")]
329340
fn incremental_only_update_fails_without_prior_updates() {
330341
let announced_update_input = vec![
331342
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -353,6 +364,7 @@ mod tests {
353364
}
354365

355366
#[test]
367+
#[cfg(feature = "std")]
356368
fn incremental_only_update_fails_without_prior_same_direction_updates() {
357369
let initialization_input = vec![
358370
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -408,6 +420,7 @@ mod tests {
408420
}
409421

410422
#[test]
423+
#[cfg(feature = "std")]
411424
fn incremental_update_succeeds_with_prior_announcements_and_full_updates() {
412425
let initialization_input = vec![
413426
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -467,6 +480,7 @@ mod tests {
467480
}
468481

469482
#[test]
483+
#[cfg(feature = "std")]
470484
fn update_succeeds_when_duplicate_gossip_is_applied() {
471485
let initialization_input = vec![
472486
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -510,6 +524,7 @@ mod tests {
510524
}
511525

512526
#[test]
527+
#[cfg(feature = "std")]
513528
fn full_update_succeeds() {
514529
let logger = TestLogger::new();
515530
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
@@ -554,6 +569,34 @@ mod tests {
554569
assert_eq!(network_graph.read_only().channels().len(), 2);
555570
}
556571

572+
#[test]
573+
fn prunes_after_update() {
574+
// this is the timestamp encoded in the binary data of valid_input below
575+
let logger = TestLogger::new();
576+
577+
let latest_nonpruning_time = VALID_BINARY_TIMESTAMP + 60 * 60 * 24 * 7;
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));
585+
assert!(update_result.is_ok());
586+
assert_eq!(network_graph.read_only().channels().len(), 2);
587+
}
588+
589+
{
590+
let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
591+
assert_eq!(network_graph.read_only().channels().len(), 0);
592+
593+
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
594+
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_nonpruning_time + 1));
595+
assert!(update_result.is_ok());
596+
assert_eq!(network_graph.read_only().channels().len(), 0);
597+
}
598+
}
599+
557600
#[test]
558601
fn timestamp_edge_cases_are_handled_correctly() {
559602
// this is the timestamp encoded in the binary data of valid_input below
@@ -569,7 +612,7 @@ mod tests {
569612
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
570613
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(latest_succeeding_time));
571614
assert!(update_result.is_ok());
572-
assert_eq!(network_graph.read_only().channels().len(), 2);
615+
assert_eq!(network_graph.read_only().channels().len(), 0);
573616
}
574617

575618
{
@@ -591,6 +634,7 @@ mod tests {
591634
}
592635

593636
#[test]
637+
#[cfg(feature = "std")]
594638
pub fn update_fails_with_unknown_version() {
595639
let unknown_version_input = vec![
596640
76, 68, 75, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,

0 commit comments

Comments
 (0)