Skip to content

Commit 3d51b11

Browse files
authored
Merge pull request #872 from valentinewallace/timer-tick-in-bg-processor
Call timer_tick_occurred in BackgroundProcessor
2 parents bc899dc + e5b99c1 commit 3d51b11

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

background-processor/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use std::time::{Duration, Instant};
2727
/// * Monitoring whether the ChannelManager needs to be re-persisted to disk, and if so,
2828
/// writing it to disk/backups by invoking the callback given to it at startup.
2929
/// ChannelManager persistence should be done in the background.
30-
/// * Calling `ChannelManager::timer_chan_freshness_every_min()` every minute (can be done in the
30+
/// * Calling `ChannelManager::timer_tick_occurred()` and
31+
/// `PeerManager::timer_tick_occurred()` every minute (can be done in the
3132
/// background).
3233
///
3334
/// Note that if ChannelManager persistence fails and the persisted manager becomes out-of-date,
@@ -42,9 +43,9 @@ pub struct BackgroundProcessor {
4243
}
4344

4445
#[cfg(not(test))]
45-
const CHAN_FRESHNESS_TIMER: u64 = 60;
46+
const FRESHNESS_TIMER: u64 = 60;
4647
#[cfg(test)]
47-
const CHAN_FRESHNESS_TIMER: u64 = 1;
48+
const FRESHNESS_TIMER: u64 = 1;
4849

