@@ -143,7 +143,7 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
143
143
/// failure so that we don't resync them from gossip. Each SCID is mapped to the instant when we
144
144
/// removed it from the network graph so that we can forget we removed it after some time (by
145
145
/// removing it from this map) and it can be resynced from gossip if it appears again.
146
- removed_channels : Mutex < HashMap < u64 , Instant > > ,
146
+ removed_channels : Mutex < HashMap < u64 , u64 > > ,
147
147
#[ cfg( feature = "std" ) ]
148
148
/// Keeps track of [`NodeId`]s we have explicitly removed due to permanent failure so that we
149
149
/// don't resync them from gossip. Each [`NodeId`] is mapped to the instant when we removed it
@@ -1588,7 +1588,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1588
1588
if let Some ( chan) = channels. remove ( & short_channel_id) {
1589
1589
let mut nodes = self . nodes . write ( ) . unwrap ( ) ;
1590
1590
#[ cfg( feature = "std" ) ]
1591
- self . removed_channels . lock ( ) . unwrap ( ) . insert ( short_channel_id, Instant :: now ( ) ) ;
1591
+ let cur_time = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ;
1592
+ self . removed_channels . lock ( ) . unwrap ( ) . insert ( short_channel_id, cur_time) ;
1592
1593
Self :: remove_channel_in_nodes ( & mut nodes, & chan, short_channel_id) ;
1593
1594
}
1594
1595
} else {
@@ -1616,8 +1617,6 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1616
1617
let mut removed_channels = self . removed_channels . lock ( ) . unwrap ( ) ;
1617
1618
#[ cfg( feature = "std" ) ]
1618
1619
let mut removed_nodes = self . removed_nodes . lock ( ) . unwrap ( ) ;
1619
- #[ cfg( feature = "std" ) ]
1620
- let time = Instant :: now ( ) ;
1621
1620
1622
1621
if let Some ( node) = nodes. remove ( & node_id) {
1623
1622
for scid in node. channels . iter ( ) {
@@ -1632,11 +1631,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1632
1631
}
1633
1632
}
1634
1633
#[ cfg( feature = "std" ) ]
1635
- removed_channels. insert ( * scid, time ) ;
1634
+ removed_channels. insert ( * scid, SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ) ;
1636
1635
}
1637
1636
}
1638
1637
#[ cfg( feature = "std" ) ]
1639
- removed_nodes. insert ( node_id, time ) ;
1638
+ removed_nodes. insert ( node_id, Instant :: now ( ) ) ;
1640
1639
}
1641
1640
}
1642
1641
@@ -1702,12 +1701,20 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1702
1701
for scid in scids_to_remove {
1703
1702
let info = channels. remove ( & scid) . expect ( "We just accessed this scid, it should be present" ) ;
1704
1703
Self :: remove_channel_in_nodes ( & mut nodes, & info, scid) ;
1704
+ self . removed_channels . lock ( ) . unwrap ( ) . insert ( scid, current_time_unix) ;
1705
1705
}
1706
1706
}
1707
1707
1708
+ self . removed_channels . lock ( ) . unwrap ( ) . retain ( |_, time| {
1709
+ if current_time_unix > * time {
1710
+ return current_time_unix - * time < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS ;
1711
+ } else {
1712
+ return true ;
1713
+ }
1714
+ } ) ;
1715
+
1708
1716
#[ cfg( feature = "std" ) ]
1709
1717
{
1710
- self . removed_channels . lock ( ) . unwrap ( ) . retain ( |_, time| time. elapsed ( ) . as_secs ( ) < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS ) ;
1711
1718
self . removed_nodes . lock ( ) . unwrap ( ) . retain ( |_, time| time. elapsed ( ) . as_secs ( ) < REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS ) ;
1712
1719
}
1713
1720
}
@@ -2586,9 +2593,6 @@ use super::STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS;
2586
2593
2587
2594
#[ cfg( feature = "std" ) ]
2588
2595
{
2589
- use std:: time:: Duration ;
2590
- use util:: time:: tests:: SinceEpoch ;
2591
-
2592
2596
// Clear tracked nodes and channels for clean slate
2593
2597
network_graph. removed_channels . lock ( ) . unwrap ( ) . clear ( ) ;
2594
2598
network_graph. removed_nodes . lock ( ) . unwrap ( ) . clear ( ) ;
@@ -2601,15 +2605,15 @@ use super::STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS;
2601
2605
// Mark the channel as permanently failed. This will also remove the two nodes
2602
2606
// and all of the entries will be tracked as removed.
2603
2607
network_graph. channel_failed ( short_channel_id, true ) ;
2608
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
2609
+ let failure_time = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ;
2604
2610
2605
2611
// Should not remove from tracking if insufficient time has passed
2606
- SinceEpoch :: advance ( Duration :: from_secs ( REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS - 1 ) ) ;
2607
- network_graph. remove_stale_channels_and_tracking ( ) ;
2612
+ network_graph. remove_stale_channels_and_tracking_with_time ( failure_time) ;
2608
2613
assert_eq ! ( network_graph. removed_channels. lock( ) . unwrap( ) . len( ) , 1 ) ;
2609
2614
2610
2615
// Advance mock time so the entries are removed on next call
2611
- SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
2612
- network_graph. remove_stale_channels_and_tracking ( ) ;
2616
+ network_graph. remove_stale_channels_and_tracking_with_time ( failure_time + REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS + 1 ) ;
2613
2617
assert ! ( network_graph. removed_channels. lock( ) . unwrap( ) . is_empty( ) ) ;
2614
2618
assert ! ( network_graph. removed_nodes. lock( ) . unwrap( ) . is_empty( ) ) ;
2615
2619
}
0 commit comments