Skip to content

Commit 16e6c5d

Browse files
committed
feat: add PaymentFailureReason to PaymentStatus
1 parent 7977b04 commit 16e6c5d

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,11 @@ enum PaymentDirection {
419419
"Outbound",
420420
};
421421

422-
enum PaymentStatus {
423-
"Pending",
424-
"Succeeded",
425-
"Failed",
422+
[Enum]
423+
interface PaymentStatus {
424+
Pending();
425+
Succeeded();
426+
Failed(PaymentFailureReason? reason);
426427
};
427428

428429
dictionary LSPFeeLimits {

src/event.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ where
582582
self.channel_manager.fail_htlc_backwards(&payment_hash);
583583

584584
let update = PaymentDetailsUpdate {
585-
status: Some(PaymentStatus::Failed),
585+
status: Some(PaymentStatus::Failed { reason: None }),
586586
..PaymentDetailsUpdate::new(payment_id)
587587
};
588588
match self.payment_store.update(&update) {
@@ -606,7 +606,7 @@ where
606606
self.channel_manager.fail_htlc_backwards(&payment_hash);
607607

608608
let update = PaymentDetailsUpdate {
609-
status: Some(PaymentStatus::Failed),
609+
status: Some(PaymentStatus::Failed { reason: None }),
610610
..PaymentDetailsUpdate::new(payment_id)
611611
};
612612
match self.payment_store.update(&update) {
@@ -647,7 +647,7 @@ where
647647

648648
let update = PaymentDetailsUpdate {
649649
hash: Some(Some(payment_hash)),
650-
status: Some(PaymentStatus::Failed),
650+
status: Some(PaymentStatus::Failed { reason: None }),
651651
..PaymentDetailsUpdate::new(payment_id)
652652
};
653653
match self.payment_store.update(&update) {
@@ -834,7 +834,7 @@ where
834834

835835
let update = PaymentDetailsUpdate {
836836
hash: Some(Some(payment_hash)),
837-
status: Some(PaymentStatus::Failed),
837+
status: Some(PaymentStatus::Failed { reason: None }),
838838
..PaymentDetailsUpdate::new(payment_id)
839839
};
840840
match self.payment_store.update(&update) {
@@ -1014,7 +1014,7 @@ where
10141014

10151015
let update = PaymentDetailsUpdate {
10161016
hash: Some(payment_hash),
1017-
status: Some(PaymentStatus::Failed),
1017+
status: Some(PaymentStatus::Failed { reason }),
10181018
..PaymentDetailsUpdate::new(payment_id)
10191019
};
10201020
match self.payment_store.update(&update) {

src/payment/bolt11.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl Bolt11Payment {
210210
invoice.amount_milli_satoshis(),
211211
None,
212212
PaymentDirection::Outbound,
213-
PaymentStatus::Failed,
213+
PaymentStatus::Failed { reason: None },
214214
);
215215

216216
self.payment_store.insert(payment)?;
@@ -346,7 +346,7 @@ impl Bolt11Payment {
346346
Some(amount_msat),
347347
None,
348348
PaymentDirection::Outbound,
349-
PaymentStatus::Failed,
349+
PaymentStatus::Failed { reason: None },
350350
);
351351
self.payment_store.insert(payment)?;
352352

@@ -430,7 +430,7 @@ impl Bolt11Payment {
430430
let payment_id = PaymentId(payment_hash.0);
431431

432432
let update = PaymentDetailsUpdate {
433-
status: Some(PaymentStatus::Failed),
433+
status: Some(PaymentStatus::Failed { reason: None }),
434434
..PaymentDetailsUpdate::new(payment_id)
435435
};
436436

src/payment/bolt12.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl Bolt12Payment {
138138
Some(offer_amount_msat),
139139
None,
140140
PaymentDirection::Outbound,
141-
PaymentStatus::Failed,
141+
PaymentStatus::Failed { reason: None },
142142
);
143143
self.payment_store.insert(payment)?;
144144
Err(Error::InvoiceRequestCreationFailed)
@@ -244,7 +244,7 @@ impl Bolt12Payment {
244244
Some(amount_msat),
245245
None,
246246
PaymentDirection::Outbound,
247-
PaymentStatus::Failed,
247+
PaymentStatus::Failed { reason: None },
248248
);
249249
self.payment_store.insert(payment)?;
250250
Err(Error::PaymentSendingFailed)

src/payment/spontaneous.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl SpontaneousPayment {
162162
Some(amount_msat),
163163
None,
164164
PaymentDirection::Outbound,
165-
PaymentStatus::Failed,
165+
PaymentStatus::Failed { reason: None },
166166
);
167167

168168
self.payment_store.insert(payment)?;

src/payment/store.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8+
use lightning::events::PaymentFailureReason;
89
use lightning::ln::channelmanager::PaymentId;
910
use lightning::ln::msgs::DecodeError;
1011
use lightning::offers::offer::OfferId;
@@ -280,8 +281,8 @@ impl StorableObject for PaymentDetails {
280281
}
281282
}
282283

283-
if let Some(status) = update.status {
284-
update_if_necessary!(self.status, status);
284+
if let Some(status) = &update.status {
285+
update_if_necessary!(self.status, status.clone());
285286
}
286287

287288
if let Some(confirmation_status) = update.confirmation_status {
@@ -323,20 +324,25 @@ impl_writeable_tlv_based_enum!(PaymentDirection,
323324
);
324325

325326
/// Represents the current status of a payment.
326-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
327+
#[derive(Clone, Debug, PartialEq, Eq)]
327328
pub enum PaymentStatus {
328329
/// The payment is still pending.
329330
Pending,
330331
/// The payment succeeded.
331332
Succeeded,
332333
/// The payment failed.
333-
Failed,
334+
Failed {
335+
/// The reason why the payment failed.
336+
reason: Option<PaymentFailureReason>,
337+
},
334338
}
335339

336340
impl_writeable_tlv_based_enum!(PaymentStatus,
337341
(0, Pending) => {},
338342
(2, Succeeded) => {},
339-
(4, Failed) => {}
343+
(4, Failed) => {
344+
(0, reason, upgradable_option),
345+
}
340346
);
341347

342348
/// Represents the kind of a payment.
@@ -593,7 +599,7 @@ impl From<&PaymentDetails> for PaymentDetailsUpdate {
593599
fee_paid_msat: Some(value.fee_paid_msat),
594600
counterparty_skimmed_fee_msat,
595601
direction: Some(value.direction),
596-
status: Some(value.status),
602+
status: Some(value.status.clone()),
597603
confirmation_status,
598604
}
599605
}

tests/common/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,10 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
826826
);
827827
node_b.bolt11_payment().fail_for_hash(manual_fail_payment_hash).unwrap();
828828
expect_event!(node_a, PaymentFailed);
829-
assert_eq!(node_a.payment(&manual_fail_payment_id).unwrap().status, PaymentStatus::Failed);
829+
assert_eq!(
830+
node_a.payment(&manual_fail_payment_id).unwrap().status,
831+
PaymentStatus::Failed { reason: None }
832+
);
830833
assert_eq!(
831834
node_a.payment(&manual_fail_payment_id).unwrap().direction,
832835
PaymentDirection::Outbound
@@ -839,7 +842,10 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
839842
node_a.payment(&manual_fail_payment_id).unwrap().kind,
840843
PaymentKind::Bolt11 { .. }
841844
));
842-
assert_eq!(node_b.payment(&manual_fail_payment_id).unwrap().status, PaymentStatus::Failed);
845+
assert_eq!(
846+
node_b.payment(&manual_fail_payment_id).unwrap().status,
847+
PaymentStatus::Failed { reason: None }
848+
);
843849
assert_eq!(
844850
node_b.payment(&manual_fail_payment_id).unwrap().direction,
845851
PaymentDirection::Inbound

0 commit comments

Comments
 (0)