Skip to content

Commit a54ef8e

Browse files
Extract monitor update res handling to fn
Generally we'd like to use functions instead of macros where we can both for readability and for compile times.
1 parent c00571c commit a54ef8e

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,37 +3629,19 @@ macro_rules! handle_monitor_update_completion {
36293629
}
36303630

36313631
macro_rules! handle_new_monitor_update {
3632-
($self: ident, $update_res: expr, $logger: expr, $channel_id: expr, _internal, $completed: expr) => { {
3633-
debug_assert!($self.background_events_processed_since_startup.load(Ordering::Acquire));
3634-
match $update_res {
3635-
ChannelMonitorUpdateStatus::UnrecoverableError => {
3636-
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
3637-
log_error!($logger, "{}", err_str);
3638-
panic!("{}", err_str);
3639-
},
3640-
ChannelMonitorUpdateStatus::InProgress => {
3641-
#[cfg(not(any(test, feature = "_externalize_tests")))]
3642-
if $self.monitor_update_type.swap(1, Ordering::Relaxed) == 2 {
3643-
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3644-
}
3645-
log_debug!($logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
3646-
$channel_id);
3647-
false
3648-
},
3649-
ChannelMonitorUpdateStatus::Completed => {
3650-
#[cfg(not(any(test, feature = "_externalize_tests")))]
3651-
if $self.monitor_update_type.swap(2, Ordering::Relaxed) == 1 {
3652-
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3653-
}
3654-
$completed;
3655-
true
3656-
},
3657-
}
3658-
} };
36593632
($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, INITIAL_MONITOR) => {
36603633
let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3661-
handle_new_monitor_update!($self, $update_res, logger, $chan.context.channel_id(), _internal,
3662-
handle_monitor_update_completion!($self, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan))
3634+
let update_completed =
3635+
$self.handle_monitor_update_res($update_res, $chan.context.channel_id(), logger);
3636+
if update_completed {
3637+
handle_monitor_update_completion!(
3638+
$self,
3639+
$peer_state_lock,
3640+
$peer_state,
3641+
$per_peer_state_lock,
3642+
$chan
3643+
);
3644+
}
36633645
};
36643646
(
36653647
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
@@ -3682,7 +3664,11 @@ macro_rules! handle_new_monitor_update {
36823664
if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
36833665
let update_res =
36843666
$self.chain_monitor.update_channel($chan_id, &$in_flight_updates[$update_idx]);
3685-
handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3667+
let update_completed = $self.handle_monitor_update_res(update_res, $chan_id, $logger);
3668+
if update_completed {
3669+
$completed;
3670+
}
3671+
update_completed
36863672
} else {
36873673
// We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
36883674
// fail to persist it. This is a fairly safe assumption, however, since anything we do
@@ -9549,6 +9535,36 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
95499535
}
95509536
}
95519537

9538+
/// Returns whether the monitor update is completed, `false` if the update is in-progress.
9539+
fn handle_monitor_update_res<LG: Logger>(
9540+
&self, update_res: ChannelMonitorUpdateStatus, channel_id: ChannelId, logger: LG,
9541+
) -> bool {
9542+
debug_assert!(self.background_events_processed_since_startup.load(Ordering::Acquire));
9543+
match update_res {
9544+
ChannelMonitorUpdateStatus::UnrecoverableError => {
9545+
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
9546+
log_error!(logger, "{}", err_str);
9547+
panic!("{}", err_str);
9548+
},
9549+
ChannelMonitorUpdateStatus::InProgress => {
9550+
#[cfg(not(any(test, feature = "_externalize_tests")))]
9551+
if self.monitor_update_type.swap(1, Ordering::Relaxed) == 2 {
9552+
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
9553+
}
9554+
log_debug!(logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
9555+
channel_id);
9556+
false
9557+
},
9558+
ChannelMonitorUpdateStatus::Completed => {
9559+
#[cfg(not(any(test, feature = "_externalize_tests")))]
9560+
if self.monitor_update_type.swap(2, Ordering::Relaxed) == 1 {
9561+
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
9562+
}
9563+
true
9564+
},
9565+
}
9566+
}
9567+
95529568
/// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
95539569
///
95549570
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,

0 commit comments

Comments
 (0)