Skip to content

Commit b4f2a6c

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 46caf9c commit b4f2a6c

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
@@ -3291,27 +3291,6 @@ macro_rules! emit_initial_channel_ready_event {
32913291
};
32923292
}
32933293

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

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

1071510724
if let Some(funded_chan) = e.insert(Channel::from(chan)).as_funded_mut() {
10716-
handle_initial_monitor!(
10717-
self,
10725+
if let Some(data) = self.handle_initial_monitor(
10726+
&mut peer_state.in_flight_monitor_updates,
10727+
&mut peer_state.monitor_update_blocked_actions,
10728+
&mut peer_state.pending_msg_events,
10729+
peer_state.is_connected,
10730+
funded_chan,
1071810731
persist_state,
10719-
peer_state_lock,
10720-
peer_state,
10721-
per_peer_state,
10722-
funded_chan
10723-
);
10732+
) {
10733+
mem::drop(peer_state_lock);
10734+
mem::drop(per_peer_state);
10735+
self.handle_monitor_update_completion_data(data);
10736+
}
1072410737
} else {
1072510738
unreachable!("This must be a funded channel as we just inserted it.");
1072610739
}
@@ -10883,7 +10896,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1088310896
})
1088410897
{
1088510898
Ok((funded_chan, persist_status)) => {
10886-
handle_initial_monitor!(self, persist_status, peer_state_lock, peer_state, per_peer_state, funded_chan);
10899+
if let Some(data) = self.handle_initial_monitor(
10900+
&mut peer_state.in_flight_monitor_updates,
10901+
&mut peer_state.monitor_update_blocked_actions,
10902+
&mut peer_state.pending_msg_events,
10903+
peer_state.is_connected,
10904+
funded_chan,
10905+
persist_status,
10906+
) {
10907+
mem::drop(peer_state_lock);
10908+
mem::drop(per_peer_state);
10909+
self.handle_monitor_update_completion_data(data);
10910+
}
1088710911
Ok(())
1088810912
},
1088910913
Err(e) => try_channel_entry!(self, peer_state, Err(e), chan_entry),
@@ -11589,8 +11613,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1158911613
if let Some(monitor) = monitor_opt {
1159011614
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
1159111615
if let Ok(persist_state) = monitor_res {
11592-
handle_initial_monitor!(self, persist_state, peer_state_lock, peer_state,
11593-
per_peer_state, chan);
11616+
if let Some(data) = self.handle_initial_monitor(
11617+
&mut peer_state.in_flight_monitor_updates,
11618+
&mut peer_state.monitor_update_blocked_actions,
11619+
&mut peer_state.pending_msg_events,
11620+
peer_state.is_connected,
11621+
chan,
11622+
persist_state,
11623+
) {
11624+
mem::drop(peer_state_lock);
11625+
mem::drop(per_peer_state);
11626+
self.handle_monitor_update_completion_data(data);
11627+
}
1159411628
} else {
1159511629
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1159611630
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");

0 commit comments

Comments
 (0)