Skip to content

Commit 85fd470

Browse files
committed
Allow ChannelError to specify the ClosureReason
This will allow us to add more granular failure reasons when returning the general `ChannelError::Close`.
1 parent 6aa0d30 commit 85fd470

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,15 +896,15 @@ pub const MIN_THEIR_CHAN_RESERVE_SATOSHIS: u64 = 1000;
896896
pub(super) enum ChannelError {
897897
Ignore(String),
898898
Warn(String),
899-
Close(String),
899+
Close((String, ClosureReason)),
900900
}
901901

902902
impl fmt::Debug for ChannelError {
903903
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
904904
match self {
905905
&ChannelError::Ignore(ref e) => write!(f, "Ignore : {}", e),
906906
&ChannelError::Warn(ref e) => write!(f, "Warn : {}", e),
907-
&ChannelError::Close(ref e) => write!(f, "Close : {}", e),
907+
&ChannelError::Close((ref e, _)) => write!(f, "Close : {}", e),
908908
}
909909
}
910910
}
@@ -914,14 +914,14 @@ impl fmt::Display for ChannelError {
914914
match self {
915915
&ChannelError::Ignore(ref e) => write!(f, "{}", e),
916916
&ChannelError::Warn(ref e) => write!(f, "{}", e),
917-
&ChannelError::Close(ref e) => write!(f, "{}", e),
917+
&ChannelError::Close((ref e, _)) => write!(f, "{}", e),
918918
}
919919
}
920920
}
921921

922922
impl ChannelError {
923923
pub(super) fn close(err: String) -> Self {
924-
ChannelError::Close(err.clone())
924+
ChannelError::Close((err.clone(), ClosureReason::ProcessingError { err }))
925925
}
926926
}
927927

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ impl MsgHandleErrInternal {
636636
err: msg,
637637
action: msgs::ErrorAction::IgnoreError,
638638
},
639-
ChannelError::Close(msg) => LightningError {
639+
ChannelError::Close((msg, _reason)) => LightningError {
640640
err: msg.clone(),
641641
action: msgs::ErrorAction::SendErrorMessage {
642642
msg: msgs::ErrorMessage {
@@ -2763,11 +2763,10 @@ macro_rules! convert_chan_phase_err {
27632763
ChannelError::Ignore(msg) => {
27642764
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
27652765
},
2766-
ChannelError::Close(msg) => {
2766+
ChannelError::Close((msg, reason)) => {
27672767
let logger = WithChannelContext::from(&$self.logger, &$channel.context, None);
27682768
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
27692769
update_maps_on_chan_removal!($self, $channel.context);
2770-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
27712770
let shutdown_res = $channel.context.force_shutdown(true, reason);
27722771
let err =
27732772
MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update);
@@ -4501,10 +4500,9 @@ where
45014500
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
45024501
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
45034502
let counterparty;
4504-
let err = if let ChannelError::Close(msg) = $err {
4503+
let err = if let ChannelError::Close((msg, reason)) = $err {
45054504
let channel_id = $chan.context.channel_id();
45064505
counterparty = chan.context.get_counterparty_node_id();
4507-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
45084506
let shutdown_res = $chan.context.force_shutdown(false, reason);
45094507
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
45104508
} else { unreachable!(); };

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7257,7 +7257,10 @@ fn test_user_configurable_csv_delay() {
72577257
&low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72587258
{
72597259
match error {
7260-
ChannelError::Close(err) => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },
7260+
ChannelError::Close((err, _)) => {
7261+
let regex = regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap();
7262+
assert!(regex.is_match(err.as_str()));
7263+
},
72617264
_ => panic!("Unexpected event"),
72627265
}
72637266
} else { assert!(false); }
@@ -7289,7 +7292,10 @@ fn test_user_configurable_csv_delay() {
72897292
&high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72907293
{
72917294
match error {
7292-
ChannelError::Close(err) => { assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(err.as_str())); },
7295+
ChannelError::Close((err, _)) => {
7296+
let regex = regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap();
7297+
assert!(regex.is_match(err.as_str()));
7298+
},
72937299
_ => panic!("Unexpected event"),
72947300
}
72957301
} else { assert!(false); }

0 commit comments

Comments
 (0)