Skip to content

Commit 6f20377

Browse files
joostjagerclaude
andcommitted
Remove handle_initial_monitor macro
Convert the handle_initial_monitor! macro to a method that returns optional completion data, allowing callers to release locks before processing the completion. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 9f6153d commit 6f20377

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,27 +3289,6 @@ macro_rules! emit_initial_channel_ready_event {
32893289
};
32903290
}
32913291

3292-
macro_rules! handle_initial_monitor {
3293-
($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => {
3294-
let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3295-
let update_completed = $self.handle_monitor_update_res($update_res, logger);
3296-
if update_completed {
3297-
let completion_data = $self.prepare_monitor_update_completion_data(
3298-
&mut $peer_state.in_flight_monitor_updates,
3299-
&mut $peer_state.monitor_update_blocked_actions,
3300-
&mut $peer_state.pending_msg_events,
3301-
$peer_state.is_connected,
3302-
$chan,
3303-
);
3304-
3305-
mem::drop($peer_state_lock);
3306-
mem::drop($per_peer_state_lock);
3307-
3308-
$self.handle_monitor_update_completion_data(completion_data);
3309-
}
3310-
};
3311-
}
3312-
33133292
macro_rules! handle_post_close_monitor_update {
33143293
(
33153294
$self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
@@ -9695,6 +9674,36 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96959674
}
96969675
}
96979676

9677+
/// Handles the initial monitor persistence, returning optionally data to process after locks
9678+
/// are released.
9679+
///
9680+
/// Note: This method takes individual fields from `PeerState` rather than the whole struct
9681+
/// to avoid borrow checker issues when the channel is borrowed from `peer_state.channel_by_id`.
9682+
fn handle_initial_monitor(
9683+
&self,
9684+
in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
9685+
monitor_update_blocked_actions: &mut BTreeMap<
9686+
ChannelId,
9687+
Vec<MonitorUpdateCompletionAction>,
9688+
>,
9689+
pending_msg_events: &mut Vec<MessageSendEvent>, is_connected: bool,
9690+
chan: &mut FundedChannel<SP>, update_res: ChannelMonitorUpdateStatus,
9691+
) -> Option<MonitorUpdateCompletionData> {
9692+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9693+
let update_completed = self.handle_monitor_update_res(update_res, logger);
9694+
if update_completed {
9695+
Some(self.prepare_monitor_update_completion_data(
9696+
in_flight_monitor_updates,
9697+
monitor_update_blocked_actions,
9698+
pending_msg_events,
9699+
is_connected,
9700+
chan,
9701+
))
9702+
} else {
9703+
None
9704+
}
9705+
}
9706+
96989707
/// Prepares data for monitor update completion while locks are still held.
96999708
/// This extracts all necessary data from the channel and peer state fields.
97009709
///
@@ -10711,14 +10720,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1071110720
}
1071210721

1071310722
if let Some(funded_chan) = e.insert(Channel::from(chan)).as_funded_mut() {
10714-
handle_initial_monitor!(
10715-
self,
10723+
if let Some(data) = self.handle_initial_monitor(
10724+
&mut peer_state.in_flight_monitor_updates,
10725+
&mut peer_state.monitor_update_blocked_actions,
10726+
&mut peer_state.pending_msg_events,
10727+
peer_state.is_connected,
10728+
funded_chan,
1071610729
persist_state,
10717-
peer_state_lock,
10718-
peer_state,
10719-
per_peer_state,
10720-
funded_chan
10721-
);
10730+
) {
10731+
mem::drop(peer_state_lock);
10732+
mem::drop(per_peer_state);
10733+
self.handle_monitor_update_completion_data(data);
10734+
}
1072210735
} else {
1072310736
unreachable!("This must be a funded channel as we just inserted it.");
1072410737
}
@@ -10881,7 +10894,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1088110894
})
1088210895
{
1088310896
Ok((funded_chan, persist_status)) => {
10884-
handle_initial_monitor!(self, persist_status, peer_state_lock, peer_state, per_peer_state, funded_chan);
10897+
if let Some(data) = self.handle_initial_monitor(
10898+
&mut peer_state.in_flight_monitor_updates,
10899+
&mut peer_state.monitor_update_blocked_actions,
10900+
&mut peer_state.pending_msg_events,
10901+
peer_state.is_connected,
10902+
funded_chan,
10903+
persist_status,
10904+
) {
10905+
mem::drop(peer_state_lock);
10906+
mem::drop(per_peer_state);
10907+
self.handle_monitor_update_completion_data(data);
10908+
}
1088510909
Ok(())
1088610910
},
1088710911
Err(e) => try_channel_entry!(self, peer_state, Err(e), chan_entry),
@@ -11587,8 +11611,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1158711611
if let Some(monitor) = monitor_opt {
1158811612
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
1158911613
if let Ok(persist_state) = monitor_res {
11590-
handle_initial_monitor!(self, persist_state, peer_state_lock, peer_state,
11591-
per_peer_state, chan);
11614+
if let Some(data) = self.handle_initial_monitor(
11615+
&mut peer_state.in_flight_monitor_updates,
11616+
&mut peer_state.monitor_update_blocked_actions,
11617+
&mut peer_state.pending_msg_events,
11618+
peer_state.is_connected,
11619+
chan,
11620+
persist_state,
11621+
) {
11622+
mem::drop(peer_state_lock);
11623+
mem::drop(per_peer_state);
11624+
self.handle_monitor_update_completion_data(data);
11625+
}
1159211626
} else {
1159311627
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1159411628
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");

0 commit comments

Comments
 (0)