@@ -108,7 +108,7 @@ const PING_TIMER: u64 = 1;
108
108
const NETWORK_PRUNE_TIMER : u64 = 60 * 60 ;
109
109
110
110
#[ cfg( not( test) ) ]
111
- const SCORER_PERSIST_TIMER : u64 = 30 ;
111
+ const SCORER_PERSIST_TIMER : u64 = 60 * 60 ;
112
112
#[ cfg( test) ]
113
113
const SCORER_PERSIST_TIMER : u64 = 1 ;
114
114
@@ -236,9 +236,11 @@ fn handle_network_graph_update<L: Deref>(
236
236
}
237
237
}
238
238
239
+ /// Updates scorer based on event and returns whether an update occurred so we can decide whether
240
+ /// to persist.
239
241
fn update_scorer < ' a , S : ' static + Deref < Target = SC > + Send + Sync , SC : ' a + WriteableScore < ' a > > (
240
242
scorer : & ' a S , event : & Event
241
- ) {
243
+ ) -> bool {
242
244
let mut score = scorer. lock ( ) ;
243
245
match event {
244
246
Event :: PaymentPathFailed { ref path, short_channel_id : Some ( scid) , .. } => {
@@ -258,8 +260,9 @@ fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + Wri
258
260
Event :: ProbeFailed { path, short_channel_id : Some ( scid) , .. } => {
259
261
score. probe_failed ( path, * scid) ;
260
262
} ,
261
- _ => { } ,
263
+ _ => return false ,
262
264
}
265
+ true
263
266
}
264
267
265
268
macro_rules! define_run_body {
@@ -352,9 +355,15 @@ macro_rules! define_run_body {
352
355
// Note that we want to run a graph prune once not long after startup before
353
356
// falling back to our usual hourly prunes. This avoids short-lived clients never
354
357
// pruning their network graph. We run once 60 seconds after startup before
355
- // continuing our normal cadence.
358
+ // continuing our normal cadence. For RGS, since 60 seconds is likely too long,
359
+ // we prune after an initial sync completes.
356
360
let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER } ;
357
- if $timer_elapsed( & mut last_prune_call, prune_timer) {
361
+ let prune_timer_elapsed = $timer_elapsed( & mut last_prune_call, prune_timer) ;
362
+ let should_prune = match $gossip_sync {
363
+ GossipSync :: Rapid ( _) => !have_pruned || prune_timer_elapsed,
364
+ _ => prune_timer_elapsed,
365
+ } ;
366
+ if should_prune {
358
367
// The network graph must not be pruned while rapid sync completion is pending
359
368
if let Some ( network_graph) = $gossip_sync. prunable_network_graph( ) {
360
369
#[ cfg( feature = "std" ) ] {
@@ -616,12 +625,19 @@ where
616
625
let network_graph = gossip_sync. network_graph ( ) ;
617
626
let event_handler = & event_handler;
618
627
let scorer = & scorer;
628
+ let logger = & logger;
629
+ let persister = & persister;
619
630
async move {
620
631
if let Some ( network_graph) = network_graph {
621
632
handle_network_graph_update ( network_graph, & event)
622
633
}
623
634
if let Some ( ref scorer) = scorer {
624
- update_scorer ( scorer, & event) ;
635
+ if update_scorer ( scorer, & event) {
636
+ log_trace ! ( logger, "Persisting scorer after update" ) ;
637
+ if let Err ( e) = persister. persist_scorer ( & scorer) {
638
+ log_error ! ( logger, "Error: Failed to persist scorer, check your disk and permissions {}" , e)
639
+ }
640
+ }
625
641
}
626
642
event_handler ( event) . await ;
627
643
}
@@ -751,7 +767,12 @@ impl BackgroundProcessor {
751
767
handle_network_graph_update ( network_graph, & event)
752
768
}
753
769
if let Some ( ref scorer) = scorer {
754
- update_scorer ( scorer, & event) ;
770
+ if update_scorer ( scorer, & event) {
771
+ log_trace ! ( logger, "Persisting scorer after update" ) ;
772
+ if let Err ( e) = persister. persist_scorer ( & scorer) {
773
+ log_error ! ( logger, "Error: Failed to persist scorer, check your disk and permissions {}" , e)
774
+ }
775
+ }
755
776
}
756
777
event_handler. handle_event ( event) ;
757
778
} ;
@@ -1708,6 +1729,10 @@ mod tests {
1708
1729
if !std:: thread:: panicking ( ) {
1709
1730
bg_processor. stop ( ) . unwrap ( ) ;
1710
1731
}
1732
+
1733
+ let log_entries = nodes[ 0 ] . logger . lines . lock ( ) . unwrap ( ) ;
1734
+ let expected_log = "Persisting scorer after update" . to_string ( ) ;
1735
+ assert_eq ! ( * log_entries. get( & ( "lightning_background_processor" . to_string( ) , expected_log) ) . unwrap( ) , 5 ) ;
1711
1736
}
1712
1737
1713
1738
#[ tokio:: test]
@@ -1750,6 +1775,10 @@ mod tests {
1750
1775
let t2 = tokio:: spawn ( async move {
1751
1776
do_test_payment_path_scoring ! ( nodes, receiver. recv( ) . await ) ;
1752
1777
exit_sender. send ( ( ) ) . unwrap ( ) ;
1778
+
1779
+ let log_entries = nodes[ 0 ] . logger . lines . lock ( ) . unwrap ( ) ;
1780
+ let expected_log = "Persisting scorer after update" . to_string ( ) ;
1781
+ assert_eq ! ( * log_entries. get( & ( "lightning_background_processor" . to_string( ) , expected_log) ) . unwrap( ) , 5 ) ;
1753
1782
} ) ;
1754
1783
1755
1784
let ( r1, r2) = tokio:: join!( t1, t2) ;
0 commit comments