4950
impl BackgroundProcessor {
5051
/// Start a background thread that takes care of responsibilities enumerated in the top-level
@@ -101,9 +102,10 @@ impl BackgroundProcessor {
101102
log_trace!(logger, "Terminating background processor.");
102103
return Ok(());
103104
}
104-
if current_time.elapsed().as_secs() > CHAN_FRESHNESS_TIMER {
105-
log_trace!(logger, "Calling manager's timer_chan_freshness_every_min");
106-
channel_manager.timer_chan_freshness_every_min();
105+
if current_time.elapsed().as_secs() > FRESHNESS_TIMER {
106+
log_trace!(logger, "Calling ChannelManager's and PeerManager's timer_tick_occurred");
107+
channel_manager.timer_tick_occurred();
108+
peer_manager.timer_tick_occurred();
107109
current_time = Instant::now();
108110
}
109111
}
@@ -294,16 +296,16 @@ mod tests {
294296
}
295297

296298
#[test]
297-
fn test_chan_freshness_called() {
298-
// Test that ChannelManager's `timer_chan_freshness_every_min` is called every
299-
// `CHAN_FRESHNESS_TIMER`.
300-
let nodes = create_nodes(1, "test_chan_freshness_called".to_string());
299+
fn test_timer_tick_called() {
300+
// Test that ChannelManager's and PeerManager's `timer_tick_occurred` is called every
301+
// `FRESHNESS_TIMER`.
302+
let nodes = create_nodes(1, "test_timer_tick_called".to_string());
301303
let data_dir = nodes[0].persister.get_data_dir();
302304
let callback = move |node: &ChannelManager<InMemorySigner, Arc<ChainMonitor>, Arc<test_utils::TestBroadcaster>, Arc<KeysManager>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>>| FilesystemPersister::persist_manager(data_dir.clone(), node);
303305
let bg_processor = BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
304306
loop {
305307
let log_entries = nodes[0].logger.lines.lock().unwrap();
306-
let desired_log = "Calling manager's timer_chan_freshness_every_min".to_string();
308+
let desired_log = "Calling ChannelManager's and PeerManager's timer_tick_occurred".to_string();
307309
if log_entries.get(&("lightning_background_processor".to_string(), desired_log)).is_some() {
308310
break
309311
}

lightning/src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
250250
/// Liveness is called to fluctuate given peer disconnecton/monitor failures/closing.
251251
/// If channel is public, network should have a liveness view announced by us on a
252252
/// best-effort, which means we may filter out some status transitions to avoid spam.
253-
/// See further timer_chan_freshness_every_min.
253+
/// See further timer_tick_occurred.
254254
#[derive(PartialEq)]
255255
enum UpdateStatus {
256256
/// Status has been gossiped.

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub(super) struct ChannelHolder<Signer: Sign> {
338338
}
339339

340340
/// Events which we process internally but cannot be procsesed immediately at the generation site
341-
/// for some reason. They are handled in timer_chan_freshness_every_min, so may be processed with
341+
/// for some reason. They are handled in timer_tick_occurred, so may be processed with
342342
/// quite some time lag.
343343
enum BackgroundEvent {
344344
/// Handle a ChannelMonitorUpdate that closes a channel, broadcasting its current latest holder
@@ -403,7 +403,7 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
403403
/// ChannelUpdate messages informing peers that the channel is temporarily disabled. To avoid
404404
/// spam due to quick disconnection/reconnection, updates are not sent until the channel has been
405405
/// offline for a full minute. In order to track this, you must call
406-
/// timer_chan_freshness_every_min roughly once per minute, though it doesn't have to be perfect.
406+
/// timer_tick_occurred roughly once per minute, though it doesn't have to be perfect.
407407
///
408408
/// Rather than using a plain ChannelManager, it is preferable to use either a SimpleArcChannelManager
409409
/// a SimpleRefChannelManager, for conciseness. See their documentation for more details, but
@@ -1959,10 +1959,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
19591959
events.append(&mut new_events);
19601960
}
19611961

1962-
/// Free the background events, generally called from timer_chan_freshness_every_min.
1962+
/// Free the background events, generally called from timer_tick_occurred.
19631963
///
19641964
/// Exposed for testing to allow us to process events quickly without generating accidental
1965-
/// BroadcastChannelUpdate events in timer_chan_freshness_every_min.
1965+
/// BroadcastChannelUpdate events in timer_tick_occurred.
19661966
///
19671967
/// Expects the caller to have a total_consistency_lock read lock.
19681968
fn process_background_events(&self) {
@@ -1991,7 +1991,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
19911991
/// This method handles all the details, and must be called roughly once per minute.
19921992
///
19931993
/// Note that in some rare cases this may generate a `chain::Watch::update_channel` call.
1994-
pub fn timer_chan_freshness_every_min(&self) {
1994+
pub fn timer_tick_occurred(&self) {
19951995
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
19961996
self.process_background_events();
19971997

@@ -3274,7 +3274,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32743274
// We cannot broadcast our latest local state via monitor update (as
32753275
// Channel::force_shutdown tries to make us do) as we may still be in initialization,
32763276
// so we track the update internally and handle it when the user next calls
3277-
// timer_chan_freshness_every_min, guaranteeing we're running normally.
3277+
// timer_tick_occurred, guaranteeing we're running normally.
32783278
if let Some((funding_txo, update)) = failure.0.take() {
32793279
assert_eq!(update.updates.len(), 1);
32803280
if let ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast } = update.updates[0] {

lightning/src/ln/functional_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7557,7 +7557,7 @@ fn test_check_htlc_underpaying() {
75577557

75587558
#[test]
75597559
fn test_announce_disable_channels() {
7560-
// Create 2 channels between A and B. Disconnect B. Call timer_chan_freshness_every_min and check for generated
7560+
// Create 2 channels between A and B. Disconnect B. Call timer_tick_occurred and check for generated
75617561
// ChannelUpdate. Reconnect B, reestablish and check there is non-generated ChannelUpdate.
75627562

75637563
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -7573,8 +7573,8 @@ fn test_announce_disable_channels() {
75737573
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
75747574
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
75757575

7576-
nodes[0].node.timer_chan_freshness_every_min(); // dirty -> stagged
7577-
nodes[0].node.timer_chan_freshness_every_min(); // staged -> fresh
7576+
nodes[0].node.timer_tick_occurred(); // dirty -> stagged
7577+
nodes[0].node.timer_tick_occurred(); // staged -> fresh
75787578
let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
75797579
assert_eq!(msg_events.len(), 3);
75807580
for e in msg_events {
@@ -7613,7 +7613,7 @@ fn test_announce_disable_channels() {
76137613
nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[2]);
76147614
handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
76157615

7616-
nodes[0].node.timer_chan_freshness_every_min();
7616+
nodes[0].node.timer_tick_occurred();
76177617
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
76187618
}
76197619

0 commit comments

Comments
 (0)