Score successful payment paths
[rust-lightning] / lightning-invoice / src / payment.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! A module for paying Lightning invoices and sending spontaneous payments.
11 //!
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
16 //! applicable.
17 //!
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.
21 //!
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.
27 //!
28 //! # Example
29 //!
30 //! ```
31 //! # extern crate lightning;
32 //! # extern crate lightning_invoice;
33 //! # extern crate secp256k1;
34 //! #
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;
49 //! #
50 //! # struct FakeEventProvider {}
51 //! # impl EventsProvider for FakeEventProvider {
52 //! #     fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {}
53 //! # }
54 //! #
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 //! # }
69 //! #
70 //! # struct FakeRouter {};
71 //! # impl<S: Score> Router<S> for FakeRouter {
72 //! #     fn find_route(
73 //! #         &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
74 //! #         first_hops: Option<&[&ChannelDetails]>, scorer: &S
75 //! #     ) -> Result<Route, LightningError> { unimplemented!() }
76 //! # }
77 //! #
78 //! # struct FakeScorer {};
79 //! # impl Writeable for FakeScorer {
80 //! #     fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> { unimplemented!(); }
81 //! # }
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
85 //! #     ) -> u64 { 0 }
86 //! #     fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
87 //! #     fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
88 //! # }
89 //! #
90 //! # struct FakeLogger {};
91 //! # impl Logger for FakeLogger {
92 //! #     fn log(&self, record: &Record) { unimplemented!() }
93 //! # }
94 //! #
95 //! # fn main() {
96 //! let event_handler = |event: &Event| {
97 //!     match event {
98 //!         Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
99 //!         Event::PaymentSent { .. } => println!("payment successful"),
100 //!         _ => {},
101 //!     }
102 //! };
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));
108 //!
109 //! let invoice = "...";
110 //! let invoice = invoice.parse::<Invoice>().unwrap();
111 //! invoice_payer.pay_invoice(&invoice).unwrap();
112 //!
113 //! # let event_provider = FakeEventProvider {};
114 //! loop {
115 //!     event_provider.process_pending_events(&invoice_payer);
116 //! }
117 //! # }
118 //! ```
119 //!
120 //! # Note
121 //!
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.
125
126 use crate::Invoice;
127
128 use bitcoin_hashes::Hash;
129 use bitcoin_hashes::sha256::Hash as Sha256;
130
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;
138
139 use secp256k1::key::PublicKey;
140
141 use std::collections::hash_map::{self, HashMap};
142 use std::ops::Deref;
143 use std::sync::Mutex;
144 use std::time::{Duration, SystemTime};
145
146 /// A utility for paying [`Invoice`]s and sending spontaneous payments.
147 ///
148 /// See [module-level documentation] for details.
149 ///
150 /// [module-level documentation]: crate::payment
151 pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
152 where
153         P::Target: Payer,
154         R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
155         S::Target: for <'a> LockableScore<'a>,
156         L::Target: Logger,
157         E: EventHandler,
158 {
159         payer: P,
160         router: R,
161         scorer: S,
162         logger: L,
163         event_handler: E,
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,
167 }
168
169 /// A trait defining behavior of an [`Invoice`] payer.
170 pub trait Payer {
171         /// Returns the payer's node id.
172         fn node_id(&self) -> PublicKey;
173
174         /// Returns the payer's channels.
175         fn first_hops(&self) -> Vec<ChannelDetails>;
176
177         /// Sends a payment over the Lightning Network using the given [`Route`].
178         fn send_payment(
179                 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
180         ) -> Result<PaymentId, PaymentSendFailure>;
181
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>;
186
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>;
189 }
190
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.
194         fn find_route(
195                 &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
196                 first_hops: Option<&[&ChannelDetails]>, scorer: &S
197         ) -> Result<Route, LightningError>;
198 }
199
200 /// Number of attempts to retry payment path failures for an [`Invoice`].
201 ///
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
204 /// payment's paths.
205 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
206 pub struct RetryAttempts(pub usize);
207
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),
217 }
218
219 impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
220 where
221         P::Target: Payer,
222         R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
223         S::Target: for <'a> LockableScore<'a>,
224         L::Target: Logger,
225         E: EventHandler,
226 {
227         /// Creates an invoice payer that retries failed payment paths.
228         ///
229         /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
230         /// `retry_attempts` has been exceeded for a given [`Invoice`].
231         pub fn new(
232                 payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
233         ) -> Self {
234                 Self {
235                         payer,
236                         router,
237                         scorer,
238                         logger,
239                         event_handler,
240                         payment_cache: Mutex::new(HashMap::new()),
241                         retry_attempts,
242                 }
243         }
244
245         /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
246         ///
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
249         /// for you.
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"))
253                 } else {
254                         self.pay_invoice_using_amount(invoice, None)
255                 }
256         }
257
258         /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
259         /// case a retry is needed.
260         ///
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
263         /// for you.
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"))
269                 } else {
270                         self.pay_invoice_using_amount(invoice, Some(amount_msats))
271                 }
272         }
273
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());
278
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),
283                 };
284
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());
291                 }
292                 let params = RouteParameters {
293                         payee,
294                         final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
295                         final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
296                 };
297
298                 let send_payment = |route: &Route| {
299                         self.payer.send_payment(route, payment_hash, &payment_secret)
300                 };
301                 self.pay_internal(&params, payment_hash, send_payment)
302                         .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
303         }
304
305         /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
306         /// case a retry is needed.
307         ///
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.
310         pub fn pay_pubkey(
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),
318                 };
319
320                 let params = RouteParameters {
321                         payee: Payee::for_keysend(pubkey),
322                         final_value_msat: amount_msats,
323                         final_cltv_expiry_delta,
324                 };
325
326                 let send_payment = |route: &Route| {
327                         self.payer.send_spontaneous_payment(route, payment_preimage)
328                 };
329                 self.pay_internal(&params, payment_hash, send_payment)
330                         .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
331         }
332
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"));
339                 }
340
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<_>>()),
345                         &self.scorer.lock()
346                 ).map_err(|e| PaymentError::Routing(e))?;
347
348                 match send_payment(&route) {
349                         Ok(payment_id) => Ok(payment_id),
350                         Err(e) => match e {
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 {
357                                                 Err(e)
358                                         } else {
359                                                 *retry_count += 1;
360                                                 std::mem::drop(payment_cache);
361                                                 Ok(self.pay_internal(params, payment_hash, send_payment)?)
362                                         }
363                                 },
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
369                                                 // errors.
370                                                 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
371                                                 Ok(payment_id)
372                                         } else {
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.
377                                                 Ok(payment_id)
378                                         }
379                                 },
380                         },
381                 }.map_err(|e| PaymentError::Sending(e))
382         }
383
384         fn retry_payment(
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()
389                         .entry(payment_hash)
390                         .and_modify(|attempts| *attempts += 1)
391                         .or_insert(1);
392
393                 if attempts >= max_payment_attempts {
394                         log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
395                         return Err(());
396                 }
397
398                 if has_expired(params) {
399                         log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
400                         return Err(());
401                 }
402
403                 let payer = self.payer.node_id();
404                 let first_hops = self.payer.first_hops();
405                 let route = self.router.find_route(
406                         &payer, &params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
407                         &self.scorer.lock()
408                 );
409                 if route.is_err() {
410                         log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
411                         return Err(());
412                 }
413
414                 match self.payer.retry_payment(&route.unwrap(), payment_id) {
415                         Ok(()) => Ok(()),
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));
419                                 Err(())
420                         },
421                         Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
422                                 self.retry_payment(payment_id, payment_hash, params)
423                         },
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);
428                                 }
429                                 Ok(())
430                         },
431                 }
432         }
433
434         /// Removes the payment cached by the given payment hash.
435         ///
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);
440         }
441 }
442
443 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
444         invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
445 }
446
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))
450         } else { false }
451 }
452
453 impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
454 where
455         P::Target: Payer,
456         R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
457         S::Target: for <'a> LockableScore<'a>,
458         L::Target: Logger,
459         E: EventHandler,
460 {
461         fn handle_event(&self, event: &Event) {
462                 match event {
463                         Event::PaymentPathFailed {
464                                 all_paths_failed, payment_id, payment_hash, rejected_by_dest, path,
465                                 short_channel_id, retry, ..
466                         } => {
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);
470                                 }
471
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.
480                                         return;
481                                 }
482
483                                 if *all_paths_failed { self.payment_cache.lock().unwrap().remove(payment_hash); }
484                         },
485                         Event::PaymentPathSuccessful { path, .. } => {
486                                 let path = path.iter().collect::<Vec<_>>();
487                                 self.scorer.lock().payment_path_successful(&path);
488                         },
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);
495                         },
496                         _ => {},
497                 }
498
499                 // Delegate to the decorated event handler unless the payment is retried.
500                 self.event_handler.handle_event(event)
501         }
502 }
503
504 #[cfg(test)]
505 mod tests {
506         use super::*;
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};
523
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]))
531                         .current_timestamp()
532                         .min_final_cltv_expiry(144)
533                         .amount_milli_satoshis(128)
534                         .build_signed(|hash| {
535                                 Secp256k1::new().sign_recoverable(hash, &private_key)
536                         })
537                         .unwrap()
538         }
539
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]))
547                         .current_timestamp()
548                         .min_final_cltv_expiry(144)
549                         .build_signed(|hash| {
550                                 Secp256k1::new().sign_recoverable(hash, &private_key)
551                         })
552                         .unwrap()
553         }
554
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))
560                         .unwrap();
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)
570                         })
571                         .unwrap()
572         }
573
574         fn pubkey() -> PublicKey {
575                 PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()
576         }
577
578         #[test]
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; };
582
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();
587
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();
592                 let invoice_payer =
593                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
594
595                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
596                 assert_eq!(*payer.attempts.borrow(), 1);
597
598                 invoice_payer.handle_event(&Event::PaymentSent {
599                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
600                 });
601                 assert_eq!(*event_handled.borrow(), true);
602                 assert_eq!(*payer.attempts.borrow(), 1);
603         }
604
605         #[test]
606         fn pays_invoice_on_retry() {
607                 let event_handled = core::cell::RefCell::new(false);
608                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
609
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();
614
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();
621                 let invoice_payer =
622                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
623
624                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
625                 assert_eq!(*payer.attempts.borrow(), 1);
626
627                 let event = Event::PaymentPathFailed {
628                         payment_id,
629                         payment_hash,
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)),
636                 };
637                 invoice_payer.handle_event(&event);
638                 assert_eq!(*event_handled.borrow(), false);
639                 assert_eq!(*payer.attempts.borrow(), 2);
640
641                 invoice_payer.handle_event(&Event::PaymentSent {
642                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
643                 });
644                 assert_eq!(*event_handled.borrow(), true);
645                 assert_eq!(*payer.attempts.borrow(), 2);
646         }
647
648         #[test]
649         fn pays_invoice_on_partial_failure() {
650                 let event_handler = |_: &_| { panic!() };
651
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();
656
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();
666                 let invoice_payer =
667                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
668
669                 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
670         }
671
672         #[test]
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; };
676
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();
681
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();
688                 let invoice_payer =
689                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
690
691                 let payment_id = Some(PaymentId([1; 32]));
692                 let event = Event::PaymentPathFailed {
693                         payment_id,
694                         payment_hash,
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)),
701                 };
702                 invoice_payer.handle_event(&event);
703                 assert_eq!(*event_handled.borrow(), false);
704                 assert_eq!(*payer.attempts.borrow(), 1);
705
706                 invoice_payer.handle_event(&event);
707                 assert_eq!(*event_handled.borrow(), false);
708                 assert_eq!(*payer.attempts.borrow(), 2);
709
710                 invoice_payer.handle_event(&Event::PaymentSent {
711                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
712                 });
713                 assert_eq!(*event_handled.borrow(), true);
714                 assert_eq!(*payer.attempts.borrow(), 2);
715         }
716
717         #[test]
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; };
721
722                 let payment_preimage = PaymentPreimage([1; 32]);
723                 let invoice = invoice(payment_preimage);
724                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
725
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();
733                 let invoice_payer =
734                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
735
736                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
737                 assert_eq!(*payer.attempts.borrow(), 1);
738
739                 let event = Event::PaymentPathFailed {
740                         payment_id,
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)),
748                 };
749                 invoice_payer.handle_event(&event);
750                 assert_eq!(*event_handled.borrow(), false);
751                 assert_eq!(*payer.attempts.borrow(), 2);
752
753                 let event = Event::PaymentPathFailed {
754                         payment_id,
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)
763                         }),
764                 };
765                 invoice_payer.handle_event(&event);
766                 assert_eq!(*event_handled.borrow(), false);
767                 assert_eq!(*payer.attempts.borrow(), 3);
768
769                 invoice_payer.handle_event(&event);
770                 assert_eq!(*event_handled.borrow(), true);
771                 assert_eq!(*payer.attempts.borrow(), 3);
772         }
773
774         #[test]
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; };
778
779                 let payment_preimage = PaymentPreimage([1; 32]);
780                 let invoice = invoice(payment_preimage);
781                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
782
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();
787                 let invoice_payer =
788                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
789
790                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
791                 assert_eq!(*payer.attempts.borrow(), 1);
792
793                 let event = Event::PaymentPathFailed {
794                         payment_id,
795                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
796                         network_update: None,
797                         rejected_by_dest: false,
798                         all_paths_failed: false,
799                         path: vec![],
800                         short_channel_id: None,
801                         retry: None,
802                 };
803                 invoice_payer.handle_event(&event);
804                 assert_eq!(*event_handled.borrow(), true);
805                 assert_eq!(*payer.attempts.borrow(), 1);
806         }
807
808         #[test]
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; };
812
813                 let payer = TestPayer::new();
814                 let router = TestRouter {};
815                 let scorer = RefCell::new(TestScorer::new());
816                 let logger = TestLogger::new();
817                 let invoice_payer =
818                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
819
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"); }
825         }
826
827         #[test]
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; };
831
832                 let payment_preimage = PaymentPreimage([1; 32]);
833                 let invoice = invoice(payment_preimage);
834                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
835
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();
840                 let invoice_payer =
841                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
842
843                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
844                 assert_eq!(*payer.attempts.borrow(), 1);
845
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 {
851                         payment_id,
852                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
853                         network_update: None,
854                         rejected_by_dest: false,
855                         all_paths_failed: false,
856                         path: vec![],
857                         short_channel_id: None,
858                         retry: Some(retry_data),
859                 };
860                 invoice_payer.handle_event(&event);
861                 assert_eq!(*event_handled.borrow(), true);
862                 assert_eq!(*payer.attempts.borrow(), 1);
863         }
864
865         #[test]
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; };
869
870                 let payment_preimage = PaymentPreimage([1; 32]);
871                 let invoice = invoice(payment_preimage);
872                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
873
874                 let payer = TestPayer::new()
875                         .fails_on_attempt(2)
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();
881                 let invoice_payer =
882                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
883
884                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
885                 assert_eq!(*payer.attempts.borrow(), 1);
886
887                 let event = Event::PaymentPathFailed {
888                         payment_id,
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)),
896                 };
897                 invoice_payer.handle_event(&event);
898                 assert_eq!(*event_handled.borrow(), true);
899                 assert_eq!(*payer.attempts.borrow(), 2);
900         }
901
902         #[test]
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; };
906
907                 let payment_preimage = PaymentPreimage([1; 32]);
908                 let invoice = invoice(payment_preimage);
909                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
910
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();
915                 let invoice_payer =
916                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
917
918                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
919                 assert_eq!(*payer.attempts.borrow(), 1);
920
921                 let event = Event::PaymentPathFailed {
922                         payment_id,
923                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
924                         network_update: None,
925                         rejected_by_dest: true,
926                         all_paths_failed: false,
927                         path: vec![],
928                         short_channel_id: None,
929                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
930                 };
931                 invoice_payer.handle_event(&event);
932                 assert_eq!(*event_handled.borrow(), true);
933                 assert_eq!(*payer.attempts.borrow(), 1);
934         }
935
936         #[test]
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; };
940
941                 let payment_preimage = PaymentPreimage([1; 32]);
942                 let invoice = invoice(payment_preimage);
943                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
944
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();
951                 let invoice_payer =
952                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
953
954                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
955
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"),
961                 }
962
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());
967
968                 // Cannot retry paying an invoice if cleared from cache.
969                 invoice_payer.remove_cached_payment(&payment_hash);
970                 let event = Event::PaymentPathFailed {
971                         payment_id,
972                         payment_hash,
973                         network_update: None,
974                         rejected_by_dest: false,
975                         all_paths_failed: false,
976                         path: vec![],
977                         short_channel_id: None,
978                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
979                 };
980                 invoice_payer.handle_event(&event);
981                 assert_eq!(*event_handled.borrow(), true);
982         }
983
984         #[test]
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();
990                 let invoice_payer =
991                         InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
992
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"),
999                 }
1000         }
1001
1002         #[test]
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();
1007
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();
1014                 let invoice_payer =
1015                         InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
1016
1017                 match invoice_payer.pay_invoice(&invoice) {
1018                         Err(PaymentError::Sending(_)) => {},
1019                         Err(_) => panic!("unexpected error"),
1020                         Ok(_) => panic!("expected sending error"),
1021                 }
1022         }
1023
1024         #[test]
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; };
1028
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;
1033
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();
1038                 let invoice_payer =
1039                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1040
1041                 let payment_id =
1042                         Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
1043                 assert_eq!(*payer.attempts.borrow(), 1);
1044
1045                 invoice_payer.handle_event(&Event::PaymentSent {
1046                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1047                 });
1048                 assert_eq!(*event_handled.borrow(), true);
1049                 assert_eq!(*payer.attempts.borrow(), 1);
1050         }
1051
1052         #[test]
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; };
1056
1057                 let payer = TestPayer::new();
1058                 let router = TestRouter {};
1059                 let scorer = RefCell::new(TestScorer::new());
1060                 let logger = TestLogger::new();
1061                 let invoice_payer =
1062                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1063
1064                 let payment_preimage = PaymentPreimage([1; 32]);
1065                 let invoice = invoice(payment_preimage);
1066
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"),
1072                 }
1073         }
1074
1075         #[test]
1076         fn pays_pubkey_with_amount() {
1077                 let event_handled = core::cell::RefCell::new(false);
1078                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1079
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;
1085
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();
1092                 let invoice_payer =
1093                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1094
1095                 let payment_id = Some(invoice_payer.pay_pubkey(
1096                                 pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
1097                         ).unwrap());
1098                 assert_eq!(*payer.attempts.borrow(), 1);
1099
1100                 let retry = RouteParameters {
1101                         payee: Payee::for_keysend(pubkey),
1102                         final_value_msat,
1103                         final_cltv_expiry_delta,
1104                 };
1105                 let event = Event::PaymentPathFailed {
1106                         payment_id,
1107                         payment_hash,
1108                         network_update: None,
1109                         rejected_by_dest: false,
1110                         all_paths_failed: false,
1111                         path: vec![],
1112                         short_channel_id: None,
1113                         retry: Some(retry),
1114                 };
1115                 invoice_payer.handle_event(&event);
1116                 assert_eq!(*event_handled.borrow(), false);
1117                 assert_eq!(*payer.attempts.borrow(), 2);
1118
1119                 invoice_payer.handle_event(&Event::PaymentSent {
1120                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1121                 });
1122                 assert_eq!(*event_handled.borrow(), true);
1123                 assert_eq!(*payer.attempts.borrow(), 2);
1124         }
1125
1126         #[test]
1127         fn scores_failed_channel() {
1128                 let event_handled = core::cell::RefCell::new(false);
1129                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1130
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);
1137
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,
1145                 }));
1146                 let logger = TestLogger::new();
1147                 let invoice_payer =
1148                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1149
1150                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
1151                 let event = Event::PaymentPathFailed {
1152                         payment_id,
1153                         payment_hash,
1154                         network_update: None,
1155                         rejected_by_dest: false,
1156                         all_paths_failed: false,
1157                         path,
1158                         short_channel_id,
1159                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
1160                 };
1161                 invoice_payer.handle_event(&event);
1162         }
1163
1164         #[test]
1165         fn scores_successful_channels() {
1166                 let event_handled = core::cell::RefCell::new(false);
1167                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1168
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);
1174
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() })
1181                 );
1182                 let logger = TestLogger::new();
1183                 let invoice_payer =
1184                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1185
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()
1189                 };
1190                 invoice_payer.handle_event(&event);
1191                 let event = Event::PaymentPathSuccessful {
1192                         payment_id, payment_hash, path: route.paths[1].clone()
1193                 };
1194                 invoice_payer.handle_event(&event);
1195         }
1196
1197         struct TestRouter;
1198
1199         impl TestRouter {
1200                 fn route_for_value(final_value_msat: u64) -> Route {
1201                         Route {
1202                                 paths: vec![
1203                                         vec![RouteHop {
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
1208                                         }],
1209                                         vec![RouteHop {
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
1214                                         }],
1215                                 ],
1216                                 payee: None,
1217                         }
1218                 }
1219
1220                 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1221                         TestRouter::route_for_value(final_value_msat).paths[0].clone()
1222                 }
1223
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());
1230                         }
1231                         let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1232                         RouteParameters {
1233                                 payee,
1234                                 final_value_msat,
1235                                 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1236                         }
1237                 }
1238         }
1239
1240         impl<S: Score> Router<S> for TestRouter {
1241                 fn find_route(
1242                         &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
1243                         _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1244                 ) -> Result<Route, LightningError> {
1245                         Ok(Route {
1246                                 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1247                         })
1248                 }
1249         }
1250
1251         struct FailingRouter;
1252
1253         impl<S: Score> Router<S> for FailingRouter {
1254                 fn find_route(
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 })
1259                 }
1260         }
1261
1262         struct TestScorer {
1263                 expectations: Option<VecDeque<PaymentPath>>,
1264         }
1265
1266         #[derive(Debug)]
1267         enum PaymentPath {
1268                 Failure { path: Vec<RouteHop>, short_channel_id: u64 },
1269                 Success { path: Vec<RouteHop> },
1270         }
1271
1272         impl TestScorer {
1273                 fn new() -> Self {
1274                         Self {
1275                                 expectations: None,
1276                         }
1277                 }
1278
1279                 fn expect(mut self, expectation: PaymentPath) -> Self {
1280                         self.expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
1281                         self
1282                 }
1283         }
1284
1285         #[cfg(c_bindings)]
1286         impl lightning::util::ser::Writeable for TestScorer {
1287                 fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); }
1288         }
1289
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
1293                 ) -> u64 { 0 }
1294
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);
1301                                         },
1302                                         Some(PaymentPath::Success { path }) => {
1303                                                 panic!("Unexpected successful payment path: {:?}", path)
1304                                         },
1305                                         None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
1306                                 }
1307                         }
1308                 }
1309
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)
1315                                         },
1316                                         Some(PaymentPath::Success { path }) => {
1317                                                 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1318                                         },
1319                                         None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),
1320                                 }
1321                         }
1322                 }
1323         }
1324
1325         impl Drop for TestScorer {
1326                 fn drop(&mut self) {
1327                         if std::thread::panicking() {
1328                                 return;
1329                         }
1330
1331                         if let Some(expectations) = &self.expectations {
1332                                 if !expectations.is_empty() {
1333                                         panic!("Unsatisfied scorer expectations: {:?}", expectations);
1334                                 }
1335                         }
1336                 }
1337         }
1338
1339         struct TestPayer {
1340                 expectations: core::cell::RefCell<VecDeque<Amount>>,
1341                 attempts: core::cell::RefCell<usize>,
1342                 failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
1343         }
1344
1345         #[derive(Clone, Debug, PartialEq, Eq)]
1346         enum Amount {
1347                 ForInvoice(u64),
1348                 Spontaneous(u64),
1349                 OnRetry(u64),
1350         }
1351
1352         struct OnAttempt(usize);
1353
1354         impl TestPayer {
1355                 fn new() -> Self {
1356                         Self {
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()),
1360                         }
1361                 }
1362
1363                 fn expect_send(self, value_msat: Amount) -> Self {
1364                         self.expectations.borrow_mut().push_back(value_msat);
1365                         self
1366                 }
1367
1368                 fn fails_on_attempt(self, attempt: usize) -> Self {
1369                         let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
1370                         self.fails_with(failure, OnAttempt(attempt))
1371                 }
1372
1373                 fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
1374                         self.fails_with(PaymentSendFailure::PartialFailure {
1375                                 results: vec![],
1376                                 failed_paths_retry: Some(retry),
1377                                 payment_id: PaymentId([1; 32]),
1378                         }, attempt)
1379                 }
1380
1381                 fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
1382                         self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
1383                         self
1384                 }
1385
1386                 fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
1387                         let mut attempts = self.attempts.borrow_mut();
1388                         *attempts += 1;
1389
1390                         match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1391                                 Some(failure) => Err(failure),
1392                                 None => Ok(PaymentId([1; 32])),
1393                         }
1394                 }
1395
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);
1400                         } else {
1401                                 panic!("Unexpected amount: {:?}", actual_value_msats);
1402                         }
1403                 }
1404         }
1405
1406         impl Drop for TestPayer {
1407                 fn drop(&mut self) {
1408                         if std::thread::panicking() {
1409                                 return;
1410                         }
1411
1412                         if !self.expectations.borrow().is_empty() {
1413                                 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1414                         }
1415                 }
1416         }
1417
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())
1422                 }
1423
1424                 fn first_hops(&self) -> Vec<ChannelDetails> {
1425                         Vec::new()
1426                 }
1427
1428                 fn send_payment(
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()
1434                 }
1435
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()
1441                 }
1442
1443                 fn retry_payment(
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(|_| ())
1448                 }
1449         }
1450
1451         // *** Full Featured Functional Tests with a Real ChannelManager ***
1452         struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1453
1454         impl<S: Score> Router<S> for ManualRouter {
1455                 fn find_route(
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()
1460                 }
1461         }
1462         impl ManualRouter {
1463                 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1464                         self.0.borrow_mut().push_back(result);
1465                 }
1466         }
1467         impl Drop for ManualRouter {
1468                 fn drop(&mut self) {
1469                         if std::thread::panicking() {
1470                                 return;
1471                         }
1472                         assert!(self.0.borrow_mut().is_empty());
1473                 }
1474         }
1475
1476         #[test]
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);
1483
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 {
1488                         paths: vec![
1489                                 vec![RouteHop {
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(),
1494                                         fee_msat: 10_000,
1495                                         cltv_expiry_delta: 100,
1496                                 }],
1497                                 vec![RouteHop {
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,
1504                                 }],
1505                         ],
1506                         payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1507                 };
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()));
1514
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));
1518
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())
1521                         .is_ok());
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);
1525         }
1526
1527         #[test]
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);
1534
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 {
1539                         paths: vec![
1540                                 vec![RouteHop {
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,
1547                                 }],
1548                         ],
1549                         payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1550                 };
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()));
1559
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));
1563
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())
1566                         .is_ok());
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);
1570         }
1571 }