Skip to content

Commit 8ac39c9

Browse files
committed
Persist scorer upon update based on event handling
1 parent cd65bb4 commit 8ac39c9

File tree

1 file changed

+31
-4
lines changed
  • lightning-background-processor/src

1 file changed

+31
-4
lines changed

lightning-background-processor/src/lib.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,36 @@ fn handle_network_graph_update<L: Deref>(
230230
}
231231
}
232232

233+
/// Updates scorer based on event and returns whether an update occurred so we can decide whether
234+
/// to persist.
233235
fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
234236
scorer: &'a S, event: &Event
235-
) {
237+
) -> bool {
236238
let mut score = scorer.lock();
237239
match event {
238240
Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
239241
score.payment_path_failed(path, *scid);
242+
true
240243
},
241244
Event::PaymentPathFailed { ref path, payment_failed_permanently: true, .. } => {
242245
// Reached if the destination explicitly failed it back. We treat this as a successful probe
243246
// because the payment made it all the way to the destination with sufficient liquidity.
244247
score.probe_successful(path);
248+
true
245249
},
246250
Event::PaymentPathSuccessful { path, .. } => {
247251
score.payment_path_successful(path);
252+
true
248253
},
249254
Event::ProbeSuccessful { path, .. } => {
250255
score.probe_successful(path);
256+
true
251257
},
252258
Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
253259
score.probe_failed(path, *scid);
260+
true
254261
},
255-
_ => {},
262+
_ => false,
256263
}
257264
}
258265

@@ -616,12 +623,19 @@ where
616623
let network_graph = gossip_sync.network_graph();
617624
let event_handler = &event_handler;
618625
let scorer = &scorer;
626+
let logger = &logger;
627+
let persister = &persister;
619628
async move {
620629
if let Some(network_graph) = network_graph {
621630
handle_network_graph_update(network_graph, &event)
622631
}
623632
if let Some(ref scorer) = scorer {
624-
update_scorer(scorer, &event);
633+
if update_scorer(scorer, &event) {
634+
log_trace!(logger, "Persisting scorer after update");
635+
if let Err(e) = persister.persist_scorer(&scorer) {
636+
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
637+
}
638+
}
625639
}
626640
event_handler(event).await;
627641
}
@@ -759,7 +773,12 @@ impl BackgroundProcessor {
759773
handle_network_graph_update(network_graph, &event)
760774
}
761775
if let Some(ref scorer) = scorer {
762-
update_scorer(scorer, &event);
776+
if update_scorer(scorer, &event) {
777+
log_trace!(logger, "Persisting scorer after update");
778+
if let Err(e) = persister.persist_scorer(&scorer) {
779+
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
780+
}
781+
}
763782
}
764783
event_handler.handle_event(event);
765784
};
@@ -1711,6 +1730,10 @@ mod tests {
17111730
if !std::thread::panicking() {
17121731
bg_processor.stop().unwrap();
17131732
}
1733+
1734+
let log_entries = nodes[0].logger.lines.lock().unwrap();
1735+
let expected_log = "Persisting scorer after update".to_string();
1736+
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
17141737
}
17151738

17161739
#[tokio::test]
@@ -1753,6 +1776,10 @@ mod tests {
17531776
let t2 = tokio::spawn(async move {
17541777
do_test_payment_path_scoring!(nodes, receiver.recv().await);
17551778
exit_sender.send(()).unwrap();
1779+
1780+
let log_entries = nodes[0].logger.lines.lock().unwrap();
1781+
let expected_log = "Persisting scorer after update".to_string();
1782+
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
17561783
});
17571784

17581785
let (r1, r2) = tokio::join!(t1, t2);

0 commit comments

Comments
 (0)