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