@@ -3972,39 +3972,6 @@ where
3972
3972
self.chain_monitor.update_channel(funding_txo, &monitor_update)
3973
3973
}
3974
3974
3975
- fn set_closed_chan_next_monitor_update_id(
3976
- peer_state: &mut PeerState<SP>, channel_id: ChannelId, monitor_update: &mut ChannelMonitorUpdate,
3977
- ) {
3978
- match peer_state.closed_channel_monitor_update_ids.entry(channel_id) {
3979
- btree_map::Entry::Vacant(entry) => {
3980
- let is_closing_unupdated_monitor = monitor_update.update_id == 1
3981
- && monitor_update.updates.len() == 1
3982
- && matches!(&monitor_update.updates[0], ChannelMonitorUpdateStep::ChannelForceClosed { .. });
3983
- // If the ChannelMonitorUpdate is closing a channel that never got past initial
3984
- // funding (to have any commitment updates), we'll skip inserting in
3985
- // `locked_close_channel`, allowing us to avoid keeping around the PeerState for
3986
- // that peer. In that specific case we expect no entry in the map here. In any
3987
- // other cases, this is a bug, but in production we go ahead and recover by
3988
- // inserting the update_id and hoping its right.
3989
- debug_assert!(is_closing_unupdated_monitor, "Expected closing monitor against an unused channel, got {:?}", monitor_update);
3990
- if !is_closing_unupdated_monitor {
3991
- entry.insert(monitor_update.update_id);
3992
- }
3993
- },
3994
- btree_map::Entry::Occupied(entry) => {
3995
- // If we're running in a threaded environment its possible we generate updates for
3996
- // a channel that is closing, then apply some preimage update, then go back and
3997
- // apply the close monitor update here. In order to ensure the updates are still
3998
- // well-ordered, we have to use the `closed_channel_monitor_update_ids` map to
3999
- // override the `update_id`, taking care to handle old monitors where the
4000
- // `latest_update_id` is already `u64::MAX`.
4001
- let latest_update_id = entry.into_mut();
4002
- *latest_update_id = latest_update_id.saturating_add(1);
4003
- monitor_update.update_id = *latest_update_id;
4004
- }
4005
- }
4006
- }
4007
-
4008
3975
/// When a channel is removed, two things need to happen:
4009
3976
/// (a) [`locked_close_channel`] must be called in the same `per_peer_state` lock as
4010
3977
/// the channel-closing action,
@@ -7219,8 +7186,19 @@ where
7219
7186
let counterparty_node_id = prev_hop.counterparty_node_id.expect("Checked immediately above");
7220
7187
let mut peer_state = peer_state_opt.expect("peer_state_opt is always Some when the counterparty_node_id is Some");
7221
7188
7222
- let mut preimage_update = ChannelMonitorUpdate {
7223
- update_id: 0, // set in set_closed_chan_next_monitor_update_id
7189
+ let update_id = if let Some(latest_update_id) = peer_state.closed_channel_monitor_update_ids.get_mut(&chan_id) {
7190
+ *latest_update_id = latest_update_id.saturating_add(1);
7191
+ *latest_update_id
7192
+ } else {
7193
+ let err = "We need the latest ChannelMonitorUpdate ID to build a new update.
7194
+ This should have been checked for availability on startup but somehow it is no longer available.
7195
+ This indicates a bug inside LDK. Please report this error at https://github.com/lightningdevkit/rust-lightning/issues/new";
7196
+ log_error!(self.logger, "{}", err);
7197
+ panic!("{}", err);
7198
+ };
7199
+
7200
+ let preimage_update = ChannelMonitorUpdate {
7201
+ update_id,
7224
7202
counterparty_node_id: prev_hop.counterparty_node_id,
7225
7203
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
7226
7204
payment_preimage,
@@ -7229,12 +7207,11 @@ where
7229
7207
channel_id: Some(prev_hop.channel_id),
7230
7208
};
7231
7209
7232
- // Note that the below is race-y - we set the `update_id` here and then drop the peer_state
7210
+ // Note that the below is race-y - we set the `update_id` above and then drop the peer_state
7233
7211
// lock before applying the update in `apply_post_close_monitor_update` (or via the
7234
7212
// background events pipeline). During that time, some other update could be created and
7235
7213
// then applied, resultin in `ChannelMonitorUpdate`s being applied out of order and causing
7236
7214
// a panic.
7237
- Self::set_closed_chan_next_monitor_update_id(&mut *peer_state, prev_hop.channel_id, &mut preimage_update);
7238
7215
7239
7216
mem::drop(peer_state);
7240
7217
mem::drop(per_peer_state);
0 commit comments