]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/ln/outbound_payment.rs
Generate PendingHTLCsForwardable upon retryable payment
[rust-lightning] / lightning / src / ln / outbound_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 //! Utilities to send payments and manage outbound payment information.
11
12 use bitcoin::hashes::Hash;
13 use bitcoin::hashes::sha256::Hash as Sha256;
14 use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
15
16 use crate::chain::keysinterface::{EntropySource, NodeSigner, Recipient};
17 use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
18 use crate::ln::channelmanager::{HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, MIN_HTLC_RELAY_HOLDING_CELL_MILLIS, PaymentId};
19 use crate::ln::msgs::DecodeError;
20 use crate::ln::onion_utils::HTLCFailReason;
21 use crate::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, RoutePath};
22 use crate::util::errors::APIError;
23 use crate::util::events;
24 use crate::util::logger::Logger;
25 use crate::util::time::Time;
26 #[cfg(all(not(feature = "no-std"), test))]
27 use crate::util::time::tests::SinceEpoch;
28
29 use core::cmp;
30 use core::fmt::{self, Display, Formatter};
31 use core::ops::Deref;
32 use core::time::Duration;
33
34 use crate::prelude::*;
35 use crate::sync::Mutex;
36
37 /// Stores the session_priv for each part of a payment that is still pending. For versions 0.0.102
38 /// and later, also stores information for retrying the payment.
39 pub(crate) enum PendingOutboundPayment {
40         Legacy {
41                 session_privs: HashSet<[u8; 32]>,
42         },
43         Retryable {
44                 retry_strategy: Retry,
45                 attempts: PaymentAttempts,
46                 route_params: Option<RouteParameters>,
47                 session_privs: HashSet<[u8; 32]>,
48                 payment_hash: PaymentHash,
49                 payment_secret: Option<PaymentSecret>,
50                 pending_amt_msat: u64,
51                 /// Used to track the fee paid. Only present if the payment was serialized on 0.0.103+.
52                 pending_fee_msat: Option<u64>,
53                 /// The total payment amount across all paths, used to verify that a retry is not overpaying.
54                 total_msat: u64,
55                 /// Our best known block height at the time this payment was initiated.
56                 starting_block_height: u32,
57         },
58         /// When a pending payment is fulfilled, we continue tracking it until all pending HTLCs have
59         /// been resolved. This ensures we don't look up pending payments in ChannelMonitors on restart
60         /// and add a pending payment that was already fulfilled.
61         Fulfilled {
62                 session_privs: HashSet<[u8; 32]>,
63                 payment_hash: Option<PaymentHash>,
64                 timer_ticks_without_htlcs: u8,
65         },
66         /// When a payer gives up trying to retry a payment, they inform us, letting us generate a
67         /// `PaymentFailed` event when all HTLCs have irrevocably failed. This avoids a number of race
68         /// conditions in MPP-aware payment retriers (1), where the possibility of multiple
69         /// `PaymentPathFailed` events with `all_paths_failed` can be pending at once, confusing a
70         /// downstream event handler as to when a payment has actually failed.
71         ///
72         /// (1) <https://github.com/lightningdevkit/rust-lightning/issues/1164>
73         Abandoned {
74                 session_privs: HashSet<[u8; 32]>,
75                 payment_hash: PaymentHash,
76         },
77 }
78
79 impl PendingOutboundPayment {
80         fn increment_attempts(&mut self) {
81                 if let PendingOutboundPayment::Retryable { attempts, .. } = self {
82                         attempts.count += 1;
83                 }
84         }
85         fn is_retryable_now(&self) -> bool {
86                 if let PendingOutboundPayment::Retryable { retry_strategy, attempts, .. } = self {
87                         return retry_strategy.is_retryable_now(&attempts)
88                 }
89                 false
90         }
91         pub fn insert_previously_failed_scid(&mut self, scid: u64) {
92                 if let PendingOutboundPayment::Retryable { route_params: Some(params), .. } = self {
93                         params.payment_params.previously_failed_channels.push(scid);
94                 }
95         }
96         pub(super) fn is_fulfilled(&self) -> bool {
97                 match self {
98                         PendingOutboundPayment::Fulfilled { .. } => true,
99                         _ => false,
100                 }
101         }
102         pub(super) fn abandoned(&self) -> bool {
103                 match self {
104                         PendingOutboundPayment::Abandoned { .. } => true,
105                         _ => false,
106                 }
107         }
108         fn get_pending_fee_msat(&self) -> Option<u64> {
109                 match self {
110                         PendingOutboundPayment::Retryable { pending_fee_msat, .. } => pending_fee_msat.clone(),
111                         _ => None,
112                 }
113         }
114
115         fn payment_hash(&self) -> Option<PaymentHash> {
116                 match self {
117                         PendingOutboundPayment::Legacy { .. } => None,
118                         PendingOutboundPayment::Retryable { payment_hash, .. } => Some(*payment_hash),
119                         PendingOutboundPayment::Fulfilled { payment_hash, .. } => *payment_hash,
120                         PendingOutboundPayment::Abandoned { payment_hash, .. } => Some(*payment_hash),
121                 }
122         }
123
124         fn mark_fulfilled(&mut self) {
125                 let mut session_privs = HashSet::new();
126                 core::mem::swap(&mut session_privs, match self {
127                         PendingOutboundPayment::Legacy { session_privs } |
128                                 PendingOutboundPayment::Retryable { session_privs, .. } |
129                                 PendingOutboundPayment::Fulfilled { session_privs, .. } |
130                                 PendingOutboundPayment::Abandoned { session_privs, .. }
131                         => session_privs,
132                 });
133                 let payment_hash = self.payment_hash();
134                 *self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash, timer_ticks_without_htlcs: 0 };
135         }
136
137         fn mark_abandoned(&mut self) -> Result<(), ()> {
138                 let mut session_privs = HashSet::new();
139                 let our_payment_hash;
140                 core::mem::swap(&mut session_privs, match self {
141                         PendingOutboundPayment::Legacy { .. } |
142                                 PendingOutboundPayment::Fulfilled { .. } =>
143                                 return Err(()),
144                                 PendingOutboundPayment::Retryable { session_privs, payment_hash, .. } |
145                                         PendingOutboundPayment::Abandoned { session_privs, payment_hash, .. } => {
146                                                 our_payment_hash = *payment_hash;
147                                                 session_privs
148                                         },
149                 });
150                 *self = PendingOutboundPayment::Abandoned { session_privs, payment_hash: our_payment_hash };
151                 Ok(())
152         }
153
154         /// panics if path is None and !self.is_fulfilled
155         fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
156                 let remove_res = match self {
157                         PendingOutboundPayment::Legacy { session_privs } |
158                                 PendingOutboundPayment::Retryable { session_privs, .. } |
159                                 PendingOutboundPayment::Fulfilled { session_privs, .. } |
160                                 PendingOutboundPayment::Abandoned { session_privs, .. } => {
161                                         session_privs.remove(session_priv)
162                                 }
163                 };
164                 if remove_res {
165                         if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
166                                 let path = path.expect("Fulfilling a payment should always come with a path");
167                                 let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
168                                 *pending_amt_msat -= path_last_hop.fee_msat;
169                                 if let Some(fee_msat) = pending_fee_msat.as_mut() {
170                                         *fee_msat -= path.get_path_fees();
171                                 }
172                         }
173                 }
174                 remove_res
175         }
176
177         pub(super) fn insert(&mut self, session_priv: [u8; 32], path: &Vec<RouteHop>) -> bool {
178                 let insert_res = match self {
179                         PendingOutboundPayment::Legacy { session_privs } |
180                                 PendingOutboundPayment::Retryable { session_privs, .. } => {
181                                         session_privs.insert(session_priv)
182                                 }
183                         PendingOutboundPayment::Fulfilled { .. } => false,
184                         PendingOutboundPayment::Abandoned { .. } => false,
185                 };
186                 if insert_res {
187                         if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
188                                 let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
189                                 *pending_amt_msat += path_last_hop.fee_msat;
190                                 if let Some(fee_msat) = pending_fee_msat.as_mut() {
191                                         *fee_msat += path.get_path_fees();
192                                 }
193                         }
194                 }
195                 insert_res
196         }
197
198         pub(super) fn remaining_parts(&self) -> usize {
199                 match self {
200                         PendingOutboundPayment::Legacy { session_privs } |
201                                 PendingOutboundPayment::Retryable { session_privs, .. } |
202                                 PendingOutboundPayment::Fulfilled { session_privs, .. } |
203                                 PendingOutboundPayment::Abandoned { session_privs, .. } => {
204                                         session_privs.len()
205                                 }
206                 }
207         }
208 }
209
210 /// Strategies available to retry payment path failures.
211 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
212 pub enum Retry {
213         /// Max number of attempts to retry payment.
214         ///
215         /// Note that this is the number of *path* failures, not full payment retries. For multi-path
216         /// payments, if this is less than the total number of paths, we will never even retry all of the
217         /// payment's paths.
218         Attempts(usize),
219         #[cfg(not(feature = "no-std"))]
220         /// Time elapsed before abandoning retries for a payment.
221         Timeout(core::time::Duration),
222 }
223
224 impl Retry {
225         pub(crate) fn is_retryable_now(&self, attempts: &PaymentAttempts) -> bool {
226                 match (self, attempts) {
227                         (Retry::Attempts(max_retry_count), PaymentAttempts { count, .. }) => {
228                                 max_retry_count > count
229                         },
230                         #[cfg(all(not(feature = "no-std"), not(test)))]
231                         (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
232                                 *max_duration >= std::time::Instant::now().duration_since(*first_attempted_at),
233                         #[cfg(all(not(feature = "no-std"), test))]
234                         (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
235                                 *max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
236                 }
237         }
238 }
239
240 pub(crate) type PaymentAttempts = PaymentAttemptsUsingTime<ConfiguredTime>;
241
242 /// Storing minimal payment attempts information required for determining if a outbound payment can
243 /// be retried.
244 pub(crate) struct PaymentAttemptsUsingTime<T: Time> {
245         /// This count will be incremented only after the result of the attempt is known. When it's 0,
246         /// it means the result of the first attempt is not known yet.
247         pub(crate) count: usize,
248         /// This field is only used when retry is `Retry::Timeout` which is only build with feature std
249         first_attempted_at: T
250 }
251
252 #[cfg(not(any(feature = "no-std", test)))]
253 type ConfiguredTime = std::time::Instant;
254 #[cfg(feature = "no-std")]
255 type ConfiguredTime = crate::util::time::Eternity;
256 #[cfg(all(not(feature = "no-std"), test))]
257 type ConfiguredTime = SinceEpoch;
258
259 impl<T: Time> PaymentAttemptsUsingTime<T> {
260         pub(crate) fn new() -> Self {
261                 PaymentAttemptsUsingTime {
262                         count: 0,
263                         first_attempted_at: T::now()
264                 }
265         }
266 }
267
268 impl<T: Time> Display for PaymentAttemptsUsingTime<T> {
269         fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
270                 #[cfg(feature = "no-std")]
271                 return write!(f, "attempts: {}", self.count);
272                 #[cfg(not(feature = "no-std"))]
273                 return write!(
274                         f,
275                         "attempts: {}, duration: {}s",
276                         self.count,
277                         T::now().duration_since(self.first_attempted_at).as_secs()
278                 );
279         }
280 }
281
282 /// If a payment fails to send, it can be in one of several states. This enum is returned as the
283 /// Err() type describing which state the payment is in, see the description of individual enum
284 /// states for more.
285 #[derive(Clone, Debug)]
286 pub enum PaymentSendFailure {
287         /// A parameter which was passed to send_payment was invalid, preventing us from attempting to
288         /// send the payment at all.
289         ///
290         /// You can freely resend the payment in full (with the parameter error fixed).
291         ///
292         /// Because the payment failed outright, no payment tracking is done, you do not need to call
293         /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
294         /// for this payment.
295         ///
296         /// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
297         /// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
298         ParameterError(APIError),
299         /// A parameter in a single path which was passed to send_payment was invalid, preventing us
300         /// from attempting to send the payment at all.
301         ///
302         /// You can freely resend the payment in full (with the parameter error fixed).
303         ///
304         /// The results here are ordered the same as the paths in the route object which was passed to
305         /// send_payment.
306         ///
307         /// Because the payment failed outright, no payment tracking is done, you do not need to call
308         /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
309         /// for this payment.
310         ///
311         /// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
312         /// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
313         PathParameterError(Vec<Result<(), APIError>>),
314         /// All paths which were attempted failed to send, with no channel state change taking place.
315         /// You can freely resend the payment in full (though you probably want to do so over different
316         /// paths than the ones selected).
317         ///
318         /// Because the payment failed outright, no payment tracking is done, you do not need to call
319         /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
320         /// for this payment.
321         ///
322         /// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
323         /// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
324         AllFailedResendSafe(Vec<APIError>),
325         /// Indicates that a payment for the provided [`PaymentId`] is already in-flight and has not
326         /// yet completed (i.e. generated an [`Event::PaymentSent`]) or been abandoned (via
327         /// [`ChannelManager::abandon_payment`]).
328         ///
329         /// [`PaymentId`]: crate::ln::channelmanager::PaymentId
330         /// [`Event::PaymentSent`]: crate::util::events::Event::PaymentSent
331         /// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
332         DuplicatePayment,
333         /// Some paths which were attempted failed to send, though possibly not all. At least some
334         /// paths have irrevocably committed to the HTLC and retrying the payment in full would result
335         /// in over-/re-payment.
336         ///
337         /// The results here are ordered the same as the paths in the route object which was passed to
338         /// send_payment, and any `Err`s which are not [`APIError::MonitorUpdateInProgress`] can be
339         /// safely retried via [`ChannelManager::retry_payment`].
340         ///
341         /// Any entries which contain `Err(APIError::MonitorUpdateInprogress)` or `Ok(())` MUST NOT be
342         /// retried as they will result in over-/re-payment. These HTLCs all either successfully sent
343         /// (in the case of `Ok(())`) or will send once a [`MonitorEvent::Completed`] is provided for
344         /// the next-hop channel with the latest update_id.
345         ///
346         /// [`ChannelManager::retry_payment`]: crate::ln::channelmanager::ChannelManager::retry_payment
347         /// [`MonitorEvent::Completed`]: crate::chain::channelmonitor::MonitorEvent::Completed
348         PartialFailure {
349                 /// The errors themselves, in the same order as the route hops.
350                 results: Vec<Result<(), APIError>>,
351                 /// If some paths failed without irrevocably committing to the new HTLC(s), this will
352                 /// contain a [`RouteParameters`] object which can be used to calculate a new route that
353                 /// will pay all remaining unpaid balance.
354                 failed_paths_retry: Option<RouteParameters>,
355                 /// The payment id for the payment, which is now at least partially pending.
356                 payment_id: PaymentId,
357         },
358 }
359
360 pub(super) struct OutboundPayments {
361         pub(super) pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
362 }
363
364 impl OutboundPayments {
365         pub(super) fn new() -> Self {
366                 Self {
367                         pending_outbound_payments: Mutex::new(HashMap::new())
368                 }
369         }
370
371         pub(super) fn send_payment_with_route<ES: Deref, NS: Deref, F>(
372                 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
373                 payment_id: PaymentId, entropy_source: &ES, node_signer: &NS, best_block_height: u32,
374                 send_payment_along_path: F
375         ) -> Result<(), PaymentSendFailure>
376         where
377                 ES::Target: EntropySource,
378                 NS::Target: NodeSigner,
379                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
380                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
381         {
382                 let onion_session_privs = self.add_new_pending_payment(payment_hash, *payment_secret, payment_id, route, Retry::Attempts(0), None, entropy_source, best_block_height)?;
383                 self.send_payment_internal(route, payment_hash, payment_secret, None, payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path)
384         }
385
386         pub(super) fn send_spontaneous_payment<ES: Deref, NS: Deref, F>(
387                 &self, route: &Route, payment_preimage: Option<PaymentPreimage>, payment_id: PaymentId,
388                 entropy_source: &ES, node_signer: &NS, best_block_height: u32, send_payment_along_path: F
389         ) -> Result<PaymentHash, PaymentSendFailure>
390         where
391                 ES::Target: EntropySource,
392                 NS::Target: NodeSigner,
393                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
394                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
395         {
396                 let preimage = match payment_preimage {
397                         Some(p) => p,
398                         None => PaymentPreimage(entropy_source.get_secure_random_bytes()),
399                 };
400                 let payment_hash = PaymentHash(Sha256::hash(&preimage.0).into_inner());
401                 let onion_session_privs = self.add_new_pending_payment(payment_hash, None, payment_id, &route, Retry::Attempts(0), None, entropy_source, best_block_height)?;
402
403                 match self.send_payment_internal(route, payment_hash, &None, Some(preimage), payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path) {
404                         Ok(()) => Ok(payment_hash),
405                         Err(e) => Err(e)
406                 }
407         }
408
409         pub(super) fn retry_payment_with_route<ES: Deref, NS: Deref, F>(
410                 &self, route: &Route, payment_id: PaymentId, entropy_source: &ES, node_signer: &NS, best_block_height: u32,
411                 send_payment_along_path: F
412         ) -> Result<(), PaymentSendFailure>
413         where
414                 ES::Target: EntropySource,
415                 NS::Target: NodeSigner,
416                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
417                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
418         {
419                 const RETRY_OVERFLOW_PERCENTAGE: u64 = 10;
420                 for path in route.paths.iter() {
421                         if path.len() == 0 {
422                                 return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
423                                         err: "length-0 path in route".to_string()
424                                 }))
425                         }
426                 }
427
428                 let mut onion_session_privs = Vec::with_capacity(route.paths.len());
429                 for _ in 0..route.paths.len() {
430                         onion_session_privs.push(entropy_source.get_secure_random_bytes());
431                 }
432
433                 let (total_msat, payment_hash, payment_secret) = {
434                         let mut outbounds = self.pending_outbound_payments.lock().unwrap();
435                         match outbounds.get_mut(&payment_id) {
436                                 Some(payment) => {
437                                         let res = match payment {
438                                                 PendingOutboundPayment::Retryable {
439                                                         total_msat, payment_hash, payment_secret, pending_amt_msat, ..
440                                                 } => {
441                                                         let retry_amt_msat: u64 = route.paths.iter().map(|path| path.last().unwrap().fee_msat).sum();
442                                                         if retry_amt_msat + *pending_amt_msat > *total_msat * (100 + RETRY_OVERFLOW_PERCENTAGE) / 100 {
443                                                                 return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
444                                                                         err: format!("retry_amt_msat of {} will put pending_amt_msat (currently: {}) more than 10% over total_payment_amt_msat of {}", retry_amt_msat, pending_amt_msat, total_msat).to_string()
445                                                                 }))
446                                                         }
447                                                         (*total_msat, *payment_hash, *payment_secret)
448                                                 },
449                                                 PendingOutboundPayment::Legacy { .. } => {
450                                                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
451                                                                 err: "Unable to retry payments that were initially sent on LDK versions prior to 0.0.102".to_string()
452                                                         }))
453                                                 },
454                                                 PendingOutboundPayment::Fulfilled { .. } => {
455                                                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
456                                                                 err: "Payment already completed".to_owned()
457                                                         }));
458                                                 },
459                                                 PendingOutboundPayment::Abandoned { .. } => {
460                                                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
461                                                                 err: "Payment already abandoned (with some HTLCs still pending)".to_owned()
462                                                         }));
463                                                 },
464                                         };
465                                         for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
466                                                 assert!(payment.insert(*session_priv_bytes, path));
467                                         }
468                                         res
469                                 },
470                                 None =>
471                                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
472                                                 err: format!("Payment with ID {} not found", log_bytes!(payment_id.0)),
473                                         })),
474                         }
475                 };
476                 self.send_payment_internal(route, payment_hash, &payment_secret, None, payment_id, Some(total_msat), onion_session_privs, node_signer, best_block_height, send_payment_along_path)
477         }
478
479         pub(super) fn send_probe<ES: Deref, NS: Deref, F>(
480                 &self, hops: Vec<RouteHop>, probing_cookie_secret: [u8; 32], entropy_source: &ES,
481                 node_signer: &NS, best_block_height: u32, send_payment_along_path: F
482         ) -> Result<(PaymentHash, PaymentId), PaymentSendFailure>
483         where
484                 ES::Target: EntropySource,
485                 NS::Target: NodeSigner,
486                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
487                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
488         {
489                 let payment_id = PaymentId(entropy_source.get_secure_random_bytes());
490
491                 let payment_hash = probing_cookie_from_id(&payment_id, probing_cookie_secret);
492
493                 if hops.len() < 2 {
494                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
495                                 err: "No need probing a path with less than two hops".to_string()
496                         }))
497                 }
498
499                 let route = Route { paths: vec![hops], payment_params: None };
500                 let onion_session_privs = self.add_new_pending_payment(payment_hash, None, payment_id, &route, Retry::Attempts(0), None, entropy_source, best_block_height)?;
501
502                 match self.send_payment_internal(&route, payment_hash, &None, None, payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path) {
503                         Ok(()) => Ok((payment_hash, payment_id)),
504                         Err(e) => Err(e)
505                 }
506         }
507
508         #[cfg(test)]
509         pub(super) fn test_add_new_pending_payment<ES: Deref>(
510                 &self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId,
511                 route: &Route, retry_strategy: Retry, entropy_source: &ES, best_block_height: u32
512         ) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where ES::Target: EntropySource {
513                 self.add_new_pending_payment(payment_hash, payment_secret, payment_id, route, retry_strategy, None, entropy_source, best_block_height)
514         }
515
516         pub(super) fn add_new_pending_payment<ES: Deref>(
517                 &self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId,
518                 route: &Route, retry_strategy: Retry, route_params: Option<RouteParameters>,
519                 entropy_source: &ES, best_block_height: u32
520         ) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where ES::Target: EntropySource {
521                 let mut onion_session_privs = Vec::with_capacity(route.paths.len());
522                 for _ in 0..route.paths.len() {
523                         onion_session_privs.push(entropy_source.get_secure_random_bytes());
524                 }
525
526                 let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
527                 match pending_outbounds.entry(payment_id) {
528                         hash_map::Entry::Occupied(_) => Err(PaymentSendFailure::DuplicatePayment),
529                         hash_map::Entry::Vacant(entry) => {
530                                 let payment = entry.insert(PendingOutboundPayment::Retryable {
531                                         retry_strategy,
532                                         attempts: PaymentAttempts::new(),
533                                         route_params,
534                                         session_privs: HashSet::new(),
535                                         pending_amt_msat: 0,
536                                         pending_fee_msat: Some(0),
537                                         payment_hash,
538                                         payment_secret,
539                                         starting_block_height: best_block_height,
540                                         total_msat: route.get_total_amount(),
541                                 });
542
543                                 for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
544                                         assert!(payment.insert(*session_priv_bytes, path));
545                                 }
546
547                                 Ok(onion_session_privs)
548                         },
549                 }
550         }
551
552         fn send_payment_internal<NS: Deref, F>(
553                 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
554                 keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>,
555                 onion_session_privs: Vec<[u8; 32]>, node_signer: &NS, best_block_height: u32,
556                 send_payment_along_path: F
557         ) -> Result<(), PaymentSendFailure>
558         where
559                 NS::Target: NodeSigner,
560                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
561                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
562         {
563                 if route.paths.len() < 1 {
564                         return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over"}));
565                 }
566                 if payment_secret.is_none() && route.paths.len() > 1 {
567                         return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_string()}));
568                 }
569                 let mut total_value = 0;
570                 let our_node_id = node_signer.get_node_id(Recipient::Node).unwrap(); // TODO no unwrap
571                 let mut path_errs = Vec::with_capacity(route.paths.len());
572                 'path_check: for path in route.paths.iter() {
573                         if path.len() < 1 || path.len() > 20 {
574                                 path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size"}));
575                                 continue 'path_check;
576                         }
577                         for (idx, hop) in path.iter().enumerate() {
578                                 if idx != path.len() - 1 && hop.pubkey == our_node_id {
579                                         path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us"}));
580                                         continue 'path_check;
581                                 }
582                         }
583                         total_value += path.last().unwrap().fee_msat;
584                         path_errs.push(Ok(()));
585                 }
586                 if path_errs.iter().any(|e| e.is_err()) {
587                         return Err(PaymentSendFailure::PathParameterError(path_errs));
588                 }
589                 if let Some(amt_msat) = recv_value_msat {
590                         debug_assert!(amt_msat >= total_value);
591                         total_value = amt_msat;
592                 }
593
594                 let cur_height = best_block_height + 1;
595                 let mut results = Vec::new();
596                 debug_assert_eq!(route.paths.len(), onion_session_privs.len());
597                 for (path, session_priv) in route.paths.iter().zip(onion_session_privs.into_iter()) {
598                         let mut path_res = send_payment_along_path(&path, &route.payment_params, &payment_hash, payment_secret, total_value, cur_height, payment_id, &keysend_preimage, session_priv);
599                         match path_res {
600                                 Ok(_) => {},
601                                 Err(APIError::MonitorUpdateInProgress) => {
602                                         // While a MonitorUpdateInProgress is an Err(_), the payment is still
603                                         // considered "in flight" and we shouldn't remove it from the
604                                         // PendingOutboundPayment set.
605                                 },
606                                 Err(_) => {
607                                         let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
608                                         if let Some(payment) = pending_outbounds.get_mut(&payment_id) {
609                                                 let removed = payment.remove(&session_priv, Some(path));
610                                                 debug_assert!(removed, "This can't happen as the payment has an entry for this path added by callers");
611                                         } else {
612                                                 debug_assert!(false, "This can't happen as the payment was added by callers");
613                                                 path_res = Err(APIError::APIMisuseError { err: "Internal error: payment disappeared during processing. Please report this bug!".to_owned() });
614                                         }
615                                 }
616                         }
617                         results.push(path_res);
618                 }
619                 let mut has_ok = false;
620                 let mut has_err = false;
621                 let mut pending_amt_unsent = 0;
622                 let mut max_unsent_cltv_delta = 0;
623                 for (res, path) in results.iter().zip(route.paths.iter()) {
624                         if res.is_ok() { has_ok = true; }
625                         if res.is_err() { has_err = true; }
626                         if let &Err(APIError::MonitorUpdateInProgress) = res {
627                                 // MonitorUpdateInProgress is inherently unsafe to retry, so we call it a
628                                 // PartialFailure.
629                                 has_err = true;
630                                 has_ok = true;
631                         } else if res.is_err() {
632                                 pending_amt_unsent += path.last().unwrap().fee_msat;
633                                 max_unsent_cltv_delta = cmp::max(max_unsent_cltv_delta, path.last().unwrap().cltv_expiry_delta);
634                         }
635                 }
636                 if has_err && has_ok {
637                         Err(PaymentSendFailure::PartialFailure {
638                                 results,
639                                 payment_id,
640                                 failed_paths_retry: if pending_amt_unsent != 0 {
641                                         if let Some(payment_params) = &route.payment_params {
642                                                 Some(RouteParameters {
643                                                         payment_params: payment_params.clone(),
644                                                         final_value_msat: pending_amt_unsent,
645                                                         final_cltv_expiry_delta: max_unsent_cltv_delta,
646                                                 })
647                                         } else { None }
648                                 } else { None },
649                         })
650                 } else if has_err {
651                         // If we failed to send any paths, we should remove the new PaymentId from the
652                         // `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
653                         let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
654                         debug_assert!(removed, "We should always have a pending payment to remove here");
655                         Err(PaymentSendFailure::AllFailedResendSafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
656                 } else {
657                         Ok(())
658                 }
659         }
660
661         #[cfg(test)]
662         pub(super) fn test_send_payment_internal<NS: Deref, F>(
663                 &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>,
664                 keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>,
665                 onion_session_privs: Vec<[u8; 32]>, node_signer: &NS, best_block_height: u32,
666                 send_payment_along_path: F
667         ) -> Result<(), PaymentSendFailure>
668         where
669                 NS::Target: NodeSigner,
670                 F: Fn(&Vec<RouteHop>, &Option<PaymentParameters>, &PaymentHash, &Option<PaymentSecret>, u64,
671                    u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
672         {
673                 self.send_payment_internal(route, payment_hash, payment_secret, keysend_preimage, payment_id,
674                         recv_value_msat, onion_session_privs, node_signer, best_block_height,
675                         send_payment_along_path)
676         }
677
678         pub(super) fn claim_htlc<L: Deref>(
679                 &self, payment_id: PaymentId, payment_preimage: PaymentPreimage, session_priv: SecretKey,
680                 path: Vec<RouteHop>, from_onchain: bool, pending_events: &Mutex<Vec<events::Event>>, logger: &L
681         ) where L::Target: Logger {
682                 let mut session_priv_bytes = [0; 32];
683                 session_priv_bytes.copy_from_slice(&session_priv[..]);
684                 let mut outbounds = self.pending_outbound_payments.lock().unwrap();
685                 let mut pending_events = pending_events.lock().unwrap();
686                 if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
687                         if !payment.get().is_fulfilled() {
688                                 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
689                                 let fee_paid_msat = payment.get().get_pending_fee_msat();
690                                 pending_events.push(
691                                         events::Event::PaymentSent {
692                                                 payment_id: Some(payment_id),
693                                                 payment_preimage,
694                                                 payment_hash,
695                                                 fee_paid_msat,
696                                         }
697                                 );
698                                 payment.get_mut().mark_fulfilled();
699                         }
700
701                         if from_onchain {
702                                 // We currently immediately remove HTLCs which were fulfilled on-chain.
703                                 // This could potentially lead to removing a pending payment too early,
704                                 // with a reorg of one block causing us to re-add the fulfilled payment on
705                                 // restart.
706                                 // TODO: We should have a second monitor event that informs us of payments
707                                 // irrevocably fulfilled.
708                                 if payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
709                                         let payment_hash = Some(PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()));
710                                         pending_events.push(
711                                                 events::Event::PaymentPathSuccessful {
712                                                         payment_id,
713                                                         payment_hash,
714                                                         path,
715                                                 }
716                                         );
717                                 }
718                         }
719                 } else {
720                         log_trace!(logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0));
721                 }
722         }
723
724         pub(super) fn finalize_claims(&self, sources: Vec<HTLCSource>, pending_events: &Mutex<Vec<events::Event>>) {
725                 let mut outbounds = self.pending_outbound_payments.lock().unwrap();
726                 let mut pending_events = pending_events.lock().unwrap();
727                 for source in sources {
728                         if let HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } = source {
729                                 let mut session_priv_bytes = [0; 32];
730                                 session_priv_bytes.copy_from_slice(&session_priv[..]);
731                                 if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
732                                         assert!(payment.get().is_fulfilled());
733                                         if payment.get_mut().remove(&session_priv_bytes, None) {
734                                                 pending_events.push(
735                                                         events::Event::PaymentPathSuccessful {
736                                                                 payment_id,
737                                                                 payment_hash: payment.get().payment_hash(),
738                                                                 path,
739                                                         }
740                                                 );
741                                         }
742                                 }
743                         }
744                 }
745         }
746
747         pub(super) fn remove_stale_resolved_payments(&self, pending_events: &Mutex<Vec<events::Event>>) {
748                 // If an outbound payment was completed, and no pending HTLCs remain, we should remove it
749                 // from the map. However, if we did that immediately when the last payment HTLC is claimed,
750                 // this could race the user making a duplicate send_payment call and our idempotency
751                 // guarantees would be violated. Instead, we wait a few timer ticks to do the actual
752                 // removal. This should be more than sufficient to ensure the idempotency of any
753                 // `send_payment` calls that were made at the same time the `PaymentSent` event was being
754                 // processed.
755                 let mut pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
756                 let pending_events = pending_events.lock().unwrap();
757                 pending_outbound_payments.retain(|payment_id, payment| {
758                         if let PendingOutboundPayment::Fulfilled { session_privs, timer_ticks_without_htlcs, .. } = payment {
759                                 let mut no_remaining_entries = session_privs.is_empty();
760                                 if no_remaining_entries {
761                                         for ev in pending_events.iter() {
762                                                 match ev {
763                                                         events::Event::PaymentSent { payment_id: Some(ev_payment_id), .. } |
764                                                                 events::Event::PaymentPathSuccessful { payment_id: ev_payment_id, .. } |
765                                                                 events::Event::PaymentPathFailed { payment_id: Some(ev_payment_id), .. } => {
766                                                                         if payment_id == ev_payment_id {
767                                                                                 no_remaining_entries = false;
768                                                                                 break;
769                                                                         }
770                                                                 },
771                                                         _ => {},
772                                                 }
773                                         }
774                                 }
775                                 if no_remaining_entries {
776                                         *timer_ticks_without_htlcs += 1;
777                                         *timer_ticks_without_htlcs <= IDEMPOTENCY_TIMEOUT_TICKS
778                                 } else {
779                                         *timer_ticks_without_htlcs = 0;
780                                         true
781                                 }
782                         } else { true }
783                 });
784         }
785
786         pub(super) fn fail_htlc<L: Deref>(
787                 &self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
788                 path: &Vec<RouteHop>, session_priv: &SecretKey, payment_id: &PaymentId,
789                 payment_params: &Option<PaymentParameters>, probing_cookie_secret: [u8; 32],
790                 secp_ctx: &Secp256k1<secp256k1::All>, pending_events: &Mutex<Vec<events::Event>>, logger: &L
791         ) where L::Target: Logger {
792                 #[cfg(test)]
793                 let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
794                 #[cfg(not(test))]
795                 let (network_update, short_channel_id, payment_retryable, _, _) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
796
797                 let mut session_priv_bytes = [0; 32];
798                 session_priv_bytes.copy_from_slice(&session_priv[..]);
799                 let mut outbounds = self.pending_outbound_payments.lock().unwrap();
800                 let mut all_paths_failed = false;
801                 let mut full_failure_ev = None;
802                 let mut pending_retry_ev = None;
803                 let attempts_remaining = if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(*payment_id) {
804                         if !payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
805                                 log_trace!(logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
806                                 return
807                         }
808                         if payment.get().is_fulfilled() {
809                                 log_trace!(logger, "Received failure of HTLC with payment_hash {} after payment completion", log_bytes!(payment_hash.0));
810                                 return
811                         }
812                         let is_retryable_now = payment.get().is_retryable_now();
813                         if let Some(scid) = short_channel_id {
814                                 payment.get_mut().insert_previously_failed_scid(scid);
815                         }
816                         if payment.get().remaining_parts() == 0 {
817                                 all_paths_failed = true;
818                                 if payment.get().abandoned() {
819                                         full_failure_ev = Some(events::Event::PaymentFailed {
820                                                 payment_id: *payment_id,
821                                                 payment_hash: payment.get().payment_hash().expect("PendingOutboundPayments::RetriesExceeded always has a payment hash set"),
822                                         });
823                                         payment.remove();
824                                 }
825                         }
826                         is_retryable_now
827                 } else {
828                         log_trace!(logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
829                         return
830                 };
831                 core::mem::drop(outbounds);
832                 let mut retry = if let Some(payment_params_data) = payment_params {
833                         let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
834                         Some(RouteParameters {
835                                 payment_params: payment_params_data.clone(),
836                                 final_value_msat: path_last_hop.fee_msat,
837                                 final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
838                         })
839                 } else { None };
840                 log_trace!(logger, "Failing outbound payment HTLC with payment_hash {}", log_bytes!(payment_hash.0));
841
842                 let path_failure = {
843                         if payment_is_probe(payment_hash, &payment_id, probing_cookie_secret) {
844                                 if !payment_retryable {
845                                         events::Event::ProbeSuccessful {
846                                                 payment_id: *payment_id,
847                                                 payment_hash: payment_hash.clone(),
848                                                 path: path.clone(),
849                                         }
850                                 } else {
851                                         events::Event::ProbeFailed {
852                                                 payment_id: *payment_id,
853                                                 payment_hash: payment_hash.clone(),
854                                                 path: path.clone(),
855                                                 short_channel_id,
856                                         }
857                                 }
858                         } else {
859                                 // TODO: If we decided to blame ourselves (or one of our channels) in
860                                 // process_onion_failure we should close that channel as it implies our
861                                 // next-hop is needlessly blaming us!
862                                 if let Some(scid) = short_channel_id {
863                                         retry.as_mut().map(|r| r.payment_params.previously_failed_channels.push(scid));
864                                 }
865                                 if payment_retryable && attempts_remaining && retry.is_some() {
866                                         debug_assert!(full_failure_ev.is_none());
867                                         pending_retry_ev = Some(events::Event::PendingHTLCsForwardable {
868                                                 time_forwardable: Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS),
869                                         });
870                                 }
871                                 events::Event::PaymentPathFailed {
872                                         payment_id: Some(*payment_id),
873                                         payment_hash: payment_hash.clone(),
874                                         payment_failed_permanently: !payment_retryable,
875                                         network_update,
876                                         all_paths_failed,
877                                         path: path.clone(),
878                                         short_channel_id,
879                                         retry,
880                                         #[cfg(test)]
881                                         error_code: onion_error_code,
882                                         #[cfg(test)]
883                                         error_data: onion_error_data
884                                 }
885                         }
886                 };
887                 let mut pending_events = pending_events.lock().unwrap();
888                 pending_events.push(path_failure);
889                 if let Some(ev) = full_failure_ev { pending_events.push(ev); }
890                 if let Some(ev) = pending_retry_ev { pending_events.push(ev); }
891         }
892
893         pub(super) fn abandon_payment(&self, payment_id: PaymentId) -> Option<events::Event> {
894                 let mut failed_ev = None;
895                 let mut outbounds = self.pending_outbound_payments.lock().unwrap();
896                 if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
897                         if let Ok(()) = payment.get_mut().mark_abandoned() {
898                                 if payment.get().remaining_parts() == 0 {
899                                         failed_ev = Some(events::Event::PaymentFailed {
900                                                 payment_id,
901                                                 payment_hash: payment.get().payment_hash().expect("PendingOutboundPayments::RetriesExceeded always has a payment hash set"),
902                                         });
903                                         payment.remove();
904                                 }
905                         }
906                 }
907                 failed_ev
908         }
909
910         #[cfg(test)]
911         pub fn has_pending_payments(&self) -> bool {
912                 !self.pending_outbound_payments.lock().unwrap().is_empty()
913         }
914
915         #[cfg(test)]
916         pub fn clear_pending_payments(&self) {
917                 self.pending_outbound_payments.lock().unwrap().clear()
918         }
919 }
920
921 /// Returns whether a payment with the given [`PaymentHash`] and [`PaymentId`] is, in fact, a
922 /// payment probe.
923 pub(super) fn payment_is_probe(payment_hash: &PaymentHash, payment_id: &PaymentId,
924         probing_cookie_secret: [u8; 32]) -> bool
925 {
926         let target_payment_hash = probing_cookie_from_id(payment_id, probing_cookie_secret);
927         target_payment_hash == *payment_hash
928 }
929
930 /// Returns the 'probing cookie' for the given [`PaymentId`].
931 fn probing_cookie_from_id(payment_id: &PaymentId, probing_cookie_secret: [u8; 32]) -> PaymentHash {
932         let mut preimage = [0u8; 64];
933         preimage[..32].copy_from_slice(&probing_cookie_secret);
934         preimage[32..].copy_from_slice(&payment_id.0);
935         PaymentHash(Sha256::hash(&preimage).into_inner())
936 }
937
938 impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
939         (0, Legacy) => {
940                 (0, session_privs, required),
941         },
942         (1, Fulfilled) => {
943                 (0, session_privs, required),
944                 (1, payment_hash, option),
945                 (3, timer_ticks_without_htlcs, (default_value, 0)),
946         },
947         (2, Retryable) => {
948                 (0, session_privs, required),
949                 (1, pending_fee_msat, option),
950                 (2, payment_hash, required),
951                 (not_written, retry_strategy, (static_value, Retry::Attempts(0))),
952                 (4, payment_secret, option),
953                 (not_written, attempts, (static_value, PaymentAttempts::new())),
954                 (6, total_msat, required),
955                 (not_written, route_params, (static_value, None)),
956                 (8, pending_amt_msat, required),
957                 (10, starting_block_height, required),
958         },
959         (3, Abandoned) => {
960                 (0, session_privs, required),
961                 (2, payment_hash, required),
962         },
963 );