From: Alec Chen Date: Mon, 24 Apr 2023 21:37:05 +0000 (-0500) Subject: Persist scorer upon update based on event handling X-Git-Tag: v0.0.116-alpha1~34^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=2afbdf5d1c1b531b08950ddca9143450ba458245;p=rust-lightning Persist scorer upon update based on event handling Now that we persist the scorer upon events, we extend timer persistence from 30 seconds to 1 hour, similar to network graph persistence. --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 406c56b25..f72e98212 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -107,7 +107,7 @@ const PING_TIMER: u64 = 1; const NETWORK_PRUNE_TIMER: u64 = 60 * 60; #[cfg(not(test))] -const SCORER_PERSIST_TIMER: u64 = 30; +const SCORER_PERSIST_TIMER: u64 = 60 * 60; #[cfg(test)] const SCORER_PERSIST_TIMER: u64 = 1; @@ -235,9 +235,11 @@ fn handle_network_graph_update( } } +/// Updates scorer based on event and returns whether an update occurred so we can decide whether +/// to persist. fn update_scorer<'a, S: 'static + Deref + Send + Sync, SC: 'a + WriteableScore<'a>>( scorer: &'a S, event: &Event -) { +) -> bool { let mut score = scorer.lock(); match event { Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => { @@ -257,8 +259,9 @@ fn update_scorer<'a, S: 'static + Deref + Send + Sync, SC: 'a + Wri Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => { score.probe_failed(path, *scid); }, - _ => {}, + _ => return false, } + true } macro_rules! define_run_body { @@ -629,12 +632,19 @@ where let network_graph = gossip_sync.network_graph(); let event_handler = &event_handler; let scorer = &scorer; + let logger = &logger; + let persister = &persister; async move { if let Some(network_graph) = network_graph { handle_network_graph_update(network_graph, &event) } if let Some(ref scorer) = scorer { - update_scorer(scorer, &event); + if update_scorer(scorer, &event) { + log_trace!(logger, "Persisting scorer after update"); + if let Err(e) = persister.persist_scorer(&scorer) { + log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e) + } + } } event_handler(event).await; } @@ -772,7 +782,12 @@ impl BackgroundProcessor { handle_network_graph_update(network_graph, &event) } if let Some(ref scorer) = scorer { - update_scorer(scorer, &event); + if update_scorer(scorer, &event) { + log_trace!(logger, "Persisting scorer after update"); + if let Err(e) = persister.persist_scorer(&scorer) { + log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e) + } + } } event_handler.handle_event(event); }; @@ -1724,6 +1739,10 @@ mod tests { if !std::thread::panicking() { bg_processor.stop().unwrap(); } + + let log_entries = nodes[0].logger.lines.lock().unwrap(); + let expected_log = "Persisting scorer after update".to_string(); + assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5); } #[tokio::test] @@ -1766,6 +1785,10 @@ mod tests { let t2 = tokio::spawn(async move { do_test_payment_path_scoring!(nodes, receiver.recv().await); exit_sender.send(()).unwrap(); + + let log_entries = nodes[0].logger.lines.lock().unwrap(); + let expected_log = "Persisting scorer after update".to_string(); + assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5); }); let (r1, r2) = tokio::join!(t1, t2);