Skip to content

Commit 6aca7e1

Browse files
authored
Merge pull request #2226 from alecchendev/2023-04-persist-network-graph-on-rgs
Update BP `NetworkGraph` and `Scorer` persist frequency
2 parents 498f233 + 2afbdf5 commit 6aca7e1

File tree

1 file changed

+36
-7
lines changed
  • lightning-background-processor/src

1 file changed

+36
-7
lines changed

lightning-background-processor/src/lib.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const PING_TIMER: u64 = 1;
108108
const NETWORK_PRUNE_TIMER: u64 = 60 * 60;
109109

110110
#[cfg(not(test))]
111-
const SCORER_PERSIST_TIMER: u64 = 30;
111+
const SCORER_PERSIST_TIMER: u64 = 60 * 60;
112112
#[cfg(test)]
113113
const SCORER_PERSIST_TIMER: u64 = 1;
114114

@@ -236,9 +236,11 @@ fn handle_network_graph_update<L: Deref>(
236236
}
237237
}
238238

239+
/// Updates scorer based on event and returns whether an update occurred so we can decide whether
240+
/// to persist.
239241
fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
240242
scorer: &'a S, event: &Event
241-
) {
243+
) -> bool {
242244
let mut score = scorer.lock();
243245
match event {
244246
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
258260
Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
259261
score.probe_failed(path, *scid);
260262
},
261-
_ => {},
263+
_ => return false,
262264
}
265+
true
263266
}
264267

265268
macro_rules! define_run_body {
@@ -352,9 +355,15 @@ macro_rules! define_run_body {
352355
// Note that we want to run a graph prune once not long after startup before
353356
// falling back to our usual hourly prunes. This avoids short-lived clients never
354357
// 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.
356360
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 {
358367
// The network graph must not be pruned while rapid sync completion is pending
359368
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
360369
#[cfg(feature = "std")] {
@@ -616,12 +625,19 @@ where
616625
let network_graph = gossip_sync.network_graph();
617626
let event_handler = &event_handler;
618627
let scorer = &scorer;
628+
let logger = &logger;
629+
let persister = &persister;
619630
async move {
620631
if let Some(network_graph) = network_graph {
621632
handle_network_graph_update(network_graph, &event)
622633
}
623634
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+
}
625641
}
626642
event_handler(event).await;
627643
}
@@ -751,7 +767,12 @@ impl BackgroundProcessor {
751767
handle_network_graph_update(network_graph, &event)
752768
}
753769
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+
}
755776
}
756777
event_handler.handle_event(event);
757778
};
@@ -1708,6 +1729,10 @@ mod tests {
17081729
if !std::thread::panicking() {
17091730
bg_processor.stop().unwrap();
17101731
}
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);
17111736
}
17121737

17131738
#[tokio::test]
@@ -1750,6 +1775,10 @@ mod tests {
17501775
let t2 = tokio::spawn(async move {
17511776
do_test_payment_path_scoring!(nodes, receiver.recv().await);
17521777
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);
17531782
});
17541783

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

0 commit comments

Comments
 (0)