(Bindings Only) Concretize LockableScore as MultiThreadedLockableScore
[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 //! #     fn abandon_payment(&self, payment_id: PaymentId) { unimplemented!() }
69 //! # }
70 //! #
71 //! # struct FakeRouter {}
72 //! # impl<S: Score> Router<S> for FakeRouter {
73 //! #     fn find_route(
74 //! #         &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
75 //! #         first_hops: Option<&[&ChannelDetails]>, scorer: &S
76 //! #     ) -> Result<Route, LightningError> { unimplemented!() }
77 //! # }
78 //! #
79 //! # struct FakeScorer {}
80 //! # impl Writeable for FakeScorer {
81 //! #     fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> { unimplemented!(); }
82 //! # }
83 //! # impl Score for FakeScorer {
84 //! #     fn channel_penalty_msat(
85 //! #         &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
86 //! #     ) -> u64 { 0 }
87 //! #     fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
88 //! #     fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
89 //! # }
90 //! #
91 //! # struct FakeLogger {}
92 //! # impl Logger for FakeLogger {
93 //! #     fn log(&self, record: &Record) { unimplemented!() }
94 //! # }
95 //! #
96 //! # fn main() {
97 //! let event_handler = |event: &Event| {
98 //!     match event {
99 //!         Event::PaymentPathFailed { .. } => println!("payment failed after retries"),
100 //!         Event::PaymentSent { .. } => println!("payment successful"),
101 //!         _ => {},
102 //!     }
103 //! };
104 //! # let payer = FakePayer {};
105 //! # let router = FakeRouter {};
106 //! # let scorer = RefCell::new(FakeScorer {});
107 //! # let logger = FakeLogger {};
108 //! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
109 //!
110 //! let invoice = "...";
111 //! if let Ok(invoice) = invoice.parse::<Invoice>() {
112 //!     invoice_payer.pay_invoice(&invoice).unwrap();
113 //!
114 //! # let event_provider = FakeEventProvider {};
115 //!     loop {
116 //!         event_provider.process_pending_events(&invoice_payer);
117 //!     }
118 //! }
119 //! # }
120 //! ```
121 //!
122 //! # Note
123 //!
124 //! The [`Route`] is computed before each payment attempt. Any updates affecting path finding such
125 //! as updates to the network graph or changes to channel scores should be applied prior to
126 //! retries, typically by way of composing [`EventHandler`]s accordingly.
127
128 use crate::Invoice;
129
130 use bitcoin_hashes::Hash;
131 use bitcoin_hashes::sha256::Hash as Sha256;
132
133 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
134 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
135 use lightning::ln::msgs::LightningError;
136 use lightning::routing::scoring::{LockableScore, MultiThreadedLockableScore, Score};
137 use lightning::routing::router::{Payee, Route, RouteParameters};
138 use lightning::util::events::{Event, EventHandler};
139 use lightning::util::logger::Logger;
140
141 use secp256k1::key::PublicKey;
142
143 use std::collections::hash_map::{self, HashMap};
144 use std::ops::Deref;
145 use std::sync::Mutex;
146 use std::time::{Duration, SystemTime};
147
148 /// A utility for paying [`Invoice`]s and sending spontaneous payments.
149 ///
150 /// See [module-level documentation] for details.
151 ///
152 /// [module-level documentation]: crate::payment
153 pub struct InvoicePayer<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler>
154 where
155         P::Target: Payer,
156         R::Target: Router<S>,
157         L::Target: Logger,
158 {
159         payer: P,
160         router: R,
161         scorer: SR,
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         /// Signals that no further retries for the given payment will occur.
191         fn abandon_payment(&self, payment_id: PaymentId);
192 }
193
194 /// A trait defining behavior for routing an [`Invoice`] payment.
195 pub trait Router<S: Score> {
196         /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
197         fn find_route(
198                 &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
199                 first_hops: Option<&[&ChannelDetails]>, scorer: &S
200         ) -> Result<Route, LightningError>;
201 }
202
203 /// Number of attempts to retry payment path failures for an [`Invoice`].
204 ///
205 /// Note that this is the number of *path* failures, not full payment retries. For multi-path
206 /// payments, if this is less than the total number of paths, we will never even retry all of the
207 /// payment's paths.
208 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
209 pub struct RetryAttempts(pub usize);
210
211 /// An error that may occur when making a payment.
212 #[derive(Clone, Debug)]
213 pub enum PaymentError {
214         /// An error resulting from the provided [`Invoice`] or payment hash.
215         Invoice(&'static str),
216         /// An error occurring when finding a route.
217         Routing(LightningError),
218         /// An error occurring when sending a payment.
219         Sending(PaymentSendFailure),
220 }
221
222 impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> InvoicePayer<P, S, R, SR, L, E>
223 where
224         P::Target: Payer,
225         R::Target: Router<S>,
226         L::Target: Logger,
227 {
228         /// Creates an invoice payer that retries failed payment paths.
229         ///
230         /// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
231         /// `retry_attempts` has been exceeded for a given [`Invoice`].
232         pub fn new(
233                 payer: P, router: R, scorer: SR, logger: L, event_handler: E, retry_attempts: RetryAttempts
234         ) -> Self {
235                 Self {
236                         payer,
237                         router,
238                         scorer,
239                         logger,
240                         event_handler,
241                         payment_cache: Mutex::new(HashMap::new()),
242                         retry_attempts,
243                 }
244         }
245
246         /// Pays the given [`Invoice`], caching it for later use in case a retry is needed.
247         ///
248         /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
249         /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
250         /// for you.
251         pub fn pay_invoice(&self, invoice: &Invoice) -> Result<PaymentId, PaymentError> {
252                 if invoice.amount_milli_satoshis().is_none() {
253                         Err(PaymentError::Invoice("amount missing"))
254                 } else {
255                         self.pay_invoice_using_amount(invoice, None)
256                 }
257         }
258
259         /// Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
260         /// case a retry is needed.
261         ///
262         /// You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
263         /// never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
264         /// for you.
265         pub fn pay_zero_value_invoice(
266                 &self, invoice: &Invoice, amount_msats: u64
267         ) -> Result<PaymentId, PaymentError> {
268                 if invoice.amount_milli_satoshis().is_some() {
269                         Err(PaymentError::Invoice("amount unexpected"))
270                 } else {
271                         self.pay_invoice_using_amount(invoice, Some(amount_msats))
272                 }
273         }
274
275         fn pay_invoice_using_amount(
276                 &self, invoice: &Invoice, amount_msats: Option<u64>
277         ) -> Result<PaymentId, PaymentError> {
278                 debug_assert!(invoice.amount_milli_satoshis().is_some() ^ amount_msats.is_some());
279
280                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
281                 match self.payment_cache.lock().unwrap().entry(payment_hash) {
282                         hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
283                         hash_map::Entry::Vacant(entry) => entry.insert(0),
284                 };
285
286                 let payment_secret = Some(invoice.payment_secret().clone());
287                 let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
288                         .with_expiry_time(expiry_time_from_unix_epoch(&invoice).as_secs())
289                         .with_route_hints(invoice.route_hints());
290                 if let Some(features) = invoice.features() {
291                         payee = payee.with_features(features.clone());
292                 }
293                 let params = RouteParameters {
294                         payee,
295                         final_value_msat: invoice.amount_milli_satoshis().or(amount_msats).unwrap(),
296                         final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
297                 };
298
299                 let send_payment = |route: &Route| {
300                         self.payer.send_payment(route, payment_hash, &payment_secret)
301                 };
302                 self.pay_internal(&params, payment_hash, send_payment)
303                         .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
304         }
305
306         /// Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
307         /// case a retry is needed.
308         ///
309         /// You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
310         /// been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
311         pub fn pay_pubkey(
312                 &self, pubkey: PublicKey, payment_preimage: PaymentPreimage, amount_msats: u64,
313                 final_cltv_expiry_delta: u32
314         ) -> Result<PaymentId, PaymentError> {
315                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
316                 match self.payment_cache.lock().unwrap().entry(payment_hash) {
317                         hash_map::Entry::Occupied(_) => return Err(PaymentError::Invoice("payment pending")),
318                         hash_map::Entry::Vacant(entry) => entry.insert(0),
319                 };
320
321                 let params = RouteParameters {
322                         payee: Payee::for_keysend(pubkey),
323                         final_value_msat: amount_msats,
324                         final_cltv_expiry_delta,
325                 };
326
327                 let send_payment = |route: &Route| {
328                         self.payer.send_spontaneous_payment(route, payment_preimage)
329                 };
330                 self.pay_internal(&params, payment_hash, send_payment)
331                         .map_err(|e| { self.payment_cache.lock().unwrap().remove(&payment_hash); e })
332         }
333
334         fn pay_internal<F: FnOnce(&Route) -> Result<PaymentId, PaymentSendFailure> + Copy>(
335                 &self, params: &RouteParameters, payment_hash: PaymentHash, send_payment: F,
336         ) -> Result<PaymentId, PaymentError> {
337                 if has_expired(params) {
338                         log_trace!(self.logger, "Invoice expired prior to send for payment {}", log_bytes!(payment_hash.0));
339                         return Err(PaymentError::Invoice("Invoice expired prior to send"));
340                 }
341
342                 let payer = self.payer.node_id();
343                 let first_hops = self.payer.first_hops();
344                 let route = self.router.find_route(
345                         &payer, params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
346                         &self.scorer.lock()
347                 ).map_err(|e| PaymentError::Routing(e))?;
348
349                 match send_payment(&route) {
350                         Ok(payment_id) => Ok(payment_id),
351                         Err(e) => match e {
352                                 PaymentSendFailure::ParameterError(_) => Err(e),
353                                 PaymentSendFailure::PathParameterError(_) => Err(e),
354                                 PaymentSendFailure::AllFailedRetrySafe(_) => {
355                                         let mut payment_cache = self.payment_cache.lock().unwrap();
356                                         let retry_count = payment_cache.get_mut(&payment_hash).unwrap();
357                                         if *retry_count >= self.retry_attempts.0 {
358                                                 Err(e)
359                                         } else {
360                                                 *retry_count += 1;
361                                                 std::mem::drop(payment_cache);
362                                                 Ok(self.pay_internal(params, payment_hash, send_payment)?)
363                                         }
364                                 },
365                                 PaymentSendFailure::PartialFailure { failed_paths_retry, payment_id, .. } => {
366                                         if let Some(retry_data) = failed_paths_retry {
367                                                 // Some paths were sent, even if we failed to send the full MPP value our
368                                                 // recipient may misbehave and claim the funds, at which point we have to
369                                                 // consider the payment sent, so return `Ok()` here, ignoring any retry
370                                                 // errors.
371                                                 let _ = self.retry_payment(payment_id, payment_hash, &retry_data);
372                                                 Ok(payment_id)
373                                         } else {
374                                                 // This may happen if we send a payment and some paths fail, but
375                                                 // only due to a temporary monitor failure or the like, implying
376                                                 // they're really in-flight, but we haven't sent the initial
377                                                 // HTLC-Add messages yet.
378                                                 Ok(payment_id)
379                                         }
380                                 },
381                         },
382                 }.map_err(|e| PaymentError::Sending(e))
383         }
384
385         fn retry_payment(
386                 &self, payment_id: PaymentId, payment_hash: PaymentHash, params: &RouteParameters
387         ) -> Result<(), ()> {
388                 let max_payment_attempts = self.retry_attempts.0 + 1;
389                 let attempts = *self.payment_cache.lock().unwrap()
390                         .entry(payment_hash)
391                         .and_modify(|attempts| *attempts += 1)
392                         .or_insert(1);
393
394                 if attempts >= max_payment_attempts {
395                         log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
396                         return Err(());
397                 }
398
399                 if has_expired(params) {
400                         log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
401                         return Err(());
402                 }
403
404                 let payer = self.payer.node_id();
405                 let first_hops = self.payer.first_hops();
406                 let route = self.router.find_route(
407                         &payer, &params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
408                         &self.scorer.lock()
409                 );
410                 if route.is_err() {
411                         log_trace!(self.logger, "Failed to find a route for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
412                         return Err(());
413                 }
414
415                 match self.payer.retry_payment(&route.unwrap(), payment_id) {
416                         Ok(()) => Ok(()),
417                         Err(PaymentSendFailure::ParameterError(_)) |
418                         Err(PaymentSendFailure::PathParameterError(_)) => {
419                                 log_trace!(self.logger, "Failed to retry for payment {} due to bogus route/payment data, not retrying.", log_bytes!(payment_hash.0));
420                                 Err(())
421                         },
422                         Err(PaymentSendFailure::AllFailedRetrySafe(_)) => {
423                                 self.retry_payment(payment_id, payment_hash, params)
424                         },
425                         Err(PaymentSendFailure::PartialFailure { failed_paths_retry, .. }) => {
426                                 if let Some(retry) = failed_paths_retry {
427                                         // Always return Ok for the same reason as noted in pay_internal.
428                                         let _ = self.retry_payment(payment_id, payment_hash, &retry);
429                                 }
430                                 Ok(())
431                         },
432                 }
433         }
434
435         /// Removes the payment cached by the given payment hash.
436         ///
437         /// Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
438         /// [`EventHandler`]. Otherwise, calling this method is unnecessary.
439         pub fn remove_cached_payment(&self, payment_hash: &PaymentHash) {
440                 self.payment_cache.lock().unwrap().remove(payment_hash);
441         }
442 }
443
444 fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
445         invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
446 }
447
448 fn has_expired(params: &RouteParameters) -> bool {
449         if let Some(expiry_time) = params.payee.expiry_time {
450                 Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, Duration::from_secs(expiry_time))
451         } else { false }
452 }
453
454 impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> EventHandler for InvoicePayer<P, S, R, SR, L, E>
455 where
456         P::Target: Payer,
457         R::Target: Router<S>,
458         L::Target: Logger,
459 {
460         fn handle_event(&self, event: &Event) {
461                 match event {
462                         Event::PaymentPathFailed {
463                                 payment_id, payment_hash, rejected_by_dest, path, short_channel_id, retry, ..
464                         } => {
465                                 if let Some(short_channel_id) = short_channel_id {
466                                         let path = path.iter().collect::<Vec<_>>();
467                                         self.scorer.lock().payment_path_failed(&path, *short_channel_id);
468                                 }
469
470                                 if payment_id.is_none() {
471                                         log_trace!(self.logger, "Payment {} has no id; not retrying", log_bytes!(payment_hash.0));
472                                 } else if *rejected_by_dest {
473                                         log_trace!(self.logger, "Payment {} rejected by destination; not retrying", log_bytes!(payment_hash.0));
474                                         self.payer.abandon_payment(payment_id.unwrap());
475                                 } else if retry.is_none() {
476                                         log_trace!(self.logger, "Payment {} missing retry params; not retrying", log_bytes!(payment_hash.0));
477                                         self.payer.abandon_payment(payment_id.unwrap());
478                                 } else if self.retry_payment(payment_id.unwrap(), *payment_hash, retry.as_ref().unwrap()).is_ok() {
479                                         // We retried at least somewhat, don't provide the PaymentPathFailed event to the user.
480                                         return;
481                                 } else {
482                                         self.payer.abandon_payment(payment_id.unwrap());
483                                 }
484                         },
485                         Event::PaymentFailed { payment_hash, .. } => {
486                                 self.remove_cached_payment(&payment_hash);
487                         },
488                         Event::PaymentPathSuccessful { path, .. } => {
489                                 let path = path.iter().collect::<Vec<_>>();
490                                 self.scorer.lock().payment_path_successful(&path);
491                         },
492                         Event::PaymentSent { payment_hash, .. } => {
493                                 let mut payment_cache = self.payment_cache.lock().unwrap();
494                                 let attempts = payment_cache
495                                         .remove(payment_hash)
496                                         .map_or(1, |attempts| attempts + 1);
497                                 log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
498                         },
499                         _ => {},
500                 }
501
502                 // Delegate to the decorated event handler unless the payment is retried.
503                 self.event_handler.handle_event(event)
504         }
505 }
506
507 #[cfg(test)]
508 mod tests {
509         use super::*;
510         use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
511         use utils::create_invoice_from_channelmanager;
512         use bitcoin_hashes::sha256::Hash as Sha256;
513         use lightning::ln::PaymentPreimage;
514         use lightning::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
515         use lightning::ln::functional_test_utils::*;
516         use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
517         use lightning::routing::network_graph::NodeId;
518         use lightning::routing::router::{Payee, Route, RouteHop};
519         use lightning::util::test_utils::TestLogger;
520         use lightning::util::errors::APIError;
521         use lightning::util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
522         use secp256k1::{SecretKey, PublicKey, Secp256k1};
523         use std::cell::RefCell;
524         use std::collections::VecDeque;
525         use std::time::{SystemTime, Duration};
526
527         fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
528                 let payment_hash = Sha256::hash(&payment_preimage.0);
529                 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
530                 InvoiceBuilder::new(Currency::Bitcoin)
531                         .description("test".into())
532                         .payment_hash(payment_hash)
533                         .payment_secret(PaymentSecret([0; 32]))
534                         .current_timestamp()
535                         .min_final_cltv_expiry(144)
536                         .amount_milli_satoshis(128)
537                         .build_signed(|hash| {
538                                 Secp256k1::new().sign_recoverable(hash, &private_key)
539                         })
540                         .unwrap()
541         }
542
543         fn zero_value_invoice(payment_preimage: PaymentPreimage) -> Invoice {
544                 let payment_hash = Sha256::hash(&payment_preimage.0);
545                 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
546                 InvoiceBuilder::new(Currency::Bitcoin)
547                         .description("test".into())
548                         .payment_hash(payment_hash)
549                         .payment_secret(PaymentSecret([0; 32]))
550                         .current_timestamp()
551                         .min_final_cltv_expiry(144)
552                         .build_signed(|hash| {
553                                 Secp256k1::new().sign_recoverable(hash, &private_key)
554                         })
555                         .unwrap()
556         }
557
558         fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
559                 let payment_hash = Sha256::hash(&payment_preimage.0);
560                 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
561                 let timestamp = SystemTime::now()
562                         .checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
563                         .unwrap();
564                 InvoiceBuilder::new(Currency::Bitcoin)
565                         .description("test".into())
566                         .payment_hash(payment_hash)
567                         .payment_secret(PaymentSecret([0; 32]))
568                         .timestamp(timestamp)
569                         .min_final_cltv_expiry(144)
570                         .amount_milli_satoshis(128)
571                         .build_signed(|hash| {
572                                 Secp256k1::new().sign_recoverable(hash, &private_key)
573                         })
574                         .unwrap()
575         }
576
577         fn pubkey() -> PublicKey {
578                 PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()
579         }
580
581         #[test]
582         fn pays_invoice_on_first_attempt() {
583                 let event_handled = core::cell::RefCell::new(false);
584                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
585
586                 let payment_preimage = PaymentPreimage([1; 32]);
587                 let invoice = invoice(payment_preimage);
588                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
589                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
590
591                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
592                 let router = TestRouter {};
593                 let scorer = RefCell::new(TestScorer::new());
594                 let logger = TestLogger::new();
595                 let invoice_payer =
596                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
597
598                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
599                 assert_eq!(*payer.attempts.borrow(), 1);
600
601                 invoice_payer.handle_event(&Event::PaymentSent {
602                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
603                 });
604                 assert_eq!(*event_handled.borrow(), true);
605                 assert_eq!(*payer.attempts.borrow(), 1);
606         }
607
608         #[test]
609         fn pays_invoice_on_retry() {
610                 let event_handled = core::cell::RefCell::new(false);
611                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
612
613                 let payment_preimage = PaymentPreimage([1; 32]);
614                 let invoice = invoice(payment_preimage);
615                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
616                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
617
618                 let payer = TestPayer::new()
619                         .expect_send(Amount::ForInvoice(final_value_msat))
620                         .expect_send(Amount::OnRetry(final_value_msat / 2));
621                 let router = TestRouter {};
622                 let scorer = RefCell::new(TestScorer::new());
623                 let logger = TestLogger::new();
624                 let invoice_payer =
625                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
626
627                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
628                 assert_eq!(*payer.attempts.borrow(), 1);
629
630                 let event = Event::PaymentPathFailed {
631                         payment_id,
632                         payment_hash,
633                         network_update: None,
634                         rejected_by_dest: false,
635                         all_paths_failed: false,
636                         path: TestRouter::path_for_value(final_value_msat),
637                         short_channel_id: None,
638                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
639                 };
640                 invoice_payer.handle_event(&event);
641                 assert_eq!(*event_handled.borrow(), false);
642                 assert_eq!(*payer.attempts.borrow(), 2);
643
644                 invoice_payer.handle_event(&Event::PaymentSent {
645                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
646                 });
647                 assert_eq!(*event_handled.borrow(), true);
648                 assert_eq!(*payer.attempts.borrow(), 2);
649         }
650
651         #[test]
652         fn pays_invoice_on_partial_failure() {
653                 let event_handler = |_: &_| { panic!() };
654
655                 let payment_preimage = PaymentPreimage([1; 32]);
656                 let invoice = invoice(payment_preimage);
657                 let retry = TestRouter::retry_for_invoice(&invoice);
658                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
659
660                 let payer = TestPayer::new()
661                         .fails_with_partial_failure(retry.clone(), OnAttempt(1))
662                         .fails_with_partial_failure(retry, OnAttempt(2))
663                         .expect_send(Amount::ForInvoice(final_value_msat))
664                         .expect_send(Amount::OnRetry(final_value_msat / 2))
665                         .expect_send(Amount::OnRetry(final_value_msat / 2));
666                 let router = TestRouter {};
667                 let scorer = RefCell::new(TestScorer::new());
668                 let logger = TestLogger::new();
669                 let invoice_payer =
670                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
671
672                 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
673         }
674
675         #[test]
676         fn retries_payment_path_for_unknown_payment() {
677                 let event_handled = core::cell::RefCell::new(false);
678                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
679
680                 let payment_preimage = PaymentPreimage([1; 32]);
681                 let invoice = invoice(payment_preimage);
682                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
683                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
684
685                 let payer = TestPayer::new()
686                         .expect_send(Amount::OnRetry(final_value_msat / 2))
687                         .expect_send(Amount::OnRetry(final_value_msat / 2));
688                 let router = TestRouter {};
689                 let scorer = RefCell::new(TestScorer::new());
690                 let logger = TestLogger::new();
691                 let invoice_payer =
692                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
693
694                 let payment_id = Some(PaymentId([1; 32]));
695                 let event = Event::PaymentPathFailed {
696                         payment_id,
697                         payment_hash,
698                         network_update: None,
699                         rejected_by_dest: false,
700                         all_paths_failed: false,
701                         path: TestRouter::path_for_value(final_value_msat),
702                         short_channel_id: None,
703                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
704                 };
705                 invoice_payer.handle_event(&event);
706                 assert_eq!(*event_handled.borrow(), false);
707                 assert_eq!(*payer.attempts.borrow(), 1);
708
709                 invoice_payer.handle_event(&event);
710                 assert_eq!(*event_handled.borrow(), false);
711                 assert_eq!(*payer.attempts.borrow(), 2);
712
713                 invoice_payer.handle_event(&Event::PaymentSent {
714                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
715                 });
716                 assert_eq!(*event_handled.borrow(), true);
717                 assert_eq!(*payer.attempts.borrow(), 2);
718         }
719
720         #[test]
721         fn fails_paying_invoice_after_max_retries() {
722                 let event_handled = core::cell::RefCell::new(false);
723                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
724
725                 let payment_preimage = PaymentPreimage([1; 32]);
726                 let invoice = invoice(payment_preimage);
727                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
728
729                 let payer = TestPayer::new()
730                         .expect_send(Amount::ForInvoice(final_value_msat))
731                         .expect_send(Amount::OnRetry(final_value_msat / 2))
732                         .expect_send(Amount::OnRetry(final_value_msat / 2));
733                 let router = TestRouter {};
734                 let scorer = RefCell::new(TestScorer::new());
735                 let logger = TestLogger::new();
736                 let invoice_payer =
737                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
738
739                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
740                 assert_eq!(*payer.attempts.borrow(), 1);
741
742                 let event = Event::PaymentPathFailed {
743                         payment_id,
744                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
745                         network_update: None,
746                         rejected_by_dest: false,
747                         all_paths_failed: true,
748                         path: TestRouter::path_for_value(final_value_msat),
749                         short_channel_id: None,
750                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
751                 };
752                 invoice_payer.handle_event(&event);
753                 assert_eq!(*event_handled.borrow(), false);
754                 assert_eq!(*payer.attempts.borrow(), 2);
755
756                 let event = Event::PaymentPathFailed {
757                         payment_id,
758                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
759                         network_update: None,
760                         rejected_by_dest: false,
761                         all_paths_failed: false,
762                         path: TestRouter::path_for_value(final_value_msat / 2),
763                         short_channel_id: None,
764                         retry: Some(RouteParameters {
765                                 final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice)
766                         }),
767                 };
768                 invoice_payer.handle_event(&event);
769                 assert_eq!(*event_handled.borrow(), false);
770                 assert_eq!(*payer.attempts.borrow(), 3);
771
772                 invoice_payer.handle_event(&event);
773                 assert_eq!(*event_handled.borrow(), true);
774                 assert_eq!(*payer.attempts.borrow(), 3);
775         }
776
777         #[test]
778         fn fails_paying_invoice_with_missing_retry_params() {
779                 let event_handled = core::cell::RefCell::new(false);
780                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
781
782                 let payment_preimage = PaymentPreimage([1; 32]);
783                 let invoice = invoice(payment_preimage);
784                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
785
786                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
787                 let router = TestRouter {};
788                 let scorer = RefCell::new(TestScorer::new());
789                 let logger = TestLogger::new();
790                 let invoice_payer =
791                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
792
793                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
794                 assert_eq!(*payer.attempts.borrow(), 1);
795
796                 let event = Event::PaymentPathFailed {
797                         payment_id,
798                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
799                         network_update: None,
800                         rejected_by_dest: false,
801                         all_paths_failed: false,
802                         path: vec![],
803                         short_channel_id: None,
804                         retry: None,
805                 };
806                 invoice_payer.handle_event(&event);
807                 assert_eq!(*event_handled.borrow(), true);
808                 assert_eq!(*payer.attempts.borrow(), 1);
809         }
810
811         #[test]
812         fn fails_paying_invoice_after_expiration() {
813                 let event_handled = core::cell::RefCell::new(false);
814                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
815
816                 let payer = TestPayer::new();
817                 let router = TestRouter {};
818                 let scorer = RefCell::new(TestScorer::new());
819                 let logger = TestLogger::new();
820                 let invoice_payer =
821                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
822
823                 let payment_preimage = PaymentPreimage([1; 32]);
824                 let invoice = expired_invoice(payment_preimage);
825                 if let PaymentError::Invoice(msg) = invoice_payer.pay_invoice(&invoice).unwrap_err() {
826                         assert_eq!(msg, "Invoice expired prior to send");
827                 } else { panic!("Expected Invoice Error"); }
828         }
829
830         #[test]
831         fn fails_retrying_invoice_after_expiration() {
832                 let event_handled = core::cell::RefCell::new(false);
833                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
834
835                 let payment_preimage = PaymentPreimage([1; 32]);
836                 let invoice = invoice(payment_preimage);
837                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
838
839                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
840                 let router = TestRouter {};
841                 let scorer = RefCell::new(TestScorer::new());
842                 let logger = TestLogger::new();
843                 let invoice_payer =
844                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
845
846                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
847                 assert_eq!(*payer.attempts.borrow(), 1);
848
849                 let mut retry_data = TestRouter::retry_for_invoice(&invoice);
850                 retry_data.payee.expiry_time = Some(SystemTime::now()
851                         .checked_sub(Duration::from_secs(2)).unwrap()
852                         .duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
853                 let event = Event::PaymentPathFailed {
854                         payment_id,
855                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
856                         network_update: None,
857                         rejected_by_dest: false,
858                         all_paths_failed: false,
859                         path: vec![],
860                         short_channel_id: None,
861                         retry: Some(retry_data),
862                 };
863                 invoice_payer.handle_event(&event);
864                 assert_eq!(*event_handled.borrow(), true);
865                 assert_eq!(*payer.attempts.borrow(), 1);
866         }
867
868         #[test]
869         fn fails_paying_invoice_after_retry_error() {
870                 let event_handled = core::cell::RefCell::new(false);
871                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
872
873                 let payment_preimage = PaymentPreimage([1; 32]);
874                 let invoice = invoice(payment_preimage);
875                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
876
877                 let payer = TestPayer::new()
878                         .fails_on_attempt(2)
879                         .expect_send(Amount::ForInvoice(final_value_msat))
880                         .expect_send(Amount::OnRetry(final_value_msat / 2));
881                 let router = TestRouter {};
882                 let scorer = RefCell::new(TestScorer::new());
883                 let logger = TestLogger::new();
884                 let invoice_payer =
885                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
886
887                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
888                 assert_eq!(*payer.attempts.borrow(), 1);
889
890                 let event = Event::PaymentPathFailed {
891                         payment_id,
892                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
893                         network_update: None,
894                         rejected_by_dest: false,
895                         all_paths_failed: false,
896                         path: TestRouter::path_for_value(final_value_msat / 2),
897                         short_channel_id: None,
898                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
899                 };
900                 invoice_payer.handle_event(&event);
901                 assert_eq!(*event_handled.borrow(), true);
902                 assert_eq!(*payer.attempts.borrow(), 2);
903         }
904
905         #[test]
906         fn fails_paying_invoice_after_rejected_by_payee() {
907                 let event_handled = core::cell::RefCell::new(false);
908                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
909
910                 let payment_preimage = PaymentPreimage([1; 32]);
911                 let invoice = invoice(payment_preimage);
912                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
913
914                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
915                 let router = TestRouter {};
916                 let scorer = RefCell::new(TestScorer::new());
917                 let logger = TestLogger::new();
918                 let invoice_payer =
919                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
920
921                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
922                 assert_eq!(*payer.attempts.borrow(), 1);
923
924                 let event = Event::PaymentPathFailed {
925                         payment_id,
926                         payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
927                         network_update: None,
928                         rejected_by_dest: true,
929                         all_paths_failed: false,
930                         path: vec![],
931                         short_channel_id: None,
932                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
933                 };
934                 invoice_payer.handle_event(&event);
935                 assert_eq!(*event_handled.borrow(), true);
936                 assert_eq!(*payer.attempts.borrow(), 1);
937         }
938
939         #[test]
940         fn fails_repaying_invoice_with_pending_payment() {
941                 let event_handled = core::cell::RefCell::new(false);
942                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
943
944                 let payment_preimage = PaymentPreimage([1; 32]);
945                 let invoice = invoice(payment_preimage);
946                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
947
948                 let payer = TestPayer::new()
949                         .expect_send(Amount::ForInvoice(final_value_msat))
950                         .expect_send(Amount::ForInvoice(final_value_msat));
951                 let router = TestRouter {};
952                 let scorer = RefCell::new(TestScorer::new());
953                 let logger = TestLogger::new();
954                 let invoice_payer =
955                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
956
957                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
958
959                 // Cannot repay an invoice pending payment.
960                 match invoice_payer.pay_invoice(&invoice) {
961                         Err(PaymentError::Invoice("payment pending")) => {},
962                         Err(_) => panic!("unexpected error"),
963                         Ok(_) => panic!("expected invoice error"),
964                 }
965
966                 // Can repay an invoice once cleared from cache.
967                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
968                 invoice_payer.remove_cached_payment(&payment_hash);
969                 assert!(invoice_payer.pay_invoice(&invoice).is_ok());
970
971                 // Cannot retry paying an invoice if cleared from cache.
972                 invoice_payer.remove_cached_payment(&payment_hash);
973                 let event = Event::PaymentPathFailed {
974                         payment_id,
975                         payment_hash,
976                         network_update: None,
977                         rejected_by_dest: false,
978                         all_paths_failed: false,
979                         path: vec![],
980                         short_channel_id: None,
981                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
982                 };
983                 invoice_payer.handle_event(&event);
984                 assert_eq!(*event_handled.borrow(), true);
985         }
986
987         #[test]
988         fn fails_paying_invoice_with_routing_errors() {
989                 let payer = TestPayer::new();
990                 let router = FailingRouter {};
991                 let scorer = RefCell::new(TestScorer::new());
992                 let logger = TestLogger::new();
993                 let invoice_payer =
994                         InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
995
996                 let payment_preimage = PaymentPreimage([1; 32]);
997                 let invoice = invoice(payment_preimage);
998                 match invoice_payer.pay_invoice(&invoice) {
999                         Err(PaymentError::Routing(_)) => {},
1000                         Err(_) => panic!("unexpected error"),
1001                         Ok(_) => panic!("expected routing error"),
1002                 }
1003         }
1004
1005         #[test]
1006         fn fails_paying_invoice_with_sending_errors() {
1007                 let payment_preimage = PaymentPreimage([1; 32]);
1008                 let invoice = invoice(payment_preimage);
1009                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1010
1011                 let payer = TestPayer::new()
1012                         .fails_on_attempt(1)
1013                         .expect_send(Amount::ForInvoice(final_value_msat));
1014                 let router = TestRouter {};
1015                 let scorer = RefCell::new(TestScorer::new());
1016                 let logger = TestLogger::new();
1017                 let invoice_payer =
1018                         InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
1019
1020                 match invoice_payer.pay_invoice(&invoice) {
1021                         Err(PaymentError::Sending(_)) => {},
1022                         Err(_) => panic!("unexpected error"),
1023                         Ok(_) => panic!("expected sending error"),
1024                 }
1025         }
1026
1027         #[test]
1028         fn pays_zero_value_invoice_using_amount() {
1029                 let event_handled = core::cell::RefCell::new(false);
1030                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1031
1032                 let payment_preimage = PaymentPreimage([1; 32]);
1033                 let invoice = zero_value_invoice(payment_preimage);
1034                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1035                 let final_value_msat = 100;
1036
1037                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1038                 let router = TestRouter {};
1039                 let scorer = RefCell::new(TestScorer::new());
1040                 let logger = TestLogger::new();
1041                 let invoice_payer =
1042                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1043
1044                 let payment_id =
1045                         Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
1046                 assert_eq!(*payer.attempts.borrow(), 1);
1047
1048                 invoice_payer.handle_event(&Event::PaymentSent {
1049                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1050                 });
1051                 assert_eq!(*event_handled.borrow(), true);
1052                 assert_eq!(*payer.attempts.borrow(), 1);
1053         }
1054
1055         #[test]
1056         fn fails_paying_zero_value_invoice_with_amount() {
1057                 let event_handled = core::cell::RefCell::new(false);
1058                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1059
1060                 let payer = TestPayer::new();
1061                 let router = TestRouter {};
1062                 let scorer = RefCell::new(TestScorer::new());
1063                 let logger = TestLogger::new();
1064                 let invoice_payer =
1065                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
1066
1067                 let payment_preimage = PaymentPreimage([1; 32]);
1068                 let invoice = invoice(payment_preimage);
1069
1070                 // Cannot repay an invoice pending payment.
1071                 match invoice_payer.pay_zero_value_invoice(&invoice, 100) {
1072                         Err(PaymentError::Invoice("amount unexpected")) => {},
1073                         Err(_) => panic!("unexpected error"),
1074                         Ok(_) => panic!("expected invoice error"),
1075                 }
1076         }
1077
1078         #[test]
1079         fn pays_pubkey_with_amount() {
1080                 let event_handled = core::cell::RefCell::new(false);
1081                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1082
1083                 let pubkey = pubkey();
1084                 let payment_preimage = PaymentPreimage([1; 32]);
1085                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
1086                 let final_value_msat = 100;
1087                 let final_cltv_expiry_delta = 42;
1088
1089                 let payer = TestPayer::new()
1090                         .expect_send(Amount::Spontaneous(final_value_msat))
1091                         .expect_send(Amount::OnRetry(final_value_msat));
1092                 let router = TestRouter {};
1093                 let scorer = RefCell::new(TestScorer::new());
1094                 let logger = TestLogger::new();
1095                 let invoice_payer =
1096                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1097
1098                 let payment_id = Some(invoice_payer.pay_pubkey(
1099                                 pubkey, payment_preimage, final_value_msat, final_cltv_expiry_delta
1100                         ).unwrap());
1101                 assert_eq!(*payer.attempts.borrow(), 1);
1102
1103                 let retry = RouteParameters {
1104                         payee: Payee::for_keysend(pubkey),
1105                         final_value_msat,
1106                         final_cltv_expiry_delta,
1107                 };
1108                 let event = Event::PaymentPathFailed {
1109                         payment_id,
1110                         payment_hash,
1111                         network_update: None,
1112                         rejected_by_dest: false,
1113                         all_paths_failed: false,
1114                         path: vec![],
1115                         short_channel_id: None,
1116                         retry: Some(retry),
1117                 };
1118                 invoice_payer.handle_event(&event);
1119                 assert_eq!(*event_handled.borrow(), false);
1120                 assert_eq!(*payer.attempts.borrow(), 2);
1121
1122                 invoice_payer.handle_event(&Event::PaymentSent {
1123                         payment_id, payment_preimage, payment_hash, fee_paid_msat: None
1124                 });
1125                 assert_eq!(*event_handled.borrow(), true);
1126                 assert_eq!(*payer.attempts.borrow(), 2);
1127         }
1128
1129         #[test]
1130         fn scores_failed_channel() {
1131                 let event_handled = core::cell::RefCell::new(false);
1132                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1133
1134                 let payment_preimage = PaymentPreimage([1; 32]);
1135                 let invoice = invoice(payment_preimage);
1136                 let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
1137                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1138                 let path = TestRouter::path_for_value(final_value_msat);
1139                 let short_channel_id = Some(path[0].short_channel_id);
1140
1141                 // Expect that scorer is given short_channel_id upon handling the event.
1142                 let payer = TestPayer::new()
1143                         .expect_send(Amount::ForInvoice(final_value_msat))
1144                         .expect_send(Amount::OnRetry(final_value_msat / 2));
1145                 let router = TestRouter {};
1146                 let scorer = RefCell::new(TestScorer::new().expect(PaymentPath::Failure {
1147                         path: path.clone(), short_channel_id: path[0].short_channel_id,
1148                 }));
1149                 let logger = TestLogger::new();
1150                 let invoice_payer =
1151                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1152
1153                 let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
1154                 let event = Event::PaymentPathFailed {
1155                         payment_id,
1156                         payment_hash,
1157                         network_update: None,
1158                         rejected_by_dest: false,
1159                         all_paths_failed: false,
1160                         path,
1161                         short_channel_id,
1162                         retry: Some(TestRouter::retry_for_invoice(&invoice)),
1163                 };
1164                 invoice_payer.handle_event(&event);
1165         }
1166
1167         #[test]
1168         fn scores_successful_channels() {
1169                 let event_handled = core::cell::RefCell::new(false);
1170                 let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1171
1172                 let payment_preimage = PaymentPreimage([1; 32]);
1173                 let invoice = invoice(payment_preimage);
1174                 let payment_hash = Some(PaymentHash(invoice.payment_hash().clone().into_inner()));
1175                 let final_value_msat = invoice.amount_milli_satoshis().unwrap();
1176                 let route = TestRouter::route_for_value(final_value_msat);
1177
1178                 // Expect that scorer is given short_channel_id upon handling the event.
1179                 let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
1180                 let router = TestRouter {};
1181                 let scorer = RefCell::new(TestScorer::new()
1182                         .expect(PaymentPath::Success { path: route.paths[0].clone() })
1183                         .expect(PaymentPath::Success { path: route.paths[1].clone() })
1184                 );
1185                 let logger = TestLogger::new();
1186                 let invoice_payer =
1187                         InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
1188
1189                 let payment_id = invoice_payer.pay_invoice(&invoice).unwrap();
1190                 let event = Event::PaymentPathSuccessful {
1191                         payment_id, payment_hash, path: route.paths[0].clone()
1192                 };
1193                 invoice_payer.handle_event(&event);
1194                 let event = Event::PaymentPathSuccessful {
1195                         payment_id, payment_hash, path: route.paths[1].clone()
1196                 };
1197                 invoice_payer.handle_event(&event);
1198         }
1199
1200         struct TestRouter;
1201
1202         impl TestRouter {
1203                 fn route_for_value(final_value_msat: u64) -> Route {
1204                         Route {
1205                                 paths: vec![
1206                                         vec![RouteHop {
1207                                                 pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
1208                                                 channel_features: ChannelFeatures::empty(),
1209                                                 node_features: NodeFeatures::empty(),
1210                                                 short_channel_id: 0, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1211                                         }],
1212                                         vec![RouteHop {
1213                                                 pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
1214                                                 channel_features: ChannelFeatures::empty(),
1215                                                 node_features: NodeFeatures::empty(),
1216                                                 short_channel_id: 1, fee_msat: final_value_msat / 2, cltv_expiry_delta: 144
1217                                         }],
1218                                 ],
1219                                 payee: None,
1220                         }
1221                 }
1222
1223                 fn path_for_value(final_value_msat: u64) -> Vec<RouteHop> {
1224                         TestRouter::route_for_value(final_value_msat).paths[0].clone()
1225                 }
1226
1227                 fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
1228                         let mut payee = Payee::from_node_id(invoice.recover_payee_pub_key())
1229                                 .with_expiry_time(expiry_time_from_unix_epoch(invoice).as_secs())
1230                                 .with_route_hints(invoice.route_hints());
1231                         if let Some(features) = invoice.features() {
1232                                 payee = payee.with_features(features.clone());
1233                         }
1234                         let final_value_msat = invoice.amount_milli_satoshis().unwrap() / 2;
1235                         RouteParameters {
1236                                 payee,
1237                                 final_value_msat,
1238                                 final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
1239                         }
1240                 }
1241         }
1242
1243         impl<S: Score> Router<S> for TestRouter {
1244                 fn find_route(
1245                         &self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
1246                         _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1247                 ) -> Result<Route, LightningError> {
1248                         Ok(Route {
1249                                 payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
1250                         })
1251                 }
1252         }
1253
1254         struct FailingRouter;
1255
1256         impl<S: Score> Router<S> for FailingRouter {
1257                 fn find_route(
1258                         &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1259                         _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1260                 ) -> Result<Route, LightningError> {
1261                         Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
1262                 }
1263         }
1264
1265         struct TestScorer {
1266                 expectations: Option<VecDeque<PaymentPath>>,
1267         }
1268
1269         #[derive(Debug)]
1270         enum PaymentPath {
1271                 Failure { path: Vec<RouteHop>, short_channel_id: u64 },
1272                 Success { path: Vec<RouteHop> },
1273         }
1274
1275         impl TestScorer {
1276                 fn new() -> Self {
1277                         Self {
1278                                 expectations: None,
1279                         }
1280                 }
1281
1282                 fn expect(mut self, expectation: PaymentPath) -> Self {
1283                         self.expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
1284                         self
1285                 }
1286         }
1287
1288         #[cfg(c_bindings)]
1289         impl lightning::util::ser::Writeable for TestScorer {
1290                 fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), std::io::Error> { unreachable!(); }
1291         }
1292
1293         impl Score for TestScorer {
1294                 fn channel_penalty_msat(
1295                         &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
1296                 ) -> u64 { 0 }
1297
1298                 fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
1299                         if let Some(expectations) = &mut self.expectations {
1300                                 match expectations.pop_front() {
1301                                         Some(PaymentPath::Failure { path, short_channel_id }) => {
1302                                                 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1303                                                 assert_eq!(actual_short_channel_id, short_channel_id);
1304                                         },
1305                                         Some(PaymentPath::Success { path }) => {
1306                                                 panic!("Unexpected successful payment path: {:?}", path)
1307                                         },
1308                                         None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
1309                                 }
1310                         }
1311                 }
1312
1313                 fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
1314                         if let Some(expectations) = &mut self.expectations {
1315                                 match expectations.pop_front() {
1316                                         Some(PaymentPath::Failure { path, .. }) => {
1317                                                 panic!("Unexpected payment path failure: {:?}", path)
1318                                         },
1319                                         Some(PaymentPath::Success { path }) => {
1320                                                 assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1321                                         },
1322                                         None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),
1323                                 }
1324                         }
1325                 }
1326         }
1327
1328         impl Drop for TestScorer {
1329                 fn drop(&mut self) {
1330                         if std::thread::panicking() {
1331                                 return;
1332                         }
1333
1334                         if let Some(expectations) = &self.expectations {
1335                                 if !expectations.is_empty() {
1336                                         panic!("Unsatisfied scorer expectations: {:?}", expectations);
1337                                 }
1338                         }
1339                 }
1340         }
1341
1342         struct TestPayer {
1343                 expectations: core::cell::RefCell<VecDeque<Amount>>,
1344                 attempts: core::cell::RefCell<usize>,
1345                 failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
1346         }
1347
1348         #[derive(Clone, Debug, PartialEq, Eq)]
1349         enum Amount {
1350                 ForInvoice(u64),
1351                 Spontaneous(u64),
1352                 OnRetry(u64),
1353         }
1354
1355         struct OnAttempt(usize);
1356
1357         impl TestPayer {
1358                 fn new() -> Self {
1359                         Self {
1360                                 expectations: core::cell::RefCell::new(VecDeque::new()),
1361                                 attempts: core::cell::RefCell::new(0),
1362                                 failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
1363                         }
1364                 }
1365
1366                 fn expect_send(self, value_msat: Amount) -> Self {
1367                         self.expectations.borrow_mut().push_back(value_msat);
1368                         self
1369                 }
1370
1371                 fn fails_on_attempt(self, attempt: usize) -> Self {
1372                         let failure = PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed);
1373                         self.fails_with(failure, OnAttempt(attempt))
1374                 }
1375
1376                 fn fails_with_partial_failure(self, retry: RouteParameters, attempt: OnAttempt) -> Self {
1377                         self.fails_with(PaymentSendFailure::PartialFailure {
1378                                 results: vec![],
1379                                 failed_paths_retry: Some(retry),
1380                                 payment_id: PaymentId([1; 32]),
1381                         }, attempt)
1382                 }
1383
1384                 fn fails_with(self, failure: PaymentSendFailure, attempt: OnAttempt) -> Self {
1385                         self.failing_on_attempt.borrow_mut().insert(attempt.0, failure);
1386                         self
1387                 }
1388
1389                 fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
1390                         let mut attempts = self.attempts.borrow_mut();
1391                         *attempts += 1;
1392
1393                         match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1394                                 Some(failure) => Err(failure),
1395                                 None => Ok(PaymentId([1; 32])),
1396                         }
1397                 }
1398
1399                 fn check_value_msats(&self, actual_value_msats: Amount) {
1400                         let expected_value_msats = self.expectations.borrow_mut().pop_front();
1401                         if let Some(expected_value_msats) = expected_value_msats {
1402                                 assert_eq!(actual_value_msats, expected_value_msats);
1403                         } else {
1404                                 panic!("Unexpected amount: {:?}", actual_value_msats);
1405                         }
1406                 }
1407         }
1408
1409         impl Drop for TestPayer {
1410                 fn drop(&mut self) {
1411                         if std::thread::panicking() {
1412                                 return;
1413                         }
1414
1415                         if !self.expectations.borrow().is_empty() {
1416                                 panic!("Unsatisfied payment expectations: {:?}", self.expectations.borrow());
1417                         }
1418                 }
1419         }
1420
1421         impl Payer for TestPayer {
1422                 fn node_id(&self) -> PublicKey {
1423                         let secp_ctx = Secp256k1::new();
1424                         PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
1425                 }
1426
1427                 fn first_hops(&self) -> Vec<ChannelDetails> {
1428                         Vec::new()
1429                 }
1430
1431                 fn send_payment(
1432                         &self, route: &Route, _payment_hash: PaymentHash,
1433                         _payment_secret: &Option<PaymentSecret>
1434                 ) -> Result<PaymentId, PaymentSendFailure> {
1435                         self.check_value_msats(Amount::ForInvoice(route.get_total_amount()));
1436                         self.check_attempts()
1437                 }
1438
1439                 fn send_spontaneous_payment(
1440                         &self, route: &Route, _payment_preimage: PaymentPreimage,
1441                 ) -> Result<PaymentId, PaymentSendFailure> {
1442                         self.check_value_msats(Amount::Spontaneous(route.get_total_amount()));
1443                         self.check_attempts()
1444                 }
1445
1446                 fn retry_payment(
1447                         &self, route: &Route, _payment_id: PaymentId
1448                 ) -> Result<(), PaymentSendFailure> {
1449                         self.check_value_msats(Amount::OnRetry(route.get_total_amount()));
1450                         self.check_attempts().map(|_| ())
1451                 }
1452
1453                 fn abandon_payment(&self, _payment_id: PaymentId) { }
1454         }
1455
1456         // *** Full Featured Functional Tests with a Real ChannelManager ***
1457         struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
1458
1459         impl<S: Score> Router<S> for ManualRouter {
1460                 fn find_route(
1461                         &self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
1462                         _first_hops: Option<&[&ChannelDetails]>, _scorer: &S
1463                 ) -> Result<Route, LightningError> {
1464                         self.0.borrow_mut().pop_front().unwrap()
1465                 }
1466         }
1467         impl ManualRouter {
1468                 fn expect_find_route(&self, result: Result<Route, LightningError>) {
1469                         self.0.borrow_mut().push_back(result);
1470                 }
1471         }
1472         impl Drop for ManualRouter {
1473                 fn drop(&mut self) {
1474                         if std::thread::panicking() {
1475                                 return;
1476                         }
1477                         assert!(self.0.borrow_mut().is_empty());
1478                 }
1479         }
1480
1481         #[test]
1482         fn retry_multi_path_single_failed_payment() {
1483                 // Tests that we can/will retry after a single path of an MPP payment failed immediately
1484                 let chanmon_cfgs = create_chanmon_cfgs(2);
1485                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1486                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1487                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1488
1489                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1490                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1491                 let chans = nodes[0].node.list_usable_channels();
1492                 let mut route = Route {
1493                         paths: vec![
1494                                 vec![RouteHop {
1495                                         pubkey: nodes[1].node.get_our_node_id(),
1496                                         node_features: NodeFeatures::known(),
1497                                         short_channel_id: chans[0].short_channel_id.unwrap(),
1498                                         channel_features: ChannelFeatures::known(),
1499                                         fee_msat: 10_000,
1500                                         cltv_expiry_delta: 100,
1501                                 }],
1502                                 vec![RouteHop {
1503                                         pubkey: nodes[1].node.get_our_node_id(),
1504                                         node_features: NodeFeatures::known(),
1505                                         short_channel_id: chans[1].short_channel_id.unwrap(),
1506                                         channel_features: ChannelFeatures::known(),
1507                                         fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1508                                         cltv_expiry_delta: 100,
1509                                 }],
1510                         ],
1511                         payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1512                 };
1513                 let router = ManualRouter(RefCell::new(VecDeque::new()));
1514                 router.expect_find_route(Ok(route.clone()));
1515                 // On retry, split the payment across both channels.
1516                 route.paths[0][0].fee_msat = 50_000_001;
1517                 route.paths[1][0].fee_msat = 50_000_000;
1518                 router.expect_find_route(Ok(route.clone()));
1519
1520                 let event_handler = |_: &_| { panic!(); };
1521                 let scorer = RefCell::new(TestScorer::new());
1522                 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1523
1524                 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1525                         &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1526                         .is_ok());
1527                 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1528                 assert_eq!(htlc_msgs.len(), 2);
1529                 check_added_monitors!(nodes[0], 2);
1530         }
1531
1532         #[test]
1533         fn immediate_retry_on_failure() {
1534                 // Tests that we can/will retry immediately after a failure
1535                 let chanmon_cfgs = create_chanmon_cfgs(2);
1536                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1537                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1538                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1539
1540                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1541                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
1542                 let chans = nodes[0].node.list_usable_channels();
1543                 let mut route = Route {
1544                         paths: vec![
1545                                 vec![RouteHop {
1546                                         pubkey: nodes[1].node.get_our_node_id(),
1547                                         node_features: NodeFeatures::known(),
1548                                         short_channel_id: chans[0].short_channel_id.unwrap(),
1549                                         channel_features: ChannelFeatures::known(),
1550                                         fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
1551                                         cltv_expiry_delta: 100,
1552                                 }],
1553                         ],
1554                         payee: Some(Payee::from_node_id(nodes[1].node.get_our_node_id())),
1555                 };
1556                 let router = ManualRouter(RefCell::new(VecDeque::new()));
1557                 router.expect_find_route(Ok(route.clone()));
1558                 // On retry, split the payment across both channels.
1559                 route.paths.push(route.paths[0].clone());
1560                 route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
1561                 route.paths[0][0].fee_msat = 50_000_000;
1562                 route.paths[1][0].fee_msat = 50_000_001;
1563                 router.expect_find_route(Ok(route.clone()));
1564
1565                 let event_handler = |_: &_| { panic!(); };
1566                 let scorer = RefCell::new(TestScorer::new());
1567                 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1568
1569                 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1570                         &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1571                         .is_ok());
1572                 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
1573                 assert_eq!(htlc_msgs.len(), 2);
1574                 check_added_monitors!(nodes[0], 2);
1575         }
1576
1577         #[test]
1578         fn no_extra_retries_on_back_to_back_fail() {
1579                 // In a previous release, we had a race where we may exceed the payment retry count if we
1580                 // get two failures in a row with the second having `all_paths_failed` set.
1581                 // Generally, when we give up trying to retry a payment, we don't know for sure what the
1582                 // current state of the ChannelManager event queue is. Specifically, we cannot be sure that
1583                 // there are not multiple additional `PaymentPathFailed` or even `PaymentSent` events
1584                 // pending which we will see later. Thus, when we previously removed the retry tracking map
1585                 // entry after a `all_paths_failed` `PaymentPathFailed` event, we may have dropped the
1586                 // retry entry even though more events for the same payment were still pending. This led to
1587                 // us retrying a payment again even though we'd already given up on it.
1588                 //
1589                 // We now have a separate event - `PaymentFailed` which indicates no HTLCs remain and which
1590                 // is used to remove the payment retry counter entries instead. This tests for the specific
1591                 // excess-retry case while also testing `PaymentFailed` generation.
1592
1593                 let chanmon_cfgs = create_chanmon_cfgs(3);
1594                 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1595                 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1596                 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1597
1598                 let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1599                 let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1600
1601                 let mut route = Route {
1602                         paths: vec![
1603                                 vec![RouteHop {
1604                                         pubkey: nodes[1].node.get_our_node_id(),
1605                                         node_features: NodeFeatures::known(),
1606                                         short_channel_id: chan_1_scid,
1607                                         channel_features: ChannelFeatures::known(),
1608                                         fee_msat: 0,
1609                                         cltv_expiry_delta: 100,
1610                                 }, RouteHop {
1611                                         pubkey: nodes[2].node.get_our_node_id(),
1612                                         node_features: NodeFeatures::known(),
1613                                         short_channel_id: chan_2_scid,
1614                                         channel_features: ChannelFeatures::known(),
1615                                         fee_msat: 100_000_000,
1616                                         cltv_expiry_delta: 100,
1617                                 }],
1618                                 vec![RouteHop {
1619                                         pubkey: nodes[1].node.get_our_node_id(),
1620                                         node_features: NodeFeatures::known(),
1621                                         short_channel_id: chan_1_scid,
1622                                         channel_features: ChannelFeatures::known(),
1623                                         fee_msat: 0,
1624                                         cltv_expiry_delta: 100,
1625                                 }, RouteHop {
1626                                         pubkey: nodes[2].node.get_our_node_id(),
1627                                         node_features: NodeFeatures::known(),
1628                                         short_channel_id: chan_2_scid,
1629                                         channel_features: ChannelFeatures::known(),
1630                                         fee_msat: 100_000_000,
1631                                         cltv_expiry_delta: 100,
1632                                 }]
1633                         ],
1634                         payee: Some(Payee::from_node_id(nodes[2].node.get_our_node_id())),
1635                 };
1636                 let router = ManualRouter(RefCell::new(VecDeque::new()));
1637                 router.expect_find_route(Ok(route.clone()));
1638                 // On retry, we'll only be asked for one path
1639                 route.paths.remove(1);
1640                 router.expect_find_route(Ok(route.clone()));
1641
1642                 let expected_events: RefCell<VecDeque<&dyn Fn(&Event)>> = RefCell::new(VecDeque::new());
1643                 let event_handler = |event: &Event| {
1644                         let event_checker = expected_events.borrow_mut().pop_front().unwrap();
1645                         event_checker(event);
1646                 };
1647                 let scorer = RefCell::new(TestScorer::new());
1648                 let invoice_payer = InvoicePayer::new(nodes[0].node, router, &scorer, nodes[0].logger, event_handler, RetryAttempts(1));
1649
1650                 assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager(
1651                         &nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string()).unwrap())
1652                         .is_ok());
1653                 let htlc_updates = SendEvent::from_node(&nodes[0]);
1654                 check_added_monitors!(nodes[0], 1);
1655                 assert_eq!(htlc_updates.msgs.len(), 1);
1656
1657                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
1658                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
1659                 check_added_monitors!(nodes[1], 1);
1660                 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1661
1662                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
1663                 check_added_monitors!(nodes[0], 1);
1664                 let second_htlc_updates = SendEvent::from_node(&nodes[0]);
1665
1666                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
1667                 check_added_monitors!(nodes[0], 1);
1668                 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1669
1670                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
1671                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
1672                 check_added_monitors!(nodes[1], 1);
1673                 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1674
1675                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
1676                 check_added_monitors!(nodes[1], 1);
1677                 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1678
1679                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
1680                 check_added_monitors!(nodes[0], 1);
1681
1682                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
1683                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
1684                 check_added_monitors!(nodes[0], 1);
1685                 let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1686
1687                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
1688                 check_added_monitors!(nodes[1], 1);
1689                 let bs_second_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1690
1691                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
1692                 check_added_monitors!(nodes[1], 1);
1693                 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1694
1695                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.update_fail_htlcs[0]);
1696                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.commitment_signed);
1697                 check_added_monitors!(nodes[0], 1);
1698
1699                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
1700                 check_added_monitors!(nodes[0], 1);
1701                 let (as_third_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1702
1703                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
1704                 check_added_monitors!(nodes[1], 1);
1705                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
1706                 check_added_monitors!(nodes[1], 1);
1707                 let bs_fourth_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1708
1709                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_fourth_raa);
1710                 check_added_monitors!(nodes[0], 1);
1711
1712                 // At this point A has sent two HTLCs which both failed due to lack of fee. It now has two
1713                 // pending `PaymentPathFailed` events, one with `all_paths_failed` unset, and the second
1714                 // with it set. The first event will use up the only retry we are allowed, with the second
1715                 // `PaymentPathFailed` being passed up to the user (us, in this case). Previously, we'd
1716                 // treated this as "HTLC complete" and dropped the retry counter, causing us to retry again
1717                 // if the final HTLC failed.
1718                 expected_events.borrow_mut().push_back(&|ev: &Event| {
1719                         if let Event::PaymentPathFailed { rejected_by_dest, all_paths_failed, .. } = ev {
1720                                 assert!(!rejected_by_dest);
1721                                 assert!(all_paths_failed);
1722                         } else { panic!("Unexpected event"); }
1723                 });
1724                 nodes[0].node.process_pending_events(&invoice_payer);
1725                 assert!(expected_events.borrow().is_empty());
1726
1727                 let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
1728                 check_added_monitors!(nodes[0], 1);
1729
1730                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
1731                 commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
1732                 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1733                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
1734                 commitment_signed_dance!(nodes[0], nodes[1], &bs_fail_update.commitment_signed, false, true);
1735
1736                 expected_events.borrow_mut().push_back(&|ev: &Event| {
1737                         if let Event::PaymentPathFailed { rejected_by_dest, all_paths_failed, .. } = ev {
1738                                 assert!(!rejected_by_dest);
1739                                 assert!(all_paths_failed);
1740                         } else { panic!("Unexpected event"); }
1741                 });
1742                 expected_events.borrow_mut().push_back(&|ev: &Event| {
1743                         if let Event::PaymentFailed { .. } = ev {
1744                         } else { panic!("Unexpected event"); }
1745                 });
1746                 nodes[0].node.process_pending_events(&invoice_payer);
1747                 assert!(expected_events.borrow().is_empty());
1748         }
1749 }