Skip to content

Commit c0f88b0

Browse files
committed
Convert macro to convert_channel_err method
1 parent f969eaa commit c0f88b0

File tree

1 file changed

+113
-42
lines changed

1 file changed

+113
-42
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 113 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,40 +3597,17 @@ fn convert_channel_err_internal<
35973597
}
35983598
}
35993599

3600-
/// When a channel is removed, two things need to happen:
3601-
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
3602-
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
3603-
/// [`ChannelManager::total_consistency_lock`]), which then calls
3604-
/// [`ChannelManager::finish_close_channel`].
3605-
///
3606-
/// Note that this step can be skipped if the channel was never opened (through the creation of a
3607-
/// [`ChannelMonitor`]/channel funding transaction) to begin with.
3608-
///
3609-
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
3610-
/// error)`, except in the `COOP_CLOSE` case, where the bool is elided (it is always implicitly
3611-
/// true).
3612-
#[rustfmt::skip]
3613-
macro_rules! convert_channel_err {
3614-
($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
3615-
match $channel.as_funded_mut() {
3616-
Some(funded_channel) => {
3617-
let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
3618-
let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3619-
$self.convert_funded_channel_err_internal(closed_update_ids, in_flight_updates, None, $err, funded_channel)
3620-
},
3621-
None => {
3622-
$self.convert_unfunded_channel_err_internal($err, $channel)
3623-
},
3624-
}
3625-
};
3626-
}
3627-
36283600
macro_rules! break_channel_entry {
36293601
($self: ident, $peer_state: expr, $res: expr, $entry: expr) => {
36303602
match $res {
36313603
Ok(res) => res,
36323604
Err(e) => {
3633-
let (drop, res) = convert_channel_err!($self, $peer_state, e, $entry.get_mut());
3605+
let (drop, res) = $self.convert_channel_err(
3606+
&mut $peer_state.closed_channel_monitor_update_ids,
3607+
&mut $peer_state.in_flight_monitor_updates,
3608+
e,
3609+
$entry.get_mut(),
3610+
);
36343611
if drop {
36353612
$entry.remove_entry();
36363613
}
@@ -3645,7 +3622,12 @@ macro_rules! try_channel_entry {
36453622
match $res {
36463623
Ok(res) => res,
36473624
Err(e) => {
3648-
let (drop, res) = convert_channel_err!($self, $peer_state, e, $entry.get_mut());
3625+
let (drop, res) = $self.convert_channel_err(
3626+
&mut $peer_state.closed_channel_monitor_update_ids,
3627+
&mut $peer_state.in_flight_monitor_updates,
3628+
e,
3629+
$entry.get_mut(),
3630+
);
36493631
if drop {
36503632
$entry.remove_entry();
36513633
}
@@ -4062,6 +4044,34 @@ where
40624044
)
40634045
}
40644046

4047+
/// When a channel is removed, two things need to happen:
4048+
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
4049+
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
4050+
/// [`ChannelManager::total_consistency_lock`]), which then calls
4051+
/// [`ChannelManager::finish_close_channel`].
4052+
///
4053+
/// Note that this step can be skipped if the channel was never opened (through the creation of a
4054+
/// [`ChannelMonitor`]/channel funding transaction) to begin with.
4055+
///
4056+
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
4057+
/// error)`.
4058+
fn convert_channel_err(
4059+
&self, closed_update_ids: &mut BTreeMap<ChannelId, u64>,
4060+
in_flight_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
4061+
err: ChannelError, channel: &mut Channel<SP>,
4062+
) -> (bool, MsgHandleErrInternal) {
4063+
match channel.as_funded_mut() {
4064+
Some(funded_channel) => self.convert_funded_channel_err_internal(
4065+
closed_update_ids,
4066+
in_flight_updates,
4067+
None,
4068+
err,
4069+
funded_channel,
4070+
),
4071+
None => self.convert_unfunded_channel_err_internal(err, channel),
4072+
}
4073+
}
4074+
40654075
/// Gets the current [`UserConfig`] which controls some global behavior and includes the
40664076
/// default configuration applied to all new channels.
40674077
pub fn get_current_config(&self) -> UserConfig {
@@ -4405,7 +4415,13 @@ where
44054415
let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
44064416
let err = ChannelError::Close((reason.to_string(), reason));
44074417
let mut chan = chan_entry.remove();
4408-
let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
4418+
let (_, mut e) = self.convert_channel_err(
4419+
&mut peer_state.closed_channel_monitor_update_ids,
4420+
&mut peer_state.in_flight_monitor_updates,
4421+
err,
4422+
&mut chan,
4423+
);
4424+
44094425
e.dont_send_error_message();
44104426
shutdown_result = Err(e);
44114427
}
@@ -4590,7 +4606,12 @@ where
45904606
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
45914607
let reason = ClosureReason::FundingBatchClosure;
45924608
let err = ChannelError::Close((reason.to_string(), reason));
4593-
let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
4609+
let (_, e) = self.convert_channel_err(
4610+
&mut peer_state.closed_channel_monitor_update_ids,
4611+
&mut peer_state.in_flight_monitor_updates,
4612+
err,
4613+
&mut chan,
4614+
);
45944615
shutdown_results.push((Err(e), counterparty_node_id));
45954616
}
45964617
}
@@ -4666,7 +4687,12 @@ where
46664687
if let Some(mut chan) = peer_state.channel_by_id.remove(channel_id) {
46674688
log_error!(logger, "Force-closing channel");
46684689
let err = ChannelError::Close((message, reason));
4669-
let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
4690+
let (_, mut e) = self.convert_channel_err(
4691+
&mut peer_state.closed_channel_monitor_update_ids,
4692+
&mut peer_state.in_flight_monitor_updates,
4693+
err,
4694+
&mut chan,
4695+
);
46704696
mem::drop(peer_state_lock);
46714697
mem::drop(per_peer_state);
46724698
if is_from_counterparty {
@@ -6444,7 +6470,12 @@ where
64446470
let err = ChannelError::Close((e.clone(), reason));
64456471
let peer_state = &mut *peer_state_lock;
64466472
let (_, e) =
6447-
convert_channel_err!(self, peer_state, err, &mut chan);
6473+
self.convert_channel_err(
6474+
&mut peer_state.closed_channel_monitor_update_ids,
6475+
&mut peer_state.in_flight_monitor_updates,
6476+
err,
6477+
&mut chan,
6478+
);
64486479
shutdown_results.push((Err(e), counterparty_node_id));
64496480
});
64506481
}
@@ -8283,7 +8314,12 @@ where
82838314
let reason = ClosureReason::FundingTimedOut;
82848315
let msg = "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned();
82858316
let err = ChannelError::Close((msg, reason));
8286-
let (_, e) = convert_channel_err!(self, peer_state, err, chan);
8317+
let (_, e) = self.convert_channel_err(
8318+
&mut peer_state.closed_channel_monitor_update_ids,
8319+
&mut peer_state.in_flight_monitor_updates,
8320+
err,
8321+
chan,
8322+
);
82878323
handle_errors.push((Err(e), counterparty_node_id));
82888324
false
82898325
} else {
@@ -10481,14 +10517,24 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1048110517
// concerning this channel as it is safe to do so.
1048210518
debug_assert!(matches!(err, ChannelError::Close(_)));
1048310519
let mut chan = Channel::from(inbound_chan);
10484-
return Err(convert_channel_err!(self, peer_state, err, &mut chan).1);
10520+
return Err(self.convert_channel_err(
10521+
&mut peer_state.closed_channel_monitor_update_ids,
10522+
&mut peer_state.in_flight_monitor_updates,
10523+
err,
10524+
&mut chan,
10525+
).1);
1048510526
},
1048610527
}
1048710528
},
1048810529
Some(Err(mut chan)) => {
1048910530
let err_msg = format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id);
1049010531
let err = ChannelError::close(err_msg);
10491-
return Err(convert_channel_err!(self, peer_state, err, &mut chan).1);
10532+
return Err(self.convert_channel_err(
10533+
&mut peer_state.closed_channel_monitor_update_ids,
10534+
&mut peer_state.in_flight_monitor_updates,
10535+
err,
10536+
&mut chan,
10537+
).1);
1049210538
},
1049310539
None => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
1049410540
};
@@ -11116,7 +11162,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1111611162
let reason = ClosureReason::CounterpartyCoopClosedUnfundedChannel;
1111711163
let err = ChannelError::Close((reason.to_string(), reason));
1111811164
let mut chan = chan_entry.remove();
11119-
let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan);
11165+
let (_, mut e) = self.convert_channel_err(
11166+
&mut peer_state.closed_channel_monitor_update_ids,
11167+
&mut peer_state.in_flight_monitor_updates,
11168+
err,
11169+
&mut chan,
11170+
);
1112011171
e.dont_send_error_message();
1112111172
return Err(e);
1112211173
},
@@ -12272,7 +12323,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1227212323
};
1227312324
let err = ChannelError::Close((reason.to_string(), reason));
1227412325
let mut chan = chan_entry.remove();
12275-
let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
12326+
let (_, e) = self.convert_channel_err(
12327+
&mut peer_state.closed_channel_monitor_update_ids,
12328+
&mut peer_state.in_flight_monitor_updates,
12329+
err,
12330+
&mut chan,
12331+
);
1227612332
failed_channels.push((Err(e), counterparty_node_id));
1227712333
}
1227812334
}
@@ -12288,7 +12344,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1228812344
let reason = ClosureReason::CommitmentTxConfirmed;
1228912345
let err = ChannelError::Close((reason.to_string(), reason));
1229012346
let mut chan = chan_entry.remove();
12291-
let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan);
12347+
let (_, e) = self.convert_channel_err(
12348+
&mut peer_state.closed_channel_monitor_update_ids,
12349+
&mut peer_state.in_flight_monitor_updates,
12350+
err,
12351+
&mut chan,
12352+
);
1229212353
failed_channels.push((Err(e), counterparty_node_id));
1229312354
}
1229412355
}
@@ -12485,7 +12546,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1248512546
_ => match unblock_chan(chan, &mut peer_state.pending_msg_events) {
1248612547
Ok(shutdown_result) => shutdown_result,
1248712548
Err(err) => {
12488-
let (_, err) = convert_channel_err!(self, peer_state, err, chan);
12549+
let (_, err) = self.convert_channel_err(
12550+
&mut peer_state.closed_channel_monitor_update_ids,
12551+
&mut peer_state.in_flight_monitor_updates,
12552+
err,
12553+
chan,
12554+
);
1248912555
shutdown_results.push((Err(err), *cp_id));
1249012556
return false;
1249112557
},
@@ -13930,7 +13996,12 @@ where
1393013996
// Clean up for removal.
1393113997
let reason = ClosureReason::DisconnectedPeer;
1393213998
let err = ChannelError::Close((reason.to_string(), reason));
13933-
let (_, e) = convert_channel_err!(self, peer_state, err, chan);
13999+
let (_, e) = self.convert_channel_err(
14000+
&mut peer_state.closed_channel_monitor_update_ids,
14001+
&mut peer_state.in_flight_monitor_updates,
14002+
err,
14003+
chan,
14004+
);
1393414005
failed_channels.push((Err(e), counterparty_node_id));
1393514006
false
1393614007
});

0 commit comments

Comments
 (0)