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.
12 //! Defines an [`InvoicePayer`] utility for paying invoices, 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`].
17 //! [`InvoicePayer`] is capable of retrying failed payments. It accomplishes this by implementing
18 //! [`EventHandler`] which decorates a user-provided handler. It will intercept any
19 //! [`Event::PaymentPathFailed`] events and retry the failed paths for a fixed number of total
20 //! attempts or until retry is no longer possible. In such a situation, [`InvoicePayer`] will pass
21 //! along the events to the user-provided handler.
26 //! # extern crate lightning;
27 //! # extern crate lightning_invoice;
28 //! # extern crate secp256k1;
30 //! # use lightning::ln::{PaymentHash, PaymentSecret};
31 //! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
32 //! # use lightning::ln::msgs::LightningError;
33 //! # use lightning::routing;
34 //! # use lightning::routing::network_graph::NodeId;
35 //! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
36 //! # use lightning::util::events::{Event, EventHandler, EventsProvider};
37 //! # use lightning::util::logger::{Logger, Record};
38 //! # use lightning_invoice::Invoice;
39 //! # use lightning_invoice::payment::{InvoicePayer, Payer, RetryAttempts, Router};
40 //! # use secp256k1::key::PublicKey;
41 //! # use std::cell::RefCell;
42 //! # use std::ops::Deref;
44 //! # struct FakeEventProvider {}
45 //! # impl EventsProvider for FakeEventProvider {
46 //! # fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {}
49 //! # struct FakePayer {}
50 //! # impl Payer for FakePayer {
51 //! # fn node_id(&self) -> PublicKey { unimplemented!() }
52 //! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
53 //! # fn send_payment(
54 //! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
55 //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
56 //! # fn retry_payment(
57 //! # &self, route: &Route, payment_id: PaymentId
58 //! # ) -> Result<(), PaymentSendFailure> { unimplemented!() }
61 //! # struct FakeRouter {};
62 //! # impl<S: routing::Score> Router<S> for FakeRouter {
64 //! # &self, payer: &PublicKey, params: &RouteParameters,
65 //! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
66 //! # ) -> Result<Route, LightningError> { unimplemented!() }
69 //! # struct FakeScorer {};
70 //! # impl routing::Score for FakeScorer {
71 //! # fn channel_penalty_msat(
72 //! # &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
74 //! # fn payment_path_failed(&mut self, _path: &Vec<RouteHop>, _short_channel_id: u64) {}
77 //! # struct FakeLogger {};
78 //! # impl Logger for FakeLogger {
79 //! # fn log(&self, record: &Record) { unimplemented!() }
83 //! let event_handler = |event: &Event| {
85 //! Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
86 //! Event::PaymentSent { .. } => println!("payment successful"),
90 //! # let payer = FakePayer {};
91 //! # let router = FakeRouter {};
92 //! # let scorer = RefCell::new(FakeScorer {});
93 //! # let logger = FakeLogger {};
94 //! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
96 //! let invoice = "...";
97 //! let invoice = invoice.parse::<Invoice>().unwrap();
98 //! invoice_payer.pay_invoice(&invoice).unwrap();
100 //! # let event_provider = FakeEventProvider {};
102 //! event_provider.process_pending_events(&invoice_payer);
109 //! The [`Route`] is computed before each payment attempt. Any updates affecting path finding such
110 //! as updates to the network graph or changes to channel scores should be applied prior to
111 //! retries, typically by way of composing [`EventHandler`]s accordingly.
115 use bitcoin_hashes::Hash;
117 use lightning::ln::{PaymentHash, PaymentSecret};
118 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
119 use lightning::ln::msgs::LightningError;
120 use lightning::routing;
121 use lightning::routing::{LockableScore, Score};
122 use lightning::routing::router::{Payee, Route, RouteParameters};
123 use lightning::util::events::{Event, EventHandler};
124 use lightning::util::logger::Logger;
126 use secp256k1::key::PublicKey;
128 use std::collections::hash_map::{self, HashMap};
130 use std::sync::Mutex;
131 use std::time::{Duration, SystemTime};
133 /// A utility for paying [`Invoice]`s.
134 pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
137 R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
138 S::Target: for <'a> routing::LockableScore<'a>,
147 payment_cache: Mutex<HashMap<PaymentHash, usize>>,
148 retry_attempts: RetryAttempts,
151 /// A trait defining behavior of an [`Invoice`] payer.
153 /// Returns the payer's node id.
154 fn node_id(&self) -> PublicKey;
156 /// Returns the payer's channels.
157 fn first_hops(&self) -> Vec<ChannelDetails>;
159 /// Sends a payment over the Lightning Network using the given [`Route`].
161 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
162 ) -> Result<PaymentId, PaymentSendFailure>;
164 /// Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
165 fn retry_payment(&self, route: &Route, payment_id: PaymentId) -> Result<(), PaymentSendFailure>;
168 /// A trait defining behavior for routing an [`Invoice`] payment.
169 pub trait Router<S: routing::Score> {
170 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
172 &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>,
174 ) -> Result<Route, LightningError>;
177 /// Number of attempts to retry payment path failures for an [`Invoice`].
179 /// Note that this is the number of *path* failures, not full payment retries. For multi-path
180 /// payments, if this is less than the total number of paths, we will never even retry all of the
182 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
183 pub struct RetryAttempts(pub usize);
185 /// An error that may occur when making a payment.
186 #[derive(Clone, Debug)]
187 pub enum PaymentError {
188 /// An error resulting from the provided [`Invoice`] or payment hash.
189 Invoice(&'static str),
190 /// An error occurring when finding a route.
191 Routing(LightningError),
192 /// An error occurring when sending a payment.
193 Sending(PaymentSendFailure),
196 impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
199 R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
200 S::Target: for <'a> routing::LockableScore<'a>,
204 /// Creates an invoice payer that retries failed payment paths.
206 /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
207 /// `retry_attempts` has been exceeded for a given [`Invoice`].
209 payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
217 payment_cache: Mutex::new(HashMap::new()),
222 /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
224 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
225 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
227 pub fn pay_invoice(&self, invoice: &Invoice) -> Result<PaymentId, PaymentError> {
228 if invoice.amount_milli_satoshis().is_none() {
229 Err(PaymentError::Invoice("amount missing"))
231 self.pay_invoice_internal(invoice, None, 0)
235 /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
236 /// case a retry is needed.
238 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
239 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
241 pub fn pay_zero_value_invoice(
242 &self, invoice: &Invoice, amount_msats: u64
243 ) -> Result<PaymentId, PaymentError> {
244 if invoice.amount_milli_satoshis().is_some() {
245 Err(PaymentError::Invoice("amount unexpected"))
247 self.pay_invoice_internal(invoice, Some(amount_msats), 0)
251 fn pay_invoice_internal(
252 &self, invoice: &Invoice, amount_msats: Option<u64>, retry_count: usize
253 ) -> Result<PaymentId, PaymentError> {
254 debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
255 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
256 let retry_data_payment_id = loop {
257 let mut payment_cache = self.payment_cache.lock().unwrap();
258 match payment_cache.entry(payment_hash) {
259 hash_map::Entry::Vacant(entry) => {
260 let payer = self.payer.node_id();
261 let mut payee = Payee::new(invoice.recover_payee_pub_key())
262 .with_expiry_time(expiry_time_from_unix_epoch(&invoice).as_secs())
263 .with_route_hints(invoice.route_hints());
264 if let Some(features) = invoice.features() {
265 payee = payee.with_features(features.clone());
267 let params = RouteParameters {
269 final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
270 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
272 let first_hops = self.payer.first_hops();
273 let route = self.router.find_route(
276 Some(&first_hops.iter().collect::<Vec<_>>()),
278 ).map_err(|e| PaymentError::Routing(e))?;
280 let payment_secret = Some(invoice.payment_secret().clone());
281 let payment_id = match self.payer.send_payment(&route, payment_hash, &payment_secret) {
282 Ok(payment_id) => payment_id,
283 Err(PaymentSendFailure::ParameterError(e)) =>
284 return Err(PaymentError::Sending(PaymentSendFailure::ParameterError(e))),
285 Err(PaymentSendFailure::PathParameterError(e)) =>
286 return Err(PaymentError::Sending(PaymentSendFailure::PathParameterError(e))),
287 Err(PaymentSendFailure::AllFailedRetrySafe(e)) => {
288 if retry_count >= self.retry_attempts.0 {
289 return Err(PaymentError::Sending(PaymentSendFailure::AllFailedRetrySafe(e)))
293 Err(PaymentSendFailure::PartialFailure { results: _, failed_paths_retry, payment_id }) => {
294 if let Some(retry_data) = failed_paths_retry {
295 entry.insert(retry_count);
296 break Some((retry_data, payment_id));
298 // This may happen if we send a payment and some paths fail, but
299 // only due to a temporary monitor failure or the like, implying
300 // they're really in-flight, but we haven't sent the initial
301 // HTLC-Add messages yet.
306 entry.insert(retry_count);
307 return Ok(payment_id);
309 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
312 if let Some((retry_data, payment_id)) = retry_data_payment_id {
313 // Some paths were sent, even if we failed to send the full MPP value our recipient may
314 // misbehave and claim the funds, at which point we have to consider the payment sent,
315 // so return `Ok()` here, ignoring any retry errors.
316 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
319 self.pay_invoice_internal(invoice, amount_msats, retry_count + 1)
323 fn retry_payment(&self, payment_id: PaymentId, payment_hash: PaymentHash, params: &RouteParameters)
327 let mut payment_cache = self.payment_cache.lock().unwrap();
329 let entry = payment_cache.entry(payment_hash);
331 hash_map::Entry::Occupied(_) => break entry,
332 hash_map::Entry::Vacant(entry) => entry.insert(0),
335 if let hash_map::Entry::Occupied(mut entry) = entry {
336 let max_payment_attempts = self.retry_attempts.0 + 1;
337 let attempts = entry.get_mut();
340 if *attempts >= max_payment_attempts {
341 log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
343 } else if has_expired(params) {
344 log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
348 let payer = self.payer.node_id();
349 let first_hops = self.payer.first_hops();
350 route = self.router.find_route(&payer, ¶ms, Some(&first_hops.iter().collect::<Vec<_>>()), &self.scorer.lock());
352 log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
360 let retry_res = self.payer.retry_payment(&route.unwrap(), payment_id);
363 Err(PaymentSendFailure::ParameterError(_)) |
364 Err(PaymentSendFailure::PathParameterError(_)) => {
365 log_trace!(self.logger, "Failed to retry for payment {} due to bogus route/payment data, not retrying.", log_bytes!(payment_hash.0));
368 Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
369 self.retry_payment(payment_id, payment_hash, params)
371 Err(PaymentSendFailure::PartialFailure { results: _, failed_paths_retry, .. }) => {
372 if let Some(retry) = failed_paths_retry {
373 self.retry_payment(payment_id, payment_hash, &retry)
381 /// Removes the payment cached by the given payment hash.
383 /// Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
384 /// [`EventHandler`]. Otherwise, calling this method is unnecessary.
385 pub fn remove_cached_payment(&self, payment_hash: &PaymentHash) {
386 self.payment_cache.lock().unwrap().remove(payment_hash);
390 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
391 invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
394 fn has_expired(params: &RouteParameters) -> bool {
395 if let Some(expiry_time) = params.payee.expiry_time {
396 Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, Duration::from_secs(expiry_time))
400 impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
403 R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
404 S::Target: for <'a> routing::LockableScore<'a>,
408 fn handle_event(&self, event: &Event) {
410 Event::PaymentPathFailed {
411 all_paths_failed, payment_id, payment_hash, rejected_by_dest, path, short_channel_id, retry, ..
413 if let Some(short_channel_id) = short_channel_id {
414 self.scorer.lock().payment_path_failed(path, *short_channel_id);
417 if *rejected_by_dest {
418 log_trace!(self.logger, "Payment {} rejected by destination; not retrying", log_bytes!(payment_hash.0));
419 } else if payment_id.is_none() {
420 log_trace!(self.logger, "Payment {} has no id; not retrying", log_bytes!(payment_hash.0));
421 } else if let Some(params) = retry {
422 if self.retry_payment(payment_id.unwrap(), *payment_hash, params).is_ok() {
423 // We retried at least somewhat, don't provide the PaymentPathFailed event to the user.
427 log_trace!(self.logger, "Payment {} missing retry params; not retrying", log_bytes!(payment_hash.0));
429 if *all_paths_failed { self.payment_cache.lock().unwrap().remove(payment_hash); }
431 Event::PaymentSent { payment_hash, .. } => {
432 let mut payment_cache = self.payment_cache.lock().unwrap();
433 let attempts = payment_cache
434 .remove(payment_hash)
435 .map_or(1, |attempts| attempts + 1);
436 log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
441 // Delegate to the decorated event handler unless the payment is retried.
442 self.event_handler.handle_event(event)
449 use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
450 use utils::create_invoice_from_channelmanager;
451 use bitcoin_hashes::sha256::Hash as Sha256;
452 use lightning::ln::PaymentPreimage;
453 use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
454 use lightning::ln::functional_test_utils::*;
455 use lightning::ln::msgs::{ErrorAction, LightningError};
456 use lightning::routing::network_graph::NodeId;
457 use lightning::routing::router::{Payee, Route, RouteHop};
458 use lightning::util::test_utils::TestLogger;
459 use lightning::util::errors::APIError;
460 use lightning::util::events::{Event, MessageSendEventsProvider};
461 use secp256k1::{SecretKey, PublicKey, Secp256k1};
462 use std::cell::RefCell;
463 use std::collections::VecDeque;
464 use std::time::{SystemTime, Duration};
466 fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
467 let payment_hash = Sha256::hash(&payment_preimage.0);
468 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
469 InvoiceBuilder::new(Currency::Bitcoin)
470 .description("test".into())
471 .payment_hash(payment_hash)
472 .payment_secret(PaymentSecret([0; 32]))
474 .min_final_cltv_expiry(144)
475 .amount_milli_satoshis(128)
476 .build_signed(|hash| {
477 Secp256k1::new().sign_recoverable(hash, &private_key)
482 fn zero_value_invoice(payment_preimage: PaymentPreimage) -> Invoice {
483 let payment_hash = Sha256::hash(&payment_preimage.0);
484 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
485 InvoiceBuilder::new(Currency::Bitcoin)
486 .description("test".into())
487 .payment_hash(payment_hash)
488 .payment_secret(PaymentSecret([0; 32]))
490 .min_final_cltv_expiry(144)
491 .build_signed(|hash| {
492 Secp256k1::new().sign_recoverable(hash, &private_key)
497 fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
498 let payment_hash = Sha256::hash(&payment_preimage.0);
499 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
500 let timestamp = SystemTime::now()
501 .checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
503 InvoiceBuilder::new(Currency::Bitcoin)
504 .description("test".into())
505 .payment_hash(payment_hash)
506 .payment_secret(PaymentSecret([0; 32]))
507 .timestamp(timestamp)
508 .min_final_cltv_expiry(144)
509 .amount_milli_satoshis(128)
510 .build_signed(|hash| {
511 Secp256k1::new().sign_recoverable(hash, &private_key)
517 fn pays_invoice_on_first_attempt() {
518 let event_handled = core::cell::RefCell::new(false);
519 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
521 let payment_preimage = PaymentPreimage([1; 32]);
522 let invoice = invoice(payment_preimage);
523 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
525 let payer = TestPayer::new();
526 let router = TestRouter {};
527 let scorer = RefCell::new(TestScorer::new());
528 let logger = TestLogger::new();
530 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
532 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
533 assert_eq!(*payer.attempts.borrow(), 1);
535 invoice_payer.handle_event(&Event::PaymentSent {
536 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
538 assert_eq!(*event_handled.borrow(), true);
539 assert_eq!(*payer.attempts.borrow(), 1);
543 fn pays_invoice_on_retry() {
544 let event_handled = core::cell::RefCell::new(false);
545 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
547 let payment_preimage = PaymentPreimage([1; 32]);
548 let invoice = invoice(payment_preimage);
549 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
550 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
552 let payer = TestPayer::new()
553 .expect_value_msat(final_value_msat)
554 .expect_value_msat(final_value_msat / 2);
555 let router = TestRouter {};
556 let scorer = RefCell::new(TestScorer::new());
557 let logger = TestLogger::new();
559 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
561 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
562 assert_eq!(*payer.attempts.borrow(), 1);
564 let event = Event::PaymentPathFailed {
567 network_update: None,
568 rejected_by_dest: false,
569 all_paths_failed: false,
570 path: TestRouter::path_for_value(final_value_msat),
571 short_channel_id: None,
572 retry: Some(TestRouter::retry_for_invoice(&invoice)),
574 invoice_payer.handle_event(&event);
575 assert_eq!(*event_handled.borrow(), false);
576 assert_eq!(*payer.attempts.borrow(), 2);
578 invoice_payer.handle_event(&Event::PaymentSent {
579 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
581 assert_eq!(*event_handled.borrow(), true);
582 assert_eq!(*payer.attempts.borrow(), 2);
586 fn retries_payment_path_for_unknown_payment() {
587 let event_handled = core::cell::RefCell::new(false);
588 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
590 let payment_preimage = PaymentPreimage([1; 32]);
591 let invoice = invoice(payment_preimage);
592 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
593 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
595 let payer = TestPayer::new();
596 let router = TestRouter {};
597 let scorer = RefCell::new(TestScorer::new());
598 let logger = TestLogger::new();
600 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
602 let payment_id = Some(PaymentId([1; 32]));
603 let event = Event::PaymentPathFailed {
606 network_update: None,
607 rejected_by_dest: false,
608 all_paths_failed: false,
609 path: TestRouter::path_for_value(final_value_msat),
610 short_channel_id: None,
611 retry: Some(TestRouter::retry_for_invoice(&invoice)),
613 invoice_payer.handle_event(&event);
614 assert_eq!(*event_handled.borrow(), false);
615 assert_eq!(*payer.attempts.borrow(), 1);
617 invoice_payer.handle_event(&event);
618 assert_eq!(*event_handled.borrow(), false);
619 assert_eq!(*payer.attempts.borrow(), 2);
621 invoice_payer.handle_event(&Event::PaymentSent {
622 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
624 assert_eq!(*event_handled.borrow(), true);
625 assert_eq!(*payer.attempts.borrow(), 2);
629 fn fails_paying_invoice_after_max_retries() {
630 let event_handled = core::cell::RefCell::new(false);
631 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
633 let payment_preimage = PaymentPreimage([1; 32]);
634 let invoice = invoice(payment_preimage);
635 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
637 let payer = TestPayer::new()
638 .expect_value_msat(final_value_msat)
639 .expect_value_msat(final_value_msat / 2)
640 .expect_value_msat(final_value_msat / 2);
641 let router = TestRouter {};
642 let scorer = RefCell::new(TestScorer::new());
643 let logger = TestLogger::new();
645 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
647 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
648 assert_eq!(*payer.attempts.borrow(), 1);
650 let event = Event::PaymentPathFailed {
652 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
653 network_update: None,
654 rejected_by_dest: false,
655 all_paths_failed: true,
656 path: TestRouter::path_for_value(final_value_msat),
657 short_channel_id: None,
658 retry: Some(TestRouter::retry_for_invoice(&invoice)),
660 invoice_payer.handle_event(&event);
661 assert_eq!(*event_handled.borrow(), false);
662 assert_eq!(*payer.attempts.borrow(), 2);
664 let event = Event::PaymentPathFailed {
666 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
667 network_update: None,
668 rejected_by_dest: false,
669 all_paths_failed: false,
670 path: TestRouter::path_for_value(final_value_msat / 2),
671 short_channel_id: None,
672 retry: Some(RouteParameters {
673 final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
676 invoice_payer.handle_event(&event);
677 assert_eq!(*event_handled.borrow(), false);
678 assert_eq!(*payer.attempts.borrow(), 3);
680 invoice_payer.handle_event(&event);
681 assert_eq!(*event_handled.borrow(), true);
682 assert_eq!(*payer.attempts.borrow(), 3);
686 fn fails_paying_invoice_with_missing_retry_params() {
687 let event_handled = core::cell::RefCell::new(false);
688 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
690 let payer = TestPayer::new();
691 let router = TestRouter {};
692 let scorer = RefCell::new(TestScorer::new());
693 let logger = TestLogger::new();
695 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
697 let payment_preimage = PaymentPreimage([1; 32]);
698 let invoice = invoice(payment_preimage);
699 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
700 assert_eq!(*payer.attempts.borrow(), 1);
702 let event = Event::PaymentPathFailed {
704 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
705 network_update: None,
706 rejected_by_dest: false,
707 all_paths_failed: false,
709 short_channel_id: None,
712 invoice_payer.handle_event(&event);
713 assert_eq!(*event_handled.borrow(), true);
714 assert_eq!(*payer.attempts.borrow(), 1);
718 fn fails_paying_invoice_after_expiration() {
719 let event_handled = core::cell::RefCell::new(false);
720 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
722 let payer = TestPayer::new();
723 let router = TestRouter {};
724 let scorer = RefCell::new(TestScorer::new());
725 let logger = TestLogger::new();
727 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
729 let payment_preimage = PaymentPreimage([1; 32]);
730 let invoice = expired_invoice(payment_preimage);
731 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
732 assert_eq!(*payer.attempts.borrow(), 1);
734 let event = Event::PaymentPathFailed {
736 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
737 network_update: None,
738 rejected_by_dest: false,
739 all_paths_failed: false,
741 short_channel_id: None,
742 retry: Some(TestRouter::retry_for_invoice(&invoice)),
744 invoice_payer.handle_event(&event);
745 assert_eq!(*event_handled.borrow(), true);
746 assert_eq!(*payer.attempts.borrow(), 1);
750 fn fails_paying_invoice_after_retry_error() {
751 let event_handled = core::cell::RefCell::new(false);
752 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
754 let payment_preimage = PaymentPreimage([1; 32]);
755 let invoice = invoice(payment_preimage);
756 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
758 let payer = TestPayer::new()
760 .expect_value_msat(final_value_msat);
761 let router = TestRouter {};
762 let scorer = RefCell::new(TestScorer::new());
763 let logger = TestLogger::new();
765 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
767 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
768 assert_eq!(*payer.attempts.borrow(), 1);
770 let event = Event::PaymentPathFailed {
772 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
773 network_update: None,
774 rejected_by_dest: false,
775 all_paths_failed: false,
776 path: TestRouter::path_for_value(final_value_msat / 2),
777 short_channel_id: None,
778 retry: Some(TestRouter::retry_for_invoice(&invoice)),
780 invoice_payer.handle_event(&event);
781 assert_eq!(*event_handled.borrow(), true);
782 assert_eq!(*payer.attempts.borrow(), 2);
786 fn fails_paying_invoice_after_rejected_by_payee() {
787 let event_handled = core::cell::RefCell::new(false);
788 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
790 let payer = TestPayer::new();
791 let router = TestRouter {};
792 let scorer = RefCell::new(TestScorer::new());
793 let logger = TestLogger::new();
795 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
797 let payment_preimage = PaymentPreimage([1; 32]);
798 let invoice = invoice(payment_preimage);
799 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
800 assert_eq!(*payer.attempts.borrow(), 1);
802 let event = Event::PaymentPathFailed {
804 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
805 network_update: None,
806 rejected_by_dest: true,
807 all_paths_failed: false,
809 short_channel_id: None,
810 retry: Some(TestRouter::retry_for_invoice(&invoice)),
812 invoice_payer.handle_event(&event);
813 assert_eq!(*event_handled.borrow(), true);
814 assert_eq!(*payer.attempts.borrow(), 1);
818 fn fails_repaying_invoice_with_pending_payment() {
819 let event_handled = core::cell::RefCell::new(false);
820 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
822 let payer = TestPayer::new();
823 let router = TestRouter {};
824 let scorer = RefCell::new(TestScorer::new());
825 let logger = TestLogger::new();
827 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
829 let payment_preimage = PaymentPreimage([1; 32]);
830 let invoice = invoice(payment_preimage);
831 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
833 // Cannot repay an invoice pending payment.
834 match invoice_payer.pay_invoice(&invoice) {
835 Err(PaymentError::Invoice("payment pending")) => {},
836 Err(_) => panic!("unexpected error"),
837 Ok(_) => panic!("expected invoice error"),
840 // Can repay an invoice once cleared from cache.
841 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
842 invoice_payer.remove_cached_payment(&payment_hash);
843 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
845 // Cannot retry paying an invoice if cleared from cache.
846 invoice_payer.remove_cached_payment(&payment_hash);
847 let event = Event::PaymentPathFailed {
850 network_update: None,
851 rejected_by_dest: false,
852 all_paths_failed: false,
854 short_channel_id: None,
855 retry: Some(TestRouter::retry_for_invoice(&invoice)),
857 invoice_payer.handle_event(&event);
858 assert_eq!(*event_handled.borrow(), true);
862 fn fails_paying_invoice_with_routing_errors() {
863 let payer = TestPayer::new();
864 let router = FailingRouter {};
865 let scorer = RefCell::new(TestScorer::new());
866 let logger = TestLogger::new();
868 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
870 let payment_preimage = PaymentPreimage([1; 32]);
871 let invoice = invoice(payment_preimage);
872 match invoice_payer.pay_invoice(&invoice) {
873 Err(PaymentError::Routing(_)) => {},
874 Err(_) => panic!("unexpected error"),
875 Ok(_) => panic!("expected routing error"),
880 fn fails_paying_invoice_with_sending_errors() {
881 let payer = TestPayer::new().fails_on_attempt(1);
882 let router = TestRouter {};
883 let scorer = RefCell::new(TestScorer::new());
884 let logger = TestLogger::new();
886 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
888 let payment_preimage = PaymentPreimage([1; 32]);
889 let invoice = invoice(payment_preimage);
890 match invoice_payer.pay_invoice(&invoice) {
891 Err(PaymentError::Sending(_)) => {},
892 Err(_) => panic!("unexpected error"),
893 Ok(_) => panic!("expected sending error"),
898 fn pays_zero_value_invoice_using_amount() {
899 let event_handled = core::cell::RefCell::new(false);
900 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
902 let payment_preimage = PaymentPreimage([1; 32]);
903 let invoice = zero_value_invoice(payment_preimage);
904 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
905 let final_value_msat = 100;
907 let payer = TestPayer::new().expect_value_msat(final_value_msat);
908 let router = TestRouter {};
909 let scorer = RefCell::new(TestScorer::new());
910 let logger = TestLogger::new();
912 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
915 Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
916 assert_eq!(*payer.attempts.borrow(), 1);
918 invoice_payer.handle_event(&Event::PaymentSent {
919 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
921 assert_eq!(*event_handled.borrow(), true);
922 assert_eq!(*payer.attempts.borrow(), 1);
926 fn fails_paying_zero_value_invoice_with_amount() {
927 let event_handled = core::cell::RefCell::new(false);
928 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
930 let payer = TestPayer::new();
931 let router = TestRouter {};
932 let scorer = RefCell::new(TestScorer::new());
933 let logger = TestLogger::new();
935 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
937 let payment_preimage = PaymentPreimage([1; 32]);
938 let invoice = invoice(payment_preimage);
940 // Cannot repay an invoice pending payment.
941 match invoice_payer.pay_zero_value_invoice(&invoice, 100) {
942 Err(PaymentError::Invoice("amount unexpected")) => {},
943 Err(_) => panic!("unexpected error"),
944 Ok(_) => panic!("expected invoice error"),
949 fn scores_failed_channel() {
950 let event_handled = core::cell::RefCell::new(false);
951 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
953 let payment_preimage = PaymentPreimage([1; 32]);
954 let invoice = invoice(payment_preimage);
955 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
956 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
957 let path = TestRouter::path_for_value(final_value_msat);
958 let short_channel_id = Some(path[0].short_channel_id);
960 // Expect that scorer is given short_channel_id upon handling the event.
961 let payer = TestPayer::new();
962 let router = TestRouter {};
963 let scorer = RefCell::new(TestScorer::new().expect_channel_failure(short_channel_id.unwrap()));
964 let logger = TestLogger::new();
966 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
968 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
969 let event = Event::PaymentPathFailed {
972 network_update: None,
973 rejected_by_dest: false,
974 all_paths_failed: false,
977 retry: Some(TestRouter::retry_for_invoice(&invoice)),
979 invoice_payer.handle_event(&event);
985 fn route_for_value(final_value_msat: u64) -> Route {
989 pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
990 channel_features: ChannelFeatures::empty(),
991 node_features: NodeFeatures::empty(),
992 short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
995 pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
996 channel_features: ChannelFeatures::empty(),
997 node_features: NodeFeatures::empty(),
998 short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1005 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1006 TestRouter::route_for_value(final_value_msat).paths[0].clone()
1009 fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
1010 let mut payee = Payee::new(invoice.recover_payee_pub_key())
1011 .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
1012 .with_route_hints(invoice.route_hints());
1013 if let Some(features) = invoice.features() {
1014 payee = payee.with_features(features.clone());
1016 let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1020 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1025 impl<S: routing::Score> Router<S> for TestRouter {
1029 params: &RouteParameters,
1030 _first_hops: Option<&[&ChannelDetails]>,
1032 ) -> Result<Route, LightningError> {
1034 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1039 struct FailingRouter;
1041 impl<S: routing::Score> Router<S> for FailingRouter {
1045 _params: &RouteParameters,
1046 _first_hops: Option<&[&ChannelDetails]>,
1048 ) -> Result<Route, LightningError> {
1049 Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
1054 expectations: VecDeque<u64>,
1060 expectations: VecDeque::new(),
1064 fn expect_channel_failure(mut self, short_channel_id: u64) -> Self {
1065 self.expectations.push_back(short_channel_id);
1070 impl routing::Score for TestScorer {
1071 fn channel_penalty_msat(
1072 &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
1075 fn payment_path_failed(&mut self, _path: &Vec<RouteHop>, short_channel_id: u64) {
1076 if let Some(expected_short_channel_id) = self.expectations.pop_front() {
1077 assert_eq!(short_channel_id, expected_short_channel_id);
1082 impl Drop for TestScorer {
1083 fn drop(&mut self) {
1084 if std::thread::panicking() {
1088 if !self.expectations.is_empty() {
1089 panic!("Unsatisfied channel failure expectations: {:?}", self.expectations);
1095 expectations: core::cell::RefCell<VecDeque<u64>>,
1096 attempts: core::cell::RefCell<usize>,
1097 failing_on_attempt: Option<usize>,
1103 expectations: core::cell::RefCell::new(VecDeque::new()),
1104 attempts: core::cell::RefCell::new(0),
1105 failing_on_attempt: None,
1109 fn expect_value_msat(self, value_msat: u64) -> Self {
1110 self.expectations.borrow_mut().push_back(value_msat);
1114 fn fails_on_attempt(self, attempt: usize) -> Self {
1116 expectations: core::cell::RefCell::new(self.expectations.borrow().clone()),
1117 attempts: core::cell::RefCell::new(0),
1118 failing_on_attempt: Some(attempt),
1122 fn check_attempts(&self) -> bool {
1123 let mut attempts = self.attempts.borrow_mut();
1125 match self.failing_on_attempt {
1127 Some(attempt) if attempt != *attempts => true,
1132 fn check_value_msats(&self, route: &Route) {
1133 let expected_value_msats = self.expectations.borrow_mut().pop_front();
1134 if let Some(expected_value_msats) = expected_value_msats {
1135 let actual_value_msats = route.get_total_amount();
1136 assert_eq!(actual_value_msats, expected_value_msats);
1141 impl Drop for TestPayer {
1142 fn drop(&mut self) {
1143 if std::thread::panicking() {
1147 if !self.expectations.borrow().is_empty() {
1148 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1153 impl Payer for TestPayer {
1154 fn node_id(&self) -> PublicKey {
1155 let secp_ctx = Secp256k1::new();
1156 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
1159 fn first_hops(&self) -> Vec<ChannelDetails> {
1166 _payment_hash: PaymentHash,
1167 _payment_secret: &Option<PaymentSecret>
1168 ) -> Result<PaymentId, PaymentSendFailure> {
1169 if self.check_attempts() {
1170 self.check_value_msats(route);
1171 Ok(PaymentId([1; 32]))
1173 Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed))
1178 &self, route: &Route, _payment_id: PaymentId
1179 ) -> Result<(), PaymentSendFailure> {
1180 if self.check_attempts() {
1181 self.check_value_msats(route);
1184 Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed))
1189 // *** Full Featured Functional Tests with a Real ChannelManager ***
1190 struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1192 impl<S: routing::Score> Router<S> for ManualRouter {
1193 fn find_route(&self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, _scorer: &S)
1194 -> Result<Route, LightningError> {
1195 self.0.borrow_mut().pop_front().unwrap()
1199 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1200 self.0.borrow_mut().push_back(result);
1203 impl Drop for ManualRouter {
1204 fn drop(&mut self) {
1205 if std::thread::panicking() {
1208 assert!(self.0.borrow_mut().is_empty());
1213 fn retry_multi_path_single_failed_payment() {
1214 // Tests that we can/will retry after a single path of an MPP payment failed immediately
1215 let chanmon_cfgs = create_chanmon_cfgs(2);
1216 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1217 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1218 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1220 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1221 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1222 let chans = nodes[0].node.list_usable_channels();
1223 let mut route = Route {
1226 pubkey: nodes[1].node.get_our_node_id(),
1227 node_features: NodeFeatures::known(),
1228 short_channel_id: chans[0].short_channel_id.unwrap(),
1229 channel_features: ChannelFeatures::known(),
1231 cltv_expiry_delta: 100,
1234 pubkey: nodes[1].node.get_our_node_id(),
1235 node_features: NodeFeatures::known(),
1236 short_channel_id: chans[1].short_channel_id.unwrap(),
1237 channel_features: ChannelFeatures::known(),
1238 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1239 cltv_expiry_delta: 100,
1242 payee: Some(Payee::new(nodes[1].node.get_our_node_id())),
1244 let router = ManualRouter(RefCell::new(VecDeque::new()));
1245 router.expect_find_route(Ok(route.clone()));
1246 // On retry, split the payment across both channels.
1247 route.paths[0][0].fee_msat = 50_000_001;
1248 route.paths[1][0].fee_msat = 50_000_000;
1249 router.expect_find_route(Ok(route.clone()));
1251 let event_handler = |_: &_| { panic!(); };
1252 let scorer = RefCell::new(TestScorer::new());
1253 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1255 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1256 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1258 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1259 assert_eq!(htlc_msgs.len(), 2);
1260 check_added_monitors!(nodes[0], 2);