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 parameterized by a [`LockableScore`], which it uses for scoring failed and
19 //! successful payment paths upon receiving [`Event::PaymentPathFailed`] and
20 //! [`Event::PaymentPathSuccessful`] events, respectively.
22 //! [`InvoicePayer`] is capable of retrying failed payments. It accomplishes this by implementing
23 //! [`EventHandler`] which decorates a user-provided handler. It will intercept any
24 //! [`Event::PaymentPathFailed`] events and retry the failed paths for a fixed number of total
25 //! attempts or until retry is no longer possible. In such a situation, [`InvoicePayer`] will pass
26 //! along the events to the user-provided handler.
31 //! # extern crate lightning;
32 //! # extern crate lightning_invoice;
33 //! # extern crate secp256k1;
35 //! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
36 //! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
37 //! # use lightning::ln::msgs::LightningError;
38 //! # use lightning::routing::scoring::Score;
39 //! # use lightning::routing::network_graph::NodeId;
40 //! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
41 //! # use lightning::util::events::{Event, EventHandler, EventsProvider};
42 //! # use lightning::util::logger::{Logger, Record};
43 //! # use lightning::util::ser::{Writeable, Writer};
44 //! # use lightning_invoice::Invoice;
45 //! # use lightning_invoice::payment::{InvoicePayer, Payer, RetryAttempts, Router};
46 //! # use secp256k1::key::PublicKey;
47 //! # use std::cell::RefCell;
48 //! # use std::ops::Deref;
50 //! # struct FakeEventProvider {}
51 //! # impl EventsProvider for FakeEventProvider {
52 //! # fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {}
55 //! # struct FakePayer {}
56 //! # impl Payer for FakePayer {
57 //! # fn node_id(&self) -> PublicKey { unimplemented!() }
58 //! # fn first_hops(&self) -> Vec<ChannelDetails> { unimplemented!() }
59 //! # fn send_payment(
60 //! # &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
61 //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
62 //! # fn send_spontaneous_payment(
63 //! # &self, route: &Route, payment_preimage: PaymentPreimage
64 //! # ) -> Result<PaymentId, PaymentSendFailure> { unimplemented!() }
65 //! # fn retry_payment(
66 //! # &self, route: &Route, payment_id: PaymentId
67 //! # ) -> Result<(), PaymentSendFailure> { unimplemented!() }
68 //! # fn abandon_payment(&self, payment_id: PaymentId) { unimplemented!() }
71 //! # struct FakeRouter {}
72 //! # impl<S: Score> Router<S> for FakeRouter {
74 //! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
75 //! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
76 //! # ) -> Result<Route, LightningError> { unimplemented!() }
79 //! # struct FakeScorer {}
80 //! # impl Writeable for FakeScorer {
81 //! # fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> { unimplemented!(); }
83 //! # impl Score for FakeScorer {
84 //! # fn channel_penalty_msat(
85 //! # &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
87 //! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
88 //! # fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
91 //! # struct FakeLogger {}
92 //! # impl Logger for FakeLogger {
93 //! # fn log(&self, record: &Record) { unimplemented!() }
97 //! let event_handler = |event: &Event| {
99 //! Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
100 //! Event::PaymentSent { .. } => println!("payment successful"),
104 //! # let payer = FakePayer {};
105 //! # let router = FakeRouter {};
106 //! # let scorer = RefCell::new(FakeScorer {});
107 //! # let logger = FakeLogger {};
108 //! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
110 //! let invoice = "...";
111 //! if let Ok(invoice) = invoice.parse::<Invoice>() {
112 //! invoice_payer.pay_invoice(&invoice).unwrap();
114 //! # let event_provider = FakeEventProvider {};
116 //! event_provider.process_pending_events(&invoice_payer);
124 //! The [`Route`] is computed before each payment attempt. Any updates affecting path finding such
125 //! as updates to the network graph or changes to channel scores should be applied prior to
126 //! retries, typically by way of composing [`EventHandler`]s accordingly.
130 use bitcoin_hashes::Hash;
131 use bitcoin_hashes::sha256::Hash as Sha256;
133 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
134 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
135 use lightning::ln::msgs::LightningError;
136 use lightning::routing::scoring::{LockableScore, MultiThreadedLockableScore, Score};
137 use lightning::routing::router::{Payee, Route, RouteParameters};
138 use lightning::util::events::{Event, EventHandler};
139 use lightning::util::logger::Logger;
141 use secp256k1::key::PublicKey;
143 use std::collections::hash_map::{self, HashMap};
145 use std::sync::Mutex;
146 use std::time::{Duration, SystemTime};
148 /// A utility for paying [`Invoice`]s and sending spontaneous payments.
150 /// See [module-level documentation] for details.
152 /// [module-level documentation]: crate::payment
153 pub struct InvoicePayer<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler>
156 R::Target: Router<S>,
164 /// Caches the overall attempts at making a payment, which is updated prior to retrying.
165 payment_cache: Mutex<HashMap<PaymentHash, usize>>,
166 retry_attempts: RetryAttempts,
169 /// A trait defining behavior of an [`Invoice`] payer.
171 /// Returns the payer's node id.
172 fn node_id(&self) -> PublicKey;
174 /// Returns the payer's channels.
175 fn first_hops(&self) -> Vec<ChannelDetails>;
177 /// Sends a payment over the Lightning Network using the given [`Route`].
179 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
180 ) -> Result<PaymentId, PaymentSendFailure>;
182 /// Sends a spontaneous payment over the Lightning Network using the given [`Route`].
183 fn send_spontaneous_payment(
184 &self, route: &Route, payment_preimage: PaymentPreimage
185 ) -> Result<PaymentId, PaymentSendFailure>;
187 /// Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
188 fn retry_payment(&self, route: &Route, payment_id: PaymentId) -> Result<(), PaymentSendFailure>;
190 /// Signals that no further retries for the given payment will occur.
191 fn abandon_payment(&self, payment_id: PaymentId);
194 /// A trait defining behavior for routing an [`Invoice`] payment.
195 pub trait Router<S: Score> {
196 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
198 &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
199 first_hops: Option<&[&ChannelDetails]>, scorer: &S
200 ) -> Result<Route, LightningError>;
203 /// Number of attempts to retry payment path failures for an [`Invoice`].
205 /// Note that this is the number of *path* failures, not full payment retries. For multi-path
206 /// payments, if this is less than the total number of paths, we will never even retry all of the
208 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
209 pub struct RetryAttempts(pub usize);
211 /// An error that may occur when making a payment.
212 #[derive(Clone, Debug)]
213 pub enum PaymentError {
214 /// An error resulting from the provided [`Invoice`] or payment hash.
215 Invoice(&'static str),
216 /// An error occurring when finding a route.
217 Routing(LightningError),
218 /// An error occurring when sending a payment.
219 Sending(PaymentSendFailure),
222 impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> InvoicePayer<P, S, R, SR, L, E>
225 R::Target: Router<S>,
228 /// Creates an invoice payer that retries failed payment paths.
230 /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
231 /// `retry_attempts` has been exceeded for a given [`Invoice`].
233 payer: P, router: R, scorer: SR, logger: L, event_handler: E, retry_attempts: RetryAttempts
241 payment_cache: Mutex::new(HashMap::new()),
246 /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
248 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
249 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
251 pub fn pay_invoice(&self, invoice: &Invoice) -> Result<PaymentId, PaymentError> {
252 if invoice.amount_milli_satoshis().is_none() {
253 Err(PaymentError::Invoice("amount missing"))
255 self.pay_invoice_using_amount(invoice, None)
259 /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
260 /// case a retry is needed.
262 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
263 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
265 pub fn pay_zero_value_invoice(
266 &self, invoice: &Invoice, amount_msats: u64
267 ) -> Result<PaymentId, PaymentError> {
268 if invoice.amount_milli_satoshis().is_some() {
269 Err(PaymentError::Invoice("amount unexpected"))
271 self.pay_invoice_using_amount(invoice, Some(amount_msats))
275 fn pay_invoice_using_amount(
276 &self, invoice: &Invoice, amount_msats: Option<u64>
277 ) -> Result<PaymentId, PaymentError> {
278 debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
280 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
281 match self.payment_cache.lock().unwrap().entry(payment_hash) {
282 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
283 hash_map::Entry::Vacant(entry) => entry.insert(0),
286 let payment_secret = Some(invoice.payment_secret().clone());
287 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
288 .with_expiry_time(expiry_time_from_unix_epoch(&invoice).as_secs())
289 .with_route_hints(invoice.route_hints());
290 if let Some(features) = invoice.features() {
291 payee = payee.with_features(features.clone());
293 let params = RouteParameters {
295 final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
296 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
299 let send_payment = |route: &Route| {
300 self.payer.send_payment(route, payment_hash, &payment_secret)
302 self.pay_internal(¶ms, payment_hash, send_payment)
303 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
306 /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
307 /// case a retry is needed.
309 /// You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
310 /// been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
312 &self, pubkey: PublicKey, payment_preimage: PaymentPreimage, amount_msats: u64,
313 final_cltv_expiry_delta: u32
314 ) -> Result<PaymentId, PaymentError> {
315 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
316 match self.payment_cache.lock().unwrap().entry(payment_hash) {
317 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
318 hash_map::Entry::Vacant(entry) => entry.insert(0),
321 let params = RouteParameters {
322 payee: Payee::for_keysend(pubkey),
323 final_value_msat: amount_msats,
324 final_cltv_expiry_delta,
327 let send_payment = |route: &Route| {
328 self.payer.send_spontaneous_payment(route, payment_preimage)
330 self.pay_internal(¶ms, payment_hash, send_payment)
331 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
334 fn pay_internal<F: FnOnce(&Route) -> Result<PaymentId, PaymentSendFailure> + Copy>(
335 &self, params: &RouteParameters, payment_hash: PaymentHash, send_payment: F,
336 ) -> Result<PaymentId, PaymentError> {
337 if has_expired(params) {
338 log_trace!(self.logger, "Invoice expired prior to send for payment {}", log_bytes!(payment_hash.0));
339 return Err(PaymentError::Invoice("Invoice expired prior to send"));
342 let payer = self.payer.node_id();
343 let first_hops = self.payer.first_hops();
344 let route = self.router.find_route(
345 &payer, params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
347 ).map_err(|e| PaymentError::Routing(e))?;
349 match send_payment(&route) {
350 Ok(payment_id) => Ok(payment_id),
352 PaymentSendFailure::ParameterError(_) => Err(e),
353 PaymentSendFailure::PathParameterError(_) => Err(e),
354 PaymentSendFailure::AllFailedRetrySafe(_) => {
355 let mut payment_cache = self.payment_cache.lock().unwrap();
356 let retry_count = payment_cache.get_mut(&payment_hash).unwrap();
357 if *retry_count >= self.retry_attempts.0 {
361 std::mem::drop(payment_cache);
362 Ok(self.pay_internal(params, payment_hash, send_payment)?)
365 PaymentSendFailure::PartialFailure { failed_paths_retry, payment_id, .. } => {
366 if let Some(retry_data) = failed_paths_retry {
367 // Some paths were sent, even if we failed to send the full MPP value our
368 // recipient may misbehave and claim the funds, at which point we have to
369 // consider the payment sent, so return `Ok()` here, ignoring any retry
371 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
374 // This may happen if we send a payment and some paths fail, but
375 // only due to a temporary monitor failure or the like, implying
376 // they're really in-flight, but we haven't sent the initial
377 // HTLC-Add messages yet.
382 }.map_err(|e| PaymentError::Sending(e))
386 &self, payment_id: PaymentId, payment_hash: PaymentHash, params: &RouteParameters
387 ) -> Result<(), ()> {
388 let max_payment_attempts = self.retry_attempts.0 + 1;
389 let attempts = *self.payment_cache.lock().unwrap()
391 .and_modify(|attempts| *attempts += 1)
394 if attempts >= max_payment_attempts {
395 log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
399 if has_expired(params) {
400 log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
404 let payer = self.payer.node_id();
405 let first_hops = self.payer.first_hops();
406 let route = self.router.find_route(
407 &payer, ¶ms, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
411 log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
415 match self.payer.retry_payment(&route.unwrap(), payment_id) {
417 Err(PaymentSendFailure::ParameterError(_)) |
418 Err(PaymentSendFailure::PathParameterError(_)) => {
419 log_trace!(self.logger, "Failed to retry for payment {} due to bogus route/payment data, not retrying.", log_bytes!(payment_hash.0));
422 Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
423 self.retry_payment(payment_id, payment_hash, params)
425 Err(PaymentSendFailure::PartialFailure { failed_paths_retry, .. }) => {
426 if let Some(retry) = failed_paths_retry {
427 // Always return Ok for the same reason as noted in pay_internal.
428 let _ = self.retry_payment(payment_id, payment_hash, &retry);
435 /// Removes the payment cached by the given payment hash.
437 /// Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
438 /// [`EventHandler`]. Otherwise, calling this method is unnecessary.
439 pub fn remove_cached_payment(&self, payment_hash: &PaymentHash) {
440 self.payment_cache.lock().unwrap().remove(payment_hash);
444 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
445 invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
448 fn has_expired(params: &RouteParameters) -> bool {
449 if let Some(expiry_time) = params.payee.expiry_time {
450 Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, Duration::from_secs(expiry_time))
454 impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> EventHandler for InvoicePayer<P, S, R, SR, L, E>
457 R::Target: Router<S>,
460 fn handle_event(&self, event: &Event) {
462 Event::PaymentPathFailed {
463 payment_id, payment_hash, rejected_by_dest, path, short_channel_id, retry, ..
465 if let Some(short_channel_id) = short_channel_id {
466 let path = path.iter().collect::<Vec<_>>();
467 self.scorer.lock().payment_path_failed(&path, *short_channel_id);
470 if payment_id.is_none() {
471 log_trace!(self.logger, "Payment {} has no id; not retrying", log_bytes!(payment_hash.0));
472 } else if *rejected_by_dest {
473 log_trace!(self.logger, "Payment {} rejected by destination; not retrying", log_bytes!(payment_hash.0));
474 self.payer.abandon_payment(payment_id.unwrap());
475 } else if retry.is_none() {
476 log_trace!(self.logger, "Payment {} missing retry params; not retrying", log_bytes!(payment_hash.0));
477 self.payer.abandon_payment(payment_id.unwrap());
478 } else if self.retry_payment(payment_id.unwrap(), *payment_hash, retry.as_ref().unwrap()).is_ok() {
479 // We retried at least somewhat, don't provide the PaymentPathFailed event to the user.
482 self.payer.abandon_payment(payment_id.unwrap());
485 Event::PaymentFailed { payment_hash, .. } => {
486 self.remove_cached_payment(&payment_hash);
488 Event::PaymentPathSuccessful { path, .. } => {
489 let path = path.iter().collect::<Vec<_>>();
490 self.scorer.lock().payment_path_successful(&path);
492 Event::PaymentSent { payment_hash, .. } => {
493 let mut payment_cache = self.payment_cache.lock().unwrap();
494 let attempts = payment_cache
495 .remove(payment_hash)
496 .map_or(1, |attempts| attempts + 1);
497 log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
502 // Delegate to the decorated event handler unless the payment is retried.
503 self.event_handler.handle_event(event)
510 use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
511 use utils::create_invoice_from_channelmanager;
512 use bitcoin_hashes::sha256::Hash as Sha256;
513 use lightning::ln::PaymentPreimage;
514 use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
515 use lightning::ln::functional_test_utils::*;
516 use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
517 use lightning::routing::network_graph::NodeId;
518 use lightning::routing::router::{Payee, Route, RouteHop};
519 use lightning::util::test_utils::TestLogger;
520 use lightning::util::errors::APIError;
521 use lightning::util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
522 use secp256k1::{SecretKey, PublicKey, Secp256k1};
523 use std::cell::RefCell;
524 use std::collections::VecDeque;
525 use std::time::{SystemTime, Duration};
527 fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
528 let payment_hash = Sha256::hash(&payment_preimage.0);
529 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
530 InvoiceBuilder::new(Currency::Bitcoin)
531 .description("test".into())
532 .payment_hash(payment_hash)
533 .payment_secret(PaymentSecret([0; 32]))
535 .min_final_cltv_expiry(144)
536 .amount_milli_satoshis(128)
537 .build_signed(|hash| {
538 Secp256k1::new().sign_recoverable(hash, &private_key)
543 fn zero_value_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 InvoiceBuilder::new(Currency::Bitcoin)
547 .description("test".into())
548 .payment_hash(payment_hash)
549 .payment_secret(PaymentSecret([0; 32]))
551 .min_final_cltv_expiry(144)
552 .build_signed(|hash| {
553 Secp256k1::new().sign_recoverable(hash, &private_key)
558 fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
559 let payment_hash = Sha256::hash(&payment_preimage.0);
560 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
561 let timestamp = SystemTime::now()
562 .checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
564 InvoiceBuilder::new(Currency::Bitcoin)
565 .description("test".into())
566 .payment_hash(payment_hash)
567 .payment_secret(PaymentSecret([0; 32]))
568 .timestamp(timestamp)
569 .min_final_cltv_expiry(144)
570 .amount_milli_satoshis(128)
571 .build_signed(|hash| {
572 Secp256k1::new().sign_recoverable(hash, &private_key)
577 fn pubkey() -> PublicKey {
578 PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()
582 fn pays_invoice_on_first_attempt() {
583 let event_handled = core::cell::RefCell::new(false);
584 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
586 let payment_preimage = PaymentPreimage([1; 32]);
587 let invoice = invoice(payment_preimage);
588 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
589 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
591 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
592 let router = TestRouter {};
593 let scorer = RefCell::new(TestScorer::new());
594 let logger = TestLogger::new();
596 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
598 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
599 assert_eq!(*payer.attempts.borrow(), 1);
601 invoice_payer.handle_event(&Event::PaymentSent {
602 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
604 assert_eq!(*event_handled.borrow(), true);
605 assert_eq!(*payer.attempts.borrow(), 1);
609 fn pays_invoice_on_retry() {
610 let event_handled = core::cell::RefCell::new(false);
611 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
613 let payment_preimage = PaymentPreimage([1; 32]);
614 let invoice = invoice(payment_preimage);
615 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
616 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
618 let payer = TestPayer::new()
619 .expect_send(Amount::ForInvoice(final_value_msat))
620 .expect_send(Amount::OnRetry(final_value_msat / 2));
621 let router = TestRouter {};
622 let scorer = RefCell::new(TestScorer::new());
623 let logger = TestLogger::new();
625 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
627 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
628 assert_eq!(*payer.attempts.borrow(), 1);
630 let event = Event::PaymentPathFailed {
633 network_update: None,
634 rejected_by_dest: false,
635 all_paths_failed: false,
636 path: TestRouter::path_for_value(final_value_msat),
637 short_channel_id: None,
638 retry: Some(TestRouter::retry_for_invoice(&invoice)),
640 invoice_payer.handle_event(&event);
641 assert_eq!(*event_handled.borrow(), false);
642 assert_eq!(*payer.attempts.borrow(), 2);
644 invoice_payer.handle_event(&Event::PaymentSent {
645 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
647 assert_eq!(*event_handled.borrow(), true);
648 assert_eq!(*payer.attempts.borrow(), 2);
652 fn pays_invoice_on_partial_failure() {
653 let event_handler = |_: &_| { panic!() };
655 let payment_preimage = PaymentPreimage([1; 32]);
656 let invoice = invoice(payment_preimage);
657 let retry = TestRouter::retry_for_invoice(&invoice);
658 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
660 let payer = TestPayer::new()
661 .fails_with_partial_failure(retry.clone(), OnAttempt(1))
662 .fails_with_partial_failure(retry, OnAttempt(2))
663 .expect_send(Amount::ForInvoice(final_value_msat))
664 .expect_send(Amount::OnRetry(final_value_msat / 2))
665 .expect_send(Amount::OnRetry(final_value_msat / 2));
666 let router = TestRouter {};
667 let scorer = RefCell::new(TestScorer::new());
668 let logger = TestLogger::new();
670 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
672 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
676 fn retries_payment_path_for_unknown_payment() {
677 let event_handled = core::cell::RefCell::new(false);
678 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
680 let payment_preimage = PaymentPreimage([1; 32]);
681 let invoice = invoice(payment_preimage);
682 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
683 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
685 let payer = TestPayer::new()
686 .expect_send(Amount::OnRetry(final_value_msat / 2))
687 .expect_send(Amount::OnRetry(final_value_msat / 2));
688 let router = TestRouter {};
689 let scorer = RefCell::new(TestScorer::new());
690 let logger = TestLogger::new();
692 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
694 let payment_id = Some(PaymentId([1; 32]));
695 let event = Event::PaymentPathFailed {
698 network_update: None,
699 rejected_by_dest: false,
700 all_paths_failed: false,
701 path: TestRouter::path_for_value(final_value_msat),
702 short_channel_id: None,
703 retry: Some(TestRouter::retry_for_invoice(&invoice)),
705 invoice_payer.handle_event(&event);
706 assert_eq!(*event_handled.borrow(), false);
707 assert_eq!(*payer.attempts.borrow(), 1);
709 invoice_payer.handle_event(&event);
710 assert_eq!(*event_handled.borrow(), false);
711 assert_eq!(*payer.attempts.borrow(), 2);
713 invoice_payer.handle_event(&Event::PaymentSent {
714 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
716 assert_eq!(*event_handled.borrow(), true);
717 assert_eq!(*payer.attempts.borrow(), 2);
721 fn fails_paying_invoice_after_max_retries() {
722 let event_handled = core::cell::RefCell::new(false);
723 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
725 let payment_preimage = PaymentPreimage([1; 32]);
726 let invoice = invoice(payment_preimage);
727 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
729 let payer = TestPayer::new()
730 .expect_send(Amount::ForInvoice(final_value_msat))
731 .expect_send(Amount::OnRetry(final_value_msat / 2))
732 .expect_send(Amount::OnRetry(final_value_msat / 2));
733 let router = TestRouter {};
734 let scorer = RefCell::new(TestScorer::new());
735 let logger = TestLogger::new();
737 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
739 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
740 assert_eq!(*payer.attempts.borrow(), 1);
742 let event = Event::PaymentPathFailed {
744 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
745 network_update: None,
746 rejected_by_dest: false,
747 all_paths_failed: true,
748 path: TestRouter::path_for_value(final_value_msat),
749 short_channel_id: None,
750 retry: Some(TestRouter::retry_for_invoice(&invoice)),
752 invoice_payer.handle_event(&event);
753 assert_eq!(*event_handled.borrow(), false);
754 assert_eq!(*payer.attempts.borrow(), 2);
756 let event = Event::PaymentPathFailed {
758 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
759 network_update: None,
760 rejected_by_dest: false,
761 all_paths_failed: false,
762 path: TestRouter::path_for_value(final_value_msat / 2),
763 short_channel_id: None,
764 retry: Some(RouteParameters {
765 final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
768 invoice_payer.handle_event(&event);
769 assert_eq!(*event_handled.borrow(), false);
770 assert_eq!(*payer.attempts.borrow(), 3);
772 invoice_payer.handle_event(&event);
773 assert_eq!(*event_handled.borrow(), true);
774 assert_eq!(*payer.attempts.borrow(), 3);
778 fn fails_paying_invoice_with_missing_retry_params() {
779 let event_handled = core::cell::RefCell::new(false);
780 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
782 let payment_preimage = PaymentPreimage([1; 32]);
783 let invoice = invoice(payment_preimage);
784 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
786 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
787 let router = TestRouter {};
788 let scorer = RefCell::new(TestScorer::new());
789 let logger = TestLogger::new();
791 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
793 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
794 assert_eq!(*payer.attempts.borrow(), 1);
796 let event = Event::PaymentPathFailed {
798 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
799 network_update: None,
800 rejected_by_dest: false,
801 all_paths_failed: false,
803 short_channel_id: None,
806 invoice_payer.handle_event(&event);
807 assert_eq!(*event_handled.borrow(), true);
808 assert_eq!(*payer.attempts.borrow(), 1);
812 fn fails_paying_invoice_after_expiration() {
813 let event_handled = core::cell::RefCell::new(false);
814 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
816 let payer = TestPayer::new();
817 let router = TestRouter {};
818 let scorer = RefCell::new(TestScorer::new());
819 let logger = TestLogger::new();
821 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
823 let payment_preimage = PaymentPreimage([1; 32]);
824 let invoice = expired_invoice(payment_preimage);
825 if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
826 assert_eq!(msg, "Invoice expired prior to send");
827 } else { panic!("Expected Invoice Error"); }
831 fn fails_retrying_invoice_after_expiration() {
832 let event_handled = core::cell::RefCell::new(false);
833 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
835 let payment_preimage = PaymentPreimage([1; 32]);
836 let invoice = invoice(payment_preimage);
837 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
839 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
840 let router = TestRouter {};
841 let scorer = RefCell::new(TestScorer::new());
842 let logger = TestLogger::new();
844 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
846 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
847 assert_eq!(*payer.attempts.borrow(), 1);
849 let mut retry_data = TestRouter::retry_for_invoice(&invoice);
850 retry_data.payee.expiry_time = Some(SystemTime::now()
851 .checked_sub(Duration::from_secs(2)).unwrap()
852 .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
853 let event = Event::PaymentPathFailed {
855 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
856 network_update: None,
857 rejected_by_dest: false,
858 all_paths_failed: false,
860 short_channel_id: None,
861 retry: Some(retry_data),
863 invoice_payer.handle_event(&event);
864 assert_eq!(*event_handled.borrow(), true);
865 assert_eq!(*payer.attempts.borrow(), 1);
869 fn fails_paying_invoice_after_retry_error() {
870 let event_handled = core::cell::RefCell::new(false);
871 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
873 let payment_preimage = PaymentPreimage([1; 32]);
874 let invoice = invoice(payment_preimage);
875 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
877 let payer = TestPayer::new()
879 .expect_send(Amount::ForInvoice(final_value_msat))
880 .expect_send(Amount::OnRetry(final_value_msat / 2));
881 let router = TestRouter {};
882 let scorer = RefCell::new(TestScorer::new());
883 let logger = TestLogger::new();
885 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
887 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
888 assert_eq!(*payer.attempts.borrow(), 1);
890 let event = Event::PaymentPathFailed {
892 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
893 network_update: None,
894 rejected_by_dest: false,
895 all_paths_failed: false,
896 path: TestRouter::path_for_value(final_value_msat / 2),
897 short_channel_id: None,
898 retry: Some(TestRouter::retry_for_invoice(&invoice)),
900 invoice_payer.handle_event(&event);
901 assert_eq!(*event_handled.borrow(), true);
902 assert_eq!(*payer.attempts.borrow(), 2);
906 fn fails_paying_invoice_after_rejected_by_payee() {
907 let event_handled = core::cell::RefCell::new(false);
908 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
910 let payment_preimage = PaymentPreimage([1; 32]);
911 let invoice = invoice(payment_preimage);
912 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
914 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
915 let router = TestRouter {};
916 let scorer = RefCell::new(TestScorer::new());
917 let logger = TestLogger::new();
919 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
921 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
922 assert_eq!(*payer.attempts.borrow(), 1);
924 let event = Event::PaymentPathFailed {
926 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
927 network_update: None,
928 rejected_by_dest: true,
929 all_paths_failed: false,
931 short_channel_id: None,
932 retry: Some(TestRouter::retry_for_invoice(&invoice)),
934 invoice_payer.handle_event(&event);
935 assert_eq!(*event_handled.borrow(), true);
936 assert_eq!(*payer.attempts.borrow(), 1);
940 fn fails_repaying_invoice_with_pending_payment() {
941 let event_handled = core::cell::RefCell::new(false);
942 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
944 let payment_preimage = PaymentPreimage([1; 32]);
945 let invoice = invoice(payment_preimage);
946 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
948 let payer = TestPayer::new()
949 .expect_send(Amount::ForInvoice(final_value_msat))
950 .expect_send(Amount::ForInvoice(final_value_msat));
951 let router = TestRouter {};
952 let scorer = RefCell::new(TestScorer::new());
953 let logger = TestLogger::new();
955 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
957 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
959 // Cannot repay an invoice pending payment.
960 match invoice_payer.pay_invoice(&invoice) {
961 Err(PaymentError::Invoice("payment pending")) => {},
962 Err(_) => panic!("unexpected error"),
963 Ok(_) => panic!("expected invoice error"),
966 // Can repay an invoice once cleared from cache.
967 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
968 invoice_payer.remove_cached_payment(&payment_hash);
969 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
971 // Cannot retry paying an invoice if cleared from cache.
972 invoice_payer.remove_cached_payment(&payment_hash);
973 let event = Event::PaymentPathFailed {
976 network_update: None,
977 rejected_by_dest: false,
978 all_paths_failed: false,
980 short_channel_id: None,
981 retry: Some(TestRouter::retry_for_invoice(&invoice)),
983 invoice_payer.handle_event(&event);
984 assert_eq!(*event_handled.borrow(), true);
988 fn fails_paying_invoice_with_routing_errors() {
989 let payer = TestPayer::new();
990 let router = FailingRouter {};
991 let scorer = RefCell::new(TestScorer::new());
992 let logger = TestLogger::new();
994 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
996 let payment_preimage = PaymentPreimage([1; 32]);
997 let invoice = invoice(payment_preimage);
998 match invoice_payer.pay_invoice(&invoice) {
999 Err(PaymentError::Routing(_)) => {},
1000 Err(_) => panic!("unexpected error"),
1001 Ok(_) => panic!("expected routing error"),
1006 fn fails_paying_invoice_with_sending_errors() {
1007 let payment_preimage = PaymentPreimage([1; 32]);
1008 let invoice = invoice(payment_preimage);
1009 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1011 let payer = TestPayer::new()
1012 .fails_on_attempt(1)
1013 .expect_send(Amount::ForInvoice(final_value_msat));
1014 let router = TestRouter {};
1015 let scorer = RefCell::new(TestScorer::new());
1016 let logger = TestLogger::new();
1018 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
1020 match invoice_payer.pay_invoice(&invoice) {
1021 Err(PaymentError::Sending(_)) => {},
1022 Err(_) => panic!("unexpected error"),
1023 Ok(_) => panic!("expected sending error"),
1028 fn pays_zero_value_invoice_using_amount() {
1029 let event_handled = core::cell::RefCell::new(false);
1030 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1032 let payment_preimage = PaymentPreimage([1; 32]);
1033 let invoice = zero_value_invoice(payment_preimage);
1034 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1035 let final_value_msat = 100;
1037 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1038 let router = TestRouter {};
1039 let scorer = RefCell::new(TestScorer::new());
1040 let logger = TestLogger::new();
1042 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1045 Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
1046 assert_eq!(*payer.attempts.borrow(), 1);
1048 invoice_payer.handle_event(&Event::PaymentSent {
1049 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1051 assert_eq!(*event_handled.borrow(), true);
1052 assert_eq!(*payer.attempts.borrow(), 1);
1056 fn fails_paying_zero_value_invoice_with_amount() {
1057 let event_handled = core::cell::RefCell::new(false);
1058 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1060 let payer = TestPayer::new();
1061 let router = TestRouter {};
1062 let scorer = RefCell::new(TestScorer::new());
1063 let logger = TestLogger::new();
1065 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1067 let payment_preimage = PaymentPreimage([1; 32]);
1068 let invoice = invoice(payment_preimage);
1070 // Cannot repay an invoice pending payment.
1071 match invoice_payer.pay_zero_value_invoice(&invoice, 100) {
1072 Err(PaymentError::Invoice("amount unexpected")) => {},
1073 Err(_) => panic!("unexpected error"),
1074 Ok(_) => panic!("expected invoice error"),
1079 fn pays_pubkey_with_amount() {
1080 let event_handled = core::cell::RefCell::new(false);
1081 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1083 let pubkey = pubkey();
1084 let payment_preimage = PaymentPreimage([1; 32]);
1085 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
1086 let final_value_msat = 100;
1087 let final_cltv_expiry_delta = 42;
1089 let payer = TestPayer::new()
1090 .expect_send(Amount::Spontaneous(final_value_msat))
1091 .expect_send(Amount::OnRetry(final_value_msat));
1092 let router = TestRouter {};
1093 let scorer = RefCell::new(TestScorer::new());
1094 let logger = TestLogger::new();
1096 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1098 let payment_id = Some(invoice_payer.pay_pubkey(
1099 pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
1101 assert_eq!(*payer.attempts.borrow(), 1);
1103 let retry = RouteParameters {
1104 payee: Payee::for_keysend(pubkey),
1106 final_cltv_expiry_delta,
1108 let event = Event::PaymentPathFailed {
1111 network_update: None,
1112 rejected_by_dest: false,
1113 all_paths_failed: false,
1115 short_channel_id: None,
1118 invoice_payer.handle_event(&event);
1119 assert_eq!(*event_handled.borrow(), false);
1120 assert_eq!(*payer.attempts.borrow(), 2);
1122 invoice_payer.handle_event(&Event::PaymentSent {
1123 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1125 assert_eq!(*event_handled.borrow(), true);
1126 assert_eq!(*payer.attempts.borrow(), 2);
1130 fn scores_failed_channel() {
1131 let event_handled = core::cell::RefCell::new(false);
1132 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1134 let payment_preimage = PaymentPreimage([1; 32]);
1135 let invoice = invoice(payment_preimage);
1136 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1137 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1138 let path = TestRouter::path_for_value(final_value_msat);
1139 let short_channel_id = Some(path[0].short_channel_id);
1141 // Expect that scorer is given short_channel_id upon handling the event.
1142 let payer = TestPayer::new()
1143 .expect_send(Amount::ForInvoice(final_value_msat))
1144 .expect_send(Amount::OnRetry(final_value_msat / 2));
1145 let router = TestRouter {};
1146 let scorer = RefCell::new(TestScorer::new().expect(PaymentPath::Failure {
1147 path: path.clone(), short_channel_id: path[0].short_channel_id,
1149 let logger = TestLogger::new();
1151 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1153 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
1154 let event = Event::PaymentPathFailed {
1157 network_update: None,
1158 rejected_by_dest: false,
1159 all_paths_failed: false,
1162 retry: Some(TestRouter::retry_for_invoice(&invoice)),
1164 invoice_payer.handle_event(&event);
1168 fn scores_successful_channels() {
1169 let event_handled = core::cell::RefCell::new(false);
1170 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1172 let payment_preimage = PaymentPreimage([1; 32]);
1173 let invoice = invoice(payment_preimage);
1174 let payment_hash = Some(PaymentHash(invoice.payment_hash().clone().into_inner()));
1175 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1176 let route = TestRouter::route_for_value(final_value_msat);
1178 // Expect that scorer is given short_channel_id upon handling the event.
1179 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1180 let router = TestRouter {};
1181 let scorer = RefCell::new(TestScorer::new()
1182 .expect(PaymentPath::Success { path: route.paths[0].clone() })
1183 .expect(PaymentPath::Success { path: route.paths[1].clone() })
1185 let logger = TestLogger::new();
1187 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1189 let payment_id = invoice_payer.pay_invoice(&invoice).unwrap();
1190 let event = Event::PaymentPathSuccessful {
1191 payment_id, payment_hash, path: route.paths[0].clone()
1193 invoice_payer.handle_event(&event);
1194 let event = Event::PaymentPathSuccessful {
1195 payment_id, payment_hash, path: route.paths[1].clone()
1197 invoice_payer.handle_event(&event);
1203 fn route_for_value(final_value_msat: u64) -> Route {
1207 pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1208 channel_features: ChannelFeatures::empty(),
1209 node_features: NodeFeatures::empty(),
1210 short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1213 pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1214 channel_features: ChannelFeatures::empty(),
1215 node_features: NodeFeatures::empty(),
1216 short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1223 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1224 TestRouter::route_for_value(final_value_msat).paths[0].clone()
1227 fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
1228 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
1229 .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
1230 .with_route_hints(invoice.route_hints());
1231 if let Some(features) = invoice.features() {
1232 payee = payee.with_features(features.clone());
1234 let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1238 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1243 impl<S: Score> Router<S> for TestRouter {
1245 &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
1246 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1247 ) -> Result<Route, LightningError> {
1249 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1254 struct FailingRouter;
1256 impl<S: Score> Router<S> for FailingRouter {
1258 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1259 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1260 ) -> Result<Route, LightningError> {
1261 Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
1266 expectations: Option<VecDeque<PaymentPath>>,
1271 Failure { path: Vec<RouteHop>, short_channel_id: u64 },
1272 Success { path: Vec<RouteHop> },
1282 fn expect(mut self, expectation: PaymentPath) -> Self {
1283 self.expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
1289 impl lightning::util::ser::Writeable for TestScorer {
1290 fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); }
1293 impl Score for TestScorer {
1294 fn channel_penalty_msat(
1295 &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
1298 fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
1299 if let Some(expectations) = &mut self.expectations {
1300 match expectations.pop_front() {
1301 Some(PaymentPath::Failure { path, short_channel_id }) => {
1302 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1303 assert_eq!(actual_short_channel_id, short_channel_id);
1305 Some(PaymentPath::Success { path }) => {
1306 panic!("Unexpected successful payment path: {:?}", path)
1308 None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
1313 fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
1314 if let Some(expectations) = &mut self.expectations {
1315 match expectations.pop_front() {
1316 Some(PaymentPath::Failure { path, .. }) => {
1317 panic!("Unexpected payment path failure: {:?}", path)
1319 Some(PaymentPath::Success { path }) => {
1320 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1322 None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),
1328 impl Drop for TestScorer {
1329 fn drop(&mut self) {
1330 if std::thread::panicking() {
1334 if let Some(expectations) = &self.expectations {
1335 if !expectations.is_empty() {
1336 panic!("Unsatisfied scorer expectations: {:?}", expectations);
1343 expectations: core::cell::RefCell<VecDeque<Amount>>,
1344 attempts: core::cell::RefCell<usize>,
1345 failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
1348 #[derive(Clone, Debug, PartialEq, Eq)]
1355 struct OnAttempt(usize);
1360 expectations: core::cell::RefCell::new(VecDeque::new()),
1361 attempts: core::cell::RefCell::new(0),
1362 failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
1366 fn expect_send(self, value_msat: Amount) -> Self {
1367 self.expectations.borrow_mut().push_back(value_msat);
1371 fn fails_on_attempt(self, attempt: usize) -> Self {
1372 let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
1373 self.fails_with(failure, OnAttempt(attempt))
1376 fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
1377 self.fails_with(PaymentSendFailure::PartialFailure {
1379 failed_paths_retry: Some(retry),
1380 payment_id: PaymentId([1; 32]),
1384 fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
1385 self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
1389 fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
1390 let mut attempts = self.attempts.borrow_mut();
1393 match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1394 Some(failure) => Err(failure),
1395 None => Ok(PaymentId([1; 32])),
1399 fn check_value_msats(&self, actual_value_msats: Amount) {
1400 let expected_value_msats = self.expectations.borrow_mut().pop_front();
1401 if let Some(expected_value_msats) = expected_value_msats {
1402 assert_eq!(actual_value_msats, expected_value_msats);
1404 panic!("Unexpected amount: {:?}", actual_value_msats);
1409 impl Drop for TestPayer {
1410 fn drop(&mut self) {
1411 if std::thread::panicking() {
1415 if !self.expectations.borrow().is_empty() {
1416 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1421 impl Payer for TestPayer {
1422 fn node_id(&self) -> PublicKey {
1423 let secp_ctx = Secp256k1::new();
1424 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
1427 fn first_hops(&self) -> Vec<ChannelDetails> {
1432 &self, route: &Route, _payment_hash: PaymentHash,
1433 _payment_secret: &Option<PaymentSecret>
1434 ) -> Result<PaymentId, PaymentSendFailure> {
1435 self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1436 self.check_attempts()
1439 fn send_spontaneous_payment(
1440 &self, route: &Route, _payment_preimage: PaymentPreimage,
1441 ) -> Result<PaymentId, PaymentSendFailure> {
1442 self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1443 self.check_attempts()
1447 &self, route: &Route, _payment_id: PaymentId
1448 ) -> Result<(), PaymentSendFailure> {
1449 self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1450 self.check_attempts().map(|_| ())
1453 fn abandon_payment(&self, _payment_id: PaymentId) { }
1456 // *** Full Featured Functional Tests with a Real ChannelManager ***
1457 struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1459 impl<S: Score> Router<S> for ManualRouter {
1461 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1462 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1463 ) -> Result<Route, LightningError> {
1464 self.0.borrow_mut().pop_front().unwrap()
1468 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1469 self.0.borrow_mut().push_back(result);
1472 impl Drop for ManualRouter {
1473 fn drop(&mut self) {
1474 if std::thread::panicking() {
1477 assert!(self.0.borrow_mut().is_empty());
1482 fn retry_multi_path_single_failed_payment() {
1483 // Tests that we can/will retry after a single path of an MPP payment failed immediately
1484 let chanmon_cfgs = create_chanmon_cfgs(2);
1485 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1486 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1487 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1489 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1490 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1491 let chans = nodes[0].node.list_usable_channels();
1492 let mut route = Route {
1495 pubkey: nodes[1].node.get_our_node_id(),
1496 node_features: NodeFeatures::known(),
1497 short_channel_id: chans[0].short_channel_id.unwrap(),
1498 channel_features: ChannelFeatures::known(),
1500 cltv_expiry_delta: 100,
1503 pubkey: nodes[1].node.get_our_node_id(),
1504 node_features: NodeFeatures::known(),
1505 short_channel_id: chans[1].short_channel_id.unwrap(),
1506 channel_features: ChannelFeatures::known(),
1507 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1508 cltv_expiry_delta: 100,
1511 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1513 let router = ManualRouter(RefCell::new(VecDeque::new()));
1514 router.expect_find_route(Ok(route.clone()));
1515 // On retry, split the payment across both channels.
1516 route.paths[0][0].fee_msat = 50_000_001;
1517 route.paths[1][0].fee_msat = 50_000_000;
1518 router.expect_find_route(Ok(route.clone()));
1520 let event_handler = |_: &_| { panic!(); };
1521 let scorer = RefCell::new(TestScorer::new());
1522 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1524 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1525 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1527 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1528 assert_eq!(htlc_msgs.len(), 2);
1529 check_added_monitors!(nodes[0], 2);
1533 fn immediate_retry_on_failure() {
1534 // Tests that we can/will retry immediately after a failure
1535 let chanmon_cfgs = create_chanmon_cfgs(2);
1536 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1537 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1538 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1540 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1541 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1542 let chans = nodes[0].node.list_usable_channels();
1543 let mut route = Route {
1546 pubkey: nodes[1].node.get_our_node_id(),
1547 node_features: NodeFeatures::known(),
1548 short_channel_id: chans[0].short_channel_id.unwrap(),
1549 channel_features: ChannelFeatures::known(),
1550 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1551 cltv_expiry_delta: 100,
1554 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1556 let router = ManualRouter(RefCell::new(VecDeque::new()));
1557 router.expect_find_route(Ok(route.clone()));
1558 // On retry, split the payment across both channels.
1559 route.paths.push(route.paths[0].clone());
1560 route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
1561 route.paths[0][0].fee_msat = 50_000_000;
1562 route.paths[1][0].fee_msat = 50_000_001;
1563 router.expect_find_route(Ok(route.clone()));
1565 let event_handler = |_: &_| { panic!(); };
1566 let scorer = RefCell::new(TestScorer::new());
1567 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1569 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1570 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1572 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1573 assert_eq!(htlc_msgs.len(), 2);
1574 check_added_monitors!(nodes[0], 2);
1578 fn no_extra_retries_on_back_to_back_fail() {
1579 // In a previous release, we had a race where we may exceed the payment retry count if we
1580 // get two failures in a row with the second having `all_paths_failed` set.
1581 // Generally, when we give up trying to retry a payment, we don't know for sure what the
1582 // current state of the ChannelManager event queue is. Specifically, we cannot be sure that
1583 // there are not multiple additional `PaymentPathFailed` or even `PaymentSent` events
1584 // pending which we will see later. Thus, when we previously removed the retry tracking map
1585 // entry after a `all_paths_failed` `PaymentPathFailed` event, we may have dropped the
1586 // retry entry even though more events for the same payment were still pending. This led to
1587 // us retrying a payment again even though we'd already given up on it.
1589 // We now have a separate event - `PaymentFailed` which indicates no HTLCs remain and which
1590 // is used to remove the payment retry counter entries instead. This tests for the specific
1591 // excess-retry case while also testing `PaymentFailed` generation.
1593 let chanmon_cfgs = create_chanmon_cfgs(3);
1594 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1595 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1596 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1598 let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1599 let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1601 let mut route = Route {
1604 pubkey: nodes[1].node.get_our_node_id(),
1605 node_features: NodeFeatures::known(),
1606 short_channel_id: chan_1_scid,
1607 channel_features: ChannelFeatures::known(),
1609 cltv_expiry_delta: 100,
1611 pubkey: nodes[2].node.get_our_node_id(),
1612 node_features: NodeFeatures::known(),
1613 short_channel_id: chan_2_scid,
1614 channel_features: ChannelFeatures::known(),
1615 fee_msat: 100_000_000,
1616 cltv_expiry_delta: 100,
1619 pubkey: nodes[1].node.get_our_node_id(),
1620 node_features: NodeFeatures::known(),
1621 short_channel_id: chan_1_scid,
1622 channel_features: ChannelFeatures::known(),
1624 cltv_expiry_delta: 100,
1626 pubkey: nodes[2].node.get_our_node_id(),
1627 node_features: NodeFeatures::known(),
1628 short_channel_id: chan_2_scid,
1629 channel_features: ChannelFeatures::known(),
1630 fee_msat: 100_000_000,
1631 cltv_expiry_delta: 100,
1634 payee: Some(Payee::from_node_id(nodes[2].node.get_our_node_id())),
1636 let router = ManualRouter(RefCell::new(VecDeque::new()));
1637 router.expect_find_route(Ok(route.clone()));
1638 // On retry, we'll only be asked for one path
1639 route.paths.remove(1);
1640 router.expect_find_route(Ok(route.clone()));
1642 let expected_events: RefCell<VecDeque<&dyn Fn(&Event)>> = RefCell::new(VecDeque::new());
1643 let event_handler = |event: &Event| {
1644 let event_checker = expected_events.borrow_mut().pop_front().unwrap();
1645 event_checker(event);
1647 let scorer = RefCell::new(TestScorer::new());
1648 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1650 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1651 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1653 let htlc_updates = SendEvent::from_node(&nodes[0]);
1654 check_added_monitors!(nodes[0], 1);
1655 assert_eq!(htlc_updates.msgs.len(), 1);
1657 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
1658 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
1659 check_added_monitors!(nodes[1], 1);
1660 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1662 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
1663 check_added_monitors!(nodes[0], 1);
1664 let second_htlc_updates = SendEvent::from_node(&nodes[0]);
1666 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
1667 check_added_monitors!(nodes[0], 1);
1668 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1670 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
1671 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
1672 check_added_monitors!(nodes[1], 1);
1673 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1675 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
1676 check_added_monitors!(nodes[1], 1);
1677 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1679 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
1680 check_added_monitors!(nodes[0], 1);
1682 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
1683 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
1684 check_added_monitors!(nodes[0], 1);
1685 let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1687 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
1688 check_added_monitors!(nodes[1], 1);
1689 let bs_second_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1691 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
1692 check_added_monitors!(nodes[1], 1);
1693 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1695 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.update_fail_htlcs[0]);
1696 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.commitment_signed);
1697 check_added_monitors!(nodes[0], 1);
1699 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
1700 check_added_monitors!(nodes[0], 1);
1701 let (as_third_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1703 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
1704 check_added_monitors!(nodes[1], 1);
1705 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
1706 check_added_monitors!(nodes[1], 1);
1707 let bs_fourth_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1709 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_fourth_raa);
1710 check_added_monitors!(nodes[0], 1);
1712 // At this point A has sent two HTLCs which both failed due to lack of fee. It now has two
1713 // pending `PaymentPathFailed` events, one with `all_paths_failed` unset, and the second
1714 // with it set. The first event will use up the only retry we are allowed, with the second
1715 // `PaymentPathFailed` being passed up to the user (us, in this case). Previously, we'd
1716 // treated this as "HTLC complete" and dropped the retry counter, causing us to retry again
1717 // if the final HTLC failed.
1718 expected_events.borrow_mut().push_back(&|ev: &Event| {
1719 if let Event::PaymentPathFailed { rejected_by_dest, all_paths_failed, .. } = ev {
1720 assert!(!rejected_by_dest);
1721 assert!(all_paths_failed);
1722 } else { panic!("Unexpected event"); }
1724 nodes[0].node.process_pending_events(&invoice_payer);
1725 assert!(expected_events.borrow().is_empty());
1727 let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
1728 check_added_monitors!(nodes[0], 1);
1730 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
1731 commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
1732 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1733 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
1734 commitment_signed_dance!(nodes[0], nodes[1], &bs_fail_update.commitment_signed, false, true);
1736 expected_events.borrow_mut().push_back(&|ev: &Event| {
1737 if let Event::PaymentPathFailed { rejected_by_dest, all_paths_failed, .. } = ev {
1738 assert!(!rejected_by_dest);
1739 assert!(all_paths_failed);
1740 } else { panic!("Unexpected event"); }
1742 expected_events.borrow_mut().push_back(&|ev: &Event| {
1743 if let Event::PaymentFailed { .. } = ev {
1744 } else { panic!("Unexpected event"); }
1746 nodes[0].node.process_pending_events(&invoice_payer);
1747 assert!(expected_events.borrow().is_empty());