Skip to content

Commit 12d90c2

Browse files
committed
Expose payment metadata in the sending path via send_payment.
1 parent 40c437a commit 12d90c2

12 files changed

+125
-126
lines changed

lightning-invoice/src/payment.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
//! # impl Payer for FakePayer {
5757
//! # fn node_id(&self) -> PublicKey { unimplemented!() }
5858
//! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
59-
//! # fn send_payment(
60-
//! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
59+
//! # fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
60+
//! # payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
6161
//! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
6262
//! # fn send_spontaneous_payment(
6363
//! # &self, route: &Route, payment_preimage: PaymentPreimage
@@ -177,8 +177,8 @@ pub trait Payer {
177177
fn first_hops(&self) -> Vec<ChannelDetails>;
178178

179179
/// Sends a payment over the Lightning Network using the given [`Route`].
180-
fn send_payment(
181-
&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
180+
fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
181+
payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
182182
) -> Result<PaymentId, PaymentSendFailure>;
183183

184184
/// Sends a spontaneous payment over the Lightning Network using the given [`Route`].
@@ -301,7 +301,7 @@ where
301301
};
302302

303303
let send_payment = |route: &Route| {
304-
self.payer.send_payment(route, payment_hash, &payment_secret)
304+
self.payer.send_payment(route, payment_hash, &payment_secret, invoice.payment_metadata().cloned())
305305
};
306306
self.pay_internal(&params, payment_hash, send_payment)
307307
.map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
@@ -1434,9 +1434,8 @@ mod tests {
14341434
Vec::new()
14351435
}
14361436

1437-
fn send_payment(
1438-
&self, route: &Route, _payment_hash: PaymentHash,
1439-
_payment_secret: &Option<PaymentSecret>
1437+
fn send_payment(&self, route: &Route, _payment_hash: PaymentHash,
1438+
_payment_secret: &Option<PaymentSecret>, _payment_metadata: Option<Vec<u8>>
14401439
) -> Result<PaymentId, PaymentSendFailure> {
14411440
self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
14421441
self.check_attempts()

lightning-invoice/src/utils.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ where
135135
self.list_usable_channels()
136136
}
137137

138-
fn send_payment(
139-
&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
138+
fn send_payment(&self, route: &Route, payment_hash: PaymentHash,
139+
payment_secret: &Option<PaymentSecret>, payment_metadata: Option<Vec<u8>>
140140
) -> Result<PaymentId, PaymentSendFailure> {
141-
self.send_payment(route, payment_hash, payment_secret)
141+
self.send_payment(route, payment_hash, payment_secret, payment_metadata)
142142
}
143143

144144
fn send_spontaneous_payment(
@@ -202,7 +202,7 @@ mod test {
202202
let payment_event = {
203203
let mut payment_hash = PaymentHash([0; 32]);
204204
payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
205-
nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
205+
nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), None).unwrap();
206206
let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
207207
assert_eq!(added_monitors.len(), 1);
208208
added_monitors.clear();

lightning/src/chain/chainmonitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ mod tests {
901901
// If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
902902
// the update through to the ChannelMonitor which will refuse it (as the channel is closed).
903903
chanmon_cfgs[0].persister.set_update_ret(Ok(()));
904-
unwrap_send_err!(nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)),
904+
unwrap_send_err!(nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), None),
905905
true, APIError::ChannelUnavailable { ref err },
906906
assert!(err.contains("ChannelMonitor storage failure")));
907907
check_added_monitors!(nodes[0], 2); // After the failure we generate a close-channel monitor update

lightning/src/chain/channelmonitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ mod tests {
33783378
// If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
33793379
// the update through to the ChannelMonitor which will refuse it (as the channel is closed).
33803380
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
3381-
unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)),
3381+
unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret), None),
33823382
true, APIError::ChannelUnavailable { ref err },
33833383
assert!(err.contains("ChannelMonitor storage failure")));
33843384
check_added_monitors!(nodes[1], 2); // After the failure we generate a close-channel monitor update

lightning/src/ln/chanmon_update_fail_tests.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn test_simple_monitor_permanent_update_fail() {
4949

5050
let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
5151
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::PermanentFailure));
52-
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), true, APIError::ChannelUnavailable {..}, {});
52+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None), true, APIError::ChannelUnavailable {..}, {});
5353
check_added_monitors!(nodes[0], 2);
5454

