Skip to content

Commit 57606ac

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 52af15c commit 57606ac

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
@@ -3565,11 +3565,11 @@ macro_rules! handle_post_close_monitor_update {
35653565
/// later time.
35663566
macro_rules! handle_new_monitor_update_locked_actions_handled_by_caller {
35673567
(
3568-
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr
3568+
$self: ident, $funding_txo: expr, $update: expr, $in_flight_monitor_updates: expr, $chan_context: expr
35693569
) => {{
35703570
let (update_completed, _all_updates_complete) = handle_new_monitor_update_internal(
35713571
$self,
3572-
&mut $peer_state.in_flight_monitor_updates,
3572+
$in_flight_monitor_updates,
35733573
$chan_context.channel_id(),
35743574
$funding_txo,
35753575
$chan_context.get_counterparty_node_id(),
@@ -3621,10 +3621,10 @@ macro_rules! locked_close_channel {
36213621
let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
36223622
debug_assert!(alias_removed);
36233623
}};
3624-
($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3624+
($self: ident, $closed_channel_monitor_update_ids: expr, $in_flight_monitor_updates: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
36253625
if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
36263626
handle_new_monitor_update_locked_actions_handled_by_caller!(
3627-
$self, funding_txo, update, $peer_state, $funded_chan.context
3627+
$self, funding_txo, update, $in_flight_monitor_updates, $funded_chan.context
36283628
);
36293629
}
36303630
// If there's a possibility that we need to generate further monitor updates for this
@@ -3634,7 +3634,7 @@ macro_rules! locked_close_channel {
36343634
let update_id = $funded_chan.context.get_latest_monitor_update_id();
36353635
if $funded_chan.funding.get_funding_tx_confirmation_height().is_some() || $funded_chan.context.minimum_depth(&$funded_chan.funding) == Some(0) || update_id > 1 {
36363636
let chan_id = $funded_chan.context.channel_id();
3637-
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3637+
$closed_channel_monitor_update_ids.insert(chan_id, update_id);
36383638
}
36393639
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
36403640
if let Some(short_id) = $funded_chan.funding.get_short_channel_id() {
@@ -3656,6 +3656,62 @@ macro_rules! locked_close_channel {
36563656
}}
36573657
}
36583658

3659+
fn convert_channel_err_internal<Close: FnOnce(ClosureReason, &str) -> (ShutdownResult, Option<(msgs::ChannelUpdate, NodeId, NodeId)>)>(
3660+
err: ChannelError, chan_id: ChannelId, close: Close,
3661+
) -> (bool, MsgHandleErrInternal) {
3662+
match err {
3663+
ChannelError::Warn(msg) => {
3664+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), chan_id))
3665+
},
3666+
ChannelError::WarnAndDisconnect(msg) => {
3667+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::WarnAndDisconnect(msg), chan_id))
3668+
},
3669+
ChannelError::Ignore(msg) => {
3670+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), chan_id))
3671+
},
3672+
ChannelError::Abort(reason) => {
3673+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Abort(reason), chan_id))
3674+
},
3675+
ChannelError::Close((msg, reason)) => {
3676+
let (shutdown_res, chan_update) = close(reason, &msg);
3677+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, chan_id, shutdown_res, chan_update))
3678+
},
3679+
ChannelError::SendError(msg) => {
3680+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), chan_id))
3681+
},
3682+
}
3683+
}
3684+
3685+
fn convert_funded_channel_err_internal<
3686+
SP: Deref,
3687+
CM: AChannelManager<SP = SP>,
3688+
>(
3689+
cm: & CM, closed_channel_monitor_update_ids: &mut BTreeMap<ChannelId, u64>,
3690+
in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
3691+
coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError, chan: &mut FundedChannel<SP>,
3692+
) -> (bool, MsgHandleErrInternal)
3693+
where
3694+
SP::Target: SignerProvider,
3695+
CM::Watch: Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
3696+
{
3697+
let chan_id = chan.context.channel_id();
3698+
convert_channel_err_internal(err, chan_id, |reason, msg| {
3699+
let cm = cm.get_cm();
3700+
let logger = WithChannelContext::from(&cm.logger, &chan.context, None);
3701+
3702+
let mut shutdown_res = if let Some(res) = coop_close_shutdown_res {
3703+
res
3704+
} else {
3705+
chan.force_shutdown(reason)
3706+
};
3707+
let chan_update = cm.get_channel_update_for_broadcast(chan).ok();
3708+
3709+
log_error!(logger, "Closed channel {} due to close-required error: {}", chan_id, msg);
3710+
locked_close_channel!(cm, closed_channel_monitor_update_ids, in_flight_monitor_updates, chan, shutdown_res, FUNDED);
3711+
(shutdown_res, chan_update)
3712+
})
3713+
}
3714+
36593715
/// When a channel is removed, two things need to happen:
36603716
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
36613717
/// (b) [`handle_error`] needs to be called without holding any locks (except
@@ -3699,35 +3755,19 @@ macro_rules! convert_channel_err {
36993755
}
37003756
} };
37013757
($self: ident, $peer_state: expr, $shutdown_result: expr, $funded_channel: expr, COOP_CLOSED) => { {
3702-
let chan_id = $funded_channel.context.channel_id();
37033758
let reason = ChannelError::Close(("Coop Closed".to_owned(), $shutdown_result.closure_reason.clone()));
3704-
let do_close = |_| {
3705-
(
3706-
$shutdown_result,
3707-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3708-
)
3709-
};
3710-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3711-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3712-
};
3759+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3760+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
37133761
let (close, mut err) =
3714-
convert_channel_err!($self, $peer_state, reason, $funded_channel, do_close, locked_close, chan_id, _internal);
3762+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, Some($shutdown_result), reason, $funded_channel);
37153763
err.dont_send_error_message();
37163764
debug_assert!(close);
37173765
err
37183766
} };
37193767
($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, FUNDED_CHANNEL) => { {
3720-
let chan_id = $funded_channel.context.channel_id();
3721-
let mut do_close = |reason| {
3722-
(
3723-
$funded_channel.force_shutdown(reason),
3724-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3725-
)
3726-
};
3727-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3728-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3729-
};
3730-
convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, chan_id, _internal)
3768+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3769+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3770+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, $funded_channel)
37313771
} };
37323772
($self: ident, $peer_state: expr, $err: expr, $channel: expr, UNFUNDED_CHANNEL) => { {
37333773
let chan_id = $channel.context().channel_id();
@@ -3738,7 +3778,9 @@ macro_rules! convert_channel_err {
37383778
($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
37393779
match $channel.as_funded_mut() {
37403780
Some(funded_channel) => {
3741-
convert_channel_err!($self, $peer_state, $err, funded_channel, FUNDED_CHANNEL)
3781+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3782+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3783+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, funded_channel)
37423784
},
37433785
None => {
37443786
convert_channel_err!($self, $peer_state, $err, $channel, UNFUNDED_CHANNEL)
@@ -4505,7 +4547,8 @@ where
45054547
let mut has_uncompleted_channel = None;
45064548
for (channel_id, counterparty_node_id, state) in affected_channels {
45074549
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4508-
let mut peer_state = peer_state_mutex.lock().unwrap();
4550+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4551+
let peer_state = &mut *peer_state_lock;
45094552
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
45104553
let reason = ClosureReason::FundingBatchClosure;
45114554
let err = ChannelError::Close((reason.to_string(), reason));
@@ -6353,9 +6396,10 @@ where
63536396
per_peer_state.get(&counterparty_node_id)
63546397
.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
63556398
.and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id).map(|chan| (chan, peer_state)))
6356-
.map(|(mut chan, mut peer_state)| {
6399+
.map(|(mut chan, mut peer_state_lock)| {
63576400
let reason = ClosureReason::ProcessingError { err: e.clone() };
63586401
let err = ChannelError::Close((e.clone(), reason));
6402+
let peer_state = &mut *peer_state_lock;
63596403
let (_, e) =
63606404
convert_channel_err!(self, peer_state, err, &mut chan);
63616405
shutdown_results.push((Err(e), counterparty_node_id));
@@ -14382,7 +14426,7 @@ where
1438214426
self,
1438314427
funding_txo,
1438414428
monitor_update,
14385-
peer_state,
14429+
&mut peer_state.in_flight_monitor_updates,
1438614430
funded_channel.context
1438714431
);
1438814432
to_process_monitor_update_actions.push((

0 commit comments

Comments
 (0)