59
59
//! # fn node_id(&self) -> PublicKey { unimplemented!() }
60
60
//! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
61
61
//! # fn send_payment(
62
- //! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
63
- //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
62
+ //! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
63
+ //! # payment_id: PaymentId
64
+ //! # ) -> Result<(), PaymentSendFailure> { unimplemented!() }
64
65
//! # fn send_spontaneous_payment(
65
- //! # &self, route: &Route, payment_preimage: PaymentPreimage
66
- //! # ) -> Result<PaymentId , PaymentSendFailure> { unimplemented!() }
66
+ //! # &self, route: &Route, payment_preimage: PaymentPreimage, payment_id: PaymentId,
67
+ //! # ) -> Result<() , PaymentSendFailure> { unimplemented!() }
67
68
//! # fn retry_payment(
68
69
//! # &self, route: &Route, payment_id: PaymentId
69
70
//! # ) -> Result<(), PaymentSendFailure> { unimplemented!() }
@@ -242,6 +243,19 @@ impl<T: Time> Display for PaymentAttempts<T> {
242
243
}
243
244
244
245
/// A trait defining behavior of an [`Invoice`] payer.
246
+ ///
247
+ /// While the behavior of [`InvoicePayer`] provides idempotency of duplicate `send_*payment` calls
248
+ /// with the same [`PaymentHash`], it is up to the `Payer` to provide idempotency across restarts.
249
+ ///
250
+ /// The lightning [`ChannelManager`] provides idempotency for duplicate payments with the same
251
+ /// [`PaymentId`].
252
+ ///
253
+ /// In order to trivially ensure idempotency for payments, the default `Payer` implementation
254
+ /// reuses the [`PaymentHash`] bytes as the [`PaymentId`]. Custom implementations wishing to
255
+ /// provide payment idempotency with a different idempotency key (i.e. [`PaymentId`]) should map
256
+ /// the [`Invoice`] or spontaneous payment target pubkey to their own idempotency key.
257
+ ///
258
+ /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
245
259
pub trait Payer {
246
260
/// Returns the payer's node id.
247
261
fn node_id ( & self ) -> PublicKey ;
@@ -251,13 +265,14 @@ pub trait Payer {
251
265
252
266
/// Sends a payment over the Lightning Network using the given [`Route`].
253
267
fn send_payment (
254
- & self , route : & Route , payment_hash : PaymentHash , payment_secret : & Option < PaymentSecret >
255
- ) -> Result < PaymentId , PaymentSendFailure > ;
268
+ & self , route : & Route , payment_hash : PaymentHash , payment_secret : & Option < PaymentSecret > ,
269
+ payment_id : PaymentId
270
+ ) -> Result < ( ) , PaymentSendFailure > ;
256
271
257
272
/// Sends a spontaneous payment over the Lightning Network using the given [`Route`].
258
273
fn send_spontaneous_payment (
259
- & self , route : & Route , payment_preimage : PaymentPreimage
260
- ) -> Result < PaymentId , PaymentSendFailure > ;
274
+ & self , route : & Route , payment_preimage : PaymentPreimage , payment_id : PaymentId
275
+ ) -> Result < ( ) , PaymentSendFailure > ;
261
276
262
277
/// Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
263
278
fn retry_payment ( & self , route : & Route , payment_id : PaymentId ) -> Result < ( ) , PaymentSendFailure > ;
@@ -346,36 +361,74 @@ where
346
361
347
362
/// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
348
363
///
349
- /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
350
- /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
351
- /// for you .
364
+ /// `invoice.payment_hash()` is used as the [`PaymentId`], which ensures idempotency as long as
365
+ /// the payment is still pending. Once the payment completes or fails, you must ensure that a
366
+ /// second payment with the same [`PaymentHash`] is never sent .
352
367
pub fn pay_invoice ( & self , invoice : & Invoice ) -> Result < PaymentId , PaymentError > {
353
368
if invoice. amount_milli_satoshis ( ) . is_none ( ) {
354
369
Err ( PaymentError :: Invoice ( "amount missing" ) )
355
370
} else {
356
- self . pay_invoice_using_amount ( invoice, None )
371
+ let payment_id = PaymentId ( invoice. payment_hash ( ) . into_inner ( ) ) ;
372
+ self . pay_invoice_using_amount ( invoice, None , payment_id) . map ( |( ) | payment_id)
373
+ }
374
+ }
375
+
376
+ /// Pays the given [`Invoice`] with a custom idempotency key, caching the invoice for later use
377
+ /// in case a retry is needed.
378
+ ///
379
+ /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
380
+ /// payment completes or fails, you must ensure that a second payment with the same
381
+ /// [`PaymentId`] is never sent.
382
+ ///
383
+ /// You should ensure that the `invoice.payment_hash()` is unique and the same [`PaymentHash`]
384
+ /// has never been paid before.
385
+ pub fn pay_invoice_with_id ( & self , invoice : & Invoice , payment_id : PaymentId ) -> Result < ( ) , PaymentError > {
386
+ if invoice. amount_milli_satoshis ( ) . is_none ( ) {
387
+ Err ( PaymentError :: Invoice ( "amount missing" ) )
388
+ } else {
389
+ self . pay_invoice_using_amount ( invoice, None , payment_id)
357
390
}
358
391
}
359
392
360
393
/// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
361
394
/// case a retry is needed.
362
395
///
363
- /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
364
- /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
365
- /// for you .
396
+ /// `invoice.payment_hash()` is used as the [`PaymentId`], which ensures idempotency as long as
397
+ /// the payment is still pending. Once the payment completes or fails, you must ensure that a
398
+ /// second payment with the same [`PaymentHash`] is never sent .
366
399
pub fn pay_zero_value_invoice (
367
400
& self , invoice : & Invoice , amount_msats : u64
368
401
) -> Result < PaymentId , PaymentError > {
369
402
if invoice. amount_milli_satoshis ( ) . is_some ( ) {
370
403
Err ( PaymentError :: Invoice ( "amount unexpected" ) )
371
404
} else {
372
- self . pay_invoice_using_amount ( invoice, Some ( amount_msats) )
405
+ let payment_id = PaymentId ( invoice. payment_hash ( ) . into_inner ( ) ) ;
406
+ self . pay_invoice_using_amount ( invoice, Some ( amount_msats) , payment_id) . map ( |( ) | payment_id)
407
+ }
408
+ }
409
+
410
+ /// Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
411
+ /// caching the invoice for later use in case a retry is needed.
412
+ ///
413
+ /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
414
+ /// payment completes or fails, you must ensure that a second payment with the same
415
+ /// [`PaymentId`] is never sent.
416
+ ///
417
+ /// You should ensure that the `invoice.payment_hash()` is unique and the same [`PaymentHash`]
418
+ /// has never been paid before.
419
+ pub fn pay_zero_value_invoice_with_id (
420
+ & self , invoice : & Invoice , amount_msats : u64 , payment_id : PaymentId
421
+ ) -> Result < ( ) , PaymentError > {
422
+ if invoice. amount_milli_satoshis ( ) . is_some ( ) {
423
+ Err ( PaymentError :: Invoice ( "amount unexpected" ) )
424
+ } else {
425
+ self . pay_invoice_using_amount ( invoice, Some ( amount_msats) , payment_id)
373
426
}
374
427
}
375
428
376
429
fn pay_invoice_using_amount (
377
- & self , invoice : & Invoice , amount_msats : Option < u64 >
378
- ) -> Result < PaymentId , PaymentError > {
430
+ & self , invoice : & Invoice , amount_msats : Option < u64 > , payment_id : PaymentId
431
+ ) -> Result < ( ) , PaymentError > {
379
432
debug_assert ! ( invoice. amount_milli_satoshis( ) . is_some( ) ^ amount_msats. is_some( ) ) ;
380
433
381
434
let payment_hash = PaymentHash ( invoice. payment_hash ( ) . clone ( ) . into_inner ( ) ) ;
@@ -398,7 +451,7 @@ where
398
451
} ;
399
452
400
453
let send_payment = |route : & Route | {
401
- self . payer . send_payment ( route, payment_hash, & payment_secret)
454
+ self . payer . send_payment ( route, payment_hash, & payment_secret, payment_id )
402
455
} ;
403
456
404
457
self . pay_internal ( & route_params, payment_hash, send_payment)
@@ -408,13 +461,39 @@ where
408
461
/// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
409
462
/// case a retry is needed.
410
463
///
411
- /// You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
412
- /// been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
464
+ /// The hash of the [`PaymentPreimage`] is used as the [`PaymentId`], which ensures idempotency
465
+ /// as long as the payment is still pending. Once the payment completes or fails, you must
466
+ /// ensure that a second payment with the same [`PaymentPreimage`] is never sent.
413
467
pub fn pay_pubkey (
414
468
& self , pubkey : PublicKey , payment_preimage : PaymentPreimage , amount_msats : u64 ,
415
469
final_cltv_expiry_delta : u32
416
470
) -> Result < PaymentId , PaymentError > {
417
471
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
472
+ let payment_id = PaymentId ( payment_hash. 0 ) ;
473
+ self . do_pay_pubkey ( pubkey, payment_preimage, payment_hash, payment_id, amount_msats,
474
+ final_cltv_expiry_delta)
475
+ . map ( |( ) | payment_id)
476
+ }
477
+
478
+ /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
479
+ /// case a retry is needed.
480
+ ///
481
+ /// The hash of the [`PaymentPreimage`] is used as the [`PaymentId`], which ensures idempotency
482
+ /// as long as the payment is still pending. Once the payment completes or fails, you must
483
+ /// ensure that a second payment with the same [`PaymentPreimage`] is never sent.
484
+ pub fn pay_pubkey_with_id (
485
+ & self , pubkey : PublicKey , payment_preimage : PaymentPreimage , payment_id : PaymentId ,
486
+ amount_msats : u64 , final_cltv_expiry_delta : u32
487
+ ) -> Result < ( ) , PaymentError > {
488
+ let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
489
+ self . do_pay_pubkey ( pubkey, payment_preimage, payment_hash, payment_id, amount_msats,
490
+ final_cltv_expiry_delta)
491
+ }
492
+
493
+ fn do_pay_pubkey (
494
+ & self , pubkey : PublicKey , payment_preimage : PaymentPreimage , payment_hash : PaymentHash ,
495
+ payment_id : PaymentId , amount_msats : u64 , final_cltv_expiry_delta : u32
496
+ ) -> Result < ( ) , PaymentError > {
418
497
match self . payment_cache . lock ( ) . unwrap ( ) . entry ( payment_hash) {
419
498
hash_map:: Entry :: Occupied ( _) => return Err ( PaymentError :: Invoice ( "payment pending" ) ) ,
420
499
hash_map:: Entry :: Vacant ( entry) => entry. insert ( PaymentInfo :: new ( ) ) ,
@@ -427,15 +506,15 @@ where
427
506
} ;
428
507
429
508
let send_payment = |route : & Route | {
430
- self . payer . send_spontaneous_payment ( route, payment_preimage)
509
+ self . payer . send_spontaneous_payment ( route, payment_preimage, payment_id )
431
510
} ;
432
511
self . pay_internal ( & route_params, payment_hash, send_payment)
433
512
. map_err ( |e| { self . payment_cache . lock ( ) . unwrap ( ) . remove ( & payment_hash) ; e } )
434
513
}
435
514
436
- fn pay_internal < F : FnOnce ( & Route ) -> Result < PaymentId , PaymentSendFailure > + Copy > (
515
+ fn pay_internal < F : FnOnce ( & Route ) -> Result < ( ) , PaymentSendFailure > + Copy > (
437
516
& self , params : & RouteParameters , payment_hash : PaymentHash , send_payment : F ,
438
- ) -> Result < PaymentId , PaymentError > {
517
+ ) -> Result < ( ) , PaymentError > {
439
518
#[ cfg( feature = "std" ) ] {
440
519
if has_expired ( params) {
441
520
log_trace ! ( self . logger, "Invoice expired prior to send for payment {}" , log_bytes!( payment_hash. 0 ) ) ;
@@ -452,11 +531,11 @@ where
452
531
) . map_err ( |e| PaymentError :: Routing ( e) ) ?;
453
532
454
533
match send_payment ( & route) {
455
- Ok ( payment_id ) => {
534
+ Ok ( ( ) ) => {
456
535
for path in route. paths {
457
536
self . process_path_inflight_htlcs ( payment_hash, path) ;
458
537
}
459
- Ok ( payment_id )
538
+ Ok ( ( ) )
460
539
} ,
461
540
Err ( e) => match e {
462
541
PaymentSendFailure :: ParameterError ( _) => Err ( e) ,
@@ -491,13 +570,13 @@ where
491
570
// consider the payment sent, so return `Ok()` here, ignoring any retry
492
571
// errors.
493
572
let _ = self . retry_payment ( payment_id, payment_hash, & retry_data) ;
494
- Ok ( payment_id )
573
+ Ok ( ( ) )
495
574
} else {
496
575
// This may happen if we send a payment and some paths fail, but
497
576
// only due to a temporary monitor failure or the like, implying
498
577
// they're really in-flight, but we haven't sent the initial
499
578
// HTLC-Add messages yet.
500
- Ok ( payment_id )
579
+ Ok ( ( ) )
501
580
}
502
581
} ,
503
582
} ,
@@ -2056,13 +2135,13 @@ mod tests {
2056
2135
self
2057
2136
}
2058
2137
2059
- fn check_attempts ( & self ) -> Result < PaymentId , PaymentSendFailure > {
2138
+ fn check_attempts ( & self ) -> Result < ( ) , PaymentSendFailure > {
2060
2139
let mut attempts = self . attempts . borrow_mut ( ) ;
2061
2140
* attempts += 1 ;
2062
2141
2063
2142
match self . failing_on_attempt . borrow_mut ( ) . remove ( & * attempts) {
2064
2143
Some ( failure) => Err ( failure) ,
2065
- None => Ok ( PaymentId ( [ 1 ; 32 ] ) ) ,
2144
+ None => Ok ( ( ) )
2066
2145
}
2067
2146
}
2068
2147
@@ -2100,15 +2179,15 @@ mod tests {
2100
2179
2101
2180
fn send_payment (
2102
2181
& self , route : & Route , _payment_hash : PaymentHash ,
2103
- _payment_secret : & Option < PaymentSecret >
2104
- ) -> Result < PaymentId , PaymentSendFailure > {
2182
+ _payment_secret : & Option < PaymentSecret > , _payment_id : PaymentId ,
2183
+ ) -> Result < ( ) , PaymentSendFailure > {
2105
2184
self . check_value_msats ( Amount :: ForInvoice ( route. get_total_amount ( ) ) ) ;
2106
2185
self . check_attempts ( )
2107
2186
}
2108
2187
2109
2188
fn send_spontaneous_payment (
2110
- & self , route : & Route , _payment_preimage : PaymentPreimage ,
2111
- ) -> Result < PaymentId , PaymentSendFailure > {
2189
+ & self , route : & Route , _payment_preimage : PaymentPreimage , _payment_id : PaymentId ,
2190
+ ) -> Result < ( ) , PaymentSendFailure > {
2112
2191
self . check_value_msats ( Amount :: Spontaneous ( route. get_total_amount ( ) ) ) ;
2113
2192
self . check_attempts ( )
2114
2193
}
@@ -2117,7 +2196,7 @@ mod tests {
2117
2196
& self , route : & Route , _payment_id : PaymentId
2118
2197
) -> Result < ( ) , PaymentSendFailure > {
2119
2198
self . check_value_msats ( Amount :: OnRetry ( route. get_total_amount ( ) ) ) ;
2120
- self . check_attempts ( ) . map ( |_| ( ) )
2199
+ self . check_attempts ( )
2121
2200
}
2122
2201
2123
2202
fn abandon_payment ( & self , _payment_id : PaymentId ) { }
0 commit comments