Skip to content

Commit 252a75a

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 2a65025 commit 252a75a

File tree

1 file changed

+73
-31
lines changed

1 file changed

+73
-31
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,11 +3566,11 @@ macro_rules! handle_post_close_monitor_update {
35663566
/// later time.
35673567
macro_rules! handle_new_monitor_update_locked_actions_handled_by_caller {
35683568
(
3569-
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr
3569+
$self: ident, $funding_txo: expr, $update: expr, $in_flight_monitor_updates: expr, $chan_context: expr
35703570
) => {{
35713571
let (update_completed, _all_updates_complete) = handle_new_monitor_update_internal(
35723572
$self,
3573-
&mut $peer_state.in_flight_monitor_updates,
3573+
$in_flight_monitor_updates,
35743574
$chan_context.channel_id(),
35753575
$funding_txo,
35763576
$chan_context.get_counterparty_node_id(),
@@ -3622,10 +3622,10 @@ macro_rules! locked_close_channel {
36223622
let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
36233623
debug_assert!(alias_removed);
36243624
}};
3625-
($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3625+
($self: ident, $closed_channel_monitor_update_ids: expr, $in_flight_monitor_updates: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
36263626
if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
36273627
handle_new_monitor_update_locked_actions_handled_by_caller!(
3628-
$self, funding_txo, update, $peer_state, $funded_chan.context
3628+
$self, funding_txo, update, $in_flight_monitor_updates, $funded_chan.context
36293629
);
36303630
}
36313631
// If there's a possibility that we need to generate further monitor updates for this
@@ -3635,7 +3635,7 @@ macro_rules! locked_close_channel {
36353635
let update_id = $funded_chan.context.get_latest_monitor_update_id();
36363636
if $funded_chan.funding.get_funding_tx_confirmation_height().is_some() || $funded_chan.context.minimum_depth(&$funded_chan.funding) == Some(0) || update_id > 1 {
36373637
let chan_id = $funded_chan.context.channel_id();
3638-
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3638+
$closed_channel_monitor_update_ids.insert(chan_id, update_id);
36393639
}
36403640
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
36413641
if let Some(short_id) = $funded_chan.funding.get_short_channel_id() {
@@ -3657,6 +3657,60 @@ macro_rules! locked_close_channel {
36573657
}}
36583658
}
36593659

3660+
fn convert_channel_err_internal<
3661+
Close: FnOnce(ClosureReason, &str) -> (ShutdownResult, Option<(msgs::ChannelUpdate, NodeId, NodeId)>),
3662+
>(
3663+
err: ChannelError, chan_id: ChannelId, close: Close,
3664+
) -> (bool, MsgHandleErrInternal) {
3665+
match err {
3666+
ChannelError::Warn(msg) => {
3667+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), chan_id))
3668+
},
3669+
ChannelError::WarnAndDisconnect(msg) => (
3670+
false,
3671+
MsgHandleErrInternal::from_chan_no_close(ChannelError::WarnAndDisconnect(msg), chan_id),
3672+
),
3673+
ChannelError::Ignore(msg) => {
3674+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), chan_id))
3675+
},
3676+
ChannelError::Abort(reason) => {
3677+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Abort(reason), chan_id))
3678+
},
3679+
ChannelError::Close((msg, reason)) => {
3680+
let (finish, chan_update) = close(reason, &msg);
3681+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, chan_id, finish, chan_update))
3682+
},
3683+
ChannelError::SendError(msg) => {
3684+
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), chan_id))
3685+
},
3686+
}
3687+
}
3688+
3689+
fn convert_funded_channel_err_internal<SP: Deref, CM: AChannelManager<SP = SP>>(
3690+
cm: &CM, closed_update_ids: &mut BTreeMap<ChannelId, u64>,
3691+
in_flight_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
3692+
coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError,
3693+
chan: &mut FundedChannel<SP>,
3694+
) -> (bool, MsgHandleErrInternal)
3695+
where
3696+
SP::Target: SignerProvider,
3697+
CM::Watch: Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
3698+
{
3699+
let chan_id = chan.context.channel_id();
3700+
convert_channel_err_internal(err, chan_id, |reason, msg| {
3701+
let cm = cm.get_cm();
3702+
let logger = WithChannelContext::from(&cm.logger, &chan.context, None);
3703+
3704+
let mut shutdown_res =
3705+
if let Some(res) = coop_close_shutdown_res { res } else { chan.force_shutdown(reason) };
3706+
let chan_update = cm.get_channel_update_for_broadcast(chan).ok();
3707+
3708+
log_error!(logger, "Closed channel due to close-required error: {}", msg);
3709+
locked_close_channel!(cm, closed_update_ids, in_flight_updates, chan, shutdown_res, FUNDED);
3710+
(shutdown_res, chan_update)
3711+
})
3712+
}
3713+
36603714
/// When a channel is removed, two things need to happen:
36613715
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
36623716
/// (b) [`handle_error`] needs to be called without holding any locks (except
@@ -3700,35 +3754,19 @@ macro_rules! convert_channel_err {
37003754
}
37013755
} };
37023756
($self: ident, $peer_state: expr, $shutdown_result: expr, $funded_channel: expr, COOP_CLOSED) => { {
3703-
let chan_id = $funded_channel.context.channel_id();
37043757
let reason = ChannelError::Close(("Coop Closed".to_owned(), $shutdown_result.closure_reason.clone()));
3705-
let do_close = |_| {
3706-
(
3707-
$shutdown_result,
3708-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3709-
)
3710-
};
3711-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3712-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3713-
};
3758+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3759+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
37143760
let (close, mut err) =
3715-
convert_channel_err!($self, $peer_state, reason, $funded_channel, do_close, locked_close, chan_id, _internal);
3761+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, Some($shutdown_result), reason, $funded_channel);
37163762
err.dont_send_error_message();
37173763
debug_assert!(close);
37183764
err
37193765
} };
37203766
($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, FUNDED_CHANNEL) => { {
3721-
let chan_id = $funded_channel.context.channel_id();
3722-
let mut do_close = |reason| {
3723-
(
3724-
$funded_channel.force_shutdown(reason),
3725-
$self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3726-
)
3727-
};
3728-
let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3729-
locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3730-
};
3731-
convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, chan_id, _internal)
3767+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3768+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3769+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, $funded_channel)
37323770
} };
37333771
($self: ident, $peer_state: expr, $err: expr, $channel: expr, UNFUNDED_CHANNEL) => { {
37343772
let chan_id = $channel.context().channel_id();
@@ -3739,7 +3777,9 @@ macro_rules! convert_channel_err {
37393777
($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
37403778
match $channel.as_funded_mut() {
37413779
Some(funded_channel) => {
3742-
convert_channel_err!($self, $peer_state, $err, funded_channel, FUNDED_CHANNEL)
3780+
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3781+
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3782+
convert_funded_channel_err_internal($self, closed_update_ids, in_flight_updates, None, $err, funded_channel)
37433783
},
37443784
None => {
37453785
convert_channel_err!($self, $peer_state, $err, $channel, UNFUNDED_CHANNEL)
@@ -4506,7 +4546,8 @@ where
45064546
let mut has_uncompleted_channel = None;
45074547
for (channel_id, counterparty_node_id, state) in affected_channels {
45084548
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
4509-
let mut peer_state = peer_state_mutex.lock().unwrap();
4549+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4550+
let peer_state = &mut *peer_state_lock;
45104551
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
45114552
let reason = ClosureReason::FundingBatchClosure;
45124553
let err = ChannelError::Close((reason.to_string(), reason));
@@ -6350,9 +6391,10 @@ where
63506391
per_peer_state.get(&counterparty_node_id)
63516392
.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
63526393
.and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id).map(|chan| (chan, peer_state)))
6353-
.map(|(mut chan, mut peer_state)| {
6394+
.map(|(mut chan, mut peer_state_lock)| {
63546395
let reason = ClosureReason::ProcessingError { err: e.clone() };
63556396
let err = ChannelError::Close((e.clone(), reason));
6397+
let peer_state = &mut *peer_state_lock;
63566398
let (_, e) =
63576399
convert_channel_err!(self, peer_state, err, &mut chan);
63586400
shutdown_results.push((Err(e), counterparty_node_id));
@@ -14385,7 +14427,7 @@ where
1438514427
self,
1438614428
funding_txo,
1438714429
monitor_update,
14388-
peer_state,
14430+
&mut peer_state.in_flight_monitor_updates,
1438914431
funded_channel.context
1439014432
);
1439114433
to_process_monitor_update_actions.push((

0 commit comments

Comments
 (0)