Skip to content

Commit d33687d

Browse files
committed
Don't generate dup force-close ChannelMonitorUpdates on startup
On startup, if we have a channel which was closed immediately before shutdown such that the `ChannelMonitorUpdate` marking the channel as closed is still in-flight, it doesn't make sense to generate a fresh `ChannelMonitorUpdate` marking the channel as closed immediately after the existing in-flight one. Here we detect this case and drop the extra update, though its not all that harmful it does avoid some test changes in the coming commits.
1 parent f9374b8 commit d33687d

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

lightning/src/ln/channelmanager.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -13599,8 +13599,36 @@ where
1359913599
}
1360013600
}
1360113601

13602-
// Note that we have to do the above replays before we push new monitor updates.
13603-
pending_background_events.append(&mut close_background_events);
13602+
// The newly generated `close_background_events` have to be added after any updates that
13603+
// were already in-flight on shutdown, so we append them here.
13604+
pending_background_events.reserve(close_background_events.len());
13605+
'each_bg_event: for mut new_event in close_background_events {
13606+
if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
13607+
counterparty_node_id, funding_txo, channel_id, update,
13608+
} = &mut new_event {
13609+
debug_assert_eq!(update.updates.len(), 1);
13610+
debug_assert!(matches!(update.updates[0], ChannelMonitorUpdateStep::ChannelForceClosed { .. }));
13611+
for pending_event in pending_background_events.iter() {
13612+
if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
13613+
counterparty_node_id: pending_cp, funding_txo: pending_funding,
13614+
channel_id: pending_chan_id, update: pending_update,
13615+
} = pending_event {
13616+
let for_same_channel = counterparty_node_id == pending_cp
13617+
&& funding_txo == pending_funding
13618+
&& channel_id == pending_chan_id;
13619+
if for_same_channel {
13620+
if pending_update.updates.iter().any(|upd| matches!(upd, ChannelMonitorUpdateStep::ChannelForceClosed { .. })) {
13621+
// If the background event we're looking at is just
13622+
// force-closing the channel which already has a pending
13623+
// force-close update, no need to duplicate it.
13624+
continue 'each_bg_event;
13625+
}
13626+
}
13627+
}
13628+
}
13629+
}
13630+
pending_background_events.push(new_event);
13631+
}
1360413632

1360513633
// If there's any preimages for forwarded HTLCs hanging around in ChannelMonitors we
1360613634
// should ensure we try them again on the inbound edge. We put them here and do so after we

0 commit comments

Comments
 (0)