Skip to content

Commit bb22a71

Browse files
committed
Automatically prune NetworkGraph of stale channels hourly in BP
1 parent f597c8e commit bb22a71

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 19 additions & 1 deletion
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,18 @@ 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+
// Note that we want to run a graph prune once not long after startup before falling
219+
// back to our usual hourly prunes. This avoids short-lived clients never pruning their
220+
// network graph. We default here to once a minute after startup.
221+
let mut last_prune_call = Instant::now() + Duration::from_secs(60) - Duration::from_secs(NETWORK_PRUNE_TIMER);
222+
213223
loop {
214224
peer_manager.process_events();
215225
channel_manager.process_pending_events(&event_handler);
@@ -247,6 +257,14 @@ impl BackgroundProcessor {
247257
peer_manager.timer_tick_occurred();
248258
last_ping_call = Instant::now();
249259
}
260+
261+
if last_prune_call.elapsed().as_secs() > NETWORK_PRUNE_TIMER {
262+
if let Some(ref handler) = net_graph_msg_handler {
263+
log_trace!(logger, "Pruning network graph of stale entries");
264+
handler.network_graph().remove_stale_channels();
265+
last_prune_call = Instant::now();
266+
}
267+
}
250268
}
251269
});
252270
Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) }

lightning/src/routing/network_graph.rs

Lines changed: 8 additions & 0 deletions
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.
@@ -1069,6 +1075,8 @@ impl NetworkGraph {
10691075
/// updates every two weeks, the non-normative section of BOLT 7 currently suggests that
10701076
/// pruning occur for updates which are at least two weeks old, which we implement here.
10711077
///
1078+
/// Note that for users of the `lightning-background-processor` crate this method may be
1079+
/// automatically called regularly for you.
10721080
///
10731081
/// This method is only available with the `std` feature. See
10741082
/// [`NetworkGraph::remove_stale_channels_with_time`] for `no-std` use.

0 commit comments

Comments
 (0)