@@ -16,6 +16,9 @@ use lightning::io;
1616use crate :: error:: GraphSyncError ;
1717use crate :: RapidGossipSync ;
1818
19+ #[ cfg( feature = "std" ) ]
20+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
21+
1922#[ cfg( not( feature = "std" ) ) ]
2023use alloc:: { vec:: Vec , borrow:: ToOwned } ;
2124
@@ -29,6 +32,10 @@ const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
2932/// avoid malicious updates being able to trigger excessive memory allocation.
3033const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY : u32 = 50_000 ;
3134
35+ /// We remove disallow gossip data that's more than two weeks old, per BOLT 7's
36+ /// suggestion.
37+ const STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS : u64 = 60 * 60 * 24 * 14 ;
38+
3239impl < NG : Deref < Target =NetworkGraph < L > > , L : Deref > RapidGossipSync < NG , L > where L :: Target : Logger {
3340 pub ( crate ) fn update_network_graph_from_byte_stream < R : io:: Read > (
3441 & self ,
@@ -46,6 +53,16 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
4653 // backdate the applied timestamp by a week
4754 let backdated_timestamp = latest_seen_timestamp. saturating_sub ( 24 * 3600 * 7 ) ;
4855
56+ #[ cfg( all( feature = "std" , not( test) , not( feature = "_test_utils" ) ) ) ]
57+ {
58+ // Note that many tests rely on being able to set arbitrarily old timestamps, thus we
59+ // disable this check during tests!
60+ let time = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ;
61+ if ( msg. timestamp as u64 ) < time - STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS {
62+ return Err ( LightningError { err : "Rapid Gossip Sync data is more than two weeks old" . to_owned ( ) , action : ErrorAction :: IgnoreError } . into ( ) ) ;
63+ }
64+ }
65+
4966 let node_id_count: u32 = Readable :: read ( read_cursor) ?;
5067 let mut node_ids: Vec < PublicKey > = Vec :: with_capacity ( core:: cmp:: min (
5168 node_id_count,
0 commit comments