Skip to content

Commit aee42ad

Browse files
committed
Check for invoice expiry in InvoicePayer before we send any HTLCs
1 parent 5989705 commit aee42ad

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lightning-invoice/src/payment.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ where
269269
final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
270270
final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
271271
};
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+
}
272276
let first_hops = self.payer.first_hops();
273277
let route = self.router.find_route(
274278
&payer,
@@ -514,6 +518,25 @@ mod tests {
514518
.unwrap()
515519
}
516520

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+
517540
#[test]
518541
fn pays_invoice_on_first_attempt() {
519542
let event_handled = core::cell::RefCell::new(false);
@@ -729,7 +752,26 @@ mod tests {
729752

730753
let payment_preimage = PaymentPreimage([1; 32]);
731754
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);
732773
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
774+
std::thread::sleep(Duration::from_secs(2));
733775
assert_eq!(*payer.attempts.borrow(), 1);
734776

735777
let event = Event::PaymentPathFailed {

0 commit comments

Comments
 (0)