Skip to content

Commit 299620e

Browse files
authored
Merge pull request #260 from tnull/2024-02-add-payment-failed-reason
Add `reason` to `Event::PaymentFailed` and `Event::ChannelClosed`
2 parents 980b14c + a062cb2 commit 299620e

File tree

4 files changed

+76
-24
lines changed

4 files changed

+76
-24
lines changed

bindings/ldk_node.udl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,35 @@ enum BuildError {
157157

158158
[Enum]
159159
interface Event {
160-
PaymentSuccessful( PaymentHash payment_hash );
161-
PaymentFailed( PaymentHash payment_hash );
162-
PaymentReceived( PaymentHash payment_hash, u64 amount_msat);
163-
ChannelPending ( ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo );
164-
ChannelReady ( ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id );
165-
ChannelClosed ( ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id );
160+
PaymentSuccessful(PaymentHash payment_hash);
161+
PaymentFailed(PaymentHash payment_hash, PaymentFailureReason? reason);
162+
PaymentReceived(PaymentHash payment_hash, u64 amount_msat);
163+
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
164+
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
165+
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
166+
};
167+
168+
enum PaymentFailureReason {
169+
"RecipientRejected",
170+
"UserAbandoned",
171+
"RetriesExhausted",
172+
"PaymentExpired",
173+
"RouteNotFound",
174+
"UnexpectedError",
175+
};
176+
177+
[Enum]
178+
interface ClosureReason {
179+
CounterpartyForceClosed ( UntrustedString peer_msg );
180+
HolderForceClosed ();
181+
CooperativeClosure ();
182+
CommitmentTxConfirmed ();
183+
FundingTimedOut ();
184+
ProcessingError ( string err );
185+
DisconnectedPeer ();
186+
OutdatedChannelManager ();
187+
CounterpartyCoopClosedUnfundedChannel ();
188+
FundingBatchClosure ();
166189
};
167190

168191
enum PaymentDirection {
@@ -300,3 +323,6 @@ typedef string UserChannelId;
300323

301324
[Custom]
302325
typedef string Mnemonic;
326+
327+
[Custom]
328+
typedef string UntrustedString;

src/event.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::io::{
1414
use crate::logger::{log_error, log_info, Logger};
1515

1616
use lightning::chain::chaininterface::ConfirmationTarget;
17-
use lightning::events::Event as LdkEvent;
18-
use lightning::events::PaymentPurpose;
17+
use lightning::events::{ClosureReason, PaymentPurpose};
18+
use lightning::events::{Event as LdkEvent, PaymentFailureReason};
1919
use lightning::impl_writeable_tlv_based_enum;
2020
use lightning::ln::{ChannelId, PaymentHash};
2121
use lightning::routing::gossip::NodeId;
@@ -52,6 +52,10 @@ pub enum Event {
5252
PaymentFailed {
5353
/// The hash of the payment.
5454
payment_hash: PaymentHash,
55+
/// The reason why the payment failed.
56+
///
57+
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
58+
reason: Option<PaymentFailureReason>,
5559
},
5660
/// A payment has been received.
5761
PaymentReceived {
@@ -81,7 +85,7 @@ pub enum Event {
8185
user_channel_id: UserChannelId,
8286
/// The `node_id` of the channel counterparty.
8387
///
84-
/// This will be `None` for events serialized by LDK Node XXX TODO and prior.
88+
/// This will be `None` for events serialized by LDK Node v0.1.0 and prior.
8589
counterparty_node_id: Option<PublicKey>,
8690
},
8791
/// A channel has been closed.
@@ -92,8 +96,10 @@ pub enum Event {
9296
user_channel_id: UserChannelId,
9397
/// The `node_id` of the channel counterparty.
9498
///
95-
/// This will be `None` for events serialized by LDK Node XXX TODO and prior.
99+
/// This will be `None` for events serialized by LDK Node v0.1.0 and prior.
96100
counterparty_node_id: Option<PublicKey>,
101+
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
102+
reason: Option<ClosureReason>,
97103
},
98104
}
99105

@@ -103,6 +109,7 @@ impl_writeable_tlv_based_enum!(Event,
103109
},
104110
(1, PaymentFailed) => {
105111
(0, payment_hash, required),
112+
(1, reason, option),
106113
},
107114
(2, PaymentReceived) => {
108115
(0, payment_hash, required),
@@ -124,6 +131,7 @@ impl_writeable_tlv_based_enum!(Event,
124131
(0, channel_id, required),
125132
(1, counterparty_node_id, option),
126133
(2, user_channel_id, required),
134+
(3, reason, upgradable_option),
127135
};
128136
);
129137

@@ -609,11 +617,12 @@ where
609617
panic!("Failed to push to event queue");
610618
});
611619
}
612-
LdkEvent::PaymentFailed { payment_hash, .. } => {
620+
LdkEvent::PaymentFailed { payment_hash, reason, .. } => {
613621
log_info!(
614622
self.logger,
615-
"Failed to send payment to payment hash {:?}.",
616-
hex_utils::to_string(&payment_hash.0)
623+
"Failed to send payment to payment hash {:?} due to {:?}.",
624+
hex_utils::to_string(&payment_hash.0),
625+
reason
617626
);
618627

619628
let update = PaymentDetailsUpdate {
@@ -624,12 +633,12 @@ where
624633
log_error!(self.logger, "Failed to access payment store: {}", e);
625634
panic!("Failed to access payment store");
626635
});
627-
self.event_queue.add_event(Event::PaymentFailed { payment_hash }).unwrap_or_else(
628-
|e| {
636+
self.event_queue
637+
.add_event(Event::PaymentFailed { payment_hash, reason })
638+
.unwrap_or_else(|e| {
629639
log_error!(self.logger, "Failed to push to event queue: {}", e);
630640
panic!("Failed to push to event queue");
631-
},
632-
);
641+
});
633642
}
634643

635644
LdkEvent::PaymentPathSuccessful { .. } => {}
@@ -846,12 +855,13 @@ where
846855
counterparty_node_id,
847856
..
848857
} => {
849-
log_info!(self.logger, "Channel {} closed due to: {:?}", channel_id, reason);
858+
log_info!(self.logger, "Channel {} closed due to: {}", channel_id, reason);
850859
self.event_queue
851860
.add_event(Event::ChannelClosed {
852861
channel_id,
853862
user_channel_id: UserChannelId(user_channel_id),
854863
counterparty_node_id,
864+
reason: Some(reason),
855865
})
856866
.unwrap_or_else(|e| {
857867
log_error!(self.logger, "Failed to push to event queue: {}", e);

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub use types::ChannelConfig;
108108
pub use io::utils::generate_entropy_mnemonic;
109109

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

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

138-
#[cfg(feature = "uniffi")]
139-
use lightning::ln::ChannelId;
140138
use lightning::sign::EntropySource;
141139

142140
use lightning::util::persist::KVStore;

src/uniffi_types.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
pub use lightning::events::{ClosureReason, PaymentFailureReason};
2+
pub use lightning::ln::ChannelId;
3+
pub use lightning::ln::PaymentSecret;
4+
pub use lightning::util::string::UntrustedString;
5+
6+
pub use bitcoin::OutPoint;
7+
8+
pub use bip39::Mnemonic;
9+
110
use crate::UniffiCustomTypeConverter;
211

312
use crate::error::Error;
@@ -9,11 +18,9 @@ use bitcoin::hashes::sha256::Hash as Sha256;
918
use bitcoin::hashes::Hash;
1019
use bitcoin::secp256k1::PublicKey;
1120
use bitcoin::{Address, Txid};
12-
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
21+
use lightning::ln::{PaymentHash, PaymentPreimage};
1322
use lightning_invoice::{Bolt11Invoice, SignedRawBolt11Invoice};
1423

15-
use bip39::Mnemonic;
16-
1724
use std::convert::TryInto;
1825
use std::str::FromStr;
1926

@@ -186,3 +193,14 @@ impl UniffiCustomTypeConverter for SocketAddress {
186193
obj.to_string()
187194
}
188195
}
196+
197+
impl UniffiCustomTypeConverter for UntrustedString {
198+
type Builtin = String;
199+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
200+
Ok(UntrustedString(val))
201+
}
202+
203+
fn from_custom(obj: Self) -> Self::Builtin {
204+
obj.to_string()
205+
}
206+
}

0 commit comments

Comments
 (0)