Skip to content

Commit

Permalink
Return channel open errors to user
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jun 17, 2024
1 parent 149318a commit be99525
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 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
15 changes: 13 additions & 2 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,19 @@ 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);
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. Reason: {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 be99525

Please sign in to comment.