@@ -114,6 +114,7 @@ use secp256k1::key::PublicKey;
114
114
use std:: collections:: hash_map:: { self , HashMap } ;
115
115
use std:: ops:: Deref ;
116
116
use std:: sync:: Mutex ;
117
+ use std:: time:: { Duration , SystemTime } ;
117
118
118
119
/// A utility for paying [`Invoice]`s.
119
120
pub struct InvoicePayer < P : Deref , R , L : Deref , E >
@@ -226,6 +227,7 @@ where
226
227
hash_map:: Entry :: Vacant ( entry) => {
227
228
let payer = self . payer . node_id ( ) ;
228
229
let mut payee = Payee :: new ( invoice. recover_payee_pub_key ( ) )
230
+ . with_expiry_time ( expiry_time_from_unix_epoch ( & invoice) )
229
231
. with_route_hints ( invoice. route_hints ( ) ) ;
230
232
if let Some ( features) = invoice. features ( ) {
231
233
payee = payee. with_features ( features. clone ( ) ) ;
@@ -273,6 +275,14 @@ where
273
275
}
274
276
}
275
277
278
+ fn expiry_time_from_unix_epoch ( invoice : & Invoice ) -> Duration {
279
+ invoice. timestamp ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) + invoice. expiry_time ( )
280
+ }
281
+
282
+ fn has_expired ( params : & RouteParameters ) -> bool {
283
+ Invoice :: is_expired_from_epoch ( & SystemTime :: UNIX_EPOCH , params. payee . expiry_time . unwrap ( ) )
284
+ }
285
+
276
286
impl < P : Deref , R , L : Deref , E > EventHandler for InvoicePayer < P , R , L , E >
277
287
where
278
288
P :: Target : Payer ,
@@ -304,6 +314,8 @@ where
304
314
log_trace ! ( self . logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})" , log_bytes!( payment_hash. 0 ) , attempts) ;
305
315
} else if retry. is_none ( ) {
306
316
log_trace ! ( self . logger, "Payment {} missing retry params; not retrying (attempts: {})" , log_bytes!( payment_hash. 0 ) , attempts) ;
317
+ } else if has_expired ( retry. as_ref ( ) . unwrap ( ) ) {
318
+ log_trace ! ( self . logger, "Invoice expired for payment {}; not retrying (attempts: {})" , log_bytes!( payment_hash. 0 ) , attempts) ;
307
319
} else if self . retry_payment ( * payment_id. as_ref ( ) . unwrap ( ) , retry. as_ref ( ) . unwrap ( ) ) . is_err ( ) {
308
320
log_trace ! ( self . logger, "Error retrying payment {}; not retrying (attempts: {})" , log_bytes!( payment_hash. 0 ) , attempts) ;
309
321
} else {
@@ -336,7 +348,7 @@ where
336
348
#[ cfg( test) ]
337
349
mod tests {
338
350
use super :: * ;
339
- use crate :: { InvoiceBuilder , Currency } ;
351
+ use crate :: { DEFAULT_EXPIRY_TIME , InvoiceBuilder , Currency } ;
340
352
use bitcoin_hashes:: sha256:: Hash as Sha256 ;
341
353
use lightning:: ln:: PaymentPreimage ;
342
354
use lightning:: ln:: features:: { ChannelFeatures , NodeFeatures } ;
@@ -346,6 +358,7 @@ mod tests {
346
358
use lightning:: util:: errors:: APIError ;
347
359
use lightning:: util:: events:: Event ;
348
360
use secp256k1:: { SecretKey , PublicKey , Secp256k1 } ;
361
+ use std:: time:: { SystemTime , Duration } ;
349
362
350
363
fn invoice ( payment_preimage : PaymentPreimage ) -> Invoice {
351
364
let payment_hash = Sha256 :: hash ( & payment_preimage. 0 ) ;
@@ -378,6 +391,25 @@ mod tests {
378
391
. unwrap ( )
379
392
}
380
393
394
+ fn expired_invoice ( payment_preimage : PaymentPreimage ) -> Invoice {
395
+ let payment_hash = Sha256 :: hash ( & payment_preimage. 0 ) ;
396
+ let private_key = SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
397
+ let timestamp = SystemTime :: now ( )
398
+ . checked_sub ( Duration :: from_secs ( DEFAULT_EXPIRY_TIME * 2 ) )
399
+ . unwrap ( ) ;
400
+ InvoiceBuilder :: new ( Currency :: Bitcoin )
401
+ . description ( "test" . into ( ) )
402
+ . payment_hash ( payment_hash)
403
+ . payment_secret ( PaymentSecret ( [ 0 ; 32 ] ) )
404
+ . timestamp ( timestamp)
405
+ . min_final_cltv_expiry ( 144 )
406
+ . amount_milli_satoshis ( 128 )
407
+ . build_signed ( |hash| {
408
+ Secp256k1 :: new ( ) . sign_recoverable ( hash, & private_key)
409
+ } )
410
+ . unwrap ( )
411
+ }
412
+
381
413
#[ test]
382
414
fn pays_invoice_on_first_attempt ( ) {
383
415
let event_handled = core:: cell:: RefCell :: new ( false ) ;
@@ -574,6 +606,37 @@ mod tests {
574
606
assert_eq ! ( * payer. attempts. borrow( ) , 1 ) ;
575
607
}
576
608
609
+ #[ test]
610
+ fn fails_paying_invoice_after_expiration ( ) {
611
+ let event_handled = core:: cell:: RefCell :: new ( false ) ;
612
+ let event_handler = |_: & _ | { * event_handled. borrow_mut ( ) = true ; } ;
613
+
614
+ let payer = TestPayer :: new ( ) ;
615
+ let router = TestRouter { } ;
616
+ let logger = TestLogger :: new ( ) ;
617
+ let invoice_payer =
618
+ InvoicePayer :: new ( & payer, router, & logger, event_handler, RetryAttempts ( 2 ) ) ;
619
+
620
+ let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
621
+ let invoice = expired_invoice ( payment_preimage) ;
622
+ let payment_id = Some ( invoice_payer. pay_invoice ( & invoice) . unwrap ( ) ) ;
623
+ assert_eq ! ( * payer. attempts. borrow( ) , 1 ) ;
624
+
625
+ let event = Event :: PaymentPathFailed {
626
+ payment_id,
627
+ payment_hash : PaymentHash ( invoice. payment_hash ( ) . clone ( ) . into_inner ( ) ) ,
628
+ network_update : None ,
629
+ rejected_by_dest : false ,
630
+ all_paths_failed : false ,
631
+ path : vec ! [ ] ,
632
+ short_channel_id : None ,
633
+ retry : Some ( TestRouter :: retry_for_invoice ( & invoice) ) ,
634
+ } ;
635
+ invoice_payer. handle_event ( & event) ;
636
+ assert_eq ! ( * event_handled. borrow( ) , true ) ;
637
+ assert_eq ! ( * payer. attempts. borrow( ) , 1 ) ;
638
+ }
639
+
577
640
#[ test]
578
641
fn fails_paying_invoice_after_retry_error ( ) {
579
642
let event_handled = core:: cell:: RefCell :: new ( false ) ;
@@ -795,6 +858,7 @@ mod tests {
795
858
796
859
fn retry_for_invoice ( invoice : & Invoice ) -> RouteParameters {
797
860
let mut payee = Payee :: new ( invoice. recover_payee_pub_key ( ) )
861
+ . with_expiry_time ( expiry_time_from_unix_epoch ( invoice) )
798
862
. with_route_hints ( invoice. route_hints ( ) ) ;
799
863
if let Some ( features) = invoice. features ( ) {
800
864
payee = payee. with_features ( features. clone ( ) ) ;
0 commit comments