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!() }
70 //! # struct FakeRouter {};
71 //! # impl<S: Score> Router<S> for FakeRouter {
73 //! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
74 //! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
75 //! # ) -> Result<Route, LightningError> { unimplemented!() }
78 //! # struct FakeScorer {};
79 //! # impl Writeable for FakeScorer {
80 //! # fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> { unimplemented!(); }
82 //! # impl Score for FakeScorer {
83 //! # fn channel_penalty_msat(
84 //! # &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
86 //! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
87 //! # fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
90 //! # struct FakeLogger {};
91 //! # impl Logger for FakeLogger {
92 //! # fn log(&self, record: &Record) { unimplemented!() }
96 //! let event_handler = |event: &Event| {
98 //! Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
99 //! Event::PaymentSent { .. } => println!("payment successful"),
103 //! # let payer = FakePayer {};
104 //! # let router = FakeRouter {};
105 //! # let scorer = RefCell::new(FakeScorer {});
106 //! # let logger = FakeLogger {};
107 //! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
109 //! let invoice = "...";
110 //! let invoice = invoice.parse::<Invoice>().unwrap();
111 //! invoice_payer.pay_invoice(&invoice).unwrap();
113 //! # let event_provider = FakeEventProvider {};
115 //! event_provider.process_pending_events(&invoice_payer);
122 //! The [`Route`] is computed before each payment attempt. Any updates affecting path finding such
123 //! as updates to the network graph or changes to channel scores should be applied prior to
124 //! retries, typically by way of composing [`EventHandler`]s accordingly.
128 use bitcoin_hashes::Hash;
129 use bitcoin_hashes::sha256::Hash as Sha256;
131 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
132 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
133 use lightning::ln::msgs::LightningError;
134 use lightning::routing::scoring::{LockableScore, Score};
135 use lightning::routing::router::{Payee, Route, RouteParameters};
136 use lightning::util::events::{Event, EventHandler};
137 use lightning::util::logger::Logger;
139 use secp256k1::key::PublicKey;
141 use std::collections::hash_map::{self, HashMap};
143 use std::sync::Mutex;
144 use std::time::{Duration, SystemTime};
146 /// A utility for paying [`Invoice`]s and sending spontaneous payments.
148 /// See [module-level documentation] for details.
150 /// [module-level documentation]: crate::payment
151 pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
154 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
155 S::Target: for <'a> LockableScore<'a>,
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>;
191 /// A trait defining behavior for routing an [`Invoice`] payment.
192 pub trait Router<S: Score> {
193 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
195 &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
196 first_hops: Option<&[&ChannelDetails]>, scorer: &S
197 ) -> Result<Route, LightningError>;
200 /// Number of attempts to retry payment path failures for an [`Invoice`].
202 /// Note that this is the number of *path* failures, not full payment retries. For multi-path
203 /// payments, if this is less than the total number of paths, we will never even retry all of the
205 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
206 pub struct RetryAttempts(pub usize);
208 /// An error that may occur when making a payment.
209 #[derive(Clone, Debug)]
210 pub enum PaymentError {
211 /// An error resulting from the provided [`Invoice`] or payment hash.
212 Invoice(&'static str),
213 /// An error occurring when finding a route.
214 Routing(LightningError),
215 /// An error occurring when sending a payment.
216 Sending(PaymentSendFailure),
219 impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
222 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
223 S::Target: for <'a> LockableScore<'a>,
227 /// Creates an invoice payer that retries failed payment paths.
229 /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
230 /// `retry_attempts` has been exceeded for a given [`Invoice`].
232 payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
240 payment_cache: Mutex::new(HashMap::new()),
245 /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
247 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
248 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
250 pub fn pay_invoice(&self, invoice: &Invoice) -> Result<PaymentId, PaymentError> {
251 if invoice.amount_milli_satoshis().is_none() {
252 Err(PaymentError::Invoice("amount missing"))
254 self.pay_invoice_using_amount(invoice, None)
258 /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
259 /// case a retry is needed.
261 /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
262 /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
264 pub fn pay_zero_value_invoice(
265 &self, invoice: &Invoice, amount_msats: u64
266 ) -> Result<PaymentId, PaymentError> {
267 if invoice.amount_milli_satoshis().is_some() {
268 Err(PaymentError::Invoice("amount unexpected"))
270 self.pay_invoice_using_amount(invoice, Some(amount_msats))
274 fn pay_invoice_using_amount(
275 &self, invoice: &Invoice, amount_msats: Option<u64>
276 ) -> Result<PaymentId, PaymentError> {
277 debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
279 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
280 match self.payment_cache.lock().unwrap().entry(payment_hash) {
281 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
282 hash_map::Entry::Vacant(entry) => entry.insert(0),
285 let payment_secret = Some(invoice.payment_secret().clone());
286 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
287 .with_expiry_time(expiry_time_from_unix_epoch(&invoice).as_secs())
288 .with_route_hints(invoice.route_hints());
289 if let Some(features) = invoice.features() {
290 payee = payee.with_features(features.clone());
292 let params = RouteParameters {
294 final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
295 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
298 let send_payment = |route: &Route| {
299 self.payer.send_payment(route, payment_hash, &payment_secret)
301 self.pay_internal(¶ms, payment_hash, send_payment)
302 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
305 /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
306 /// case a retry is needed.
308 /// You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
309 /// been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
311 &self, pubkey: PublicKey, payment_preimage: PaymentPreimage, amount_msats: u64,
312 final_cltv_expiry_delta: u32
313 ) -> Result<PaymentId, PaymentError> {
314 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
315 match self.payment_cache.lock().unwrap().entry(payment_hash) {
316 hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
317 hash_map::Entry::Vacant(entry) => entry.insert(0),
320 let params = RouteParameters {
321 payee: Payee::for_keysend(pubkey),
322 final_value_msat: amount_msats,
323 final_cltv_expiry_delta,
326 let send_payment = |route: &Route| {
327 self.payer.send_spontaneous_payment(route, payment_preimage)
329 self.pay_internal(¶ms, payment_hash, send_payment)
330 .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
333 fn pay_internal<F: FnOnce(&Route) -> Result<PaymentId, PaymentSendFailure> + Copy>(
334 &self, params: &RouteParameters, payment_hash: PaymentHash, send_payment: F,
335 ) -> Result<PaymentId, PaymentError> {
336 if has_expired(params) {
337 log_trace!(self.logger, "Invoice expired prior to send for payment {}", log_bytes!(payment_hash.0));
338 return Err(PaymentError::Invoice("Invoice expired prior to send"));
341 let payer = self.payer.node_id();
342 let first_hops = self.payer.first_hops();
343 let route = self.router.find_route(
344 &payer, params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
346 ).map_err(|e| PaymentError::Routing(e))?;
348 match send_payment(&route) {
349 Ok(payment_id) => Ok(payment_id),
351 PaymentSendFailure::ParameterError(_) => Err(e),
352 PaymentSendFailure::PathParameterError(_) => Err(e),
353 PaymentSendFailure::AllFailedRetrySafe(_) => {
354 let mut payment_cache = self.payment_cache.lock().unwrap();
355 let retry_count = payment_cache.get_mut(&payment_hash).unwrap();
356 if *retry_count >= self.retry_attempts.0 {
360 std::mem::drop(payment_cache);
361 Ok(self.pay_internal(params, payment_hash, send_payment)?)
364 PaymentSendFailure::PartialFailure { failed_paths_retry, payment_id, .. } => {
365 if let Some(retry_data) = failed_paths_retry {
366 // Some paths were sent, even if we failed to send the full MPP value our
367 // recipient may misbehave and claim the funds, at which point we have to
368 // consider the payment sent, so return `Ok()` here, ignoring any retry
370 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
373 // This may happen if we send a payment and some paths fail, but
374 // only due to a temporary monitor failure or the like, implying
375 // they're really in-flight, but we haven't sent the initial
376 // HTLC-Add messages yet.
381 }.map_err(|e| PaymentError::Sending(e))
385 &self, payment_id: PaymentId, payment_hash: PaymentHash, params: &RouteParameters
386 ) -> Result<(), ()> {
387 let max_payment_attempts = self.retry_attempts.0 + 1;
388 let attempts = *self.payment_cache.lock().unwrap()
390 .and_modify(|attempts| *attempts += 1)
393 if attempts >= max_payment_attempts {
394 log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
398 if has_expired(params) {
399 log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
403 let payer = self.payer.node_id();
404 let first_hops = self.payer.first_hops();
405 let route = self.router.find_route(
406 &payer, ¶ms, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
410 log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
414 match self.payer.retry_payment(&route.unwrap(), payment_id) {
416 Err(PaymentSendFailure::ParameterError(_)) |
417 Err(PaymentSendFailure::PathParameterError(_)) => {
418 log_trace!(self.logger, "Failed to retry for payment {} due to bogus route/payment data, not retrying.", log_bytes!(payment_hash.0));
421 Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
422 self.retry_payment(payment_id, payment_hash, params)
424 Err(PaymentSendFailure::PartialFailure { failed_paths_retry, .. }) => {
425 if let Some(retry) = failed_paths_retry {
426 // Always return Ok for the same reason as noted in pay_internal.
427 let _ = self.retry_payment(payment_id, payment_hash, &retry);
434 /// Removes the payment cached by the given payment hash.
436 /// Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
437 /// [`EventHandler`]. Otherwise, calling this method is unnecessary.
438 pub fn remove_cached_payment(&self, payment_hash: &PaymentHash) {
439 self.payment_cache.lock().unwrap().remove(payment_hash);
443 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
444 invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
447 fn has_expired(params: &RouteParameters) -> bool {
448 if let Some(expiry_time) = params.payee.expiry_time {
449 Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, Duration::from_secs(expiry_time))
453 impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
456 R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
457 S::Target: for <'a> LockableScore<'a>,
461 fn handle_event(&self, event: &Event) {
463 Event::PaymentPathFailed {
464 all_paths_failed, payment_id, payment_hash, rejected_by_dest, path,
465 short_channel_id, retry, ..
467 if let Some(short_channel_id) = short_channel_id {
468 let path = path.iter().collect::<Vec<_>>();
469 self.scorer.lock().payment_path_failed(&path, *short_channel_id);
472 if *rejected_by_dest {
473 log_trace!(self.logger, "Payment {} rejected by destination; not retrying", log_bytes!(payment_hash.0));
474 } else if payment_id.is_none() {
475 log_trace!(self.logger, "Payment {} has no id; not retrying", log_bytes!(payment_hash.0));
476 } else if retry.is_none() {
477 log_trace!(self.logger, "Payment {} missing retry params; not retrying", log_bytes!(payment_hash.0));
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.
483 if *all_paths_failed { self.payment_cache.lock().unwrap().remove(payment_hash); }
485 Event::PaymentPathSuccessful { path, .. } => {
486 let path = path.iter().collect::<Vec<_>>();
487 self.scorer.lock().payment_path_successful(&path);
489 Event::PaymentSent { payment_hash, .. } => {
490 let mut payment_cache = self.payment_cache.lock().unwrap();
491 let attempts = payment_cache
492 .remove(payment_hash)
493 .map_or(1, |attempts| attempts + 1);
494 log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
499 // Delegate to the decorated event handler unless the payment is retried.
500 self.event_handler.handle_event(event)
507 use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
508 use utils::create_invoice_from_channelmanager;
509 use bitcoin_hashes::sha256::Hash as Sha256;
510 use lightning::ln::PaymentPreimage;
511 use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
512 use lightning::ln::functional_test_utils::*;
513 use lightning::ln::msgs::{ErrorAction, LightningError};
514 use lightning::routing::network_graph::NodeId;
515 use lightning::routing::router::{Payee, Route, RouteHop};
516 use lightning::util::test_utils::TestLogger;
517 use lightning::util::errors::APIError;
518 use lightning::util::events::{Event, MessageSendEventsProvider};
519 use secp256k1::{SecretKey, PublicKey, Secp256k1};
520 use std::cell::RefCell;
521 use std::collections::VecDeque;
522 use std::time::{SystemTime, Duration};
524 fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
525 let payment_hash = Sha256::hash(&payment_preimage.0);
526 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
527 InvoiceBuilder::new(Currency::Bitcoin)
528 .description("test".into())
529 .payment_hash(payment_hash)
530 .payment_secret(PaymentSecret([0; 32]))
532 .min_final_cltv_expiry(144)
533 .amount_milli_satoshis(128)
534 .build_signed(|hash| {
535 Secp256k1::new().sign_recoverable(hash, &private_key)
540 fn zero_value_invoice(payment_preimage: PaymentPreimage) -> Invoice {
541 let payment_hash = Sha256::hash(&payment_preimage.0);
542 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
543 InvoiceBuilder::new(Currency::Bitcoin)
544 .description("test".into())
545 .payment_hash(payment_hash)
546 .payment_secret(PaymentSecret([0; 32]))
548 .min_final_cltv_expiry(144)
549 .build_signed(|hash| {
550 Secp256k1::new().sign_recoverable(hash, &private_key)
555 fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
556 let payment_hash = Sha256::hash(&payment_preimage.0);
557 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
558 let timestamp = SystemTime::now()
559 .checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
561 InvoiceBuilder::new(Currency::Bitcoin)
562 .description("test".into())
563 .payment_hash(payment_hash)
564 .payment_secret(PaymentSecret([0; 32]))
565 .timestamp(timestamp)
566 .min_final_cltv_expiry(144)
567 .amount_milli_satoshis(128)
568 .build_signed(|hash| {
569 Secp256k1::new().sign_recoverable(hash, &private_key)
574 fn pubkey() -> PublicKey {
575 PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()
579 fn pays_invoice_on_first_attempt() {
580 let event_handled = core::cell::RefCell::new(false);
581 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
583 let payment_preimage = PaymentPreimage([1; 32]);
584 let invoice = invoice(payment_preimage);
585 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
586 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
588 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
589 let router = TestRouter {};
590 let scorer = RefCell::new(TestScorer::new());
591 let logger = TestLogger::new();
593 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
595 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
596 assert_eq!(*payer.attempts.borrow(), 1);
598 invoice_payer.handle_event(&Event::PaymentSent {
599 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
601 assert_eq!(*event_handled.borrow(), true);
602 assert_eq!(*payer.attempts.borrow(), 1);
606 fn pays_invoice_on_retry() {
607 let event_handled = core::cell::RefCell::new(false);
608 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
610 let payment_preimage = PaymentPreimage([1; 32]);
611 let invoice = invoice(payment_preimage);
612 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
613 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
615 let payer = TestPayer::new()
616 .expect_send(Amount::ForInvoice(final_value_msat))
617 .expect_send(Amount::OnRetry(final_value_msat / 2));
618 let router = TestRouter {};
619 let scorer = RefCell::new(TestScorer::new());
620 let logger = TestLogger::new();
622 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
624 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
625 assert_eq!(*payer.attempts.borrow(), 1);
627 let event = Event::PaymentPathFailed {
630 network_update: None,
631 rejected_by_dest: false,
632 all_paths_failed: false,
633 path: TestRouter::path_for_value(final_value_msat),
634 short_channel_id: None,
635 retry: Some(TestRouter::retry_for_invoice(&invoice)),
637 invoice_payer.handle_event(&event);
638 assert_eq!(*event_handled.borrow(), false);
639 assert_eq!(*payer.attempts.borrow(), 2);
641 invoice_payer.handle_event(&Event::PaymentSent {
642 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
644 assert_eq!(*event_handled.borrow(), true);
645 assert_eq!(*payer.attempts.borrow(), 2);
649 fn pays_invoice_on_partial_failure() {
650 let event_handler = |_: &_| { panic!() };
652 let payment_preimage = PaymentPreimage([1; 32]);
653 let invoice = invoice(payment_preimage);
654 let retry = TestRouter::retry_for_invoice(&invoice);
655 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
657 let payer = TestPayer::new()
658 .fails_with_partial_failure(retry.clone(), OnAttempt(1))
659 .fails_with_partial_failure(retry, OnAttempt(2))
660 .expect_send(Amount::ForInvoice(final_value_msat))
661 .expect_send(Amount::OnRetry(final_value_msat / 2))
662 .expect_send(Amount::OnRetry(final_value_msat / 2));
663 let router = TestRouter {};
664 let scorer = RefCell::new(TestScorer::new());
665 let logger = TestLogger::new();
667 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
669 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
673 fn retries_payment_path_for_unknown_payment() {
674 let event_handled = core::cell::RefCell::new(false);
675 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
677 let payment_preimage = PaymentPreimage([1; 32]);
678 let invoice = invoice(payment_preimage);
679 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
680 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
682 let payer = TestPayer::new()
683 .expect_send(Amount::OnRetry(final_value_msat / 2))
684 .expect_send(Amount::OnRetry(final_value_msat / 2));
685 let router = TestRouter {};
686 let scorer = RefCell::new(TestScorer::new());
687 let logger = TestLogger::new();
689 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
691 let payment_id = Some(PaymentId([1; 32]));
692 let event = Event::PaymentPathFailed {
695 network_update: None,
696 rejected_by_dest: false,
697 all_paths_failed: false,
698 path: TestRouter::path_for_value(final_value_msat),
699 short_channel_id: None,
700 retry: Some(TestRouter::retry_for_invoice(&invoice)),
702 invoice_payer.handle_event(&event);
703 assert_eq!(*event_handled.borrow(), false);
704 assert_eq!(*payer.attempts.borrow(), 1);
706 invoice_payer.handle_event(&event);
707 assert_eq!(*event_handled.borrow(), false);
708 assert_eq!(*payer.attempts.borrow(), 2);
710 invoice_payer.handle_event(&Event::PaymentSent {
711 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
713 assert_eq!(*event_handled.borrow(), true);
714 assert_eq!(*payer.attempts.borrow(), 2);
718 fn fails_paying_invoice_after_max_retries() {
719 let event_handled = core::cell::RefCell::new(false);
720 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
722 let payment_preimage = PaymentPreimage([1; 32]);
723 let invoice = invoice(payment_preimage);
724 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
726 let payer = TestPayer::new()
727 .expect_send(Amount::ForInvoice(final_value_msat))
728 .expect_send(Amount::OnRetry(final_value_msat / 2))
729 .expect_send(Amount::OnRetry(final_value_msat / 2));
730 let router = TestRouter {};
731 let scorer = RefCell::new(TestScorer::new());
732 let logger = TestLogger::new();
734 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
736 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
737 assert_eq!(*payer.attempts.borrow(), 1);
739 let event = Event::PaymentPathFailed {
741 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
742 network_update: None,
743 rejected_by_dest: false,
744 all_paths_failed: true,
745 path: TestRouter::path_for_value(final_value_msat),
746 short_channel_id: None,
747 retry: Some(TestRouter::retry_for_invoice(&invoice)),
749 invoice_payer.handle_event(&event);
750 assert_eq!(*event_handled.borrow(), false);
751 assert_eq!(*payer.attempts.borrow(), 2);
753 let event = Event::PaymentPathFailed {
755 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
756 network_update: None,
757 rejected_by_dest: false,
758 all_paths_failed: false,
759 path: TestRouter::path_for_value(final_value_msat / 2),
760 short_channel_id: None,
761 retry: Some(RouteParameters {
762 final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
765 invoice_payer.handle_event(&event);
766 assert_eq!(*event_handled.borrow(), false);
767 assert_eq!(*payer.attempts.borrow(), 3);
769 invoice_payer.handle_event(&event);
770 assert_eq!(*event_handled.borrow(), true);
771 assert_eq!(*payer.attempts.borrow(), 3);
775 fn fails_paying_invoice_with_missing_retry_params() {
776 let event_handled = core::cell::RefCell::new(false);
777 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
779 let payment_preimage = PaymentPreimage([1; 32]);
780 let invoice = invoice(payment_preimage);
781 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
783 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
784 let router = TestRouter {};
785 let scorer = RefCell::new(TestScorer::new());
786 let logger = TestLogger::new();
788 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
790 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
791 assert_eq!(*payer.attempts.borrow(), 1);
793 let event = Event::PaymentPathFailed {
795 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
796 network_update: None,
797 rejected_by_dest: false,
798 all_paths_failed: false,
800 short_channel_id: None,
803 invoice_payer.handle_event(&event);
804 assert_eq!(*event_handled.borrow(), true);
805 assert_eq!(*payer.attempts.borrow(), 1);
809 fn fails_paying_invoice_after_expiration() {
810 let event_handled = core::cell::RefCell::new(false);
811 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
813 let payer = TestPayer::new();
814 let router = TestRouter {};
815 let scorer = RefCell::new(TestScorer::new());
816 let logger = TestLogger::new();
818 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
820 let payment_preimage = PaymentPreimage([1; 32]);
821 let invoice = expired_invoice(payment_preimage);
822 if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
823 assert_eq!(msg, "Invoice expired prior to send");
824 } else { panic!("Expected Invoice Error"); }
828 fn fails_retrying_invoice_after_expiration() {
829 let event_handled = core::cell::RefCell::new(false);
830 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
832 let payment_preimage = PaymentPreimage([1; 32]);
833 let invoice = invoice(payment_preimage);
834 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
836 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
837 let router = TestRouter {};
838 let scorer = RefCell::new(TestScorer::new());
839 let logger = TestLogger::new();
841 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
843 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
844 assert_eq!(*payer.attempts.borrow(), 1);
846 let mut retry_data = TestRouter::retry_for_invoice(&invoice);
847 retry_data.payee.expiry_time = Some(SystemTime::now()
848 .checked_sub(Duration::from_secs(2)).unwrap()
849 .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
850 let event = Event::PaymentPathFailed {
852 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
853 network_update: None,
854 rejected_by_dest: false,
855 all_paths_failed: false,
857 short_channel_id: None,
858 retry: Some(retry_data),
860 invoice_payer.handle_event(&event);
861 assert_eq!(*event_handled.borrow(), true);
862 assert_eq!(*payer.attempts.borrow(), 1);
866 fn fails_paying_invoice_after_retry_error() {
867 let event_handled = core::cell::RefCell::new(false);
868 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
870 let payment_preimage = PaymentPreimage([1; 32]);
871 let invoice = invoice(payment_preimage);
872 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
874 let payer = TestPayer::new()
876 .expect_send(Amount::ForInvoice(final_value_msat))
877 .expect_send(Amount::OnRetry(final_value_msat / 2));
878 let router = TestRouter {};
879 let scorer = RefCell::new(TestScorer::new());
880 let logger = TestLogger::new();
882 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
884 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
885 assert_eq!(*payer.attempts.borrow(), 1);
887 let event = Event::PaymentPathFailed {
889 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
890 network_update: None,
891 rejected_by_dest: false,
892 all_paths_failed: false,
893 path: TestRouter::path_for_value(final_value_msat / 2),
894 short_channel_id: None,
895 retry: Some(TestRouter::retry_for_invoice(&invoice)),
897 invoice_payer.handle_event(&event);
898 assert_eq!(*event_handled.borrow(), true);
899 assert_eq!(*payer.attempts.borrow(), 2);
903 fn fails_paying_invoice_after_rejected_by_payee() {
904 let event_handled = core::cell::RefCell::new(false);
905 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
907 let payment_preimage = PaymentPreimage([1; 32]);
908 let invoice = invoice(payment_preimage);
909 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
911 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
912 let router = TestRouter {};
913 let scorer = RefCell::new(TestScorer::new());
914 let logger = TestLogger::new();
916 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
918 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
919 assert_eq!(*payer.attempts.borrow(), 1);
921 let event = Event::PaymentPathFailed {
923 payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
924 network_update: None,
925 rejected_by_dest: true,
926 all_paths_failed: false,
928 short_channel_id: None,
929 retry: Some(TestRouter::retry_for_invoice(&invoice)),
931 invoice_payer.handle_event(&event);
932 assert_eq!(*event_handled.borrow(), true);
933 assert_eq!(*payer.attempts.borrow(), 1);
937 fn fails_repaying_invoice_with_pending_payment() {
938 let event_handled = core::cell::RefCell::new(false);
939 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
941 let payment_preimage = PaymentPreimage([1; 32]);
942 let invoice = invoice(payment_preimage);
943 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
945 let payer = TestPayer::new()
946 .expect_send(Amount::ForInvoice(final_value_msat))
947 .expect_send(Amount::ForInvoice(final_value_msat));
948 let router = TestRouter {};
949 let scorer = RefCell::new(TestScorer::new());
950 let logger = TestLogger::new();
952 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
954 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
956 // Cannot repay an invoice pending payment.
957 match invoice_payer.pay_invoice(&invoice) {
958 Err(PaymentError::Invoice("payment pending")) => {},
959 Err(_) => panic!("unexpected error"),
960 Ok(_) => panic!("expected invoice error"),
963 // Can repay an invoice once cleared from cache.
964 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
965 invoice_payer.remove_cached_payment(&payment_hash);
966 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
968 // Cannot retry paying an invoice if cleared from cache.
969 invoice_payer.remove_cached_payment(&payment_hash);
970 let event = Event::PaymentPathFailed {
973 network_update: None,
974 rejected_by_dest: false,
975 all_paths_failed: false,
977 short_channel_id: None,
978 retry: Some(TestRouter::retry_for_invoice(&invoice)),
980 invoice_payer.handle_event(&event);
981 assert_eq!(*event_handled.borrow(), true);
985 fn fails_paying_invoice_with_routing_errors() {
986 let payer = TestPayer::new();
987 let router = FailingRouter {};
988 let scorer = RefCell::new(TestScorer::new());
989 let logger = TestLogger::new();
991 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
993 let payment_preimage = PaymentPreimage([1; 32]);
994 let invoice = invoice(payment_preimage);
995 match invoice_payer.pay_invoice(&invoice) {
996 Err(PaymentError::Routing(_)) => {},
997 Err(_) => panic!("unexpected error"),
998 Ok(_) => panic!("expected routing error"),
1003 fn fails_paying_invoice_with_sending_errors() {
1004 let payment_preimage = PaymentPreimage([1; 32]);
1005 let invoice = invoice(payment_preimage);
1006 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1008 let payer = TestPayer::new()
1009 .fails_on_attempt(1)
1010 .expect_send(Amount::ForInvoice(final_value_msat));
1011 let router = TestRouter {};
1012 let scorer = RefCell::new(TestScorer::new());
1013 let logger = TestLogger::new();
1015 InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
1017 match invoice_payer.pay_invoice(&invoice) {
1018 Err(PaymentError::Sending(_)) => {},
1019 Err(_) => panic!("unexpected error"),
1020 Ok(_) => panic!("expected sending error"),
1025 fn pays_zero_value_invoice_using_amount() {
1026 let event_handled = core::cell::RefCell::new(false);
1027 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1029 let payment_preimage = PaymentPreimage([1; 32]);
1030 let invoice = zero_value_invoice(payment_preimage);
1031 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1032 let final_value_msat = 100;
1034 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1035 let router = TestRouter {};
1036 let scorer = RefCell::new(TestScorer::new());
1037 let logger = TestLogger::new();
1039 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1042 Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
1043 assert_eq!(*payer.attempts.borrow(), 1);
1045 invoice_payer.handle_event(&Event::PaymentSent {
1046 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1048 assert_eq!(*event_handled.borrow(), true);
1049 assert_eq!(*payer.attempts.borrow(), 1);
1053 fn fails_paying_zero_value_invoice_with_amount() {
1054 let event_handled = core::cell::RefCell::new(false);
1055 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1057 let payer = TestPayer::new();
1058 let router = TestRouter {};
1059 let scorer = RefCell::new(TestScorer::new());
1060 let logger = TestLogger::new();
1062 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1064 let payment_preimage = PaymentPreimage([1; 32]);
1065 let invoice = invoice(payment_preimage);
1067 // Cannot repay an invoice pending payment.
1068 match invoice_payer.pay_zero_value_invoice(&invoice, 100) {
1069 Err(PaymentError::Invoice("amount unexpected")) => {},
1070 Err(_) => panic!("unexpected error"),
1071 Ok(_) => panic!("expected invoice error"),
1076 fn pays_pubkey_with_amount() {
1077 let event_handled = core::cell::RefCell::new(false);
1078 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1080 let pubkey = pubkey();
1081 let payment_preimage = PaymentPreimage([1; 32]);
1082 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
1083 let final_value_msat = 100;
1084 let final_cltv_expiry_delta = 42;
1086 let payer = TestPayer::new()
1087 .expect_send(Amount::Spontaneous(final_value_msat))
1088 .expect_send(Amount::OnRetry(final_value_msat));
1089 let router = TestRouter {};
1090 let scorer = RefCell::new(TestScorer::new());
1091 let logger = TestLogger::new();
1093 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1095 let payment_id = Some(invoice_payer.pay_pubkey(
1096 pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
1098 assert_eq!(*payer.attempts.borrow(), 1);
1100 let retry = RouteParameters {
1101 payee: Payee::for_keysend(pubkey),
1103 final_cltv_expiry_delta,
1105 let event = Event::PaymentPathFailed {
1108 network_update: None,
1109 rejected_by_dest: false,
1110 all_paths_failed: false,
1112 short_channel_id: None,
1115 invoice_payer.handle_event(&event);
1116 assert_eq!(*event_handled.borrow(), false);
1117 assert_eq!(*payer.attempts.borrow(), 2);
1119 invoice_payer.handle_event(&Event::PaymentSent {
1120 payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1122 assert_eq!(*event_handled.borrow(), true);
1123 assert_eq!(*payer.attempts.borrow(), 2);
1127 fn scores_failed_channel() {
1128 let event_handled = core::cell::RefCell::new(false);
1129 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1131 let payment_preimage = PaymentPreimage([1; 32]);
1132 let invoice = invoice(payment_preimage);
1133 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1134 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1135 let path = TestRouter::path_for_value(final_value_msat);
1136 let short_channel_id = Some(path[0].short_channel_id);
1138 // Expect that scorer is given short_channel_id upon handling the event.
1139 let payer = TestPayer::new()
1140 .expect_send(Amount::ForInvoice(final_value_msat))
1141 .expect_send(Amount::OnRetry(final_value_msat / 2));
1142 let router = TestRouter {};
1143 let scorer = RefCell::new(TestScorer::new().expect(PaymentPath::Failure {
1144 path: path.clone(), short_channel_id: path[0].short_channel_id,
1146 let logger = TestLogger::new();
1148 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1150 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
1151 let event = Event::PaymentPathFailed {
1154 network_update: None,
1155 rejected_by_dest: false,
1156 all_paths_failed: false,
1159 retry: Some(TestRouter::retry_for_invoice(&invoice)),
1161 invoice_payer.handle_event(&event);
1165 fn scores_successful_channels() {
1166 let event_handled = core::cell::RefCell::new(false);
1167 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1169 let payment_preimage = PaymentPreimage([1; 32]);
1170 let invoice = invoice(payment_preimage);
1171 let payment_hash = Some(PaymentHash(invoice.payment_hash().clone().into_inner()));
1172 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1173 let route = TestRouter::route_for_value(final_value_msat);
1175 // Expect that scorer is given short_channel_id upon handling the event.
1176 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1177 let router = TestRouter {};
1178 let scorer = RefCell::new(TestScorer::new()
1179 .expect(PaymentPath::Success { path: route.paths[0].clone() })
1180 .expect(PaymentPath::Success { path: route.paths[1].clone() })
1182 let logger = TestLogger::new();
1184 InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1186 let payment_id = invoice_payer.pay_invoice(&invoice).unwrap();
1187 let event = Event::PaymentPathSuccessful {
1188 payment_id, payment_hash, path: route.paths[0].clone()
1190 invoice_payer.handle_event(&event);
1191 let event = Event::PaymentPathSuccessful {
1192 payment_id, payment_hash, path: route.paths[1].clone()
1194 invoice_payer.handle_event(&event);
1200 fn route_for_value(final_value_msat: u64) -> Route {
1204 pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1205 channel_features: ChannelFeatures::empty(),
1206 node_features: NodeFeatures::empty(),
1207 short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1210 pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1211 channel_features: ChannelFeatures::empty(),
1212 node_features: NodeFeatures::empty(),
1213 short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1220 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1221 TestRouter::route_for_value(final_value_msat).paths[0].clone()
1224 fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
1225 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
1226 .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
1227 .with_route_hints(invoice.route_hints());
1228 if let Some(features) = invoice.features() {
1229 payee = payee.with_features(features.clone());
1231 let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1235 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1240 impl<S: Score> Router<S> for TestRouter {
1242 &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
1243 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1244 ) -> Result<Route, LightningError> {
1246 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1251 struct FailingRouter;
1253 impl<S: Score> Router<S> for FailingRouter {
1255 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1256 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1257 ) -> Result<Route, LightningError> {
1258 Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
1263 expectations: Option<VecDeque<PaymentPath>>,
1268 Failure { path: Vec<RouteHop>, short_channel_id: u64 },
1269 Success { path: Vec<RouteHop> },
1279 fn expect(mut self, expectation: PaymentPath) -> Self {
1280 self.expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
1286 impl lightning::util::ser::Writeable for TestScorer {
1287 fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); }
1290 impl Score for TestScorer {
1291 fn channel_penalty_msat(
1292 &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
1295 fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
1296 if let Some(expectations) = &mut self.expectations {
1297 match expectations.pop_front() {
1298 Some(PaymentPath::Failure { path, short_channel_id }) => {
1299 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1300 assert_eq!(actual_short_channel_id, short_channel_id);
1302 Some(PaymentPath::Success { path }) => {
1303 panic!("Unexpected successful payment path: {:?}", path)
1305 None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
1310 fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
1311 if let Some(expectations) = &mut self.expectations {
1312 match expectations.pop_front() {
1313 Some(PaymentPath::Failure { path, .. }) => {
1314 panic!("Unexpected payment path failure: {:?}", path)
1316 Some(PaymentPath::Success { path }) => {
1317 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1319 None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),
1325 impl Drop for TestScorer {
1326 fn drop(&mut self) {
1327 if std::thread::panicking() {
1331 if let Some(expectations) = &self.expectations {
1332 if !expectations.is_empty() {
1333 panic!("Unsatisfied scorer expectations: {:?}", expectations);
1340 expectations: core::cell::RefCell<VecDeque<Amount>>,
1341 attempts: core::cell::RefCell<usize>,
1342 failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
1345 #[derive(Clone, Debug, PartialEq, Eq)]
1352 struct OnAttempt(usize);
1357 expectations: core::cell::RefCell::new(VecDeque::new()),
1358 attempts: core::cell::RefCell::new(0),
1359 failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
1363 fn expect_send(self, value_msat: Amount) -> Self {
1364 self.expectations.borrow_mut().push_back(value_msat);
1368 fn fails_on_attempt(self, attempt: usize) -> Self {
1369 let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
1370 self.fails_with(failure, OnAttempt(attempt))
1373 fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
1374 self.fails_with(PaymentSendFailure::PartialFailure {
1376 failed_paths_retry: Some(retry),
1377 payment_id: PaymentId([1; 32]),
1381 fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
1382 self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
1386 fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
1387 let mut attempts = self.attempts.borrow_mut();
1390 match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1391 Some(failure) => Err(failure),
1392 None => Ok(PaymentId([1; 32])),
1396 fn check_value_msats(&self, actual_value_msats: Amount) {
1397 let expected_value_msats = self.expectations.borrow_mut().pop_front();
1398 if let Some(expected_value_msats) = expected_value_msats {
1399 assert_eq!(actual_value_msats, expected_value_msats);
1401 panic!("Unexpected amount: {:?}", actual_value_msats);
1406 impl Drop for TestPayer {
1407 fn drop(&mut self) {
1408 if std::thread::panicking() {
1412 if !self.expectations.borrow().is_empty() {
1413 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1418 impl Payer for TestPayer {
1419 fn node_id(&self) -> PublicKey {
1420 let secp_ctx = Secp256k1::new();
1421 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
1424 fn first_hops(&self) -> Vec<ChannelDetails> {
1429 &self, route: &Route, _payment_hash: PaymentHash,
1430 _payment_secret: &Option<PaymentSecret>
1431 ) -> Result<PaymentId, PaymentSendFailure> {
1432 self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1433 self.check_attempts()
1436 fn send_spontaneous_payment(
1437 &self, route: &Route, _payment_preimage: PaymentPreimage,
1438 ) -> Result<PaymentId, PaymentSendFailure> {
1439 self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1440 self.check_attempts()
1444 &self, route: &Route, _payment_id: PaymentId
1445 ) -> Result<(), PaymentSendFailure> {
1446 self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1447 self.check_attempts().map(|_| ())
1451 // *** Full Featured Functional Tests with a Real ChannelManager ***
1452 struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1454 impl<S: Score> Router<S> for ManualRouter {
1456 &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1457 _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1458 ) -> Result<Route, LightningError> {
1459 self.0.borrow_mut().pop_front().unwrap()
1463 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1464 self.0.borrow_mut().push_back(result);
1467 impl Drop for ManualRouter {
1468 fn drop(&mut self) {
1469 if std::thread::panicking() {
1472 assert!(self.0.borrow_mut().is_empty());
1477 fn retry_multi_path_single_failed_payment() {
1478 // Tests that we can/will retry after a single path of an MPP payment failed immediately
1479 let chanmon_cfgs = create_chanmon_cfgs(2);
1480 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1481 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1482 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1484 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1485 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1486 let chans = nodes[0].node.list_usable_channels();
1487 let mut route = Route {
1490 pubkey: nodes[1].node.get_our_node_id(),
1491 node_features: NodeFeatures::known(),
1492 short_channel_id: chans[0].short_channel_id.unwrap(),
1493 channel_features: ChannelFeatures::known(),
1495 cltv_expiry_delta: 100,
1498 pubkey: nodes[1].node.get_our_node_id(),
1499 node_features: NodeFeatures::known(),
1500 short_channel_id: chans[1].short_channel_id.unwrap(),
1501 channel_features: ChannelFeatures::known(),
1502 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1503 cltv_expiry_delta: 100,
1506 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1508 let router = ManualRouter(RefCell::new(VecDeque::new()));
1509 router.expect_find_route(Ok(route.clone()));
1510 // On retry, split the payment across both channels.
1511 route.paths[0][0].fee_msat = 50_000_001;
1512 route.paths[1][0].fee_msat = 50_000_000;
1513 router.expect_find_route(Ok(route.clone()));
1515 let event_handler = |_: &_| { panic!(); };
1516 let scorer = RefCell::new(TestScorer::new());
1517 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1519 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1520 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1522 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1523 assert_eq!(htlc_msgs.len(), 2);
1524 check_added_monitors!(nodes[0], 2);
1528 fn immediate_retry_on_failure() {
1529 // Tests that we can/will retry immediately after a failure
1530 let chanmon_cfgs = create_chanmon_cfgs(2);
1531 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1532 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1533 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1535 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1536 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1537 let chans = nodes[0].node.list_usable_channels();
1538 let mut route = Route {
1541 pubkey: nodes[1].node.get_our_node_id(),
1542 node_features: NodeFeatures::known(),
1543 short_channel_id: chans[0].short_channel_id.unwrap(),
1544 channel_features: ChannelFeatures::known(),
1545 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1546 cltv_expiry_delta: 100,
1549 payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1551 let router = ManualRouter(RefCell::new(VecDeque::new()));
1552 router.expect_find_route(Ok(route.clone()));
1553 // On retry, split the payment across both channels.
1554 route.paths.push(route.paths[0].clone());
1555 route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
1556 route.paths[0][0].fee_msat = 50_000_000;
1557 route.paths[1][0].fee_msat = 50_000_001;
1558 router.expect_find_route(Ok(route.clone()));
1560 let event_handler = |_: &_| { panic!(); };
1561 let scorer = RefCell::new(TestScorer::new());
1562 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1564 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1565 &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1567 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1568 assert_eq!(htlc_msgs.len(), 2);
1569 check_added_monitors!(nodes[0], 2);