Skip to content

Commit c8e24b7

Browse files
committed
Check for invoice expiry in InvoicePayer before we send any HTLCs
1 parent 6e70287 commit c8e24b7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lightning-invoice/src/payment.rs

+42
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ where
257257
final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
258258
final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
259259
};
260+
if has_expired(&params) {
261+
log_trace!(self.logger, "Invoice expired prior to first send for payment {}", log_bytes!(payment_hash.0));
262+
return Err(PaymentError::Invoice("Invoice expired prior to send"));
263+
}
260264
let first_hops = self.payer.first_hops();
261265
let route = self.router.find_route(
262266
&payer,
@@ -505,6 +509,25 @@ mod tests {
505509
.unwrap()
506510
}
507511

512+
fn will_expire_in_1_sec_invoice(payment_preimage: PaymentPreimage) -> Invoice {
513+
let payment_hash = Sha256::hash(&payment_preimage.0);
514+
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
515+
let timestamp = SystemTime::now()
516+
.checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME - 1))
517+
.unwrap();
518+
InvoiceBuilder::new(Currency::Bitcoin)
519+
.description("test".into())
520+
.payment_hash(payment_hash)
521+
.payment_secret(PaymentSecret([0; 32]))
522+
.timestamp(timestamp)
523+
.min_final_cltv_expiry(144)
524+
.amount_milli_satoshis(128)
525+
.build_signed(|hash| {
526+
Secp256k1::new().sign_recoverable(hash, &private_key)
527+
})
528+
.unwrap()
529+
}
530+
508531
#[test]
509532
fn pays_invoice_on_first_attempt() {
510533
let event_handled = core::cell::RefCell::new(false);
@@ -720,7 +743,26 @@ mod tests {
720743

721744
let payment_preimage = PaymentPreimage([1; 32]);
722745
let invoice = expired_invoice(payment_preimage);
746+
if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
747+
assert_eq!(msg, "Invoice expired prior to send");
748+
} else { panic!("Expected Invoice Error"); }
749+
}
750+
751+
#[test]
752+
fn fails_retrying_invoice_after_expiration() {
753+
let event_handled = core::cell::RefCell::new(false);
754+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
755+
756+
let payer = TestPayer::new();
757+
let router = TestRouter {};
758+
let logger = TestLogger::new();
759+
let invoice_payer =
760+
InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
761+
762+
let payment_preimage = PaymentPreimage([1; 32]);
763+
let invoice = will_expire_in_1_sec_invoice(payment_preimage);
723764
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
765+
std::thread::sleep(Duration::from_secs(2));
724766
assert_eq!(*payer.attempts.borrow(), 1);
725767

726768
let event = Event::PaymentPathFailed {

0 commit comments

Comments
 (0)