Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panicking on wallclock time going backwards across restart #1603

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lightning/src/routing/scoring.rs
Expand Up @@ -1177,10 +1177,22 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
(2, max_liquidity_offset_msat, required),
(4, duration_since_epoch, required),
});
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
// We write `last_updated` as wallclock time even though its ultimately an `Instant` (which
// is a time from a monotonic clock usually represented as an offset against boot time).
// Thus, we have to construct an `Instant` by subtracting the difference in wallclock time
// from the one that was written. However, because `Instant` can panic if we construct one
// in the future, we must handle wallclock time jumping backwards, which we do by simply
// using `Instant::now()` in that case.
let wall_clock_now = T::duration_since_epoch();
let now = T::now();
let last_updated = if wall_clock_now > duration_since_epoch {
now - (wall_clock_now - duration_since_epoch)
} else { now };
Ok(Self {
min_liquidity_offset_msat,
max_liquidity_offset_msat,
last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
last_updated,
})
}
}
Expand Down