Skip to content

Commit 23c7064

Browse files
committed
Add reason to Event::PaymentFailed
This includes adding a reason to `PendingOutboundPayment::Abandoned` and using that reason when pushing an `Event::PaymentFailed`.
1 parent 432f0e6 commit 23c7064

File tree

7 files changed

+104
-66
lines changed

7 files changed

+104
-66
lines changed

lightning/src/events/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ pub enum Event {
475475
///
476476
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
477477
payment_hash: PaymentHash,
478+
/// The reason the payment failed. This is only `None` for events generated or serialized
479+
/// by versions prior to 0.0.115.
480+
reason: Option<PaymentFailureReason>,
478481
},
479482
/// Indicates that a path for an outbound payment was successful.
480483
///
@@ -940,10 +943,11 @@ impl Writeable for Event {
940943
(4, *path, vec_type)
941944
})
942945
},
943-
&Event::PaymentFailed { ref payment_id, ref payment_hash } => {
946+
&Event::PaymentFailed { ref payment_id, ref payment_hash, ref reason } => {
944947
15u8.write(writer)?;
945948
write_tlv_fields!(writer, {
946949
(0, payment_id, required),
950+
(1, reason, option),
947951
(2, payment_hash, required),
948952
})
949953
},
@@ -1245,13 +1249,16 @@ impl MaybeReadable for Event {
12451249
let f = || {
12461250
let mut payment_hash = PaymentHash([0; 32]);
12471251
let mut payment_id = PaymentId([0; 32]);
1252+
let mut reason = None;
12481253
read_tlv_fields!(reader, {
12491254
(0, payment_id, required),
1255+
(1, reason, upgradable_option),
12501256
(2, payment_hash, required),
12511257
});
12521258
Ok(Some(Event::PaymentFailed {
12531259
payment_id,
12541260
payment_hash,
1261+
reason,
12551262
}))
12561263
};
12571264
f()

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, Fee
3636
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
3737
use crate::chain::transaction::{OutPoint, TransactionData};
3838
use crate::events;
39-
use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
39+
use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason};
4040
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4141
// construct one themselves.
4242
use crate::ln::{inbound_payment, PaymentHash, PaymentPreimage, PaymentSecret};
@@ -2711,7 +2711,7 @@ where
27112711
/// [`Event::PaymentSent`]: events::Event::PaymentSent
27122712
pub fn abandon_payment(&self, payment_id: PaymentId) {
27132713
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
2714-
self.pending_outbound_payments.abandon_payment(payment_id, &self.pending_events);
2714+
self.pending_outbound_payments.abandon_payment(payment_id, PaymentFailureReason::UserAbandoned, &self.pending_events);
27152715
}
27162716

27172717
/// Send a spontaneous payment, which is a payment that does not require the recipient to have

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use crate::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen, Watch, keysinterface::EntropySource};
1414
use crate::chain::channelmonitor::ChannelMonitor;
1515
use crate::chain::transaction::OutPoint;
16-
use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose};
16+
use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason};
1717
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1818
use crate::ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA};
1919
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate};
@@ -1937,9 +1937,14 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
19371937
};
19381938
if !conditions.expected_mpp_parts_remain {
19391939
match &payment_failed_events[1] {
1940-
Event::PaymentFailed { ref payment_hash, ref payment_id } => {
1940+
Event::PaymentFailed { ref payment_hash, ref payment_id, ref reason } => {
19411941
assert_eq!(*payment_hash, expected_payment_hash, "unexpected second payment_hash");
19421942
assert_eq!(*payment_id, expected_payment_id);
1943+
assert_eq!(reason.unwrap(), if expected_payment_failed_permanently {
1944+
PaymentFailureReason::RecipientRejected
1945+
} else {
1946+
PaymentFailureReason::RetriesExhausted
1947+
});
19431948
}
19441949
_ => panic!("Unexpected second event"),
19451950
}
@@ -2240,10 +2245,10 @@ pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
22402245
let expected_destinations: Vec<HTLCDestination> = repeat(HTLCDestination::FailedPayment { payment_hash: our_payment_hash }).take(expected_paths.len()).collect();
22412246
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(expected_paths[0].last().unwrap(), expected_destinations);
22422247

2243-
pass_failed_payment_back(origin_node, expected_paths, skip_last, our_payment_hash);
2248+
pass_failed_payment_back(origin_node, expected_paths, skip_last, our_payment_hash, PaymentFailureReason::RecipientRejected);
22442249
}
22452250

2246-
pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths_slice: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_hash: PaymentHash) {
2251+
pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths_slice: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_hash: PaymentHash, expected_fail_reason: PaymentFailureReason) {
22472252
let mut expected_paths: Vec<_> = expected_paths_slice.iter().collect();
22482253
check_added_monitors!(expected_paths[0].last().unwrap(), expected_paths.len());
22492254

@@ -2329,9 +2334,10 @@ pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
23292334
};
23302335
if i == expected_paths.len() - 1 {
23312336
match events[1] {
2332-
Event::PaymentFailed { ref payment_hash, ref payment_id } => {
2337+
Event::PaymentFailed { ref payment_hash, ref payment_id, ref reason } => {
23332338
assert_eq!(*payment_hash, our_payment_hash, "unexpected second payment_hash");
23342339
assert_eq!(*payment_id, expected_payment_id);
2340+
assert_eq!(reason.unwrap(), expected_fail_reason);
23352341
}
23362342
_ => panic!("Unexpected second event"),
23372343
}

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::chain::channelmonitor;
1818
use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
1919
use crate::chain::transaction::OutPoint;
2020
use crate::chain::keysinterface::{ChannelSigner, EcdsaChannelSigner, EntropySource};
21-
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination};
21+
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
2222
use crate::ln::{PaymentPreimage, PaymentSecret, PaymentHash};
2323
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT};
2424
use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA};
@@ -9608,7 +9608,7 @@ fn test_double_partial_claim() {
96089608
];
96099609
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations);
96109610

9611-
pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
9611+
pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
96129612

96139613
// nodes[1] now retries one of the two paths...
96149614
nodes[0].node.send_payment_with_route(&route, payment_hash,

lightning/src/ln/onion_route_tests.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
1515
use crate::chain::keysinterface::{EntropySource, NodeSigner, Recipient};
16-
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure};
16+
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason};
1717
use crate::ln::{PaymentHash, PaymentSecret};
1818
use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
1919
use crate::ln::channelmanager::{HTLCForwardInfo, FailureCode, CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId, RecipientOnionFields};
@@ -213,9 +213,14 @@ fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case:
213213
panic!("Unexpected event");
214214
}
215215
match events[1] {
216-
Event::PaymentFailed { payment_hash: ev_payment_hash, payment_id: ev_payment_id } => {
216+
Event::PaymentFailed { payment_hash: ev_payment_hash, payment_id: ev_payment_id, reason: ref ev_reason } => {
217217
assert_eq!(*payment_hash, ev_payment_hash);
218218
assert_eq!(payment_id, ev_payment_id);
219+
assert_eq!(if expected_retryable {
220+
PaymentFailureReason::RetriesExhausted
221+
} else {
222+
PaymentFailureReason::RecipientRejected
223+
}, ev_reason.unwrap());
219224
}
220225
_ => panic!("Unexpected second event"),
221226
}

0 commit comments

Comments
 (0)