@@ -16,6 +16,9 @@ use lightning::io;
16
16
use crate :: error:: GraphSyncError ;
17
17
use crate :: RapidGossipSync ;
18
18
19
+ #[ cfg( feature = "std" ) ]
20
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
21
+
19
22
#[ cfg( not( feature = "std" ) ) ]
20
23
use alloc:: { vec:: Vec , borrow:: ToOwned } ;
21
24
@@ -29,6 +32,10 @@ const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
29
32
/// avoid malicious updates being able to trigger excessive memory allocation.
30
33
const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY : u32 = 50_000 ;
31
34
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
+
32
39
impl < NG : Deref < Target =NetworkGraph < L > > , L : Deref > RapidGossipSync < NG , L > where L :: Target : Logger {
33
40
pub ( crate ) fn update_network_graph_from_byte_stream < R : io:: Read > (
34
41
& self ,
@@ -46,6 +53,16 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
46
53
// backdate the applied timestamp by a week
47
54
let backdated_timestamp = latest_seen_timestamp. saturating_sub ( 24 * 3600 * 7 ) ;
48
55
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
+
49
66
let node_id_count: u32 = Readable :: read ( read_cursor) ?;
50
67
let mut node_ids: Vec < PublicKey > = Vec :: with_capacity ( core:: cmp:: min (
51
68
node_id_count,
0 commit comments