- .unwrap()
- }
-
- #[test]
- fn pays_invoice_on_first_attempt() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payment_preimage = PaymentPreimage([1; 32]);
- let invoice = invoice(payment_preimage);
- let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
-
- let payer = TestPayer::new();
- let router = TestRouter {};
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(0));
-
- let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
- assert_eq!(*payer.attempts.borrow(), 1);
-
- invoice_payer.handle_event(&Event::PaymentSent {
- payment_id, payment_preimage, payment_hash
- });
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 1);
- }
-
- #[test]
- fn pays_invoice_on_retry() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payment_preimage = PaymentPreimage([1; 32]);
- let invoice = invoice(payment_preimage);
- let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
- let final_value_msat = invoice.amount_milli_satoshis().unwrap();
-
- let payer = TestPayer::new()
- .expect_value_msat(final_value_msat)
- .expect_value_msat(final_value_msat / 2);
- let router = TestRouter {};
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
-
- let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
- assert_eq!(*payer.attempts.borrow(), 1);
-
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash,
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path: TestRouter::path_for_value(final_value_msat),
- short_channel_id: None,
- retry: Some(TestRouter::retry_for_invoice(&invoice)),
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 2);
-
- invoice_payer.handle_event(&Event::PaymentSent {
- payment_id, payment_preimage, payment_hash
- });
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 2);
- }
-
- #[test]
- fn retries_payment_path_for_unknown_payment() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payment_preimage = PaymentPreimage([1; 32]);
- let invoice = invoice(payment_preimage);
- let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
- let final_value_msat = invoice.amount_milli_satoshis().unwrap();
-
- let payer = TestPayer::new();
- let router = TestRouter {};
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
-
- let payment_id = Some(PaymentId([1; 32]));
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash,
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path: TestRouter::path_for_value(final_value_msat),
- short_channel_id: None,
- retry: Some(TestRouter::retry_for_invoice(&invoice)),
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 1);
-
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 2);
-
- invoice_payer.handle_event(&Event::PaymentSent {
- payment_id, payment_preimage, payment_hash
- });
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 2);
- }
-
- #[test]
- fn fails_paying_invoice_after_max_retries() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payment_preimage = PaymentPreimage([1; 32]);
- let invoice = invoice(payment_preimage);
- let final_value_msat = invoice.amount_milli_satoshis().unwrap();
-
- let payer = TestPayer::new()
- .expect_value_msat(final_value_msat)
- .expect_value_msat(final_value_msat / 2)
- .expect_value_msat(final_value_msat / 2);
- let router = TestRouter {};
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
-
- let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
- assert_eq!(*payer.attempts.borrow(), 1);
-
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: true,
- path: TestRouter::path_for_value(final_value_msat),
- short_channel_id: None,
- retry: Some(TestRouter::retry_for_invoice(&invoice)),
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 2);
-
- let event = Event::PaymentPathFailed {
- payment_id,
- payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path: TestRouter::path_for_value(final_value_msat / 2),
- short_channel_id: None,
- retry: Some(RouteParameters {
- final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
- }),
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), false);
- assert_eq!(*payer.attempts.borrow(), 3);
-
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 3);
- }
-
- #[test]
- fn fails_paying_invoice_with_missing_retry_params() {
- let event_handled = core::cell::RefCell::new(false);
- let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
-
- let payer = TestPayer::new();
- let router = TestRouter {};
- let logger = TestLogger::new();
- let invoice_payer =
- InvoicePayer::new(&payer, router, &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 event = Event::PaymentPathFailed {
- payment_id,
- payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
- network_update: None,
- rejected_by_dest: false,
- all_paths_failed: false,
- path: vec![],
- short_channel_id: None,
- retry: None,
- };
- invoice_payer.handle_event(&event);
- assert_eq!(*event_handled.borrow(), true);
- assert_eq!(*payer.attempts.borrow(), 1);