Copy Retry from invoice module to outbound_payment module
authorValentine Wallace <vwallace@protonmail.com>
Sun, 18 Dec 2022 22:02:17 +0000 (17:02 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Tue, 24 Jan 2023 19:11:59 +0000 (14:11 -0500)
Also configure it such that in std tests, it will use SinceEpoch instead of
Instant so time can be manually advanced.

lightning/src/ln/outbound_payment.rs

index a4def55cfeb5edb8dd19390334a0caaa7a1e9258..a9c1e393d2b34d62cc043fdf399aaa3e93d8c948 100644 (file)
@@ -187,6 +187,36 @@ impl PendingOutboundPayment {
        }
 }
 
+/// Strategies available to retry payment path failures.
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
+pub enum Retry {
+       /// Max number of attempts to retry payment.
+       ///
+       /// Note that this is the number of *path* failures, not full payment retries. For multi-path
+       /// payments, if this is less than the total number of paths, we will never even retry all of the
+       /// payment's paths.
+       Attempts(usize),
+       #[cfg(not(feature = "no-std"))]
+       /// Time elapsed before abandoning retries for a payment.
+       Timeout(core::time::Duration),
+}
+
+impl Retry {
+       pub(crate) fn is_retryable_now(&self, attempts: &PaymentAttempts) -> bool {
+               match (self, attempts) {
+                       (Retry::Attempts(max_retry_count), PaymentAttempts { count, .. }) => {
+                               max_retry_count > count
+                       },
+                       #[cfg(all(not(feature = "no-std"), not(test)))]
+                       (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
+                               *max_duration >= std::time::Instant::now().duration_since(*first_attempted_at),
+                       #[cfg(all(not(feature = "no-std"), test))]
+                       (Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
+                               *max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
+               }
+       }
+}
+
 pub(crate) type PaymentAttempts = PaymentAttemptsUsingTime<ConfiguredTime>;
 
 /// Storing minimal payment attempts information required for determining if a outbound payment can