Skip to content

Commit 08a47b0

Browse files
committed
Add a RecipientOnionFields argument to spontaneous payment sends
While most lightning nodes don't (currently) support providing a payment secret or payment metadata for spontaneous payments, there's no specific technical reason why we shouldn't support sending those fields to a recipient. Further, when we eventually move to allowing custom TLV entries in the recipient's onion TLV stream, we'll want to support it for spontaneous payments as well. Here we simply add the new `RecipientOnionFields` struct as an argument to the spontaneous payment send methods. We don't yet plumb it through the payment sending logic, which will come when we plumb the new struct through the sending logic to replace the existing payment secret arguments.
1 parent 5dc6a05 commit 08a47b0

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ pub struct RecipientOnionFields {
230230
///
231231
/// If you do not have one, the [`Route`] you pay over must not contain multiple paths as
232232
/// multi-path payments require a recipient-provided secret.
233+
///
234+
/// Note that for spontaneous payments most lightning nodes do not currently support MPP
235+
/// receives, thus you should generally never be providing a secret here for spontaneous
236+
/// payments.
233237
pub payment_secret: Option<PaymentSecret>,
234238
}
235239

@@ -2727,7 +2731,7 @@ where
27272731
/// Note that `route` must have exactly one path.
27282732
///
27292733
/// [`send_payment`]: Self::send_payment
2730-
pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
2734+
pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
27312735
let best_block_height = self.best_block.read().unwrap().height();
27322736
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
27332737
self.pending_outbound_payments.send_spontaneous_payment_with_route(
@@ -2744,7 +2748,7 @@ where
27442748
/// payments.
27452749
///
27462750
/// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
2747-
pub fn send_spontaneous_payment_with_retry(&self, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<PaymentHash, RetryableSendFailure> {
2751+
pub fn send_spontaneous_payment_with_retry(&self, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<PaymentHash, RetryableSendFailure> {
27482752
let best_block_height = self.best_block.read().unwrap().height();
27492753
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
27502754
self.pending_outbound_payments.send_spontaneous_payment(payment_preimage, payment_id,
@@ -8007,7 +8011,8 @@ mod tests {
80078011
pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
80088012

80098013
// Next, send a keysend payment with the same payment_hash and make sure it fails.
8010-
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8014+
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8015+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
80118016
check_added_monitors!(nodes[0], 1);
80128017
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
80138018
assert_eq!(events.len(), 1);
@@ -8127,7 +8132,8 @@ mod tests {
81278132
&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
81288133
None, nodes[0].logger, &scorer, &random_seed_bytes
81298134
).unwrap();
8130-
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8135+
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8136+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
81318137
check_added_monitors!(nodes[0], 1);
81328138
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
81338139
assert_eq!(events.len(), 1);
@@ -8160,7 +8166,8 @@ mod tests {
81608166
&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
81618167
None, nodes[0].logger, &scorer, &random_seed_bytes
81628168
).unwrap();
8163-
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8169+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8170+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
81648171
check_added_monitors!(nodes[0], 1);
81658172
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
81668173
assert_eq!(events.len(), 1);

lightning/src/ln/functional_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9318,7 +9318,8 @@ fn test_keysend_payments_to_public_node() {
93189318
let route = find_route(&payer_pubkey, &route_params, &network_graph, None, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
93199319

93209320
let test_preimage = PaymentPreimage([42; 32]);
9321-
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage), PaymentId(test_preimage.0)).unwrap();
9321+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage),
9322+
RecipientOnionFields::spontaneous_empty(), PaymentId(test_preimage.0)).unwrap();
93229323
check_added_monitors!(nodes[0], 1);
93239324
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
93249325
assert_eq!(events.len(), 1);
@@ -9353,7 +9354,8 @@ fn test_keysend_payments_to_private_node() {
93539354
).unwrap();
93549355

93559356
let test_preimage = PaymentPreimage([42; 32]);
9356-
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage), PaymentId(test_preimage.0)).unwrap();
9357+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage),
9358+
RecipientOnionFields::spontaneous_empty(), PaymentId(test_preimage.0)).unwrap();
93579359
check_added_monitors!(nodes[0], 1);
93589360
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
93599361
assert_eq!(events.len(), 1);

lightning/src/ln/payment_tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,8 @@ fn claimed_send_payment_idempotent() {
10791079

10801080
// Further, if we try to send a spontaneous payment with the same payment_id it should
10811081
// also be rejected.
1082-
let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
1082+
let send_result = nodes[0].node.send_spontaneous_payment(
1083+
&route, None, RecipientOnionFields::spontaneous_empty(), payment_id);
10831084
match send_result {
10841085
Err(PaymentSendFailure::DuplicatePayment) => {},
10851086
_ => panic!("Unexpected send result: {:?}", send_result),
@@ -1153,7 +1154,8 @@ fn abandoned_send_payment_idempotent() {
11531154

11541155
// Further, if we try to send a spontaneous payment with the same payment_id it should
11551156
// also be rejected.
1156-
let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
1157+
let send_result = nodes[0].node.send_spontaneous_payment(
1158+
&route, None, RecipientOnionFields::spontaneous_empty(), payment_id);
11571159
match send_result {
11581160
Err(PaymentSendFailure::DuplicatePayment) => {},
11591161
_ => panic!("Unexpected send result: {:?}", send_result),
@@ -1663,7 +1665,9 @@ fn do_automatic_retries(test: AutoRetry) {
16631665
pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, Some(payment_secret), msg_events.pop().unwrap(), true, None);
16641666
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
16651667
} else if test == AutoRetry::Spontaneous {
1666-
nodes[0].node.send_spontaneous_payment_with_retry(Some(payment_preimage), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
1668+
nodes[0].node.send_spontaneous_payment_with_retry(Some(payment_preimage),
1669+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params,
1670+
Retry::Attempts(1)).unwrap();
16671671
pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
16681672

16691673
// Open a new channel with liquidity on the second hop so we can find a route for the retry

0 commit comments

Comments
 (0)