Skip to content

Commit

Permalink
Merge pull request #1224 from MutinyWallet/ln-chan-errs
Browse files Browse the repository at this point in the history
Return channel open errors to user
  • Loading branch information
TonyGiorgio authored Jun 18, 2024
2 parents 149318a + d4e7322 commit b11e68d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
7 changes: 7 additions & 0 deletions mutiny-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub enum MutinyError {
/// A channel could not be opened.
#[error("Failed to create channel.")]
ChannelCreationFailed,
/// A channel could not be opened.
#[error("Failed to create channel. {0}")]
ChannelCreationFailedWithReason(String),
/// A channel could not be closed.
#[error("Failed to close channel.")]
ChannelClosingFailed,
Expand Down Expand Up @@ -237,6 +240,10 @@ impl PartialEq for MutinyError {
(Self::RoutingFailed, Self::RoutingFailed) => true,
(Self::PeerInfoParseFailed, Self::PeerInfoParseFailed) => true,
(Self::ChannelCreationFailed, Self::ChannelCreationFailed) => true,
(
Self::ChannelCreationFailedWithReason(x),
Self::ChannelCreationFailedWithReason(y),
) => x == y,
(Self::ChannelClosingFailed, Self::ChannelClosingFailed) => true,
(Self::PersistenceFailed { source }, Self::PersistenceFailed { source: source2 }) => {
source == source2
Expand Down
17 changes: 14 additions & 3 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,20 @@ impl<S: MutinyStorage> EventHandler<S> {
..
} => {
// if we still have channel open params, then it was just a failed channel open
// we should not persist this as a closed channel and just delete the channel open params
if let Ok(Some(_)) = self.persister.get_channel_open_params(user_channel_id) {
let _ = self.persister.delete_channel_open_params(user_channel_id);
// we should not persist this as a closed channel and pass back the failure reason
if let Ok(Some(mut params)) =
self.persister.get_channel_open_params(user_channel_id)
{
// Remove the LDK fluff from the error message
let reason_str = reason.to_string().replace(
"Channel closed because counterparty force-closed with message: ",
"",
);

params.failure_reason = Some(reason_str);
let _ = self
.persister
.persist_channel_open_params(user_channel_id, params);
return;
};

Expand Down
4 changes: 4 additions & 0 deletions mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ pub(crate) struct ChannelOpenParams {
pub(crate) labels: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) opening_tx: Option<Transaction>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) failure_reason: Option<String>,
}

impl ChannelOpenParams {
Expand All @@ -560,6 +562,7 @@ impl ChannelOpenParams {
utxos: None,
labels: None,
opening_tx: None,
failure_reason: None,
}
}

Expand All @@ -574,6 +577,7 @@ impl ChannelOpenParams {
utxos: Some(utxos),
labels: None,
opening_tx: None,
failure_reason: None,
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,10 +1874,16 @@ impl<S: MutinyStorage> Node<S> {
return Err(MutinyError::NotRunning);
}

// We will get a channel closure event if the peer rejects the channel
// todo return closure reason to user
if let Ok(Some(_closure)) = self.persister.get_channel_closure(user_channel_id) {
return Err(MutinyError::ChannelCreationFailed);
// We'll set failure reason if the peer rejects the channel
if let Some(failure_reason) = self
.persister
.get_channel_open_params(user_channel_id)?
.and_then(|p| p.failure_reason)
{
log_error!(self.logger, "Channel funding tx failed: {failure_reason}");
// can now safely delete the channel open params
let _ = self.persister.delete_channel_open_params(user_channel_id);
return Err(MutinyError::ChannelCreationFailedWithReason(failure_reason));
}

let channels = self.channel_manager.list_channels_with_counterparty(pubkey);
Expand Down
6 changes: 6 additions & 0 deletions mutiny-wasm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ pub enum MutinyJsError {
/// A channel could not be opened.
#[error("Failed to create channel.")]
ChannelCreationFailed,
/// A channel could not be opened.
#[error("Failed to create channel. {0}")]
ChannelCreationFailedWithReason(String),
/// A channel could not be closed.
#[error("Failed to close channel.")]
ChannelClosingFailed,
Expand Down Expand Up @@ -203,6 +206,9 @@ impl From<MutinyError> for MutinyJsError {
MutinyError::RoutingFailed => MutinyJsError::RoutingFailed,
MutinyError::PeerInfoParseFailed => MutinyJsError::PeerInfoParseFailed,
MutinyError::ChannelCreationFailed => MutinyJsError::ChannelCreationFailed,
MutinyError::ChannelCreationFailedWithReason(x) => {
MutinyJsError::ChannelCreationFailedWithReason(x)
}
MutinyError::ChannelClosingFailed => MutinyJsError::ChannelClosingFailed,
MutinyError::PersistenceFailed { source: _ } => MutinyJsError::PersistenceFailed,
MutinyError::ReadError { source: _ } => MutinyJsError::ReadError,
Expand Down

0 comments on commit b11e68d

Please sign in to comment.