@@ -269,6 +269,10 @@ where
269
269
final_value_msat : invoice. amount_milli_satoshis ( ) . or ( amount_msats) . unwrap ( ) ,
270
270
final_cltv_expiry_delta : invoice. min_final_cltv_expiry ( ) as u32 ,
271
271
} ;
272
+ if has_expired ( & params) {
273
+ log_trace ! ( self . logger, "Invoice expired prior to first send for payment {}" , log_bytes!( payment_hash. 0 ) ) ;
274
+ return Err ( PaymentError :: Invoice ( "Invoice expired prior to send" ) ) ;
275
+ }
272
276
let first_hops = self . payer . first_hops ( ) ;
273
277
let route = self . router . find_route (
274
278
& payer,
@@ -514,6 +518,25 @@ mod tests {
514
518
. unwrap ( )
515
519
}
516
520
521
+ fn will_expire_in_1_sec_invoice ( payment_preimage : PaymentPreimage ) -> Invoice {
522
+ let payment_hash = Sha256 :: hash ( & payment_preimage. 0 ) ;
523
+ let private_key = SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
524
+ let timestamp = SystemTime :: now ( )
525
+ . checked_sub ( Duration :: from_secs ( DEFAULT_EXPIRY_TIME - 1 ) )
526
+ . unwrap ( ) ;
527
+ InvoiceBuilder :: new ( Currency :: Bitcoin )
528
+ . description ( "test" . into ( ) )
529
+ . payment_hash ( payment_hash)
530
+ . payment_secret ( PaymentSecret ( [ 0 ; 32 ] ) )
531
+ . timestamp ( timestamp)
532
+ . min_final_cltv_expiry ( 144 )
533
+ . amount_milli_satoshis ( 128 )
534
+ . build_signed ( |hash| {
535
+ Secp256k1 :: new ( ) . sign_recoverable ( hash, & private_key)
536
+ } )
537
+ . unwrap ( )
538
+ }
539
+
517
540
#[ test]
518
541
fn pays_invoice_on_first_attempt ( ) {
519
542
let event_handled = core:: cell:: RefCell :: new ( false ) ;
@@ -729,7 +752,26 @@ mod tests {
729
752
730
753
let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
731
754
let invoice = expired_invoice ( payment_preimage) ;
755
+ if let PaymentError :: Invoice ( msg) = invoice_payer. pay_invoice ( & invoice) . unwrap_err ( ) {
756
+ assert_eq ! ( msg, "Invoice expired prior to send" ) ;
757
+ } else { panic ! ( "Expected Invoice Error" ) ; }
758
+ }
759
+
760
+ #[ test]
761
+ fn fails_retrying_invoice_after_expiration ( ) {
762
+ let event_handled = core:: cell:: RefCell :: new ( false ) ;
763
+ let event_handler = |_: & _ | { * event_handled. borrow_mut ( ) = true ; } ;
764
+
765
+ let payer = TestPayer :: new ( ) ;
766
+ let router = TestRouter { } ;
767
+ let logger = TestLogger :: new ( ) ;
768
+ let invoice_payer =
769
+ InvoicePayer :: new ( & payer, router, & logger, event_handler, RetryAttempts ( 2 ) ) ;
770
+
771
+ let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
772
+ let invoice = will_expire_in_1_sec_invoice ( payment_preimage) ;
732
773
let payment_id = Some ( invoice_payer. pay_invoice ( & invoice) . unwrap ( ) ) ;
774
+ std:: thread:: sleep ( Duration :: from_secs ( 2 ) ) ;
733
775
assert_eq ! ( * payer. attempts. borrow( ) , 1 ) ;
734
776
735
777
let event = Event :: PaymentPathFailed {
0 commit comments