]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Check for invoice expiry in InvoicePayer before we send any HTLCs 2021-10-payment-id-on-partial-failure
authorMatt Corallo <git@bluematt.me>
Thu, 28 Oct 2021 18:46:02 +0000 (18:46 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 31 Oct 2021 17:43:26 +0000 (17:43 +0000)
lightning-invoice/src/payment.rs

index f75464689ec73b425f2f1651e39f3d829376c81e..a5160ea9b69a4d570e67100fb0389fb0eed16226 100644 (file)
@@ -253,6 +253,10 @@ where
        ) -> Result<PaymentId, PaymentError> {
                debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
                let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
+               if invoice.is_expired() {
+                       log_trace!(self.logger, "Invoice expired prior to first send for payment {}", log_bytes!(payment_hash.0));
+                       return Err(PaymentError::Invoice("Invoice expired prior to send"));
+               }
                let retry_data_payment_id = loop {
                        let mut payment_cache = self.payment_cache.lock().unwrap();
                        match payment_cache.entry(payment_hash) {
@@ -728,9 +732,32 @@ mod tests {
 
                let payment_preimage = PaymentPreimage([1; 32]);
                let invoice = expired_invoice(payment_preimage);
+               if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
+                       assert_eq!(msg, "Invoice expired prior to send");
+               } else { panic!("Expected Invoice Error"); }
+       }
+
+       #[test]
+       fn fails_retrying_invoice_after_expiration() {
+               let event_handled = core::cell::RefCell::new(false);
+               let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
+
+               let payer = TestPayer::new();
+               let router = TestRouter {};
+               let scorer = RefCell::new(TestScorer::new());
+               let logger = TestLogger::new();
+               let invoice_payer =
+                       InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
+
+               let payment_preimage = PaymentPreimage([1; 32]);
+               let invoice = invoice(payment_preimage);
                let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
                assert_eq!(*payer.attempts.borrow(), 1);
 
+               let mut retry_data = TestRouter::retry_for_invoice(&invoice);
+               retry_data.payee.expiry_time = Some(SystemTime::now()
+                       .checked_sub(Duration::from_secs(2)).unwrap()
+                       .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
                let event = Event::PaymentPathFailed {
                        payment_id,
                        payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
@@ -739,7 +766,7 @@ mod tests {
                        all_paths_failed: false,
                        path: vec![],
                        short_channel_id: None,
-                       retry: Some(TestRouter::retry_for_invoice(&invoice)),
+                       retry: Some(retry_data),
                };
                invoice_payer.handle_event(&event);
                assert_eq!(*event_handled.borrow(), true);