Skip to content

Commit 93011c3

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 3e09d99 commit 93011c3

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

lightning/src/ln/channel.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -710,15 +710,15 @@ pub const MIN_THEIR_CHAN_RESERVE_SATOSHIS: u64 = 1000;
710710
pub(super) enum ChannelError {
711711
Ignore(String),
712712
Warn(String),
713-
Close(String),
713+
Close((String, ClosureReason)),
714714
}
715715

716716
impl fmt::Debug for ChannelError {
717717
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
718718
match self {
719719
&ChannelError::Ignore(ref e) => write!(f, "Ignore : {}", e),
720720
&ChannelError::Warn(ref e) => write!(f, "Warn : {}", e),
721-
&ChannelError::Close(ref e) => write!(f, "Close : {}", e),
721+
&ChannelError::Close((ref e, _)) => write!(f, "Close : {}", e),
722722
}
723723
}
724724
}
@@ -728,14 +728,14 @@ impl fmt::Display for ChannelError {
728728
match self {
729729
&ChannelError::Ignore(ref e) => write!(f, "{}", e),
730730
&ChannelError::Warn(ref e) => write!(f, "{}", e),
731-
&ChannelError::Close(ref e) => write!(f, "{}", e),
731+
&ChannelError::Close((ref e, _)) => write!(f, "{}", e),
732732
}
733733
}
734734
}
735735

736736
impl ChannelError {
737737
pub(super) fn close(err: String) -> Self {
738-
ChannelError::Close(err.clone())
738+
ChannelError::Close((err.clone(), ClosureReason::ProcessingError { err }))
739739
}
740740
}
741741

lightning/src/ln/channelmanager.rs

+3-5
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 {
@@ -2446,11 +2446,10 @@ macro_rules! convert_chan_phase_err {
24462446
ChannelError::Ignore(msg) => {
24472447
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
24482448
},
2449-
ChannelError::Close(msg) => {
2449+
ChannelError::Close((msg, reason)) => {
24502450
let logger = WithChannelContext::from(&$self.logger, &$channel.context, None);
24512451
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
24522452
update_maps_on_chan_removal!($self, $channel.context);
2453-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
24542453
let shutdown_res = $channel.context.force_shutdown(true, reason);
24552454
let err =
24562455
MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update);
@@ -4201,10 +4200,9 @@ where
42014200
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
42024201
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
42034202
let counterparty;
4204-
let err = if let ChannelError::Close(msg) = $err {
4203+
let err = if let ChannelError::Close((msg, reason)) = $err {
42054204
let channel_id = $chan.context.channel_id();
42064205
counterparty = chan.context.get_counterparty_node_id();
4207-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
42084206
let shutdown_res = $chan.context.force_shutdown(false, reason);
42094207
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
42104208
} else { unreachable!(); };

lightning/src/ln/functional_tests.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7263,7 +7263,10 @@ fn test_user_configurable_csv_delay() {
72637263
&low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72647264
{
72657265
match error {
7266-
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())); },
7266+
ChannelError::Close((err, _)) => {
7267+
let regex = regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap();
7268+
assert!(regex.is_match(err.as_str()));
7269+
},
72677270
_ => panic!("Unexpected event"),
72687271
}
72697272
} else { assert!(false); }
@@ -7295,7 +7298,10 @@ fn test_user_configurable_csv_delay() {
72957298
&high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
72967299
{
72977300
match error {
7298-
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())); },
7301+
ChannelError::Close((err, _)) => {
7302+
let regex = regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap();
7303+
assert!(regex.is_match(err.as_str()));
7304+
},
72997305
_ => panic!("Unexpected event"),
73007306
}
73017307
} else { assert!(false); }

0 commit comments

Comments
 (0)