Skip to content

Commit 2aa91ff

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 bc319a6 commit 2aa91ff

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lightning/src/ln/channel.rs

+10
Original file line numberDiff line numberDiff line change
@@ -5044,10 +5044,20 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50445044
self.pending_monitor_updates.is_empty()
50455045
}
50465046

5047+
pub fn complete_all_mon_updates_through(&mut self, update_id: u64) {
5048+
self.pending_monitor_updates.retain(|upd| upd.update.update_id > update_id);
5049+
}
5050+
50475051
pub fn complete_one_mon_update(&mut self, update_id: u64) {
50485052
self.pending_monitor_updates.retain(|upd| upd.update.update_id != update_id);
50495053
}
50505054

5055+
/// Returns an iterator over all unblocked monitor updates which have not yet completed.
5056+
pub fn uncompleted_unblocked_mon_updates(&self) -> impl Iterator<Item=&ChannelMonitorUpdate> {
5057+
self.pending_monitor_updates.iter()
5058+
.filter_map(|upd| if upd.blocked { None } else { Some(&upd.update) })
5059+
}
5060+
50515061
/// Returns true if funding_created was sent/received.
50525062
pub fn is_funding_initiated(&self) -> bool {
50535063
self.channel_state >= ChannelState::FundingSent as u32

lightning/src/ln/channelmanager.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -7788,7 +7788,10 @@ where
77887788
}
77897789
}
77907790
} else {
7791-
log_info!(args.logger, "Successfully loaded channel {}", log_bytes!(channel.channel_id()));
7791+
log_info!(args.logger, "Successfully loaded channel {} at update_id {} against monitor at update id {}",
7792+
log_bytes!(channel.channel_id()), channel.get_latest_monitor_update_id(),
7793+
monitor.get_latest_update_id());
7794+
channel.complete_all_mon_updates_through(monitor.get_latest_update_id());
77927795
if let Some(short_channel_id) = channel.get_short_channel_id() {
77937796
short_to_chan_info.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
77947797
}
@@ -7903,6 +7906,23 @@ where
79037906
}
79047907
}
79057908

7909+
for (node_id, peer_mtx) in per_peer_state.iter() {
7910+
let peer_state = peer_mtx.lock().unwrap();
7911+
for (_, chan) in peer_state.channel_by_id.iter() {
7912+
for monitor_update in chan.uncompleted_unblocked_mon_updates() {
7913+
if let Some(funding_outpoint) = chan.get_funding_txo() {
7914+
log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for channel {}",
7915+
monitor_update.update_id, log_bytes!(funding_outpoint.to_channel_id()));
7916+
pending_background_events.push(
7917+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(
7918+
(*node_id, funding_outpoint, monitor_update.clone())));
7919+
} else {
7920+
return Err(DecodeError::InvalidValue);
7921+
}
7922+
}
7923+
}
7924+
}
7925+
79067926
let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
79077927
let highest_seen_timestamp: u32 = Readable::read(reader)?;
79087928

0 commit comments

Comments
 (0)