Skip to content

Commit 66adb78

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 66adb78

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
@@ -3628,38 +3628,50 @@ macro_rules! handle_monitor_update_completion {
36283628
} }
36293629
}
36303630

3631+
/// Returns whether the monitor update is completed, `false` if the update is in-progress.
3632+
fn handle_monitor_update_res<CM: AChannelManager, LG: Logger>(
3633+
cm: &CM, update_res: ChannelMonitorUpdateStatus, channel_id: ChannelId, logger: LG,
3634+
) -> bool {
3635+
debug_assert!(cm.get_cm().background_events_processed_since_startup.load(Ordering::Acquire));
3636+
match update_res {
3637+
ChannelMonitorUpdateStatus::UnrecoverableError => {
3638+
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
3639+
log_error!(logger, "{}", err_str);
3640+
panic!("{}", err_str);
3641+
},
3642+
ChannelMonitorUpdateStatus::InProgress => {
3643+
#[cfg(not(any(test, feature = "_externalize_tests")))]
3644+
if cm.get_cm().monitor_update_type.swap(1, Ordering::Relaxed) == 2 {
3645+
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3646+
}
3647+
log_debug!(logger, "ChannelMonitor update for {} in flight, holding messages until the update completes.",
3648+
channel_id);
3649+
false
3650+
},
3651+
ChannelMonitorUpdateStatus::Completed => {
3652+
#[cfg(not(any(test, feature = "_externalize_tests")))]
3653+
if cm.get_cm().monitor_update_type.swap(2, Ordering::Relaxed) == 1 {
3654+
panic!("Cannot use both ChannelMonitorUpdateStatus modes InProgress and Completed without restart");
3655+
}
3656+
true
3657+
},
3658+
}
3659+
}
3660+
36313661
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-
} };
36593662
($self: ident, $update_res: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, INITIAL_MONITOR) => {
36603663
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))
3664+
let update_completed =
3665+
handle_monitor_update_res($self, $update_res, $chan.context.channel_id(), logger);
3666+
if update_completed {
3667+
handle_monitor_update_completion!(
3668+
$self,
3669+
$peer_state_lock,
3670+
$peer_state,
3671+
$per_peer_state_lock,
3672+
$chan
3673+
);
3674+
}
36633675
};
36643676
(
36653677
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
@@ -3682,7 +3694,11 @@ macro_rules! handle_new_monitor_update {
36823694
if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
36833695
let update_res =
36843696
$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)
3697+
let update_completed = handle_monitor_update_res($self, update_res, $chan_id, $logger);
3698+
if update_completed {
3699+
$completed;
3700+
}
3701+
update_completed
36863702
} else {
36873703
// We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
36883704
// fail to persist it. This is a fairly safe assumption, however, since anything we do

0 commit comments

Comments
 (0)