@@ -244,30 +244,30 @@ fn handle_network_graph_update<L: Deref>(
244
244
/// Updates scorer based on event and returns whether an update occurred so we can decide whether
245
245
/// to persist.
246
246
fn update_scorer < ' a , S : ' static + Deref < Target = SC > + Send + Sync , SC : ' a + WriteableScore < ' a > > (
247
- scorer : & ' a S , event : & Event
247
+ scorer : & ' a S , event : & Event , duration_since_epoch : Duration ,
248
248
) -> bool {
249
249
match event {
250
250
Event :: PaymentPathFailed { ref path, short_channel_id : Some ( scid) , .. } => {
251
251
let mut score = scorer. write_lock ( ) ;
252
- score. payment_path_failed ( path, * scid) ;
252
+ score. payment_path_failed ( path, * scid, duration_since_epoch ) ;
253
253
} ,
254
254
Event :: PaymentPathFailed { ref path, payment_failed_permanently : true , .. } => {
255
255
// Reached if the destination explicitly failed it back. We treat this as a successful probe
256
256
// because the payment made it all the way to the destination with sufficient liquidity.
257
257
let mut score = scorer. write_lock ( ) ;
258
- score. probe_successful ( path) ;
258
+ score. probe_successful ( path, duration_since_epoch ) ;
259
259
} ,
260
260
Event :: PaymentPathSuccessful { path, .. } => {
261
261
let mut score = scorer. write_lock ( ) ;
262
- score. payment_path_successful ( path) ;
262
+ score. payment_path_successful ( path, duration_since_epoch ) ;
263
263
} ,
264
264
Event :: ProbeSuccessful { path, .. } => {
265
265
let mut score = scorer. write_lock ( ) ;
266
- score. probe_successful ( path) ;
266
+ score. probe_successful ( path, duration_since_epoch ) ;
267
267
} ,
268
268
Event :: ProbeFailed { path, short_channel_id : Some ( scid) , .. } => {
269
269
let mut score = scorer. write_lock ( ) ;
270
- score. probe_failed ( path, * scid) ;
270
+ score. probe_failed ( path, * scid, duration_since_epoch ) ;
271
271
} ,
272
272
_ => return false ,
273
273
}
@@ -280,7 +280,7 @@ macro_rules! define_run_body {
280
280
$channel_manager: ident, $process_channel_manager_events: expr,
281
281
$peer_manager: ident, $process_onion_message_handler_events: expr, $gossip_sync: ident,
282
282
$logger: ident, $scorer: ident, $loop_exit_check: expr, $await: expr, $get_timer: expr,
283
- $timer_elapsed: expr, $check_slow_await: expr
283
+ $timer_elapsed: expr, $check_slow_await: expr, $time_fetch : expr ,
284
284
) => { {
285
285
log_trace!( $logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
286
286
$channel_manager. timer_tick_occurred( ) ;
@@ -383,11 +383,10 @@ macro_rules! define_run_body {
383
383
if should_prune {
384
384
// The network graph must not be pruned while rapid sync completion is pending
385
385
if let Some ( network_graph) = $gossip_sync. prunable_network_graph( ) {
386
- # [ cfg ( feature = "std" ) ] {
386
+ if let Some ( duration_since_epoch ) = $time_fetch ( ) {
387
387
log_trace!( $logger, "Pruning and persisting network graph." ) ;
388
- network_graph. remove_stale_channels_and_tracking( ) ;
389
- }
390
- #[ cfg( not( feature = "std" ) ) ] {
388
+ network_graph. remove_stale_channels_and_tracking_with_time( duration_since_epoch. as_secs( ) ) ;
389
+ } else {
391
390
log_warn!( $logger, "Not pruning network graph, consider enabling `std` or doing so manually with remove_stale_channels_and_tracking_with_time." ) ;
392
391
log_trace!( $logger, "Persisting network graph." ) ;
393
392
}
@@ -510,12 +509,16 @@ use core::task;
510
509
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
511
510
/// are hundreds or thousands of simultaneous process calls running.
512
511
///
512
+ /// The `fetch_time` parameter should return the current wall clock time, if one is available. If
513
+ /// no time is available, some features may be disabled, however the node will still operate fine.
514
+ ///
513
515
/// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
514
516
/// could setup `process_events_async` like this:
515
517
/// ```
516
518
/// # use lightning::io;
517
519
/// # use std::sync::{Arc, RwLock};
518
520
/// # use std::sync::atomic::{AtomicBool, Ordering};
521
+ /// # use std::time::SystemTime;
519
522
/// # use lightning_background_processor::{process_events_async, GossipSync};
520
523
/// # struct MyStore {}
521
524
/// # impl lightning::util::persist::KVStore for MyStore {
@@ -584,6 +587,7 @@ use core::task;
584
587
/// Some(background_scorer),
585
588
/// sleeper,
586
589
/// mobile_interruptable_platform,
590
+ /// || Some(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap())
587
591
/// )
588
592
/// .await
589
593
/// .expect("Failed to process events");
@@ -620,11 +624,12 @@ pub async fn process_events_async<
620
624
S : ' static + Deref < Target = SC > + Send + Sync ,
621
625
SC : for < ' b > WriteableScore < ' b > ,
622
626
SleepFuture : core:: future:: Future < Output = bool > + core:: marker:: Unpin ,
623
- Sleeper : Fn ( Duration ) -> SleepFuture
627
+ Sleeper : Fn ( Duration ) -> SleepFuture ,
628
+ FetchTime : Fn ( ) -> Option < Duration > ,
624
629
> (
625
630
persister : PS , event_handler : EventHandler , chain_monitor : M , channel_manager : CM ,
626
631
gossip_sync : GossipSync < PGS , RGS , G , UL , L > , peer_manager : PM , logger : L , scorer : Option < S > ,
627
- sleeper : Sleeper , mobile_interruptable_platform : bool ,
632
+ sleeper : Sleeper , mobile_interruptable_platform : bool , fetch_time : FetchTime ,
628
633
) -> Result < ( ) , lightning:: io:: Error >
629
634
where
630
635
UL :: Target : ' static + UtxoLookup ,
@@ -648,15 +653,18 @@ where
648
653
let scorer = & scorer;
649
654
let logger = & logger;
650
655
let persister = & persister;
656
+ let fetch_time = & fetch_time;
651
657
async move {
652
658
if let Some ( network_graph) = network_graph {
653
659
handle_network_graph_update ( network_graph, & event)
654
660
}
655
661
if let Some ( ref scorer) = scorer {
656
- if update_scorer ( scorer, & event) {
657
- log_trace ! ( logger, "Persisting scorer after update" ) ;
658
- if let Err ( e) = persister. persist_scorer ( & scorer) {
659
- log_error ! ( logger, "Error: Failed to persist scorer, check your disk and permissions {}" , e)
662
+ if let Some ( duration_since_epoch) = fetch_time ( ) {
663
+ if update_scorer ( scorer, & event, duration_since_epoch) {
664
+ log_trace ! ( logger, "Persisting scorer after update" ) ;
665
+ if let Err ( e) = persister. persist_scorer ( & scorer) {
666
+ log_error ! ( logger, "Error: Failed to persist scorer, check your disk and permissions {}" , e)
667
+ }
660
668
}
661
669
}
662
670
}
@@ -688,7 +696,7 @@ where
688
696
task:: Poll :: Ready ( exit) => { should_break = exit; true } ,
689
697
task:: Poll :: Pending => false ,
690
698
}
691
- } , mobile_interruptable_platform
699
+ } , mobile_interruptable_platform, fetch_time ,
692
700
)
693
701
}
694
702
@@ -810,7 +818,10 @@ impl BackgroundProcessor {
810
818
handle_network_graph_update ( network_graph, & event)
811
819
}
812
820
if let Some ( ref scorer) = scorer {
813
- if update_scorer ( scorer, & event) {
821
+ use std:: time:: SystemTime ;
822
+ let duration_since_epoch = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH )
823
+ . expect ( "Time should be sometime after 1970" ) ;
824
+ if update_scorer ( scorer, & event, duration_since_epoch) {
814
825
log_trace ! ( logger, "Persisting scorer after update" ) ;
815
826
if let Err ( e) = persister. persist_scorer ( & scorer) {
816
827
log_error ! ( logger, "Error: Failed to persist scorer, check your disk and permissions {}" , e)
@@ -829,7 +840,12 @@ impl BackgroundProcessor {
829
840
channel_manager. get_event_or_persistence_needed_future( ) ,
830
841
chain_monitor. get_update_future( )
831
842
) . wait_timeout( Duration :: from_millis( 100 ) ) ; } ,
832
- |_| Instant :: now( ) , |time: & Instant , dur| time. elapsed( ) . as_secs( ) > dur, false
843
+ |_| Instant :: now( ) , |time: & Instant , dur| time. elapsed( ) . as_secs( ) > dur, false ,
844
+ || {
845
+ use std:: time:: SystemTime ;
846
+ Some ( SystemTime :: now( ) . duration_since( SystemTime :: UNIX_EPOCH )
847
+ . expect( "Time should be sometime after 1970" ) )
848
+ } ,
833
849
)
834
850
} ) ;
835
851
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
@@ -1117,7 +1133,7 @@ mod tests {
1117
1133
}
1118
1134
1119
1135
impl ScoreUpdate for TestScorer {
1120
- fn payment_path_failed ( & mut self , actual_path : & Path , actual_short_channel_id : u64 ) {
1136
+ fn payment_path_failed ( & mut self , actual_path : & Path , actual_short_channel_id : u64 , _ : Duration ) {
1121
1137
if let Some ( expectations) = & mut self . event_expectations {
1122
1138
match expectations. pop_front ( ) . unwrap ( ) {
1123
1139
TestResult :: PaymentFailure { path, short_channel_id } => {
@@ -1137,7 +1153,7 @@ mod tests {
1137
1153
}
1138
1154
}
1139
1155
1140
- fn payment_path_successful ( & mut self , actual_path : & Path ) {
1156
+ fn payment_path_successful ( & mut self , actual_path : & Path , _ : Duration ) {
1141
1157
if let Some ( expectations) = & mut self . event_expectations {
1142
1158
match expectations. pop_front ( ) . unwrap ( ) {
1143
1159
TestResult :: PaymentFailure { path, .. } => {
@@ -1156,7 +1172,7 @@ mod tests {
1156
1172
}
1157
1173
}
1158
1174
1159
- fn probe_failed ( & mut self , actual_path : & Path , _: u64 ) {
1175
+ fn probe_failed ( & mut self , actual_path : & Path , _: u64 , _ : Duration ) {
1160
1176
if let Some ( expectations) = & mut self . event_expectations {
1161
1177
match expectations. pop_front ( ) . unwrap ( ) {
1162
1178
TestResult :: PaymentFailure { path, .. } => {
@@ -1174,7 +1190,7 @@ mod tests {
1174
1190
}
1175
1191
}
1176
1192
}
1177
- fn probe_successful ( & mut self , actual_path : & Path ) {
1193
+ fn probe_successful ( & mut self , actual_path : & Path , _ : Duration ) {
1178
1194
if let Some ( expectations) = & mut self . event_expectations {
1179
1195
match expectations. pop_front ( ) . unwrap ( ) {
1180
1196
TestResult :: PaymentFailure { path, .. } => {
@@ -1469,7 +1485,7 @@ mod tests {
1469
1485
tokio:: time:: sleep ( dur) . await ;
1470
1486
false // Never exit
1471
1487
} )
1472
- } , false ,
1488
+ } , false , || Some ( Duration :: ZERO ) ,
1473
1489
) ;
1474
1490
match bp_future. await {
1475
1491
Ok ( _) => panic ! ( "Expected error persisting manager" ) ,
@@ -1699,7 +1715,7 @@ mod tests {
1699
1715
_ = exit_receiver. changed( ) => true ,
1700
1716
}
1701
1717
} )
1702
- } , false ,
1718
+ } , false , || Some ( Duration :: from_secs ( 1696300000 ) ) ,
1703
1719
) ;
1704
1720
1705
1721
let t1 = tokio:: spawn ( bp_future) ;
@@ -1874,7 +1890,7 @@ mod tests {
1874
1890
_ = exit_receiver. changed( ) => true ,
1875
1891
}
1876
1892
} )
1877
- } , false ,
1893
+ } , false , || Some ( Duration :: ZERO ) ,
1878
1894
) ;
1879
1895
let t1 = tokio:: spawn ( bp_future) ;
1880
1896
let t2 = tokio:: spawn ( async move {
0 commit comments