Skip to content

Commit dd21fce

Browse files
committed
Correctly handle new ChannelMonitorUpdates to old post-FC chans
In 0.1 (1481216) we started setting `ChannelMonitorUpdate::update_id` to a non-`u64::MAX` value for updates generated after a channel has been closed. This is great, but in 71a364c we then started calculating the next `update_id` by incrementing the last `update_id` we saw when we started and were looking at the `ChannelMonitor`. However, the last-applied `update_id` may well be `u64::MAX` for old `ChannelMonitor`s which were closed prior to 0.1. In that case the increment would overflow. Here we fix this naively by simply replacing the increment with a `saturating_add`. While its possible this will result in a `ChannelMonitorUpdate` being tracked as in-flight (only for the `ReleasePaymentComplete` updates added in 71a364c) at the same `update_id` as other updates already in-flight and handling post-`ChannelMonitorUpdate` actions too early, this should only apply to releasing payment complete updates, which have no post-`ChannelMonitorUpdate` action. Its also possible that this leads to a regression in the future, where we have some new post-closure update that does have a post-`ChannelMonitorUpdate` action and we run it too early, but by then presumably its fairly rare to have a `ChannelMonitor` for a channel closed pre-0.1 that still needs multiple updates.
1 parent 54ed941 commit dd21fce

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,8 @@ where
16011601
/// the highest `update_id` of all the pending in-flight updates (note that any pending updates
16021602
/// not yet applied sitting in [`ChannelManager::pending_background_events`] will also be
16031603
/// considered as they are also in [`Self::in_flight_monitor_updates`]).
1604+
///
1605+
/// Note that channels which were closed prior to LDK 0.1 may have a value here of `u64::MAX`.
16041606
closed_channel_monitor_update_ids: BTreeMap<ChannelId, u64>,
16051607
/// The peer is currently connected (i.e. we've seen a
16061608
/// [`BaseMessageHandler::peer_connected`] and no corresponding
@@ -13262,7 +13264,8 @@ where
1326213264
.closed_channel_monitor_update_ids
1326313265
.get_mut(&channel_id)
1326413266
.expect("Channels originating a payment resolution must have a monitor");
13265-
*update_id += 1;
13267+
// Note that for channels closed pre-0.1, the latest update_id is `u64::MAX`.
13268+
*update_id = update_id.saturating_add(1);
1326613269

1326713270
let update = ChannelMonitorUpdate {
1326813271
update_id: *update_id,
@@ -16523,7 +16526,9 @@ where
1652316526
should_queue_fc_update = !monitor.no_further_updates_allowed();
1652416527
let mut latest_update_id = monitor.get_latest_update_id();
1652516528
if should_queue_fc_update {
16526-
latest_update_id += 1;
16529+
// Note that for channels closed pre-0.1, the latest update_id is
16530+
// `u64::MAX`.
16531+
latest_update_id = latest_update_id.saturating_add(1);
1652716532
}
1652816533
per_peer_state
1652916534
.entry(counterparty_node_id)
@@ -17170,7 +17175,9 @@ where
1717017175
.closed_channel_monitor_update_ids
1717117176
.get_mut(channel_id)
1717217177
.expect("Channels originating a preimage must have a monitor");
17173-
*update_id += 1;
17178+
// Note that for channels closed pre-0.1, the latest
17179+
// update_id is `u64::MAX`.
17180+
*update_id = update_id.saturating_add(1);
1717417181

1717517182
pending_background_events.push(BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
1717617183
counterparty_node_id: monitor.get_counterparty_node_id(),

0 commit comments

Comments
 (0)