Skip to content

Commit 4f71640

Browse files
committed
Reapply pending ChannelMonitorUpdates on startup
If a `ChannelMonitorUpdate` was created and given to the user but left uncompleted when the `ChannelManager` is persisted prior to a restart, the user likely lost the `ChannelMonitorUpdate`(s). Thus, we need to replay them for the user, which we do here using the new `BackgroundEvent::MonitorUpdateRegeneratedOnStartup` variant.
1 parent 4bb8b0e commit 4f71640

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5050,10 +5050,20 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50505050
self.pending_monitor_updates.is_empty()
50515051
}
50525052

5053+
pub fn complete_all_mon_updates_through(&mut self, update_id: u64) {
5054+
self.pending_monitor_updates.retain(|upd| upd.update.update_id > update_id);
5055+
}
5056+
50535057
pub fn complete_one_mon_update(&mut self, update_id: u64) {
50545058
self.pending_monitor_updates.retain(|upd| upd.update.update_id != update_id);
50555059
}
50565060

5061+
/// Returns an iterator over all unblocked monitor updates which have not yet completed.
5062+
pub fn uncompleted_unblocked_mon_updates(&self) -> impl Iterator<Item=&ChannelMonitorUpdate> {
5063+
self.pending_monitor_updates.iter()
5064+
.filter_map(|upd| if upd.blocked { None } else { Some(&upd.update) })
5065+
}
5066+
50575067
/// Returns true if funding_created was sent/received.
50585068
pub fn is_funding_initiated(&self) -> bool {
50595069
self.channel_state >= ChannelState::FundingSent as u32

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7880,7 +7880,10 @@ where
78807880
}
78817881
}
78827882
} else {
7883-
log_info!(args.logger, "Successfully loaded channel {}", log_bytes!(channel.channel_id()));
7883+
log_info!(args.logger, "Successfully loaded channel {} at update_id {} against monitor at update id {}",
7884+
log_bytes!(channel.channel_id()), channel.get_latest_monitor_update_id(),
7885+
monitor.get_latest_update_id());
7886+
channel.complete_all_mon_updates_through(monitor.get_latest_update_id());
78847887
if let Some(short_channel_id) = channel.get_short_channel_id() {
78857888
short_to_chan_info.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
78867889
}
@@ -7994,6 +7997,23 @@ where
79947997
}
79957998
}
79967999

8000+
for (node_id, peer_mtx) in per_peer_state.iter() {
8001+
let peer_state = peer_mtx.lock().unwrap();
8002+
for (_, chan) in peer_state.channel_by_id.iter() {
8003+
for monitor_update in chan.uncompleted_unblocked_mon_updates() {
8004+
if let Some(funding_outpoint) = chan.get_funding_txo() {
8005+
log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for channel {}",
8006+
monitor_update.update_id, log_bytes!(funding_outpoint.to_channel_id()));
8007+
pending_background_events.push(
8008+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(
8009+
(*node_id, funding_outpoint, monitor_update.clone())));
8010+
} else {
8011+
return Err(DecodeError::InvalidValue);
8012+
}
8013+
}
8014+
}
8015+
}
8016+
79978017
let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
79988018
let highest_seen_timestamp: u32 = Readable::read(reader)?;
79998019

0 commit comments

Comments
 (0)