@@ -3972,39 +3972,6 @@ where
39723972 self.chain_monitor.update_channel(funding_txo, &monitor_update)
39733973 }
39743974
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-
40083975 /// When a channel is removed, two things need to happen:
40093976 /// (a) [`locked_close_channel`] must be called in the same `per_peer_state` lock as
40103977 /// the channel-closing action,
@@ -7219,8 +7186,19 @@ where
72197186 let counterparty_node_id = prev_hop.counterparty_node_id.expect("Checked immediately above");
72207187 let mut peer_state = peer_state_opt.expect("peer_state_opt is always Some when the counterparty_node_id is Some");
72217188
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,
72247202 counterparty_node_id: prev_hop.counterparty_node_id,
72257203 updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
72267204 payment_preimage,
@@ -7229,12 +7207,11 @@ where
72297207 channel_id: Some(prev_hop.channel_id),
72307208 };
72317209
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
72337211 // lock before applying the update in `apply_post_close_monitor_update` (or via the
72347212 // background events pipeline). During that time, some other update could be created and
72357213 // then applied, resultin in `ChannelMonitorUpdate`s being applied out of order and causing
72367214 // a panic.
7237- Self::set_closed_chan_next_monitor_update_id(&mut *peer_state, prev_hop.channel_id, &mut preimage_update);
72387215
72397216 mem::drop(peer_state);
72407217 mem::drop(per_peer_state);
0 commit comments