Skip to content

Commit

Permalink
Avoid panicking on wallclock time going backwards across restart
Browse files Browse the repository at this point in the history
Because we serialize `Instant`s using wallclock time in
`ProbabilisticScorer`, if time goes backwards across restarts we
may end up with `Instant`s in the future, which causes rustc prior
to 1.60 to panic when calculating durations. Here we simply avoid
this by setting the time to `now` if we get a time in the future.
  • Loading branch information
TheBlueMatt committed Jul 8, 2022
1 parent f3d5b94 commit 67c9e54
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lightning/src/routing/scoring.rs
Expand Up @@ -1177,10 +1177,20 @@ 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.
// Because we calculate "now" against wallclock time when we were written are reload it
// here, we may cause time to go backwards if wallclock time is before when we were
// written. Thus, we check if we'd end up with a time in the future and juse use `now`
// instead to avoid panicing later.
let wall_clock_now = T::duration_since_epoch();
let now = T::now();
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

0 comments on commit 67c9e54

Please sign in to comment.