Skip to content

Commit 948ebc6

Browse files
Copy PaymentAttempts from invoice module to outbound_payment module
1 parent 153b048 commit 948ebc6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ use crate::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters
2222
use crate::util::errors::APIError;
2323
use crate::util::events;
2424
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;
2528

2629
use core::cmp;
30+
use core::fmt::{self, Display, Formatter};
2731
use core::ops::Deref;
32+
2833
use crate::prelude::*;
2934
use crate::sync::Mutex;
3035

@@ -182,6 +187,48 @@ impl PendingOutboundPayment {
182187
}
183188
}
184189

190+
pub(crate) type PaymentAttempts = PaymentAttemptsUsingTime<ConfiguredTime>;
191+
192+
/// Storing minimal payment attempts information required for determining if a outbound payment can
193+
/// be retried.
194+
pub(crate) struct PaymentAttemptsUsingTime<T: Time> {
195+
/// This count will be incremented only after the result of the attempt is known. When it's 0,
196+
/// it means the result of the first attempt is not known yet.
197+
pub(crate) count: usize,
198+
/// This field is only used when retry is `Retry::Timeout` which is only build with feature std
199+
first_attempted_at: T
200+
}
201+
202+
#[cfg(not(any(feature = "no-std", test)))]
203+
type ConfiguredTime = std::time::Instant;
204+
#[cfg(feature = "no-std")]
205+
type ConfiguredTime = crate::util::time::Eternity;
206+
#[cfg(all(not(feature = "no-std"), test))]
207+
type ConfiguredTime = SinceEpoch;
208+
209+
impl<T: Time> PaymentAttemptsUsingTime<T> {
210+
pub(crate) fn new() -> Self {
211+
PaymentAttemptsUsingTime {
212+
count: 0,
213+
first_attempted_at: T::now()
214+
}
215+
}
216+
}
217+
218+
impl<T: Time> Display for PaymentAttemptsUsingTime<T> {
219+
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
220+
#[cfg(feature = "no-std")]
221+
return write!(f, "attempts: {}", self.count);
222+
#[cfg(not(feature = "no-std"))]
223+
return write!(
224+
f,
225+
"attempts: {}, duration: {}s",
226+
self.count,
227+
T::now().duration_since(self.first_attempted_at).as_secs()
228+
);
229+
}
230+
}
231+
185232
/// If a payment fails to send, it can be in one of several states. This enum is returned as the
186233
/// Err() type describing which state the payment is in, see the description of individual enum
187234
/// states for more.

0 commit comments

Comments
 (0)