Skip to content

Commit a7384af

Browse files
committed
change count to retry_count being stored in PaymentAttempts
1 parent 5b3feb7 commit a7384af

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lightning-invoice/src/payment.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,15 @@ where
194194
/// be retried.
195195
#[derive(Clone, Copy)]
196196
struct PaymentAttempts<T: Time> {
197-
/// This should be >= 1 as we only insert PaymentAttempts for a PaymentHash if there is at least
198-
/// one attempt.
199-
count: usize,
197+
retry_count: usize,
200198
/// This field is only used when retry is [`Retry::Timeout`] which is only build with feature std
201199
first_attempted_at: T
202200
}
203201

204202
impl<T: Time> PaymentAttempts<T> {
205203
fn new() -> Self {
206204
PaymentAttempts {
207-
count: 1,
205+
retry_count: 0,
208206
first_attempted_at: T::now()
209207
}
210208
}
@@ -216,13 +214,13 @@ impl<T: Time> Debug for PaymentAttempts<T> {
216214
return write!(
217215
f,
218216
"attempts: {}",
219-
self.count
217+
self.retry_count + 1
220218
);
221219
#[cfg(not(feature = "no-std"))]
222220
return write!(
223221
f,
224222
"attempts: {}, duration: {:?}",
225-
self.count,
223+
self.retry_count + 1,
226224
T::now().duration_since(self.first_attempted_at)
227225
);
228226
}
@@ -280,8 +278,7 @@ pub enum Retry {
280278
impl Retry {
281279
fn is_retryable_now<T: Time>(&self, attempts: &PaymentAttempts<T>) -> bool {
282280
match self {
283-
Retry::Attempts(0) => false,
284-
Retry::Attempts(max_retry_count) => max_retry_count + 1 > attempts.count,
281+
Retry::Attempts(max_retry_count) => max_retry_count > &attempts.retry_count,
285282
#[cfg(feature = "std")]
286283
Retry::Timeout(max_duration) => *max_duration >= T::now().duration_since(attempts.first_attempted_at)
287284
}
@@ -438,7 +435,7 @@ where
438435
let mut payment_cache = self.payment_cache.lock().unwrap();
439436
let payment_attempts = payment_cache.get_mut(&payment_hash).unwrap();
440437
if self.retry.is_retryable_now(payment_attempts) {
441-
payment_attempts.count += 1;
438+
payment_attempts.retry_count += 1;
442439
core::mem::drop(payment_cache);
443440
Ok(self.pay_internal(params, payment_hash, send_payment)?)
444441
} else {
@@ -471,12 +468,17 @@ where
471468
let attempts = *self.payment_cache.lock().unwrap()
472469
.entry(payment_hash)
473470
.and_modify(|attempts| {
474-
attempts.count += 1;
471+
attempts.retry_count += 1;
475472
})
476473
.or_insert(PaymentAttempts::new());
477474

475+
let prevoius_retry_count = match attempts.retry_count {
476+
0 => 0,
477+
retry_count => retry_count - 1
478+
};
479+
478480
if ! self.retry.is_retryable_now(&PaymentAttempts{
479-
count: attempts.count -1,
481+
retry_count: prevoius_retry_count,
480482
..attempts
481483
}) {
482484
log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying ({:?})", log_bytes!(payment_hash.0), attempts);
@@ -584,7 +586,7 @@ where
584586
let mut payment_cache = self.payment_cache.lock().unwrap();
585587
let attempts = payment_cache
586588
.remove(payment_hash)
587-
.map_or(1, |attempts| attempts.count + 1);
589+
.map_or(1, |attempts| attempts.retry_count + 1);
588590
log_trace!(self.logger, "Payment {} succeeded (attempts: {})", log_bytes!(payment_hash.0), attempts);
589591
},
590592
_ => {},

0 commit comments

Comments
 (0)