Skip to content

Commit 73e8dc4

Browse files
committed
Automatically prune NetworkGraph of stale channels hourly in BP
1 parent cd43ff4 commit 73e8dc4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lightning-background-processor/src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use std::ops::Deref;
3434
/// [`ChannelManager`] persistence should be done in the background.
3535
/// * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
3636
/// at the appropriate intervals.
37+
/// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`NetGraphMsgHandler`] is provided to
38+
/// [`BackgroundProcessor::start`]).
3739
///
3840
/// It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
3941
/// upon as doing so may result in high latency.
@@ -68,6 +70,9 @@ const PING_TIMER: u64 = 30;
6870
#[cfg(test)]
6971
const PING_TIMER: u64 = 1;
7072

73+
/// Prune the network graph of stale entries hourly.
74+
const NETWORK_PRUNE_TIMER: u64 = 60 * 60;
75+
7176
/// Trait which handles persisting a [`ChannelManager`] to disk.
7277
///
7378
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -203,13 +208,16 @@ impl BackgroundProcessor {
203208
let stop_thread = Arc::new(AtomicBool::new(false));
204209
let stop_thread_clone = stop_thread.clone();
205210
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
206-
let event_handler = DecoratingEventHandler { event_handler, net_graph_msg_handler };
211+
let event_handler = DecoratingEventHandler { event_handler, net_graph_msg_handler: net_graph_msg_handler.as_ref().map(|t| t.deref()) };
207212

208213
log_trace!(logger, "Calling ChannelManager's timer_tick_occurred on startup");
209214
channel_manager.timer_tick_occurred();
210215

211216
let mut last_freshness_call = Instant::now();
212217
let mut last_ping_call = Instant::now();
218+
let mut last_prune_call = Instant::now();
219+
let mut have_pruned = false;
220+
213221
loop {
214222
peer_manager.process_events();
215223
channel_manager.process_pending_events(&event_handler);
@@ -247,6 +255,19 @@ impl BackgroundProcessor {
247255
peer_manager.timer_tick_occurred();
248256
last_ping_call = Instant::now();
249257
}
258+
259+
// Note that we want to run a graph prune once not long after startup before
260+
// falling back to our usual hourly prunes. This avoids short-lived clients never
261+
// pruning their network graph. We run once 60 seconds after startup before
262+
// continuing our normal cadence.
263+
if last_prune_call.elapsed().as_secs() > if have_pruned { NETWORK_PRUNE_TIMER } else { 60 } {
264+
if let Some(ref handler) = net_graph_msg_handler {
265+
log_trace!(logger, "Pruning network graph of stale entries");
266+
handler.network_graph().remove_stale_channels();
267+
last_prune_call = Instant::now();
268+
have_pruned = true;
269+
}
270+
}
250271
}
251272
});
252273
Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) }

lightning/src/routing/network_graph.rs

+8
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ where C::Target: chain::Access, L::Target: Logger
249249
self.chain_access = chain_access;
250250
}
251251

252+
/// Gets a reference to the underlying [`NetworkGraph`] which was provided in
253+
/// [`NetGraphMsgHandler::new`].
254+
pub fn network_graph(&self) -> &G {
255+
&self.network_graph
256+
}
257+
252258
/// Returns true when a full routing table sync should be performed with a peer.
253259
fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
254260
//TODO: Determine whether to request a full sync based on the network map.
@@ -1074,6 +1080,8 @@ impl NetworkGraph {
10741080
/// updates every two weeks, the non-normative section of BOLT 7 currently suggests that
10751081
/// pruning occur for updates which are at least two weeks old, which we implement here.
10761082
///
1083+
/// Note that for users of the `lightning-background-processor` crate this method may be
1084+
/// automatically called regularly for you.
10771085
///
10781086
/// This method is only available with the `std` feature. See
10791087
/// [`NetworkGraph::remove_stale_channels_with_time`] for `no-std` use.

0 commit comments

Comments
 (0)