Skip to content

Commit 47864f1

Browse files
committed
f fix tests/test expiry-before-payment
1 parent f2ab405 commit 47864f1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lightning-invoice/src/payment.rs

+38
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,25 @@ mod tests {
483483
.unwrap()
484484
}
485485

486+
fn will_expire_in_1_sec_invoice(payment_preimage: PaymentPreimage) -> Invoice {
487+
let payment_hash = Sha256::hash(&payment_preimage.0);
488+
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
489+
let timestamp = SystemTime::now()
490+
.checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME - 1))
491+
.unwrap();
492+
InvoiceBuilder::new(Currency::Bitcoin)
493+
.description("test".into())
494+
.payment_hash(payment_hash)
495+
.payment_secret(PaymentSecret([0; 32]))
496+
.timestamp(timestamp)
497+
.min_final_cltv_expiry(144)
498+
.amount_milli_satoshis(128)
499+
.build_signed(|hash| {
500+
Secp256k1::new().sign_recoverable(hash, &private_key)
501+
})
502+
.unwrap()
503+
}
504+
486505
#[test]
487506
fn pays_invoice_on_first_attempt() {
488507
let event_handled = core::cell::RefCell::new(false);
@@ -692,7 +711,26 @@ mod tests {
692711

693712
let payment_preimage = PaymentPreimage([1; 32]);
694713
let invoice = expired_invoice(payment_preimage);
714+
if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
715+
assert_eq!(msg, "Invoice expired prior to send");
716+
} else { panic!("Expected Invoice Error"); }
717+
}
718+
719+
#[test]
720+
fn fails_retrying_invoice_after_expiration() {
721+
let event_handled = core::cell::RefCell::new(false);
722+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
723+
724+
let payer = TestPayer::new();
725+
let router = TestRouter {};
726+
let logger = TestLogger::new();
727+
let invoice_payer =
728+
InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
729+
730+
let payment_preimage = PaymentPreimage([1; 32]);
731+
let invoice = will_expire_in_1_sec_invoice(payment_preimage);
695732
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
733+
std::thread::sleep(Duration::from_secs(2));
696734
assert_eq!(*payer.attempts.borrow(), 1);
697735

698736
let event = Event::PaymentPathFailed {

0 commit comments

Comments
 (0)