Allow ChannelError to specify the ClosureReason

This will allow us to add more granular failure reasons when
returning the general `ChannelError::Close`.
This commit is contained in:
Matt Corallo 2024-05-06 00:02:09 +00:00
parent 3e09d9937e
commit 93011c377c
3 changed files with 15 additions and 11 deletions

View file

@ -710,7 +710,7 @@ pub const MIN_THEIR_CHAN_RESERVE_SATOSHIS: u64 = 1000;
pub(super) enum ChannelError {
Ignore(String),
Warn(String),
Close(String),
Close((String, ClosureReason)),
}
impl fmt::Debug for ChannelError {
@ -718,7 +718,7 @@ impl fmt::Debug for ChannelError {
match self {
&ChannelError::Ignore(ref e) => write!(f, "Ignore : {}", e),
&ChannelError::Warn(ref e) => write!(f, "Warn : {}", e),
&ChannelError::Close(ref e) => write!(f, "Close : {}", e),
&ChannelError::Close((ref e, _)) => write!(f, "Close : {}", e),
}
}
}
@ -728,14 +728,14 @@ impl fmt::Display for ChannelError {
match self {
&ChannelError::Ignore(ref e) => write!(f, "{}", e),
&ChannelError::Warn(ref e) => write!(f, "{}", e),
&ChannelError::Close(ref e) => write!(f, "{}", e),
&ChannelError::Close((ref e, _)) => write!(f, "{}", e),
}
}
}
impl ChannelError {
pub(super) fn close(err: String) -> Self {
ChannelError::Close(err.clone())
ChannelError::Close((err.clone(), ClosureReason::ProcessingError { err }))
}
}

View file

@ -636,7 +636,7 @@ impl MsgHandleErrInternal {
err: msg,
action: msgs::ErrorAction::IgnoreError,
},
ChannelError::Close(msg) => LightningError {
ChannelError::Close((msg, _reason)) => LightningError {
err: msg.clone(),
action: msgs::ErrorAction::SendErrorMessage {
msg: msgs::ErrorMessage {
@ -2446,11 +2446,10 @@ macro_rules! convert_chan_phase_err {
ChannelError::Ignore(msg) => {
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
},
ChannelError::Close(msg) => {
ChannelError::Close((msg, reason)) => {
let logger = WithChannelContext::from(&$self.logger, &$channel.context, None);
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
update_maps_on_chan_removal!($self, $channel.context);
let reason = ClosureReason::ProcessingError { err: msg.clone() };
let shutdown_res = $channel.context.force_shutdown(true, reason);
let err =
MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update);
@ -4201,10 +4200,9 @@ where
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
let counterparty;
let err = if let ChannelError::Close(msg) = $err {
let err = if let ChannelError::Close((msg, reason)) = $err {
let channel_id = $chan.context.channel_id();
counterparty = chan.context.get_counterparty_node_id();
let reason = ClosureReason::ProcessingError { err: msg.clone() };
let shutdown_res = $chan.context.force_shutdown(false, reason);
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
} else { unreachable!(); };

View file

@ -7263,7 +7263,10 @@ fn test_user_configurable_csv_delay() {
&low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
{
match error {
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())); },
ChannelError::Close((err, _)) => {
let regex = regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap();
assert!(regex.is_match(err.as_str()));
},
_ => panic!("Unexpected event"),
}
} else { assert!(false); }
@ -7295,7 +7298,10 @@ fn test_user_configurable_csv_delay() {
&high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
{
match error {
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())); },
ChannelError::Close((err, _)) => {
let regex = regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap();
assert!(regex.is_match(err.as_str()));
},
_ => panic!("Unexpected event"),
}
} else { assert!(false); }