Skip to content

Commit f8caf60

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 aa0922b commit f8caf60

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
@@ -634,7 +634,7 @@ impl MsgHandleErrInternal {
634634
err: msg,
635635
action: msgs::ErrorAction::IgnoreError,
636636
},
637-
ChannelError::Close(msg) => LightningError {
637+
ChannelError::Close((msg, _reason)) => LightningError {
638638
err: msg.clone(),
639639
action: msgs::ErrorAction::SendErrorMessage {
640640
msg: msgs::ErrorMessage {
@@ -2759,11 +2759,10 @@ macro_rules! convert_chan_phase_err {
27592759
ChannelError::Ignore(msg) => {
27602760
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
27612761
},
2762-
ChannelError::Close(msg) => {
2762+
ChannelError::Close((msg, reason)) => {
27632763
let logger = WithChannelContext::from(&$self.logger, &$channel.context);
27642764
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
27652765
update_maps_on_chan_removal!($self, $channel.context);
2766-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
27672766
let shutdown_res = $channel.context.force_shutdown(true, reason);
27682767
let err =
27692768
MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update);
@@ -4497,10 +4496,9 @@ where
44974496
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
44984497
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
44994498
let counterparty;
4500-
let err = if let ChannelError::Close(msg) = $err {
4499+
let err = if let ChannelError::Close((msg, reason)) = $err {
45014500
let channel_id = $chan.context.channel_id();
45024501
counterparty = chan.context.get_counterparty_node_id();
4503-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
45044502
let shutdown_res = $chan.context.force_shutdown(false, reason);
45054503
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
45064504
} else { unreachable!(); };

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7217,7 +7217,10 @@ fn test_user_configurable_csv_delay() {
72177217
&low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72187218
{
72197219
match error {
7220-
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())); },
7220+
ChannelError::Close((err, _)) => {
7221+
let regex = regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap();
7222+
assert!(regex.is_match(err.as_str()));
7223+
},
72217224
_ => panic!("Unexpected event"),
72227225
}
72237226
} else { assert!(false); }
@@ -7249,7 +7252,10 @@ fn test_user_configurable_csv_delay() {
72497252
&high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72507253
{
72517254
match error {
7252-
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())); },
7255+
ChannelError::Close((err, _)) => {
7256+
let regex = regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap();
7257+
assert!(regex.is_match(err.as_str()));
7258+
},
72537259
_ => panic!("Unexpected event"),
72547260
}
72557261
} else { assert!(false); }

0 commit comments

Comments
 (0)