Skip to content

Commit 5eff10d

Browse files
committed
Extract hold time calculation
Preparation for reuse of the logic.
1 parent 8633d46 commit 5eff10d

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
@@ -7496,12 +7496,9 @@ where
74967496
);
74977497
// We really want take() here, but, again, non-mut ref :(
74987498
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7499-
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7500-
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7501-
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7502-
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7499+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
75037500
reason.set_hold_time(hold_time);
7504-
}
7501+
});
75057502

75067503
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
75077504
} else {
@@ -13565,6 +13562,17 @@ fn duration_since_epoch() -> Option<Duration> {
1356513562
now
1356613563
}
1356713564

13565+
/// Returns the time expressed in hold time units (1 unit = 100 ms) that has elapsed between send_timestamp and now. If
13566+
/// any of the arguments are `None`, returns `None`.
13567+
fn hold_time(send_timestamp: Option<Duration>, now: Option<Duration>) -> Option<u32> {
13568+
send_timestamp.and_then(|t| {
13569+
now.map(|now| {
13570+
let elapsed = now.saturating_sub(t).as_millis() / HOLD_TIME_UNIT_MILLIS;
13571+
u32::try_from(elapsed).unwrap_or(u32::MAX)
13572+
})
13573+
})
13574+
}
13575+
1356813576
#[cfg(test)]
1356913577
mod tests {
1357013578
use crate::chain::chaininterface::LowerBoundedFeeEstimator;

0 commit comments

Comments
 (0)