5555
let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
@@ -157,7 +157,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
157157
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
158158

159159
{
160-
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), false, APIError::MonitorUpdateFailed, {});
160+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None), false, APIError::MonitorUpdateFailed, {});
161161
check_added_monitors!(nodes[0], 1);
162162
}
163163

@@ -208,7 +208,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
208208
let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
209209
{
210210
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
211-
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateFailed, {});
211+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None), false, APIError::MonitorUpdateFailed, {});
212212
check_added_monitors!(nodes[0], 1);
213213
}
214214

@@ -272,7 +272,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
272272
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
273273
{
274274
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
275-
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateFailed, {});
275+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None), false, APIError::MonitorUpdateFailed, {});
276276
check_added_monitors!(nodes[0], 1);
277277
}
278278

@@ -609,7 +609,7 @@ fn test_monitor_update_fail_cs() {
609609

610610
let (route, our_payment_hash, payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
611611
{
612-
nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
612+
nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), None).unwrap();
613613
check_added_monitors!(nodes[0], 1);
614614
}
615615

@@ -701,7 +701,7 @@ fn test_monitor_update_fail_no_rebroadcast() {
701701

702702
let (route, our_payment_hash, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
703703
{
704-
nodes[0].node.send_payment(&route, our_payment_hash, &Some(payment_secret_1)).unwrap();
704+
nodes[0].node.send_payment(&route, our_payment_hash, &Some(payment_secret_1), None).unwrap();
705705
check_added_monitors!(nodes[0], 1);
706706
}
707707

@@ -749,14 +749,14 @@ fn test_monitor_update_raa_while_paused() {
749749
send_payment(&nodes[0], &[&nodes[1]], 5000000);
750750
let (route, our_payment_hash_1, payment_preimage_1, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
751751
{
752-
nodes[0].node.send_payment(&route, our_payment_hash_1, &Some(our_payment_secret_1)).unwrap();
752+
nodes[0].node.send_payment(&route, our_payment_hash_1, &Some(our_payment_secret_1), None).unwrap();
753753
check_added_monitors!(nodes[0], 1);
754754
}
755755
let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
756756

757757
let (route, our_payment_hash_2, payment_preimage_2, our_payment_secret_2) = get_route_and_payment_hash!(nodes[1], nodes[0], 1000000);
758758
{
759-
nodes[1].node.send_payment(&route, our_payment_hash_2, &Some(our_payment_secret_2)).unwrap();
759+
nodes[1].node.send_payment(&route, our_payment_hash_2, &Some(our_payment_secret_2), None).unwrap();
760760
check_added_monitors!(nodes[1], 1);
761761
}
762762
let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0));
@@ -845,7 +845,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
845845
// holding cell.
846846
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
847847
{
848-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
848+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
849849
check_added_monitors!(nodes[0], 1);
850850
}
851851

@@ -870,7 +870,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
870870
// being paused waiting a monitor update.
871871
let (route, payment_hash_3, _, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
872872
{
873-
nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
873+
nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), None).unwrap();
874874
check_added_monitors!(nodes[0], 1);
875875
}
876876

@@ -889,7 +889,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
889889
let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs {
890890
// Try to route another payment backwards from 2 to make sure 1 holds off on responding
891891
let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
892-
nodes[2].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4)).unwrap();
892+
nodes[2].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4), None).unwrap();
893893
check_added_monitors!(nodes[2], 1);
894894

895895
send_event = SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
@@ -1181,9 +1181,9 @@ fn raa_no_response_awaiting_raa_state() {
11811181
// requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
11821182
// generation during RAA while in monitor-update-failed state.
11831183
{
1184-
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1184+
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None).unwrap();
11851185
check_added_monitors!(nodes[0], 1);
1186-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1186+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
11871187
check_added_monitors!(nodes[0], 0);
11881188
}
11891189

