Skip to content

Commit f36dcfb

Browse files
committed
Extract hold time calculation
Preparation for reuse of the logic.
1 parent 36904df commit f36dcfb

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7523,12 +7523,9 @@ where
75237523
);
75247524
// We really want take() here, but, again, non-mut ref :(
75257525
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7526-
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7527-
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7528-
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7529-
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7526+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
75307527
reason.set_hold_time(hold_time);
7531-
}
7528+
});
75327529

75337530
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
75347531
} else {
@@ -13601,6 +13598,17 @@ fn duration_since_epoch() -> Option<Duration> {
1360113598
now
1360213599
}
1360313600

13601+
/// Returns the time expressed in hold time units (1 unit = 100 ms) that has elapsed between send_timestamp and now. If
13602+
/// any of the arguments are `None`, returns `None`.
13603+
fn hold_time(send_timestamp: Option<Duration>, now: Option<Duration>) -> Option<u32> {
13604+
send_timestamp.and_then(|t| {
13605+
now.map(|now| {
13606+
let elapsed = now.saturating_sub(t).as_millis() / HOLD_TIME_UNIT_MILLIS;
13607+
u32::try_from(elapsed).unwrap_or(u32::MAX)
13608+
})
13609+
})
13610+
}
13611+
1360413612
#[cfg(test)]
1360513613
mod tests {
1360613614
use crate::chain::chaininterface::LowerBoundedFeeEstimator;

0 commit comments

Comments
 (0)