Skip to content

Commit a452ab8

Browse files
committed
Extract hold time calculation
Preparation for reuse of the logic.
1 parent d4a26dc commit a452ab8

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7526,12 +7526,9 @@ where
75267526
);
75277527
// We really want take() here, but, again, non-mut ref :(
75287528
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7529-
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7530-
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7531-
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7532-
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7529+
get_hold_time(htlc.send_timestamp, now).map(|hold_time| {
75337530
reason.set_hold_time(hold_time);
7534-
}
7531+
});
75357532

75367533
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
75377534
} else {
@@ -13580,6 +13577,15 @@ fn duration_since_epoch() -> Option<Duration> {
1358013577
now
1358113578
}
1358213579

13580+
fn get_hold_time(send_timestamp: Option<Duration>, now: Option<Duration>) -> Option<u32> {
13581+
send_timestamp.and_then(|t| {
13582+
now.map(|now| {
13583+
let elapsed = now.saturating_sub(t).as_millis() / HOLD_TIME_UNIT_MILLIS;
13584+
u32::try_from(elapsed).unwrap_or(u32::MAX)
13585+
})
13586+
})
13587+
}
13588+
1358313589
#[cfg(test)]
1358413590
mod tests {
1358513591
use crate::chain::chaininterface::LowerBoundedFeeEstimator;

0 commit comments

Comments
 (0)