Skip to content

Commit f6a4505

Browse files
authored
Merge pull request #2567 from G8XSU/payment-id
Add PaymentId in ChannelManager.list_recent_payments()
2 parents 1c9df02 + 073899a commit f6a4505

10 files changed

+125
-113
lines changed

lightning/src/chain/chainmonitor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,8 @@ mod tests {
854854
create_announced_chan_between_nodes(&nodes, 0, 1);
855855

856856
// Route two payments to be claimed at the same time.
857-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
858-
let (payment_preimage_2, payment_hash_2, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
857+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
858+
let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
859859

860860
chanmon_cfgs[1].persister.offchain_monitor_updates.lock().unwrap().clear();
861861
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
@@ -962,7 +962,7 @@ mod tests {
962962
let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
963963

964964
// First route a payment that we will claim on chain and give the recipient the preimage.
965-
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
965+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
966966
nodes[1].node.claim_funds(payment_preimage);
967967
expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
968968
nodes[1].node.get_and_clear_pending_msg_events();

lightning/src/ln/chanmon_update_fail_tests.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn test_monitor_and_persister_update_fail() {
9292
send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
9393

9494
// Route an HTLC from node 0 to node 1 (but don't settle)
95-
let (preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
95+
let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
9696

9797
// Make a copy of the ChainMonitor so we can capture the error it returns on a
9898
// bogus update. Note that if instead we updated the nodes[0]'s ChainMonitor
@@ -286,7 +286,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
286286
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
287287
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
288288

289-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
289+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
290290

291291
// Now try to send a second payment which will fail to send
292292
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
@@ -858,7 +858,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
858858
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
859859

860860
// Route a first payment that we'll fail backwards
861-
let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
861+
let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
862862

863863
// Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
864864
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
@@ -1128,7 +1128,7 @@ fn test_monitor_update_fail_reestablish() {
11281128
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
11291129
create_announced_chan_between_nodes(&nodes, 1, 2);
11301130

1131-
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
1131+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
11321132

11331133
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11341134
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
@@ -1344,7 +1344,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13441344
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
13451345

13461346
// Forward a payment for B to claim
1347-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1347+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
13481348

13491349
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
13501350
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
@@ -1644,7 +1644,7 @@ fn test_monitor_update_fail_claim() {
16441644
// Rebalance a bit so that we can send backwards from 3 to 2.
16451645
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
16461646

1647-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1647+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
16481648

16491649
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
16501650
nodes[1].node.claim_funds(payment_preimage_1);
@@ -1763,7 +1763,7 @@ fn test_monitor_update_on_pending_forwards() {
17631763
// Rebalance a bit so that we can send backwards from 3 to 1.
17641764
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
17651765

1766-
let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1766+
let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
17671767
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
17681768
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_1 }]);
17691769
check_added_monitors!(nodes[2], 1);
@@ -1835,7 +1835,7 @@ fn monitor_update_claim_fail_no_response() {
18351835
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
18361836

18371837
// Forward a payment for B to claim
1838-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1838+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
18391839

18401840
// Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
18411841
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
@@ -2177,7 +2177,7 @@ fn test_fail_htlc_on_broadcast_after_claim() {
21772177
create_announced_chan_between_nodes(&nodes, 0, 1);
21782178
let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
21792179

2180-
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 2000);
2180+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 2000);
21812181

21822182
let bs_txn = get_local_commitment_txn!(nodes[2], chan_id_2);
21832183
assert_eq!(bs_txn.len(), 1);
@@ -2343,7 +2343,7 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
23432343
//
23442344
// Note that because, at the end, MonitorUpdateInProgress is still set, the HTLC generated in
23452345
// (c) will not be freed from the holding cell.
2346-
let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[1], &[&nodes[0]], 100_000);
2346+
let (payment_preimage_0, payment_hash_0, ..) = route_payment(&nodes[1], &[&nodes[0]], 100_000);
23472347

23482348
nodes[0].node.send_payment_with_route(&route, payment_hash_1,
23492349
RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
@@ -2517,7 +2517,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
25172517
create_announced_chan_between_nodes(&nodes, 0, 1);
25182518
let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
25192519

2520-
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
2520+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
25212521

25222522
let mut as_raa = None;
25232523
if htlc_status == HTLCStatusAtDupClaim::HoldingCell {
@@ -2747,8 +2747,8 @@ fn double_temp_error() {
27472747

27482748
let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
27492749

2750-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
2751-
let (payment_preimage_2, payment_hash_2, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
2750+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
2751+
let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
27522752

27532753
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
27542754
// `claim_funds` results in a ChannelMonitorUpdate.
@@ -3045,8 +3045,8 @@ fn test_blocked_chan_preimage_release() {
30453045

30463046
// Tee up two payments in opposite directions across nodes[1], one it sent to generate a
30473047
// PaymentSent event and one it forwards.
3048-
let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[1], &[&nodes[2]], 1_000_000);
3049-
let (payment_preimage_2, payment_hash_2, _) = route_payment(&nodes[2], &[&nodes[1], &nodes[0]], 1_000_000);
3048+
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[1], &[&nodes[2]], 1_000_000);
3049+
let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[2], &[&nodes[1], &nodes[0]], 1_000_000);
30503050

30513051
// Claim the first payment to get a `PaymentSent` event (but don't handle it yet).
30523052
nodes[2].node.claim_funds(payment_preimage_1);

lightning/src/ln/channelmanager.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ impl From<&ClaimableHTLC> for events::ClaimedHTLC {
233233
}
234234
}
235235

236-
/// A payment identifier used to uniquely identify a payment to LDK.
236+
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
237+
/// a payment and ensure idempotency in LDK.
237238
///
238239
/// This is not exported to bindings users as we just use [u8; 32] directly
239240
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
@@ -1669,11 +1670,15 @@ pub enum ChannelShutdownState {
16691670
pub enum RecentPaymentDetails {
16701671
/// When an invoice was requested and thus a payment has not yet been sent.
16711672
AwaitingInvoice {
1672-
/// Identifier for the payment to ensure idempotency.
1673+
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1674+
/// a payment and ensure idempotency in LDK.
16731675
payment_id: PaymentId,
16741676
},
16751677
/// When a payment is still being sent and awaiting successful delivery.
16761678
Pending {
1679+
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1680+
/// a payment and ensure idempotency in LDK.
1681+
payment_id: PaymentId,
16771682
/// Hash of the payment that is currently being sent but has yet to be fulfilled or
16781683
/// abandoned.
16791684
payment_hash: PaymentHash,
@@ -1685,6 +1690,9 @@ pub enum RecentPaymentDetails {
16851690
/// been resolved. Upon receiving [`Event::PaymentSent`], we delay for a few minutes before the
16861691
/// payment is removed from tracking.
16871692
Fulfilled {
1693+
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1694+
/// a payment and ensure idempotency in LDK.
1695+
payment_id: PaymentId,
16881696
/// Hash of the payment that was claimed. `None` for serializations of [`ChannelManager`]
16891697
/// made before LDK version 0.0.104.
16901698
payment_hash: Option<PaymentHash>,
@@ -1693,6 +1701,9 @@ pub enum RecentPaymentDetails {
16931701
/// abandoned via [`ChannelManager::abandon_payment`], it is marked as abandoned until all
16941702
/// pending HTLCs for this payment resolve and an [`Event::PaymentFailed`] is generated.
16951703
Abandoned {
1704+
/// A user-provided identifier in [`ChannelManager::send_payment`] used to uniquely identify
1705+
/// a payment and ensure idempotency in LDK.
1706+
payment_id: PaymentId,
16961707
/// Hash of the payment that we have given up trying to send.
16971708
payment_hash: PaymentHash,
16981709
},
@@ -2429,15 +2440,16 @@ where
24292440
},
24302441
PendingOutboundPayment::Retryable { payment_hash, total_msat, .. } => {
24312442
Some(RecentPaymentDetails::Pending {
2443+
payment_id: *payment_id,
24322444
payment_hash: *payment_hash,
24332445
total_msat: *total_msat,
24342446
})
24352447
},
24362448
PendingOutboundPayment::Abandoned { payment_hash, .. } => {
2437-
Some(RecentPaymentDetails::Abandoned { payment_hash: *payment_hash })
2449+
Some(RecentPaymentDetails::Abandoned { payment_id: *payment_id, payment_hash: *payment_hash })
24382450
},
24392451
PendingOutboundPayment::Fulfilled { payment_hash, .. } => {
2440-
Some(RecentPaymentDetails::Fulfilled { payment_hash: *payment_hash })
2452+
Some(RecentPaymentDetails::Fulfilled { payment_id: *payment_id, payment_hash: *payment_hash })
24412453
},
24422454
PendingOutboundPayment::Legacy { .. } => None
24432455
})
@@ -9824,7 +9836,7 @@ mod tests {
98249836

98259837
// To start (1), send a regular payment but don't claim it.
98269838
let expected_route = [&nodes[1]];
9827-
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &expected_route, 100_000);
9839+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &expected_route, 100_000);
98289840

98299841
// Next, attempt a keysend payment and make sure it fails.
98309842
let route_params = RouteParameters::from_payment_params_and_value(

lightning/src/ln/functional_test_utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2467,7 +2467,7 @@ pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route:
24672467

24682468
pub const TEST_FINAL_CLTV: u32 = 70;
24692469

2470-
pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
2470+
pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret, PaymentId) {
24712471
let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV)
24722472
.with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap();
24732473
let route_params = RouteParameters::from_payment_params_and_value(payment_params, recv_value);
@@ -2479,7 +2479,7 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route:
24792479
}
24802480

24812481
let res = send_along_route(origin_node, route, expected_route, recv_value);
2482-
(res.0, res.1, res.2)
2482+
(res.0, res.1, res.2, res.3)
24832483
}
24842484

24852485
pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) {
@@ -2506,7 +2506,7 @@ pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_rou
25062506
assert!(err.contains("Cannot send value that would put us over the max HTLC value in flight our peer will accept")));
25072507
}
25082508

2509-
pub fn send_payment<'a, 'b, 'c>(origin: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
2509+
pub fn send_payment<'a, 'b, 'c>(origin: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret, PaymentId) {
25102510
let res = route_payment(&origin, expected_route, recv_value);
25112511
claim_payment(&origin, expected_route, res.0);
25122512
res

0 commit comments

Comments
 (0)