Skip to content

Commit 5eecba3

Browse files
committed
Extract hold time calculation
Preparation for reuse of the logic.
1 parent 47ce774 commit 5eecba3

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
@@ -7478,12 +7478,9 @@ where
74787478
);
74797479
// We really want take() here, but, again, non-mut ref :(
74807480
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7481-
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7482-
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7483-
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7484-
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7481+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
74857482
reason.set_hold_time(hold_time);
7486-
}
7483+
});
74877484

74887485
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
74897486
} else {
@@ -13540,6 +13537,17 @@ fn duration_since_epoch() -> Option<Duration> {
1354013537
now
1354113538
}
1354213539

13540+
/// Returns the time expressed in hold time units (1 unit = 100 ms) that has elapsed between send_timestamp and now. If
13541+
/// any of the arguments are `None`, returns `None`.
13542+
fn hold_time(send_timestamp: Option<Duration>, now: Option<Duration>) -> Option<u32> {
13543+
send_timestamp.and_then(|t| {
13544+
now.map(|now| {
13545+
let elapsed = now.saturating_sub(t).as_millis() / HOLD_TIME_UNIT_MILLIS;
13546+
u32::try_from(elapsed).unwrap_or(u32::MAX)
13547+
})
13548+
})
13549+
}
13550+
1354313551
#[cfg(test)]
1354413552
mod tests {
1354513553
use crate::chain::chaininterface::LowerBoundedFeeEstimator;

0 commit comments

Comments
 (0)