@@ -3289,32 +3289,6 @@ macro_rules! emit_initial_channel_ready_event {
32893289 };
32903290}
32913291
3292- macro_rules! handle_post_close_monitor_update {
3293- (
3294- $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3295- $per_peer_state_lock: expr, $counterparty_node_id: expr, $channel_id: expr
3296- ) => {{
3297- let (update_completed, all_updates_complete) = $self.handle_new_monitor_update_internal(
3298- &mut $peer_state.in_flight_monitor_updates,
3299- $channel_id,
3300- $funding_txo,
3301- $counterparty_node_id,
3302- $update,
3303- );
3304- if all_updates_complete {
3305- let update_actions = $peer_state
3306- .monitor_update_blocked_actions
3307- .remove(&$channel_id)
3308- .unwrap_or(Vec::new());
3309-
3310- mem::drop($peer_state_lock);
3311- mem::drop($per_peer_state_lock);
3312-
3313- $self.handle_monitor_update_completion_actions(update_actions);
3314- }
3315- update_completed
3316- }};
3317- }
33183292macro_rules! handle_new_monitor_update {
33193293 (
33203294 $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
@@ -4149,10 +4123,18 @@ where
41494123 hash_map::Entry::Vacant(_) => {},
41504124 }
41514125
4152- handle_post_close_monitor_update!(
4153- self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
4154- counterparty_node_id, channel_id
4155- );
4126+ if let Some(actions) = self.handle_post_close_monitor_update(
4127+ &mut peer_state.in_flight_monitor_updates,
4128+ &mut peer_state.monitor_update_blocked_actions,
4129+ funding_txo,
4130+ monitor_update,
4131+ counterparty_node_id,
4132+ channel_id,
4133+ ) {
4134+ mem::drop(peer_state_lock);
4135+ mem::drop(per_peer_state);
4136+ self.handle_monitor_update_completion_actions(actions);
4137+ }
41564138 }
41574139
41584140 /// When a channel is removed, two things need to happen:
@@ -9118,16 +9100,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
91189100 .push(action);
91199101 }
91209102
9121- handle_post_close_monitor_update!(
9122- self,
9103+ if let Some(actions) = self.handle_post_close_monitor_update(
9104+ &mut peer_state.in_flight_monitor_updates,
9105+ &mut peer_state.monitor_update_blocked_actions,
91239106 prev_hop.funding_txo,
91249107 preimage_update,
9125- peer_state_lock,
9126- peer_state,
9127- per_peer_state,
91289108 prev_hop.counterparty_node_id,
9129- chan_id
9130- );
9109+ chan_id,
9110+ ) {
9111+ mem::drop(peer_state_lock);
9112+ mem::drop(per_peer_state);
9113+ self.handle_monitor_update_completion_actions(actions);
9114+ }
91319115 }
91329116
91339117 fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
@@ -9642,6 +9626,34 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96429626 }
96439627 }
96449628
9629+ /// Handles a monitor update for a closed channel, returning optionally the completion actions
9630+ /// to process after locks are released.
9631+ ///
9632+ /// Returns `Some` if all in-flight updates are complete.
9633+ fn handle_post_close_monitor_update(
9634+ &self,
9635+ in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
9636+ monitor_update_blocked_actions: &mut BTreeMap<
9637+ ChannelId,
9638+ Vec<MonitorUpdateCompletionAction>,
9639+ >,
9640+ funding_txo: OutPoint, update: ChannelMonitorUpdate, counterparty_node_id: PublicKey,
9641+ channel_id: ChannelId,
9642+ ) -> Option<Vec<MonitorUpdateCompletionAction>> {
9643+ let (_update_completed, all_updates_complete) = self.handle_new_monitor_update_internal(
9644+ in_flight_monitor_updates,
9645+ channel_id,
9646+ funding_txo,
9647+ counterparty_node_id,
9648+ update,
9649+ );
9650+ if all_updates_complete {
9651+ Some(monitor_update_blocked_actions.remove(&channel_id).unwrap_or(Vec::new()))
9652+ } else {
9653+ None
9654+ }
9655+ }
9656+
96459657 /// Returns whether the monitor update is completed, `false` if the update is in-progress.
96469658 fn handle_monitor_update_res<LG: Logger>(
96479659 &self, update_res: ChannelMonitorUpdateStatus, logger: LG,
@@ -14059,10 +14071,11 @@ where
1405914071 },
1406014072 ) => {
1406114073 let per_peer_state = self.per_peer_state.read().unwrap();
14062- let mut peer_state = per_peer_state
14074+ let mut peer_state_lock = per_peer_state
1406314075 .get(&counterparty_node_id)
1406414076 .map(|state| state.lock().unwrap())
1406514077 .expect("Channels originating a payment resolution must have peer state");
14078+ let peer_state = &mut *peer_state_lock;
1406614079 let update_id = peer_state
1406714080 .closed_channel_monitor_update_ids
1406814081 .get_mut(&channel_id)
@@ -14089,16 +14102,18 @@ where
1408914102 };
1409014103 self.pending_background_events.lock().unwrap().push(event);
1409114104 } else {
14092- handle_post_close_monitor_update!(
14093- self,
14105+ if let Some(actions) = self.handle_post_close_monitor_update(
14106+ &mut peer_state.in_flight_monitor_updates,
14107+ &mut peer_state.monitor_update_blocked_actions,
1409414108 channel_funding_outpoint,
1409514109 update,
14096- peer_state,
14097- peer_state,
14098- per_peer_state,
1409914110 counterparty_node_id,
14100- channel_id
14101- );
14111+ channel_id,
14112+ ) {
14113+ mem::drop(peer_state_lock);
14114+ mem::drop(per_peer_state);
14115+ self.handle_monitor_update_completion_actions(actions);
14116+ }
1410214117 }
1410314118 },
1410414119 }
0 commit comments