Skip to content

Commit 5e6e74c

Browse files
committed
Fix update_id gap during force_shutdown
When a channel is force-closed, there might be blocked monitor updates not yet applied. But `latest_monitor_update_id` has been incremented and assigned to these updates. This results in a panic when trying to apply the `ChannelForceClosed` update. Use the unblocked update id instead. Resolves: #3857
1 parent 3333b6d commit 5e6e74c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,13 @@ where
34403440
self.latest_monitor_update_id
34413441
}
34423442

3443+
pub fn get_latest_unblocked_monitor_update_id(&self) -> u64 {
3444+
if self.blocked_monitor_updates.is_empty() {
3445+
return self.get_latest_monitor_update_id();
3446+
}
3447+
self.blocked_monitor_updates[0].update.update_id - 1
3448+
}
3449+
34433450
pub fn should_announce(&self) -> bool {
34443451
self.config.announce_for_forwarding
34453452
}
@@ -5220,7 +5227,7 @@ where
52205227
// monitor update to the user, even if we return one).
52215228
// See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more.
52225229
if !self.channel_state.is_pre_funded_state() {
5223-
self.latest_monitor_update_id += 1;
5230+
self.latest_monitor_update_id = self.get_latest_unblocked_monitor_update_id() + 1;
52245231
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), ChannelMonitorUpdate {
52255232
update_id: self.latest_monitor_update_id,
52265233
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
@@ -8582,10 +8589,8 @@ where
85828589
}
85838590

85848591
/// Gets the latest [`ChannelMonitorUpdate`] ID which has been released and is in-flight.
8585-
#[rustfmt::skip]
85868592
pub fn get_latest_unblocked_monitor_update_id(&self) -> u64 {
8587-
if self.context.blocked_monitor_updates.is_empty() { return self.context.get_latest_monitor_update_id(); }
8588-
self.context.blocked_monitor_updates[0].update.update_id - 1
8593+
self.context.get_latest_unblocked_monitor_update_id()
85898594
}
85908595

85918596
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a

0 commit comments

Comments
 (0)