1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! A module for paying Lightning invoices and sending spontaneous payments.
12 //! Defines an [`InvoicePayer`] utility for sending payments, parameterized by [`Payer`] and
13 //! [`Router`] traits. Implementations of [`Payer`] provide the payer's node id, channels, and means
14 //! to send a payment over a [`Route`]. Implementations of [`Router`] find a [`Route`] between payer
15 //! and payee using information provided by the payer and from the payee's [`Invoice`], when
18 //! [`InvoicePayer`] is capable of retrying failed payments. It accomplishes this by implementing
19 //! [`EventHandler`] which decorates a user-provided handler. It will intercept any
20 //! [`Event::PaymentPathFailed`] events and retry the failed paths for a fixed number of total
21 //! attempts or until retry is no longer possible. In such a situation, [`InvoicePayer`] will pass
22 //! along the events to the user-provided handler.
27 //! # extern crate lightning;
28 //! # extern crate lightning_invoice;
29 //! # extern crate secp256k1;
31 //! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
32 //! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
33 //! # use lightning::ln::msgs::LightningError;
34 //! # use lightning::routing::scoring::Score;
35 //! # use lightning::routing::network_graph::NodeId;
36 //! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
37 //! # use lightning::util::events::{Event, EventHandler, EventsProvider};
38 //! # use lightning::util::logger::{Logger, Record};
39 //! # use lightning::util::ser::{Writeable, Writer};
40 //! # use lightning_invoice::Invoice;
41 //! # use lightning_invoice::payment::{InvoicePayer, Payer, RetryAttempts, Router};
42 //! # use secp256k1::key::PublicKey;
43 //! # use std::cell::RefCell;
44 //! # use std::ops::Deref;
46 //! # struct FakeEventProvider {}
47 //! # impl EventsProvider for FakeEventProvider {
48 //! # fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {}
51 //! # struct FakePayer {}
52 //! # impl Payer for FakePayer {
53 //! # fn node_id(&self) -> PublicKey { unimplemented!() }
54 //! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
55 //! # fn send_payment(
56 //! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
57 //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
58 //! # fn send_spontaneous_payment(
59 //! # &self, route: &Route, payment_preimage: PaymentPreimage
60 //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
61 //! # fn retry_payment(
62 //! # &self, route: &Route, payment_id: PaymentId
63 //! # ) -> Result<(), PaymentSendFailure> { unimplemented!() }
66 //! # struct FakeRouter {}
67 //! # impl<S: Score> Router<S> for FakeRouter {
69 //! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
70 //! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
71 //! # ) -> Result<Route, LightningError> { unimplemented!() }
74 //! # struct FakeScorer {}
75 //! # impl Writeable for FakeScorer {
76 //! # fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> { unimplemented!(); }
78 //! # impl Score for FakeScorer {
79 //! # fn channel_penalty_msat(
80 //! # &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
82 //! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
85 //! # struct FakeLogger {}
86 //! # impl Logger for FakeLogger {
87 //! # fn log(&self, record: &Record) { unimplemented!() }
91 //! let event_handler = |event: &Event| {
93 //! Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
94 //! Event::PaymentSent { .. } => println!("payment successful"),
98 //! # let payer = FakePayer {};
99 //! # let router = FakeRouter {};
100 //! # let scorer = RefCell::new(FakeScorer {});
101 //! # let logger = FakeLogger {};
102 //! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
104 //! let invoice = "...";
105 //! if let Ok(invoice) = invoice.parse::<Invoice>() {
106 //! invoice_payer.pay_invoice(&invoice).unwrap();
108 //! # let event_provider = FakeEventProvider {};
110 //! event_provider.process_pending_events(&invoice_payer);
118 //! The [`Route`] is computed before each payment attempt. Any updates affecting path finding such
119 //! as updates to the network graph or changes to channel scores should be applied prior to
120 //! retries, typically by way of composing [`EventHandler`]s accordingly.
124 use bitcoin_hashes::Hash;
125 use bitcoin_hashes::sha256::Hash as Sha256;
127 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
128 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
129 use lightning::ln::msgs::LightningError;
130 use lightning::routing::scoring::{LockableScore, Score};
131 use lightning::routing::router::{Payee, Route, RouteParameters};
132 use lightning::util::events::{Event, EventHandler};
133 use lightning::util::logger::Logger;
135 use secp256k1::key::PublicKey;
137 use std::collections::hash_map::{self, HashMap};
139 use std::sync::Mutex;
140 use std::time::{Duration, SystemTime};
142 /// A utility for paying [`Invoice`]s and sending spontaneous payments.
143 pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
146 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
147 S::Target: for <'a> LockableScore<'a>,
156 /// Caches the overall attempts at making a payment, which is updated prior to retrying.
157 payment_cache: Mutex<HashMap<PaymentHash, usize>>,
158 retry_attempts: RetryAttempts,
161 /// A trait defining behavior of an [`Invoice`] payer.
163 /// Returns the payer's node id.
164 fn node_id(&self) -> PublicKey;
166 /// Returns the payer's channels.
167 fn first_hops(&self) -> Vec<ChannelDetails>;
169 /// Sends a payment over the Lightning Network using the given [`Route`].
171 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
172 ) -> Result<PaymentId, PaymentSendFailure>;
174 /// Sends a spontaneous payment over the Lightning Network using the given [`Route`].
175 fn send_spontaneous_payment(
176 &self, route: &Route, payment_preimage: PaymentPreimage
177 ) -> Result<PaymentId, PaymentSendFailure>;
179 /// Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
180 fn retry_payment(&self, route: &Route, payment_id: PaymentId) -> Result<(), PaymentSendFailure>;
183 /// A trait defining behavior for routing an [`Invoice`] payment.
184 pub trait Router<S: Score> {
185 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
187 &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
188 first_hops: Option<&[&ChannelDetails]>, scorer: &S
189 ) -> Result<Route, LightningError>;
192 /// Number of attempts to retry payment path failures for an [`Invoice`].
194 /// Note that this is the number of *path* failures, not full payment retries. For multi-path
195 /// payments, if this is less than the total number of paths, we will never even retry all of the
197 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
198 pub struct RetryAttempts(pub usize);
200 /// An error that may occur when making a payment.
201 #[derive(Clone, Debug)]
202 pub enum PaymentError {
203 /// An error resulting from the provided [`Invoice`] or payment hash.
204 Invoice(&'static str),
205 /// An error occurring when finding a route.
206 Routing(LightningError),
207 /// An error occurring when sending a payment.
208 Sending(PaymentSendFailure),
211 impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
214 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
215 S::Target: for <'a> LockableScore<'a>,
219 /// Creates an invoice payer that retries failed payment paths.
221 /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
222 /// `retry_attempts` has been exceeded for a given [`Invoice`].
224 payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
232 payment_cache: Mutex::new(HashMap::new()),
237 /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
239 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
240 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
242 pub fn pay_invoice(&self, invoice: &Invoice) -> Result<PaymentId, PaymentError> {
243 if invoice.amount_milli_satoshis().is_none() {
244 Err(PaymentError::Invoice("amount missing"))
246 self.pay_invoice_using_amount(invoice, None)
250 /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
251 /// case a retry is needed.
253 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
254 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
256 pub fn pay_zero_value_invoice(
257 &self, invoice: &Invoice, amount_msats: u64
258 ) -> Result<PaymentId, PaymentError> {
259 if invoice.amount_milli_satoshis().is_some() {
260 Err(PaymentError::Invoice("amount unexpected"))
262 self.pay_invoice_using_amount(invoice, Some(amount_msats))
266 fn pay_invoice_using_amount(
267 &self, invoice: &Invoice, amount_msats: Option<u64>
268 ) -> Result<PaymentId, PaymentError> {
269 debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
271 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
272 match self.payment_cache.lock().unwrap().entry(payment_hash) {
273 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
274 hash_map::Entry::Vacant(entry) => entry.insert(0),
277 let payment_secret = Some(invoice.payment_secret().clone());
278 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
279 .with_expiry_time(expiry_time_from_unix_epoch(&invoice).as_secs())
280 .with_route_hints(invoice.route_hints());
281 if let Some(features) = invoice.features() {
282 payee = payee.with_features(features.clone());
284 let params = RouteParameters {
286 final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
287 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
290 let send_payment = |route: &Route| {
291 self.payer.send_payment(route, payment_hash, &payment_secret)
293 self.pay_internal(¶ms, payment_hash, send_payment)
294 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
297 /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
298 /// case a retry is needed.
300 /// You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
301 /// been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
303 &self, pubkey: PublicKey, payment_preimage: PaymentPreimage, amount_msats: u64,
304 final_cltv_expiry_delta: u32
305 ) -> Result<PaymentId, PaymentError> {
306 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
307 match self.payment_cache.lock().unwrap().entry(payment_hash) {
308 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
309 hash_map::Entry::Vacant(entry) => entry.insert(0),
312 let params = RouteParameters {
313 payee: Payee::for_keysend(pubkey),
314 final_value_msat: amount_msats,
315 final_cltv_expiry_delta,
318 let send_payment = |route: &Route| {
319 self.payer.send_spontaneous_payment(route, payment_preimage)
321 self.pay_internal(¶ms, payment_hash, send_payment)
322 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
325 fn pay_internal<F: FnOnce(&Route) -> Result<PaymentId, PaymentSendFailure> + Copy>(
326 &self, params: &RouteParameters, payment_hash: PaymentHash, send_payment: F,
327 ) -> Result<PaymentId, PaymentError> {
328 if has_expired(params) {
329 log_trace!(self.logger, "Invoice expired prior to send for payment {}", log_bytes!(payment_hash.0));
330 return Err(PaymentError::Invoice("Invoice expired prior to send"));
333 let payer = self.payer.node_id();
334 let first_hops = self.payer.first_hops();
335 let route = self.router.find_route(
336 &payer, params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
338 ).map_err(|e| PaymentError::Routing(e))?;
340 match send_payment(&route) {
341 Ok(payment_id) => Ok(payment_id),
343 PaymentSendFailure::ParameterError(_) => Err(e),
344 PaymentSendFailure::PathParameterError(_) => Err(e),
345 PaymentSendFailure::AllFailedRetrySafe(_) => {
346 let mut payment_cache = self.payment_cache.lock().unwrap();
347 let retry_count = payment_cache.get_mut(&payment_hash).unwrap();
348 if *retry_count >= self.retry_attempts.0 {
352 std::mem::drop(payment_cache);
353 Ok(self.pay_internal(params, payment_hash, send_payment)?)
356 PaymentSendFailure::PartialFailure { failed_paths_retry, payment_id, .. } => {
357 if let Some(retry_data) = failed_paths_retry {
358 // Some paths were sent, even if we failed to send the full MPP value our
359 // recipient may misbehave and claim the funds, at which point we have to
360 // consider the payment sent, so return `Ok()` here, ignoring any retry
362 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
365 // This may happen if we send a payment and some paths fail, but
366 // only due to a temporary monitor failure or the like, implying
367 // they're really in-flight, but we haven't sent the initial
368 // HTLC-Add messages yet.
373 }.map_err(|e| PaymentError::Sending(e))
377 &self, payment_id: PaymentId, payment_hash: PaymentHash, params: &RouteParameters
378 ) -> Result<(), ()> {
379 let max_payment_attempts = self.retry_attempts.0 + 1;
380 let attempts = *self.payment_cache.lock().unwrap()
382 .and_modify(|attempts| *attempts += 1)
385 if attempts >= max_payment_attempts {
386 log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
390 if has_expired(params) {
391 log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
395 let payer = self.payer.node_id();
396 let first_hops = self.payer.first_hops();
397 let route = self.router.find_route(
398 &payer, ¶ms, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
402 log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
406 match self.payer.retry_payment(&route.unwrap(), payment_id) {
408 Err(PaymentSendFailure::ParameterError(_)) |
409 Err(PaymentSendFailure::PathParameterError(_)) => {
410 log_trace!(self.logger, "Failed to retry for payment {} due to bogus route/payment data, not retrying.", log_bytes!(payment_hash.0));
413 Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
414 self.retry_payment(payment_id, payment_hash, params)
416 Err(PaymentSendFailure::PartialFailure { failed_paths_retry, .. }) => {
417 if let Some(retry) = failed_paths_retry {
418 // Always return Ok for the same reason as noted in pay_internal.
419 let _ = self.retry_payment(payment_id, payment_hash, &retry);
426 /// Removes the payment cached by the given payment hash.
428 /// Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
429 /// [`EventHandler`]. Otherwise, calling this method is unnecessary.
430 pub fn remove_cached_payment(&self, payment_hash: &PaymentHash) {
431 self.payment_cache.lock().unwrap().remove(payment_hash);
435 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
436 invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
439 fn has_expired(params: &RouteParameters) -> bool {
440 if let Some(expiry_time) = params.payee.expiry_time {
441 Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, Duration::from_secs(expiry_time))
445 impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
448 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
449 S::Target: for <'a> LockableScore<'a>,
453 fn handle_event(&self, event: &Event) {
455 Event::PaymentPathFailed {
456 all_paths_failed, payment_id, payment_hash, rejected_by_dest, path,
457 short_channel_id, retry, ..
459 if let Some(short_channel_id) = short_channel_id {
460 let path = path.iter().collect::<Vec<_>>();
461 self.scorer.lock().payment_path_failed(&path, *short_channel_id);
464 if *rejected_by_dest {
465 log_trace!(self.logger, "Payment {} rejected by destination; not retrying", log_bytes!(payment_hash.0));
466 } else if payment_id.is_none() {
467 log_trace!(self.logger, "Payment {} has no id; not retrying", log_bytes!(payment_hash.0));
468 } else if retry.is_none() {
469 log_trace!(self.logger, "Payment {} missing retry params; not retrying", log_bytes!(payment_hash.0));
470 } else if self.retry_payment(payment_id.unwrap(), *payment_hash, retry.as_ref().unwrap()).is_ok() {
471 // We retried at least somewhat, don't provide the PaymentPathFailed event to the user.
475 if *all_paths_failed { self.payment_cache.lock().unwrap().remove(payment_hash); }
477 Event::PaymentSent { payment_hash, .. } => {
478 let mut payment_cache = self.payment_cache.lock().unwrap();
479 let attempts = payment_cache
480 .remove(payment_hash)
481 .map_or(1, |attempts| attempts + 1);
482 log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
487 // Delegate to the decorated event handler unless the payment is retried.
488 self.event_handler.handle_event(event)
495 use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
496 use utils::create_invoice_from_channelmanager;
497 use bitcoin_hashes::sha256::Hash as Sha256;
498 use lightning::ln::PaymentPreimage;
499 use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
500 use lightning::ln::functional_test_utils::*;
501 use lightning::ln::msgs::{ErrorAction, LightningError};
502 use lightning::routing::network_graph::NodeId;
503 use lightning::routing::router::{Payee, Route, RouteHop};
504 use lightning::util::test_utils::TestLogger;
505 use lightning::util::errors::APIError;
506 use lightning::util::events::{Event, MessageSendEventsProvider};
507 use secp256k1::{SecretKey, PublicKey, Secp256k1};
508 use std::cell::RefCell;
509 use std::collections::VecDeque;
510 use std::time::{SystemTime, Duration};
512 fn 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 InvoiceBuilder::new(Currency::Bitcoin)
516 .description("test".into())
517 .payment_hash(payment_hash)
518 .payment_secret(PaymentSecret([0; 32]))
520 .min_final_cltv_expiry(144)
521 .amount_milli_satoshis(128)
522 .build_signed(|hash| {
523 Secp256k1::new().sign_recoverable(hash, &private_key)
528 fn zero_value_invoice(payment_preimage: PaymentPreimage) -> Invoice {
529 let payment_hash = Sha256::hash(&payment_preimage.0);
530 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
531 InvoiceBuilder::new(Currency::Bitcoin)
532 .description("test".into())
533 .payment_hash(payment_hash)
534 .payment_secret(PaymentSecret([0; 32]))
536 .min_final_cltv_expiry(144)
537 .build_signed(|hash| {
538 Secp256k1::new().sign_recoverable(hash, &private_key)
543 fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
544 let payment_hash = Sha256::hash(&payment_preimage.0);
545 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
546 let timestamp = SystemTime::now()
547 .checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
549 InvoiceBuilder::new(Currency::Bitcoin)
550 .description("test".into())
551 .payment_hash(payment_hash)
552 .payment_secret(PaymentSecret([0; 32]))
553 .timestamp(timestamp)
554 .min_final_cltv_expiry(144)
555 .amount_milli_satoshis(128)
556 .build_signed(|hash| {
557 Secp256k1::new().sign_recoverable(hash, &private_key)
562 fn pubkey() -> PublicKey {
563 PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()
567 fn pays_invoice_on_first_attempt() {
568 let event_handled = core::cell::RefCell::new(false);
569 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
571 let payment_preimage = PaymentPreimage([1; 32]);
572 let invoice = invoice(payment_preimage);
573 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
574 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
576 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
577 let router = TestRouter {};
578 let scorer = RefCell::new(TestScorer::new());
579 let logger = TestLogger::new();
581 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
583 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
584 assert_eq!(*payer.attempts.borrow(), 1);
586 invoice_payer.handle_event(&Event::PaymentSent {
587 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
589 assert_eq!(*event_handled.borrow(), true);
590 assert_eq!(*payer.attempts.borrow(), 1);
594 fn pays_invoice_on_retry() {
595 let event_handled = core::cell::RefCell::new(false);
596 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
598 let payment_preimage = PaymentPreimage([1; 32]);
599 let invoice = invoice(payment_preimage);
600 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
601 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
603 let payer = TestPayer::new()
604 .expect_send(Amount::ForInvoice(final_value_msat))
605 .expect_send(Amount::OnRetry(final_value_msat / 2));
606 let router = TestRouter {};
607 let scorer = RefCell::new(TestScorer::new());
608 let logger = TestLogger::new();
610 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
612 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
613 assert_eq!(*payer.attempts.borrow(), 1);
615 let event = Event::PaymentPathFailed {
618 network_update: None,
619 rejected_by_dest: false,
620 all_paths_failed: false,
621 path: TestRouter::path_for_value(final_value_msat),
622 short_channel_id: None,
623 retry: Some(TestRouter::retry_for_invoice(&invoice)),
625 invoice_payer.handle_event(&event);
626 assert_eq!(*event_handled.borrow(), false);
627 assert_eq!(*payer.attempts.borrow(), 2);
629 invoice_payer.handle_event(&Event::PaymentSent {
630 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
632 assert_eq!(*event_handled.borrow(), true);
633 assert_eq!(*payer.attempts.borrow(), 2);
637 fn pays_invoice_on_partial_failure() {
638 let event_handler = |_: &_| { panic!() };
640 let payment_preimage = PaymentPreimage([1; 32]);
641 let invoice = invoice(payment_preimage);
642 let retry = TestRouter::retry_for_invoice(&invoice);
643 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
645 let payer = TestPayer::new()
646 .fails_with_partial_failure(retry.clone(), OnAttempt(1))
647 .fails_with_partial_failure(retry, OnAttempt(2))
648 .expect_send(Amount::ForInvoice(final_value_msat))
649 .expect_send(Amount::OnRetry(final_value_msat / 2))
650 .expect_send(Amount::OnRetry(final_value_msat / 2));
651 let router = TestRouter {};
652 let scorer = RefCell::new(TestScorer::new());
653 let logger = TestLogger::new();
655 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
657 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
661 fn retries_payment_path_for_unknown_payment() {
662 let event_handled = core::cell::RefCell::new(false);
663 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
665 let payment_preimage = PaymentPreimage([1; 32]);
666 let invoice = invoice(payment_preimage);
667 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
668 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
670 let payer = TestPayer::new()
671 .expect_send(Amount::OnRetry(final_value_msat / 2))
672 .expect_send(Amount::OnRetry(final_value_msat / 2));
673 let router = TestRouter {};
674 let scorer = RefCell::new(TestScorer::new());
675 let logger = TestLogger::new();
677 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
679 let payment_id = Some(PaymentId([1; 32]));
680 let event = Event::PaymentPathFailed {
683 network_update: None,
684 rejected_by_dest: false,
685 all_paths_failed: false,
686 path: TestRouter::path_for_value(final_value_msat),
687 short_channel_id: None,
688 retry: Some(TestRouter::retry_for_invoice(&invoice)),
690 invoice_payer.handle_event(&event);
691 assert_eq!(*event_handled.borrow(), false);
692 assert_eq!(*payer.attempts.borrow(), 1);
694 invoice_payer.handle_event(&event);
695 assert_eq!(*event_handled.borrow(), false);
696 assert_eq!(*payer.attempts.borrow(), 2);
698 invoice_payer.handle_event(&Event::PaymentSent {
699 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
701 assert_eq!(*event_handled.borrow(), true);
702 assert_eq!(*payer.attempts.borrow(), 2);
706 fn fails_paying_invoice_after_max_retries() {
707 let event_handled = core::cell::RefCell::new(false);
708 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
710 let payment_preimage = PaymentPreimage([1; 32]);
711 let invoice = invoice(payment_preimage);
712 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
714 let payer = TestPayer::new()
715 .expect_send(Amount::ForInvoice(final_value_msat))
716 .expect_send(Amount::OnRetry(final_value_msat / 2))
717 .expect_send(Amount::OnRetry(final_value_msat / 2));
718 let router = TestRouter {};
719 let scorer = RefCell::new(TestScorer::new());
720 let logger = TestLogger::new();
722 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
724 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
725 assert_eq!(*payer.attempts.borrow(), 1);
727 let event = Event::PaymentPathFailed {
729 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
730 network_update: None,
731 rejected_by_dest: false,
732 all_paths_failed: true,
733 path: TestRouter::path_for_value(final_value_msat),
734 short_channel_id: None,
735 retry: Some(TestRouter::retry_for_invoice(&invoice)),
737 invoice_payer.handle_event(&event);
738 assert_eq!(*event_handled.borrow(), false);
739 assert_eq!(*payer.attempts.borrow(), 2);
741 let event = Event::PaymentPathFailed {
743 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
744 network_update: None,
745 rejected_by_dest: false,
746 all_paths_failed: false,
747 path: TestRouter::path_for_value(final_value_msat / 2),
748 short_channel_id: None,
749 retry: Some(RouteParameters {
750 final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
753 invoice_payer.handle_event(&event);
754 assert_eq!(*event_handled.borrow(), false);
755 assert_eq!(*payer.attempts.borrow(), 3);
757 invoice_payer.handle_event(&event);
758 assert_eq!(*event_handled.borrow(), true);
759 assert_eq!(*payer.attempts.borrow(), 3);
763 fn fails_paying_invoice_with_missing_retry_params() {
764 let event_handled = core::cell::RefCell::new(false);
765 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
767 let payment_preimage = PaymentPreimage([1; 32]);
768 let invoice = invoice(payment_preimage);
769 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
771 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
772 let router = TestRouter {};
773 let scorer = RefCell::new(TestScorer::new());
774 let logger = TestLogger::new();
776 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
778 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
779 assert_eq!(*payer.attempts.borrow(), 1);
781 let event = Event::PaymentPathFailed {
783 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
784 network_update: None,
785 rejected_by_dest: false,
786 all_paths_failed: false,
788 short_channel_id: None,
791 invoice_payer.handle_event(&event);
792 assert_eq!(*event_handled.borrow(), true);
793 assert_eq!(*payer.attempts.borrow(), 1);
797 fn fails_paying_invoice_after_expiration() {
798 let event_handled = core::cell::RefCell::new(false);
799 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
801 let payer = TestPayer::new();
802 let router = TestRouter {};
803 let scorer = RefCell::new(TestScorer::new());
804 let logger = TestLogger::new();
806 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
808 let payment_preimage = PaymentPreimage([1; 32]);
809 let invoice = expired_invoice(payment_preimage);
810 if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
811 assert_eq!(msg, "Invoice expired prior to send");
812 } else { panic!("Expected Invoice Error"); }
816 fn fails_retrying_invoice_after_expiration() {
817 let event_handled = core::cell::RefCell::new(false);
818 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
820 let payment_preimage = PaymentPreimage([1; 32]);
821 let invoice = invoice(payment_preimage);
822 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
824 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
825 let router = TestRouter {};
826 let scorer = RefCell::new(TestScorer::new());
827 let logger = TestLogger::new();
829 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
831 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
832 assert_eq!(*payer.attempts.borrow(), 1);
834 let mut retry_data = TestRouter::retry_for_invoice(&invoice);
835 retry_data.payee.expiry_time = Some(SystemTime::now()
836 .checked_sub(Duration::from_secs(2)).unwrap()
837 .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
838 let event = Event::PaymentPathFailed {
840 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
841 network_update: None,
842 rejected_by_dest: false,
843 all_paths_failed: false,
845 short_channel_id: None,
846 retry: Some(retry_data),
848 invoice_payer.handle_event(&event);
849 assert_eq!(*event_handled.borrow(), true);
850 assert_eq!(*payer.attempts.borrow(), 1);
854 fn fails_paying_invoice_after_retry_error() {
855 let event_handled = core::cell::RefCell::new(false);
856 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
858 let payment_preimage = PaymentPreimage([1; 32]);
859 let invoice = invoice(payment_preimage);
860 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
862 let payer = TestPayer::new()
864 .expect_send(Amount::ForInvoice(final_value_msat))
865 .expect_send(Amount::OnRetry(final_value_msat / 2));
866 let router = TestRouter {};
867 let scorer = RefCell::new(TestScorer::new());
868 let logger = TestLogger::new();
870 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
872 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
873 assert_eq!(*payer.attempts.borrow(), 1);
875 let event = Event::PaymentPathFailed {
877 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
878 network_update: None,
879 rejected_by_dest: false,
880 all_paths_failed: false,
881 path: TestRouter::path_for_value(final_value_msat / 2),
882 short_channel_id: None,
883 retry: Some(TestRouter::retry_for_invoice(&invoice)),
885 invoice_payer.handle_event(&event);
886 assert_eq!(*event_handled.borrow(), true);
887 assert_eq!(*payer.attempts.borrow(), 2);
891 fn fails_paying_invoice_after_rejected_by_payee() {
892 let event_handled = core::cell::RefCell::new(false);
893 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
895 let payment_preimage = PaymentPreimage([1; 32]);
896 let invoice = invoice(payment_preimage);
897 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
899 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
900 let router = TestRouter {};
901 let scorer = RefCell::new(TestScorer::new());
902 let logger = TestLogger::new();
904 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
906 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
907 assert_eq!(*payer.attempts.borrow(), 1);
909 let event = Event::PaymentPathFailed {
911 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
912 network_update: None,
913 rejected_by_dest: true,
914 all_paths_failed: false,
916 short_channel_id: None,
917 retry: Some(TestRouter::retry_for_invoice(&invoice)),
919 invoice_payer.handle_event(&event);
920 assert_eq!(*event_handled.borrow(), true);
921 assert_eq!(*payer.attempts.borrow(), 1);
925 fn fails_repaying_invoice_with_pending_payment() {
926 let event_handled = core::cell::RefCell::new(false);
927 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
929 let payment_preimage = PaymentPreimage([1; 32]);
930 let invoice = invoice(payment_preimage);
931 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
933 let payer = TestPayer::new()
934 .expect_send(Amount::ForInvoice(final_value_msat))
935 .expect_send(Amount::ForInvoice(final_value_msat));
936 let router = TestRouter {};
937 let scorer = RefCell::new(TestScorer::new());
938 let logger = TestLogger::new();
940 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
942 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
944 // Cannot repay an invoice pending payment.
945 match invoice_payer.pay_invoice(&invoice) {
946 Err(PaymentError::Invoice("payment pending")) => {},
947 Err(_) => panic!("unexpected error"),
948 Ok(_) => panic!("expected invoice error"),
951 // Can repay an invoice once cleared from cache.
952 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
953 invoice_payer.remove_cached_payment(&payment_hash);
954 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
956 // Cannot retry paying an invoice if cleared from cache.
957 invoice_payer.remove_cached_payment(&payment_hash);
958 let event = Event::PaymentPathFailed {
961 network_update: None,
962 rejected_by_dest: false,
963 all_paths_failed: false,
965 short_channel_id: None,
966 retry: Some(TestRouter::retry_for_invoice(&invoice)),
968 invoice_payer.handle_event(&event);
969 assert_eq!(*event_handled.borrow(), true);
973 fn fails_paying_invoice_with_routing_errors() {
974 let payer = TestPayer::new();
975 let router = FailingRouter {};
976 let scorer = RefCell::new(TestScorer::new());
977 let logger = TestLogger::new();
979 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
981 let payment_preimage = PaymentPreimage([1; 32]);
982 let invoice = invoice(payment_preimage);
983 match invoice_payer.pay_invoice(&invoice) {
984 Err(PaymentError::Routing(_)) => {},
985 Err(_) => panic!("unexpected error"),
986 Ok(_) => panic!("expected routing error"),
991 fn fails_paying_invoice_with_sending_errors() {
992 let payment_preimage = PaymentPreimage([1; 32]);
993 let invoice = invoice(payment_preimage);
994 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
996 let payer = TestPayer::new()
998 .expect_send(Amount::ForInvoice(final_value_msat));
999 let router = TestRouter {};
1000 let scorer = RefCell::new(TestScorer::new());
1001 let logger = TestLogger::new();
1003 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
1005 match invoice_payer.pay_invoice(&invoice) {
1006 Err(PaymentError::Sending(_)) => {},
1007 Err(_) => panic!("unexpected error"),
1008 Ok(_) => panic!("expected sending error"),
1013 fn pays_zero_value_invoice_using_amount() {
1014 let event_handled = core::cell::RefCell::new(false);
1015 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1017 let payment_preimage = PaymentPreimage([1; 32]);
1018 let invoice = zero_value_invoice(payment_preimage);
1019 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1020 let final_value_msat = 100;
1022 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1023 let router = TestRouter {};
1024 let scorer = RefCell::new(TestScorer::new());
1025 let logger = TestLogger::new();
1027 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1030 Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
1031 assert_eq!(*payer.attempts.borrow(), 1);
1033 invoice_payer.handle_event(&Event::PaymentSent {
1034 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1036 assert_eq!(*event_handled.borrow(), true);
1037 assert_eq!(*payer.attempts.borrow(), 1);
1041 fn fails_paying_zero_value_invoice_with_amount() {
1042 let event_handled = core::cell::RefCell::new(false);
1043 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1045 let payer = TestPayer::new();
1046 let router = TestRouter {};
1047 let scorer = RefCell::new(TestScorer::new());
1048 let logger = TestLogger::new();
1050 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1052 let payment_preimage = PaymentPreimage([1; 32]);
1053 let invoice = invoice(payment_preimage);
1055 // Cannot repay an invoice pending payment.
1056 match invoice_payer.pay_zero_value_invoice(&invoice, 100) {
1057 Err(PaymentError::Invoice("amount unexpected")) => {},
1058 Err(_) => panic!("unexpected error"),
1059 Ok(_) => panic!("expected invoice error"),
1064 fn pays_pubkey_with_amount() {
1065 let event_handled = core::cell::RefCell::new(false);
1066 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1068 let pubkey = pubkey();
1069 let payment_preimage = PaymentPreimage([1; 32]);
1070 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
1071 let final_value_msat = 100;
1072 let final_cltv_expiry_delta = 42;
1074 let payer = TestPayer::new()
1075 .expect_send(Amount::Spontaneous(final_value_msat))
1076 .expect_send(Amount::OnRetry(final_value_msat));
1077 let router = TestRouter {};
1078 let scorer = RefCell::new(TestScorer::new());
1079 let logger = TestLogger::new();
1081 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1083 let payment_id = Some(invoice_payer.pay_pubkey(
1084 pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
1086 assert_eq!(*payer.attempts.borrow(), 1);
1088 let retry = RouteParameters {
1089 payee: Payee::for_keysend(pubkey),
1091 final_cltv_expiry_delta,
1093 let event = Event::PaymentPathFailed {
1096 network_update: None,
1097 rejected_by_dest: false,
1098 all_paths_failed: false,
1100 short_channel_id: None,
1103 invoice_payer.handle_event(&event);
1104 assert_eq!(*event_handled.borrow(), false);
1105 assert_eq!(*payer.attempts.borrow(), 2);
1107 invoice_payer.handle_event(&Event::PaymentSent {
1108 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1110 assert_eq!(*event_handled.borrow(), true);
1111 assert_eq!(*payer.attempts.borrow(), 2);
1115 fn scores_failed_channel() {
1116 let event_handled = core::cell::RefCell::new(false);
1117 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1119 let payment_preimage = PaymentPreimage([1; 32]);
1120 let invoice = invoice(payment_preimage);
1121 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1122 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1123 let path = TestRouter::path_for_value(final_value_msat);
1124 let short_channel_id = Some(path[0].short_channel_id);
1126 // Expect that scorer is given short_channel_id upon handling the event.
1127 let payer = TestPayer::new()
1128 .expect_send(Amount::ForInvoice(final_value_msat))
1129 .expect_send(Amount::OnRetry(final_value_msat / 2));
1130 let router = TestRouter {};
1131 let scorer = RefCell::new(TestScorer::new().expect_channel_failure(short_channel_id.unwrap()));
1132 let logger = TestLogger::new();
1134 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1136 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
1137 let event = Event::PaymentPathFailed {
1140 network_update: None,
1141 rejected_by_dest: false,
1142 all_paths_failed: false,
1145 retry: Some(TestRouter::retry_for_invoice(&invoice)),
1147 invoice_payer.handle_event(&event);
1153 fn route_for_value(final_value_msat: u64) -> Route {
1157 pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1158 channel_features: ChannelFeatures::empty(),
1159 node_features: NodeFeatures::empty(),
1160 short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1163 pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1164 channel_features: ChannelFeatures::empty(),
1165 node_features: NodeFeatures::empty(),
1166 short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1173 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1174 TestRouter::route_for_value(final_value_msat).paths[0].clone()
1177 fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
1178 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
1179 .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
1180 .with_route_hints(invoice.route_hints());
1181 if let Some(features) = invoice.features() {
1182 payee = payee.with_features(features.clone());
1184 let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1188 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1193 impl<S: Score> Router<S> for TestRouter {
1195 &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
1196 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1197 ) -> Result<Route, LightningError> {
1199 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1204 struct FailingRouter;
1206 impl<S: Score> Router<S> for FailingRouter {
1208 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1209 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1210 ) -> Result<Route, LightningError> {
1211 Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
1216 expectations: VecDeque<u64>,
1222 expectations: VecDeque::new(),
1226 fn expect_channel_failure(mut self, short_channel_id: u64) -> Self {
1227 self.expectations.push_back(short_channel_id);
1233 impl lightning::util::ser::Writeable for TestScorer {
1234 fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); }
1236 impl Score for TestScorer {
1237 fn channel_penalty_msat(
1238 &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
1241 fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
1242 if let Some(expected_short_channel_id) = self.expectations.pop_front() {
1243 assert_eq!(short_channel_id, expected_short_channel_id);
1248 impl Drop for TestScorer {
1249 fn drop(&mut self) {
1250 if std::thread::panicking() {
1254 if !self.expectations.is_empty() {
1255 panic!("Unsatisfied channel failure expectations: {:?}", self.expectations);
1261 expectations: core::cell::RefCell<VecDeque<Amount>>,
1262 attempts: core::cell::RefCell<usize>,
1263 failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
1266 #[derive(Clone, Debug, PartialEq, Eq)]
1273 struct OnAttempt(usize);
1278 expectations: core::cell::RefCell::new(VecDeque::new()),
1279 attempts: core::cell::RefCell::new(0),
1280 failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
1284 fn expect_send(self, value_msat: Amount) -> Self {
1285 self.expectations.borrow_mut().push_back(value_msat);
1289 fn fails_on_attempt(self, attempt: usize) -> Self {
1290 let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
1291 self.fails_with(failure, OnAttempt(attempt))
1294 fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
1295 self.fails_with(PaymentSendFailure::PartialFailure {
1297 failed_paths_retry: Some(retry),
1298 payment_id: PaymentId([1; 32]),
1302 fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
1303 self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
1307 fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
1308 let mut attempts = self.attempts.borrow_mut();
1311 match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1312 Some(failure) => Err(failure),
1313 None => Ok(PaymentId([1; 32])),
1317 fn check_value_msats(&self, actual_value_msats: Amount) {
1318 let expected_value_msats = self.expectations.borrow_mut().pop_front();
1319 if let Some(expected_value_msats) = expected_value_msats {
1320 assert_eq!(actual_value_msats, expected_value_msats);
1322 panic!("Unexpected amount: {:?}", actual_value_msats);
1327 impl Drop for TestPayer {
1328 fn drop(&mut self) {
1329 if std::thread::panicking() {
1333 if !self.expectations.borrow().is_empty() {
1334 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1339 impl Payer for TestPayer {
1340 fn node_id(&self) -> PublicKey {
1341 let secp_ctx = Secp256k1::new();
1342 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
1345 fn first_hops(&self) -> Vec<ChannelDetails> {
1350 &self, route: &Route, _payment_hash: PaymentHash,
1351 _payment_secret: &Option<PaymentSecret>
1352 ) -> Result<PaymentId, PaymentSendFailure> {
1353 self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1354 self.check_attempts()
1357 fn send_spontaneous_payment(
1358 &self, route: &Route, _payment_preimage: PaymentPreimage,
1359 ) -> Result<PaymentId, PaymentSendFailure> {
1360 self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1361 self.check_attempts()
1365 &self, route: &Route, _payment_id: PaymentId
1366 ) -> Result<(), PaymentSendFailure> {
1367 self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1368 self.check_attempts().map(|_| ())
1372 // *** Full Featured Functional Tests with a Real ChannelManager ***
1373 struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1375 impl<S: Score> Router<S> for ManualRouter {
1377 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1378 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1379 ) -> Result<Route, LightningError> {
1380 self.0.borrow_mut().pop_front().unwrap()
1384 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1385 self.0.borrow_mut().push_back(result);
1388 impl Drop for ManualRouter {
1389 fn drop(&mut self) {
1390 if std::thread::panicking() {
1393 assert!(self.0.borrow_mut().is_empty());
1398 fn retry_multi_path_single_failed_payment() {
1399 // Tests that we can/will retry after a single path of an MPP payment failed immediately
1400 let chanmon_cfgs = create_chanmon_cfgs(2);
1401 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1402 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1403 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1405 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1406 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1407 let chans = nodes[0].node.list_usable_channels();
1408 let mut route = Route {
1411 pubkey: nodes[1].node.get_our_node_id(),
1412 node_features: NodeFeatures::known(),
1413 short_channel_id: chans[0].short_channel_id.unwrap(),
1414 channel_features: ChannelFeatures::known(),
1416 cltv_expiry_delta: 100,
1419 pubkey: nodes[1].node.get_our_node_id(),
1420 node_features: NodeFeatures::known(),
1421 short_channel_id: chans[1].short_channel_id.unwrap(),
1422 channel_features: ChannelFeatures::known(),
1423 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1424 cltv_expiry_delta: 100,
1427 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1429 let router = ManualRouter(RefCell::new(VecDeque::new()));
1430 router.expect_find_route(Ok(route.clone()));
1431 // On retry, split the payment across both channels.
1432 route.paths[0][0].fee_msat = 50_000_001;
1433 route.paths[1][0].fee_msat = 50_000_000;
1434 router.expect_find_route(Ok(route.clone()));
1436 let event_handler = |_: &_| { panic!(); };
1437 let scorer = RefCell::new(TestScorer::new());
1438 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1440 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1441 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1443 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1444 assert_eq!(htlc_msgs.len(), 2);
1445 check_added_monitors!(nodes[0], 2);
1449 fn immediate_retry_on_failure() {
1450 // Tests that we can/will retry immediately after a failure
1451 let chanmon_cfgs = create_chanmon_cfgs(2);
1452 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1453 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1454 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1456 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1457 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1458 let chans = nodes[0].node.list_usable_channels();
1459 let mut route = Route {
1462 pubkey: nodes[1].node.get_our_node_id(),
1463 node_features: NodeFeatures::known(),
1464 short_channel_id: chans[0].short_channel_id.unwrap(),
1465 channel_features: ChannelFeatures::known(),
1466 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1467 cltv_expiry_delta: 100,
1470 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1472 let router = ManualRouter(RefCell::new(VecDeque::new()));
1473 router.expect_find_route(Ok(route.clone()));
1474 // On retry, split the payment across both channels.
1475 route.paths.push(route.paths[0].clone());
1476 route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
1477 route.paths[0][0].fee_msat = 50_000_000;
1478 route.paths[1][0].fee_msat = 50_000_001;
1479 router.expect_find_route(Ok(route.clone()));
1481 let event_handler = |_: &_| { panic!(); };
1482 let scorer = RefCell::new(TestScorer::new());
1483 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1485 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1486 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1488 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1489 assert_eq!(htlc_msgs.len(), 2);
1490 check_added_monitors!(nodes[0], 2);