Skip to content

Commit b0938ef

Browse files
committed
Avoid persisting a ChannelManager update after each timer tick
Currently, when a user calls `ChannelManager::timer_tick_occurred` we always set the persister's update flag to true. This results in a ChannelManager persistence after each timer tick, even when nothing happened. Instead, we add a new flag to `PersistenceNotifierGuard` to indicate if we should skip setting the update flag.
1 parent 63377a1 commit b0938ef

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ impl BestBlock {
530530
/// updates are ready for persistence).
531531
struct PersistenceNotifierGuard<'a> {
532532
persistence_notifier: &'a PersistenceNotifier,
533+
persistence_update: bool,
533534
// We hold onto this result so the lock doesn't get released immediately.
534535
_read_guard: RwLockReadGuard<'a, ()>,
535536
}
@@ -540,14 +541,17 @@ impl<'a> PersistenceNotifierGuard<'a> {
540541

541542
Self {
542543
persistence_notifier: notifier,
544+
persistence_update: true,
543545
_read_guard: read_guard,
544546
}
545547
}
546548
}
547549

548550
impl<'a> Drop for PersistenceNotifierGuard<'a> {
549551
fn drop(&mut self) {
550-
self.persistence_notifier.notify();
552+
if self.persistence_update {
553+
self.persistence_notifier.notify();
554+
}
551555
}
552556
}
553557

@@ -2094,9 +2098,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20942098
/// BroadcastChannelUpdate events in timer_tick_occurred.
20952099
///
20962100
/// Expects the caller to have a total_consistency_lock read lock.
2097-
fn process_background_events(&self) {
2101+
fn process_background_events(&self) -> bool {
20982102
let mut background_events = Vec::new();
20992103
mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
2104+
if background_events.is_empty() {
2105+
return false;
2106+
}
2107+
21002108
for event in background_events.drain(..) {
21012109
match event {
21022110
BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)) => {
@@ -2106,6 +2114,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21062114
},
21072115
}
21082116
}
2117+
true
21092118
}
21102119

21112120
#[cfg(any(test, feature = "_test_utils"))]
@@ -2121,8 +2130,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21212130
///
21222131
/// Note that in some rare cases this may generate a `chain::Watch::update_channel` call.
21232132
pub fn timer_tick_occurred(&self) {
2124-
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
2125-
self.process_background_events();
2133+
let mut persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
2134+
let mut did_work = self.process_background_events();
21262135

21272136
let mut channel_state_lock = self.channel_state.lock().unwrap();
21282137
let channel_state = &mut *channel_state_lock;
@@ -2138,6 +2147,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21382147
msg: update
21392148
});
21402149
}
2150+
did_work = true;
21412151
chan.set_update_status(UpdateStatus::Disabled);
21422152
},
21432153
UpdateStatus::EnabledStaged if chan.is_live() => {
@@ -2146,11 +2156,16 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21462156
msg: update
21472157
});
21482158
}
2159+
did_work = true;
21492160
chan.set_update_status(UpdateStatus::Enabled);
21502161
},
21512162
_ => {},
21522163
}
21532164
}
2165+
2166+
if !did_work {
2167+
persistence_guard.persistence_update = false;
2168+
}
21542169
}
21552170

21562171
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect

0 commit comments

Comments
 (0)