Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reason to Event::PaymentFailed and Event::ChannelClosed #260

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,35 @@ enum BuildError {

[Enum]
interface Event {
PaymentSuccessful( PaymentHash payment_hash );
PaymentFailed( PaymentHash payment_hash );
PaymentReceived( PaymentHash payment_hash, u64 amount_msat);
ChannelPending ( ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo );
ChannelReady ( ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id );
ChannelClosed ( ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id );
PaymentSuccessful(PaymentHash payment_hash);
PaymentFailed(PaymentHash payment_hash, PaymentFailureReason? reason);
PaymentReceived(PaymentHash payment_hash, u64 amount_msat);
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
};

enum PaymentFailureReason {
"RecipientRejected",
"UserAbandoned",
"RetriesExhausted",
"PaymentExpired",
"RouteNotFound",
"UnexpectedError",
};

[Enum]
interface ClosureReason {
CounterpartyForceClosed ( UntrustedString peer_msg );
HolderForceClosed ();
CooperativeClosure ();
CommitmentTxConfirmed ();
FundingTimedOut ();
ProcessingError ( string err );
DisconnectedPeer ();
OutdatedChannelManager ();
CounterpartyCoopClosedUnfundedChannel ();
FundingBatchClosure ();
};

enum PaymentDirection {
Expand Down Expand Up @@ -300,3 +323,6 @@ typedef string UserChannelId;

[Custom]
typedef string Mnemonic;

[Custom]
typedef string UntrustedString;
34 changes: 22 additions & 12 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::io::{
use crate::logger::{log_error, log_info, Logger};

use lightning::chain::chaininterface::ConfirmationTarget;
use lightning::events::Event as LdkEvent;
use lightning::events::PaymentPurpose;
use lightning::events::{ClosureReason, PaymentPurpose};
use lightning::events::{Event as LdkEvent, PaymentFailureReason};
use lightning::impl_writeable_tlv_based_enum;
use lightning::ln::{ChannelId, PaymentHash};
use lightning::routing::gossip::NodeId;
Expand Down Expand Up @@ -52,6 +52,10 @@ pub enum Event {
PaymentFailed {
/// The hash of the payment.
payment_hash: PaymentHash,
/// The reason why the payment failed.
///
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
reason: Option<PaymentFailureReason>,
},
/// A payment has been received.
PaymentReceived {
Expand Down Expand Up @@ -81,7 +85,7 @@ pub enum Event {
user_channel_id: UserChannelId,
/// The `node_id` of the channel counterparty.
///
/// This will be `None` for events serialized by LDK Node XXX TODO and prior.
/// This will be `None` for events serialized by LDK Node v0.1.0 and prior.
counterparty_node_id: Option<PublicKey>,
},
/// A channel has been closed.
Expand All @@ -92,8 +96,10 @@ pub enum Event {
user_channel_id: UserChannelId,
/// The `node_id` of the channel counterparty.
///
/// This will be `None` for events serialized by LDK Node XXX TODO and prior.
/// This will be `None` for events serialized by LDK Node v0.1.0 and prior.
counterparty_node_id: Option<PublicKey>,
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
reason: Option<ClosureReason>,
},
}

Expand All @@ -103,6 +109,7 @@ impl_writeable_tlv_based_enum!(Event,
},
(1, PaymentFailed) => {
(0, payment_hash, required),
(1, reason, option),
},
(2, PaymentReceived) => {
(0, payment_hash, required),
Expand All @@ -124,6 +131,7 @@ impl_writeable_tlv_based_enum!(Event,
(0, channel_id, required),
(1, counterparty_node_id, option),
(2, user_channel_id, required),
(3, reason, upgradable_option),
};
);

Expand Down Expand Up @@ -609,11 +617,12 @@ where
panic!("Failed to push to event queue");
});
}
LdkEvent::PaymentFailed { payment_hash, .. } => {
LdkEvent::PaymentFailed { payment_hash, reason, .. } => {
log_info!(
self.logger,
"Failed to send payment to payment hash {:?}.",
hex_utils::to_string(&payment_hash.0)
"Failed to send payment to payment hash {:?} due to {:?}.",
hex_utils::to_string(&payment_hash.0),
reason
);

let update = PaymentDetailsUpdate {
Expand All @@ -624,12 +633,12 @@ where
log_error!(self.logger, "Failed to access payment store: {}", e);
panic!("Failed to access payment store");
});
self.event_queue.add_event(Event::PaymentFailed { payment_hash }).unwrap_or_else(
|e| {
self.event_queue
.add_event(Event::PaymentFailed { payment_hash, reason })
.unwrap_or_else(|e| {
log_error!(self.logger, "Failed to push to event queue: {}", e);
panic!("Failed to push to event queue");
},
);
});
}

LdkEvent::PaymentPathSuccessful { .. } => {}
Expand Down Expand Up @@ -846,12 +855,13 @@ where
counterparty_node_id,
..
} => {
log_info!(self.logger, "Channel {} closed due to: {:?}", channel_id, reason);
log_info!(self.logger, "Channel {} closed due to: {}", channel_id, reason);
self.event_queue
.add_event(Event::ChannelClosed {
channel_id,
user_channel_id: UserChannelId(user_channel_id),
counterparty_node_id,
reason: Some(reason),
})
.unwrap_or_else(|e| {
log_error!(self.logger, "Failed to push to event queue: {}", e);
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub use types::ChannelConfig;
pub use io::utils::generate_entropy_mnemonic;

#[cfg(feature = "uniffi")]
use {bip39::Mnemonic, bitcoin::OutPoint, lightning::ln::PaymentSecret, uniffi_types::*};
use uniffi_types::*;

#[cfg(feature = "uniffi")]
pub use builder::ArcedNodeBuilder as Builder;
Expand All @@ -135,8 +135,6 @@ use lightning::ln::channelmanager::{self, PaymentId, RecipientOnionFields, Retry
use lightning::ln::msgs::SocketAddress;
use lightning::ln::{PaymentHash, PaymentPreimage};

#[cfg(feature = "uniffi")]
use lightning::ln::ChannelId;
use lightning::sign::EntropySource;

use lightning::util::persist::KVStore;
Expand Down
24 changes: 21 additions & 3 deletions src/uniffi_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
pub use lightning::events::{ClosureReason, PaymentFailureReason};
pub use lightning::ln::ChannelId;
pub use lightning::ln::PaymentSecret;
pub use lightning::util::string::UntrustedString;

pub use bitcoin::OutPoint;

pub use bip39::Mnemonic;

use crate::UniffiCustomTypeConverter;

use crate::error::Error;
Expand All @@ -9,11 +18,9 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Address, Txid};
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::{PaymentHash, PaymentPreimage};
use lightning_invoice::{Bolt11Invoice, SignedRawBolt11Invoice};

use bip39::Mnemonic;

use std::convert::TryInto;
use std::str::FromStr;

Expand Down Expand Up @@ -186,3 +193,14 @@ impl UniffiCustomTypeConverter for SocketAddress {
obj.to_string()
}
}

impl UniffiCustomTypeConverter for UntrustedString {
type Builtin = String;
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(UntrustedString(val))
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.to_string()
}
}
Loading