Skip to content

Commit 09db50f

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 3480b41 commit 09db50f

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13215,8 +13215,36 @@ where
1321513215
}
1321613216
}
1321713217

13218-
// Note that we have to do the above replays before we push new monitor updates.
13219-
pending_background_events.append(&mut close_background_events);
13218+
// The newly generated `close_background_events` have to be added after any updates that
13219+
// were already in-flight on shutdown, so we append them here.
13220+
pending_background_events.reserve(close_background_events.len());
13221+
'each_bg_event: for mut new_event in close_background_events {
13222+
if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
13223+
counterparty_node_id, funding_txo, channel_id, update,
13224+
} = &mut new_event {
13225+
debug_assert_eq!(update.updates.len(), 1);
13226+
debug_assert!(matches!(update.updates[0], ChannelMonitorUpdateStep::ChannelForceClosed { .. }));
13227+
for pending_event in pending_background_events.iter() {
13228+
if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
13229+
counterparty_node_id: pending_cp, funding_txo: pending_funding,
13230+
channel_id: pending_chan_id, update: pending_update,
13231+
} = pending_event {
13232+
let for_same_channel = counterparty_node_id == pending_cp
13233+
&& funding_txo == pending_funding
13234+
&& channel_id == pending_chan_id;
13235+
if for_same_channel {
13236+
if pending_update.updates.iter().any(|upd| matches!(upd, ChannelMonitorUpdateStep::ChannelForceClosed { .. })) {
13237+
// If the background event we're looking at is just
13238+
// force-closing the channel which already has a pending
13239+
// force-close update, no need to duplicate it.
13240+
continue 'each_bg_event;
13241+
}
13242+
}
13243+
}
13244+
}
13245+
}
13246+
pending_background_events.push(new_event);
13247+
}
1322013248

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

0 commit comments

Comments
 (0)