Skip to content

Commit d567f4a

Browse files
committed
Functionize the FUNDED cases in convert_channel_err
`convert_channel_err` is used extensively in `channelmanager.rs` (often indirectly via `try_channel_entry`) and generates nontrivial code (especially once you include `locked_close_channel`). Here we take the `FUNDED` cases of it and move them to an internal function reducing the generated code size. On the same machine as described two commits ago, this further reduces build times by about another second.
1 parent a6665dc commit d567f4a

File tree

1 file changed

+75
-31
lines changed

1 file changed

+75
-31
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,11 +3544,11 @@ macro_rules! handle_post_close_monitor_update {
35443544
/// later time.
35453545
macro_rules! handle_new_monitor_update_locked_actions_handled_by_caller {
35463546
(
3547-
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr
3547+
$self: ident, $funding_txo: expr, $update: expr, $in_flight_monitor_updates: expr, $chan_context: expr
35483548
) => {{
35493549
let (update_completed, _all_updates_complete) = handle_new_monitor_update_internal(
35503550
$self,
3551-
&mut $peer_state.in_flight_monitor_updates,
3551+
$in_flight_monitor_updates,
35523552
$chan_context.channel_id(),
35533553
$funding_txo,
35543554
$chan_context.get_counterparty_node_id(),
@@ -3600,10 +3600,10 @@ macro_rules! locked_close_channel {
36003600
let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
36013601
debug_assert!(alias_removed);
36023602
}};
3603-
($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3603+
($self: ident, $closed_channel_monitor_update_ids: expr, $in_flight_monitor_updates: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
36043604
if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
36053605
handle_new_monitor_update_locked_actions_handled_by_caller!(
3606-
$self, funding_txo, update, $peer_state, $funded_chan.context
3606+
$self, funding_txo, update, $in_flight_monitor_updates, $funded_chan.context
36073607
);
36083608
}
36093609
// If there's a possibility that we need to generate further monitor updates for this
@@ -3613,7 +3613,7 @@ macro_rules! locked_close_channel {
36133613
let update_id = $funded_chan.context.get_latest_monitor_update_id();
36143614
if $funded_chan.funding.get_funding_tx_confirmation_height().is_some() || $funded_chan.context.minimum_depth(&$funded_chan.funding) == Some(0) || update_id > 1 {
36153615
let chan_id = $funded_chan.context.channel_id();
3616-
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3616+
$closed_channel_monitor_update_ids.insert(chan_id, update_id);
36173617
}
36183618
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
36193619
if let Some(short_id) = $funded_chan.funding.get_short_channel_id() {
@@ -3635,6 +3635,62 @@ macro_rules! locked_close_channel {
36353635
}}
36363636
}
36373637

3638+
fn convert_channel_err_internal<Close: FnOnce(ClosureReason, &str) -> (ShutdownResult, Option<(msgs::ChannelUpdate, NodeId, NodeId)>)>(
3639+
err: ChannelError, chan_id: ChannelId, close: Close,
3640+
) -> (bool, MsgHandleErrInternal) {
3641+
match err {
3642+
ChannelError::Warn(msg) => {
3643+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), chan_id))
3644+
},
3645+
ChannelError::WarnAndDisconnect(msg) => {
3646+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::WarnAndDisconnect(msg), chan_id))
3647+
},
3648+
ChannelError::Ignore(msg) => {
3649+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), chan_id))
3650+
},
3651+
ChannelError::Abort(reason) => {
3652+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Abort(reason), chan_id))
3653+
},
3654+
ChannelError::Close((msg, reason)) => {
3655+
let (shutdown_res, chan_update) = close(reason, &msg);
3656+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, chan_id, shutdown_res, chan_update))
3657+
},
3658+
ChannelError::SendError(msg) => {
3659+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), chan_id))
3660+
},
3661+
}
3662+
}
3663+
3664+
fn convert_funded_channel_err_internal<
3665+
SP: Deref,
3666+
CM: AChannelManager<SP = SP>,
3667+
>(
3668+
cm: & CM, closed_channel_monitor_update_ids: &mut BTreeMap<ChannelId, u64>,
3669+
in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
3670+
coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError, chan: &mut FundedChannel<SP>,
3671+
) -> (bool, MsgHandleErrInternal)
3672+
where
3673+
SP::Target: SignerProvider,
3674+
CM::Watch: Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
3675+
{
3676+
let chan_id = chan.context.channel_id();
3677+
convert_channel_err_internal(err, chan_id, |reason, msg| {
3678+
let cm = cm.get_cm();
3679+
let logger = WithChannelContext::from(&cm.logger, &chan.context, None);
3680+
3681+
let mut shutdown_res = if let Some(res) = coop_close_shutdown_res {
3682+
res
3683+
} else {
3684+
chan.force_shutdown(reason)
3685+
};
3686+
let chan_update = cm.get_channel_update_for_broadcast(chan).ok();
3687+
3688+
log_error!(logger, "Closed channel {} due to close-required error: {}", chan_id, msg);
3689+
locked_close_channel!(cm, closed_channel_monitor_update_ids, in_flight_monitor_updates, chan, shutdown_res, FUNDED);
3690+
(shutdown_res, chan_update)
3691+
})
3692+
}
3693+
36383694
/// When a channel is removed, two things need to happen:
36393695
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
36403696
/// (b) [`handle_error`] needs to be called without holding any locks (except
@@ -3678,35 +3734,19 @@ macro_rules! convert_channel_err {
36783734
}
36793735
} };
36803736
($self: ident, $peer_state: expr, $shutdown_result: expr, $funded_channel: expr, COOP_CLOSED) => { {
3681-
let chan_id = $funded_channel.context.channel_id();
36823737
let reason = ChannelError::Close(("Coop Closed".to_owned(), $shutdown_result.closure_reason.clone()));
3683-
let do_close = |_| {
3684-
(
3685-
$shutdown_result,
3686-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3687-
)
3688-
};
3689-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3690-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3691-
};
3738+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3739+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
36923740
let (close, mut err) =
3693-
convert_channel_err!($self, $peer_state, reason, $funded_channel, do_close, locked_close, chan_id, _internal);
3741+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, Some($shutdown_result), reason, $funded_channel);
36943742
err.dont_send_error_message();
36953743
debug_assert!(close);
36963744
err
36973745
} };
36983746
($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, FUNDED_CHANNEL) => { {
3699-
let chan_id = $funded_channel.context.channel_id();
3700-
let mut do_close = |reason| {
3701-
(
3702-
$funded_channel.force_shutdown(reason),
3703-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3704-
)
3705-
};
3706-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3707-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3708-
};
3709-
convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, chan_id, _internal)
3747+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3748+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3749+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, $funded_channel)
37103750
} };
37113751
($self: ident, $peer_state: expr, $err: expr, $channel: expr, UNFUNDED_CHANNEL) => { {
37123752
let chan_id = $channel.context().channel_id();
@@ -3717,7 +3757,9 @@ macro_rules! convert_channel_err {
37173757
($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
37183758
match $channel.as_funded_mut() {
37193759
Some(funded_channel) => {
3720-
convert_channel_err!($self, $peer_state, $err, funded_channel, FUNDED_CHANNEL)
3760+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3761+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3762+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, funded_channel)
37213763
},
37223764
None => {
37233765
convert_channel_err!($self, $peer_state, $err, $channel, UNFUNDED_CHANNEL)
@@ -4484,7 +4526,8 @@ where
44844526
let mut has_uncompleted_channel = None;
44854527
for (channel_id, counterparty_node_id, state) in affected_channels {
44864528
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4487-
let mut peer_state = peer_state_mutex.lock().unwrap();
4529+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4530+
let peer_state = &mut *peer_state_lock;
44884531
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
44894532
let reason = ClosureReason::FundingBatchClosure;
44904533
let err = ChannelError::Close((reason.to_string(), reason));
@@ -6332,9 +6375,10 @@ where
63326375
per_peer_state.get(&counterparty_node_id)
63336376
.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
63346377
.and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id).map(|chan| (chan, peer_state)))
6335-
.map(|(mut chan, mut peer_state)| {
6378+
.map(|(mut chan, mut peer_state_lock)| {
63366379
let reason = ClosureReason::ProcessingError { err: e.clone() };
63376380
let err = ChannelError::Close((e.clone(), reason));
6381+
let peer_state = &mut *peer_state_lock;
63386382
let (_, e) =
63396383
convert_channel_err!(self, peer_state, err, &mut chan);
63406384
shutdown_results.push((Err(e), counterparty_node_id));
@@ -14357,7 +14401,7 @@ where
1435714401
self,
1435814402
funding_txo,
1435914403
monitor_update,
14360-
peer_state,
14404+
&mut peer_state.in_flight_monitor_updates,
1436114405
funded_channel.context
1436214406
);
1436314407
to_process_monitor_update_actions.push((

0 commit comments

Comments
 (0)