Skip to content

Commit 5e6a0d5

Browse files
committed
Persist full monitor if there is an error while applying monitor_update
Motivation: When there is an error while applying monitor_update to a channel_monitor, we don't want to persist a 'monitor_update' which results in a failure to apply later while reading 'channel_monitor' with updates from storage. Instead, we should persist the entire 'channel_monitor' here.
1 parent 13eac47 commit 5e6a0d5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,20 @@ where C::Target: chain::Filter,
764764
let monitor = &monitor_state.monitor;
765765
log_trace!(self.logger, "Updating ChannelMonitor for channel {}", log_funding_info!(monitor));
766766
let update_res = monitor.update_monitor(update, &self.broadcaster, &*self.fee_estimator, &self.logger);
767-
if update_res.is_err() {
768-
log_error!(self.logger, "Failed to update ChannelMonitor for channel {}.", log_funding_info!(monitor));
769-
}
770-
// Even if updating the monitor returns an error, the monitor's state will
771-
// still be changed. So, persist the updated monitor despite the error.
767+
772768
let update_id = MonitorUpdateId::from_monitor_update(update);
773769
let mut pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
774-
let persist_res = self.persister.update_persisted_channel(funding_txo, Some(update), monitor, update_id);
770+
let persist_res = if update_res.is_err() {
771+
// Even if updating the monitor returns an error, the monitor's state will
772+
// still be changed. Therefore, we should persist the updated monitor despite the error.
773+
// We don't want to persist a `monitor_update` which results in a failure to apply later
774+
// while reading `channel_monitor` with updates from storage. Instead, we should persist
775+
// the entire `channel_monitor` here.
776+
log_warn!(self.logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor", log_funding_info!(monitor));
777+
self.persister.update_persisted_channel(funding_txo, None, monitor, update_id)
778+
} else {
779+
self.persister.update_persisted_channel(funding_txo, Some(update), monitor, update_id)
780+
};
775781
match persist_res {
776782
ChannelMonitorUpdateStatus::InProgress => {
777783
pending_monitor_updates.push(update_id);

0 commit comments

Comments
 (0)