@@ -1233,7 +1233,7 @@ fn raa_no_response_awaiting_raa_state() {
12331233
// chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
12341234
// commitment transaction states) whereas here we can explicitly check for it.
12351235
{
1236-
nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
1236+
nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), None).unwrap();
12371237
check_added_monitors!(nodes[0], 0);
12381238
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
12391239
}
@@ -1322,7 +1322,7 @@ fn claim_while_disconnected_monitor_update_fail() {
13221322
// the monitor still failed
13231323
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
13241324
{
1325-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1325+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
13261326
check_added_monitors!(nodes[0], 1);
13271327
}
13281328

@@ -1407,7 +1407,7 @@ fn monitor_failed_no_reestablish_response() {
14071407
// on receipt).
14081408
let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
14091409
{
1410-
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1410+
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None).unwrap();
14111411
check_added_monitors!(nodes[0], 1);
14121412
}
14131413

@@ -1480,7 +1480,7 @@ fn first_message_on_recv_ordering() {
14801480
// can deliver it and fail the monitor update.
14811481
let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
14821482
{
1483-
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1483+
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None).unwrap();
14841484
check_added_monitors!(nodes[0], 1);
14851485
}
14861486

@@ -1503,7 +1503,7 @@ fn first_message_on_recv_ordering() {
15031503
// Route the second payment, generating an update_add_htlc/commitment_signed
15041504
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
15051505
{
1506-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1506+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
15071507
check_added_monitors!(nodes[0], 1);
15081508
}
15091509
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
@@ -1586,7 +1586,7 @@ fn test_monitor_update_fail_claim() {
15861586

15871587
let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1_000_000);
15881588
{
1589-
nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1589+
nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
15901590
check_added_monitors!(nodes[2], 1);
15911591
}
15921592

@@ -1603,7 +1603,7 @@ fn test_monitor_update_fail_claim() {
16031603
commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
16041604

16051605
let (_, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[0]);
1606-
nodes[2].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
1606+
nodes[2].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), None).unwrap();
16071607
check_added_monitors!(nodes[2], 1);
16081608

16091609
let mut events = nodes[2].node.get_and_clear_pending_msg_events();
@@ -1693,7 +1693,7 @@ fn test_monitor_update_on_pending_forwards() {
16931693

16941694
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
16951695
{
1696-
nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1696+
nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
16971697
check_added_monitors!(nodes[2], 1);
16981698
}
16991699

@@ -1753,7 +1753,7 @@ fn monitor_update_claim_fail_no_response() {
17531753
// Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
17541754
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
17551755
{
1756-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1756+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
17571757
check_added_monitors!(nodes[0], 1);
17581758
}
17591759

@@ -1923,7 +1923,7 @@ fn test_path_paused_mpp() {
19231923
// Now check that we get the right return value, indicating that the first path succeeded but
19241924
// the second got a MonitorUpdateFailed err. This implies PaymentSendFailure::PartialFailure as
19251925
// some paths succeeded, preventing retry.
1926-
if let Err(PaymentSendFailure::PartialFailure { results, ..}) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) {
1926+
if let Err(PaymentSendFailure::PartialFailure { results, ..}) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), None) {
19271927
assert_eq!(results.len(), 2);
19281928
if let Ok(()) = results[0] {} else { panic!(); }
19291929
if let Err(APIError::MonitorUpdateFailed) = results[1] {} else { panic!(); }
@@ -1968,7 +1968,7 @@ fn test_pending_update_fee_ack_on_reconnect() {
19681968
send_payment(&nodes[0], &[&nodes[1]], 100_000_00);
19691969

19701970
let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[1], nodes[0], 1_000_000);
1971-
nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
1971+
nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret), None).unwrap();
19721972
check_added_monitors!(nodes[1], 1);
19731973
let bs_initial_send_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
19741974
// bs_initial_send_msgs are not delivered until they are re-generated after reconnect
@@ -2172,12 +2172,12 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
21722172
// will not be freed from the holding cell.
21732173
let (payment_preimage_0, _, _) = route_payment(&nodes[1], &[&nodes[0]], 100000);
21742174

2175-
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
2175+
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), None).unwrap();
21762176
check_added_monitors!(nodes[0], 1);
21772177
let send = SendEvent::from_node(&nodes[0]);
21782178
assert_eq!(send.msgs.len(), 1);
21792179

2180-
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
2180+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), None).unwrap();
21812181
check_added_monitors!(nodes[0], 0);
21822182

21832183
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
@@ -2367,7 +2367,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
23672367
// In order to get the HTLC claim into the holding cell at nodes[1], we need nodes[1] to be
23682368
// awaiting a remote revoke_and_ack from nodes[0].
23692369
let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
2370-
nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
2370+
nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), None).unwrap();
23712371
check_added_monitors!(nodes[0], 1);
23722372

23732373
let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));

0 commit comments

Comments
 (0)