Skip to content

Commit b54d3cc

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 ee1fa5e commit b54d3cc

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
@@ -228,6 +228,10 @@ pub struct RecipientOnionFields {
228228
///
229229
/// If you do not have one, the [`Route`] you pay over must not contain multiple paths as
230230
/// multi-path payments require a recipient-provided secret.
231+
///
232+
/// Note that for spontaneous payments most lightning nodes do not currently support MPP
233+
/// receives, thus you should generally never be providing a secret here for spontaneous
234+
/// payments.
231235
pub payment_secret: Option<PaymentSecret>,
232236
}
233237

@@ -2724,7 +2728,7 @@ where
27242728
/// Note that `route` must have exactly one path.
27252729
///
27262730
/// [`send_payment`]: Self::send_payment
2727-
pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
2731+
pub fn send_spontaneous_payment(&self, route: &Route, payment_preimage: Option<PaymentPreimage>, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<PaymentHash, PaymentSendFailure> {
27282732
let best_block_height = self.best_block.read().unwrap().height();
27292733
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
27302734
self.pending_outbound_payments.send_spontaneous_payment_with_route(
@@ -2741,7 +2745,7 @@ where
27412745
/// payments.
27422746
///
27432747
/// [`PaymentParameters::for_keysend`]: crate::routing::router::PaymentParameters::for_keysend
2744-
pub fn send_spontaneous_payment_with_retry(&self, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<PaymentHash, RetryableSendFailure> {
2748+
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> {
27452749
let best_block_height = self.best_block.read().unwrap().height();
27462750
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
27472751
self.pending_outbound_payments.send_spontaneous_payment(payment_preimage, payment_id,
@@ -8004,7 +8008,8 @@ mod tests {
80048008
pass_along_path(&nodes[0], &[&nodes[1]], 200_000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
80058009

80068010
// Next, send a keysend payment with the same payment_hash and make sure it fails.
8007-
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8011+
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8012+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
80088013
check_added_monitors!(nodes[0], 1);
80098014
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
80108015
assert_eq!(events.len(), 1);
@@ -8124,7 +8129,8 @@ mod tests {
81248129
&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
81258130
None, nodes[0].logger, &scorer, &random_seed_bytes
81268131
).unwrap();
8127-
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8132+
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8133+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
81288134
check_added_monitors!(nodes[0], 1);
81298135
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
81308136
assert_eq!(events.len(), 1);
@@ -8157,7 +8163,8 @@ mod tests {
81578163
&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
81588164
None, nodes[0].logger, &scorer, &random_seed_bytes
81598165
).unwrap();
8160-
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), PaymentId(payment_preimage.0)).unwrap();
8166+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8167+
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_preimage.0)).unwrap();
81618168
check_added_monitors!(nodes[0], 1);
81628169
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
81638170
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
@@ -9317,7 +9317,8 @@ fn test_keysend_payments_to_public_node() {
93179317
let route = find_route(&payer_pubkey, &route_params, &network_graph, None, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
93189318

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

93549355
let test_preimage = PaymentPreimage([42; 32]);
9355-
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage), PaymentId(test_preimage.0)).unwrap();
9356+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage),
9357+
RecipientOnionFields::spontaneous_empty(), PaymentId(test_preimage.0)).unwrap();
93569358
check_added_monitors!(nodes[0], 1);
93579359
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
93589360
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
@@ -1078,7 +1078,8 @@ fn claimed_send_payment_idempotent() {
10781078

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

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

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

0 commit comments

Comments
 (0)