Skip to content

Commit cd65bb4

Browse files
committed
Prune and persist network graph on startup
Previously, we'd initially prune/persist after 60 seconds, which may be too long or too short. Now we prune/persist as soon as we can on startup, whether that be immediately for normal P2P gossip, or after the first RGS update.
1 parent ec3aa49 commit cd65bb4

File tree

1 file changed

+6
-13
lines changed
  • lightning-background-processor/src

1 file changed

+6
-13
lines changed

lightning-background-processor/src/lib.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ const SCORER_PERSIST_TIMER: u64 = 30;
111111
#[cfg(test)]
112112
const SCORER_PERSIST_TIMER: u64 = 1;
113113

114-
#[cfg(not(test))]
115-
const FIRST_NETWORK_PRUNE_TIMER: u64 = 60;
116-
#[cfg(test)]
117-
const FIRST_NETWORK_PRUNE_TIMER: u64 = 1;
118-
119114
#[cfg(not(test))]
120115
const REBROADCAST_TIMER: u64 = 30;
121116
#[cfg(test)]
@@ -126,7 +121,7 @@ const REBROADCAST_TIMER: u64 = 1;
126121
const fn min_u64(a: u64, b: u64) -> u64 { if a < b { a } else { b } }
127122
#[cfg(feature = "futures")]
128123
const FASTEST_TIMER: u64 = min_u64(min_u64(FRESHNESS_TIMER, PING_TIMER),
129-
min_u64(SCORER_PERSIST_TIMER, min_u64(FIRST_NETWORK_PRUNE_TIMER, REBROADCAST_TIMER)));
124+
min_u64(SCORER_PERSIST_TIMER, REBROADCAST_TIMER));
130125

131126
/// Either [`P2PGossipSync`] or [`RapidGossipSync`].
132127
pub enum GossipSync<
@@ -275,7 +270,7 @@ macro_rules! define_run_body {
275270

276271
let mut last_freshness_call = $get_timer(FRESHNESS_TIMER);
277272
let mut last_ping_call = $get_timer(PING_TIMER);
278-
let mut last_prune_call = $get_timer(FIRST_NETWORK_PRUNE_TIMER);
273+
let mut last_prune_call = $get_timer(NETWORK_PRUNE_TIMER);
279274
let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
280275
let mut last_rebroadcast_call = $get_timer(REBROADCAST_TIMER);
281276
let mut have_pruned = false;
@@ -352,8 +347,7 @@ macro_rules! define_run_body {
352347
// falling back to our usual hourly prunes. This avoids short-lived clients never
353348
// pruning their network graph. We run once 60 seconds after startup before
354349
// continuing our normal cadence.
355-
let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER };
356-
if $timer_elapsed(&mut last_prune_call, prune_timer) {
350+
if !have_pruned || $timer_elapsed(&mut last_prune_call, NETWORK_PRUNE_TIMER) {
357351
// The network graph must not be pruned while rapid sync completion is pending
358352
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
359353
#[cfg(feature = "std")] {
@@ -371,8 +365,7 @@ macro_rules! define_run_body {
371365

372366
have_pruned = true;
373367
}
374-
let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER };
375-
last_prune_call = $get_timer(prune_timer);
368+
last_prune_call = $get_timer(NETWORK_PRUNE_TIMER);
376369
}
377370

378371
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
@@ -1557,7 +1550,7 @@ mod tests {
15571550
let background_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].rapid_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone()));
15581551

15591552
do_test_not_pruning_network_graph_until_graph_sync_completion!(nodes,
1560-
receiver.recv_timeout(Duration::from_secs(super::FIRST_NETWORK_PRUNE_TIMER * 5)),
1553+
receiver.recv_timeout(Duration::from_secs(5)),
15611554
std::thread::sleep(Duration::from_millis(1)));
15621555

15631556
background_processor.stop().unwrap();
@@ -1592,7 +1585,7 @@ mod tests {
15921585
do_test_not_pruning_network_graph_until_graph_sync_completion!(nodes, {
15931586
let mut i = 0;
15941587
loop {
1595-
tokio::time::sleep(Duration::from_secs(super::FIRST_NETWORK_PRUNE_TIMER)).await;
1588+
tokio::time::sleep(Duration::from_secs(1)).await;
15961589
if let Ok(()) = receiver.try_recv() { break Ok::<(), ()>(()); }
15971590
assert!(i < 5);
15981591
i += 1;

0 commit comments

Comments
 (0)