@@ -3291,32 +3291,6 @@ macro_rules! emit_initial_channel_ready_event {
32913291 };
32923292}
32933293
3294- macro_rules! handle_post_close_monitor_update {
3295- (
3296- $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3297- $per_peer_state_lock: expr, $counterparty_node_id: expr, $channel_id: expr
3298- ) => {{
3299- let (update_completed, all_updates_complete) = $self.handle_new_monitor_update_internal(
3300- &mut $peer_state.in_flight_monitor_updates,
3301- $channel_id,
3302- $funding_txo,
3303- $counterparty_node_id,
3304- $update,
3305- );
3306- if all_updates_complete {
3307- let update_actions = $peer_state
3308- .monitor_update_blocked_actions
3309- .remove(&$channel_id)
3310- .unwrap_or(Vec::new());
3311-
3312- mem::drop($peer_state_lock);
3313- mem::drop($per_peer_state_lock);
3314-
3315- $self.handle_monitor_update_completion_actions(update_actions);
3316- }
3317- update_completed
3318- }};
3319- }
33203294macro_rules! handle_new_monitor_update {
33213295 (
33223296 $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
@@ -4151,10 +4125,18 @@ where
41514125 hash_map::Entry::Vacant(_) => {},
41524126 }
41534127
4154- handle_post_close_monitor_update!(
4155- self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
4156- counterparty_node_id, channel_id
4157- );
4128+ if let Some(actions) = self.handle_post_close_monitor_update(
4129+ &mut peer_state.in_flight_monitor_updates,
4130+ &mut peer_state.monitor_update_blocked_actions,
4131+ funding_txo,
4132+ monitor_update,
4133+ counterparty_node_id,
4134+ channel_id,
4135+ ) {
4136+ mem::drop(peer_state_lock);
4137+ mem::drop(per_peer_state);
4138+ self.handle_monitor_update_completion_actions(actions);
4139+ }
41584140 }
41594141
41604142 /// When a channel is removed, two things need to happen:
@@ -9120,16 +9102,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
91209102 .push(action);
91219103 }
91229104
9123- handle_post_close_monitor_update!(
9124- self,
9105+ if let Some(actions) = self.handle_post_close_monitor_update(
9106+ &mut peer_state.in_flight_monitor_updates,
9107+ &mut peer_state.monitor_update_blocked_actions,
91259108 prev_hop.funding_txo,
91269109 preimage_update,
9127- peer_state_lock,
9128- peer_state,
9129- per_peer_state,
91309110 prev_hop.counterparty_node_id,
9131- chan_id
9132- );
9111+ chan_id,
9112+ ) {
9113+ mem::drop(peer_state_lock);
9114+ mem::drop(per_peer_state);
9115+ self.handle_monitor_update_completion_actions(actions);
9116+ }
91339117 }
91349118
91359119 fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
@@ -9644,6 +9628,34 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96449628 }
96459629 }
96469630
9631+ /// Handles a monitor update for a closed channel, returning optionally the completion actions
9632+ /// to process after locks are released.
9633+ ///
9634+ /// Returns `Some` if all in-flight updates are complete.
9635+ fn handle_post_close_monitor_update(
9636+ &self,
9637+ in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
9638+ monitor_update_blocked_actions: &mut BTreeMap<
9639+ ChannelId,
9640+ Vec<MonitorUpdateCompletionAction>,
9641+ >,
9642+ funding_txo: OutPoint, update: ChannelMonitorUpdate, counterparty_node_id: PublicKey,
9643+ channel_id: ChannelId,
9644+ ) -> Option<Vec<MonitorUpdateCompletionAction>> {
9645+ let (_update_completed, all_updates_complete) = self.handle_new_monitor_update_internal(
9646+ in_flight_monitor_updates,
9647+ channel_id,
9648+ funding_txo,
9649+ counterparty_node_id,
9650+ update,
9651+ );
9652+ if all_updates_complete {
9653+ Some(monitor_update_blocked_actions.remove(&channel_id).unwrap_or(Vec::new()))
9654+ } else {
9655+ None
9656+ }
9657+ }
9658+
96479659 /// Returns whether the monitor update is completed, `false` if the update is in-progress.
96489660 fn handle_monitor_update_res<LG: Logger>(
96499661 &self, update_res: ChannelMonitorUpdateStatus, logger: LG,
@@ -14061,10 +14073,11 @@ where
1406114073 },
1406214074 ) => {
1406314075 let per_peer_state = self.per_peer_state.read().unwrap();
14064- let mut peer_state = per_peer_state
14076+ let mut peer_state_lock = per_peer_state
1406514077 .get(&counterparty_node_id)
1406614078 .map(|state| state.lock().unwrap())
1406714079 .expect("Channels originating a payment resolution must have peer state");
14080+ let peer_state = &mut *peer_state_lock;
1406814081 let update_id = peer_state
1406914082 .closed_channel_monitor_update_ids
1407014083 .get_mut(&channel_id)
@@ -14091,16 +14104,18 @@ where
1409114104 };
1409214105 self.pending_background_events.lock().unwrap().push(event);
1409314106 } else {
14094- handle_post_close_monitor_update!(
14095- self,
14107+ if let Some(actions) = self.handle_post_close_monitor_update(
14108+ &mut peer_state.in_flight_monitor_updates,
14109+ &mut peer_state.monitor_update_blocked_actions,
1409614110 channel_funding_outpoint,
1409714111 update,
14098- peer_state,
14099- peer_state,
14100- per_peer_state,
1410114112 counterparty_node_id,
14102- channel_id
14103- );
14113+ channel_id,
14114+ ) {
14115+ mem::drop(peer_state_lock);
14116+ mem::drop(per_peer_state);
14117+ self.handle_monitor_update_completion_actions(actions);
14118+ }
1410414119 }
1410514120 },
1410614121 }
0 commit comments