Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit b1ae845

Browse files
committed
Return channel open errors to user
1 parent 149318a commit b1ae845

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

mutiny-core/src/error.rs

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ pub enum MutinyError {
9595
/// A channel could not be opened.
9696
#[error("Failed to create channel.")]
9797
ChannelCreationFailed,
98+
/// A channel could not be opened.
99+
#[error("Failed to create channel. {0}")]
100+
ChannelCreationFailedWithReason(String),
98101
/// A channel could not be closed.
99102
#[error("Failed to close channel.")]
100103
ChannelClosingFailed,
@@ -237,6 +240,10 @@ impl PartialEq for MutinyError {
237240
(Self::RoutingFailed, Self::RoutingFailed) => true,
238241
(Self::PeerInfoParseFailed, Self::PeerInfoParseFailed) => true,
239242
(Self::ChannelCreationFailed, Self::ChannelCreationFailed) => true,
243+
(
244+
Self::ChannelCreationFailedWithReason(x),
245+
Self::ChannelCreationFailedWithReason(y),
246+
) => x == y,
240247
(Self::ChannelClosingFailed, Self::ChannelClosingFailed) => true,
241248
(Self::PersistenceFailed { source }, Self::PersistenceFailed { source: source2 }) => {
242249
source == source2

mutiny-core/src/event.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,19 @@ impl<S: MutinyStorage> EventHandler<S> {
524524
} => {
525525
// if we still have channel open params, then it was just a failed channel open
526526
// we should not persist this as a closed channel and just delete the channel open params
527-
if let Ok(Some(_)) = self.persister.get_channel_open_params(user_channel_id) {
528-
let _ = self.persister.delete_channel_open_params(user_channel_id);
527+
if let Ok(Some(mut params)) =
528+
self.persister.get_channel_open_params(user_channel_id)
529+
{
530+
// Remove the LDK fluff from the error message
531+
let reason_str = reason.to_string().replace(
532+
"Channel closed because counterparty force-closed with message: ",
533+
"",
534+
);
535+
536+
params.failure_reason = Some(reason_str);
537+
let _ = self
538+
.persister
539+
.persist_channel_open_params(user_channel_id, params);
529540
return;
530541
};
531542

mutiny-core/src/ldkstorage.rs

+4
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ pub(crate) struct ChannelOpenParams {
550550
pub(crate) labels: Option<Vec<String>>,
551551
#[serde(skip_serializing_if = "Option::is_none")]
552552
pub(crate) opening_tx: Option<Transaction>,
553+
#[serde(skip_serializing_if = "Option::is_none")]
554+
pub(crate) failure_reason: Option<String>,
553555
}
554556

555557
impl ChannelOpenParams {
@@ -560,6 +562,7 @@ impl ChannelOpenParams {
560562
utxos: None,
561563
labels: None,
562564
opening_tx: None,
565+
failure_reason: None,
563566
}
564567
}
565568

@@ -574,6 +577,7 @@ impl ChannelOpenParams {
574577
utxos: Some(utxos),
575578
labels: None,
576579
opening_tx: None,
580+
failure_reason: None,
577581
}
578582
}
579583
}

mutiny-core/src/node.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1874,10 +1874,16 @@ impl<S: MutinyStorage> Node<S> {
18741874
return Err(MutinyError::NotRunning);
18751875
}
18761876

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

18831889
let channels = self.channel_manager.list_channels_with_counterparty(pubkey);

mutiny-wasm/src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub enum MutinyJsError {
8686
/// A channel could not be opened.
8787
#[error("Failed to create channel.")]
8888
ChannelCreationFailed,
89+
/// A channel could not be opened.
90+
#[error("Failed to create channel. {0}")]
91+
ChannelCreationFailedWithReason(String),
8992
/// A channel could not be closed.
9093
#[error("Failed to close channel.")]
9194
ChannelClosingFailed,
@@ -203,6 +206,9 @@ impl From<MutinyError> for MutinyJsError {
203206
MutinyError::RoutingFailed => MutinyJsError::RoutingFailed,
204207
MutinyError::PeerInfoParseFailed => MutinyJsError::PeerInfoParseFailed,
205208
MutinyError::ChannelCreationFailed => MutinyJsError::ChannelCreationFailed,
209+
MutinyError::ChannelCreationFailedWithReason(x) => {
210+
MutinyJsError::ChannelCreationFailedWithReason(x)
211+
}
206212
MutinyError::ChannelClosingFailed => MutinyJsError::ChannelClosingFailed,
207213
MutinyError::PersistenceFailed { source: _ } => MutinyJsError::PersistenceFailed,
208214
MutinyError::ReadError { source: _ } => MutinyJsError::ReadError,

0 commit comments

Comments
 (0)