Skip to content

Commit 5ceeb27

Browse files
committed
Avoid panicking on wallclock time going backwards across restart
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.
1 parent f3d5b94 commit 5ceeb27

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lightning/src/routing/scoring.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,10 +1177,20 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
11771177
(2, max_liquidity_offset_msat, required),
11781178
(4, duration_since_epoch, required),
11791179
});
1180+
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
1181+
// Because we calculate "now" against wallclock time when we were written are reloading
1182+
// here, we may cause time to go backwards if wallclock time is before when we were
1183+
// written. Thus, we check if we'd end up with a time in the future and just use `now`
1184+
// instead to avoid panicing later.
1185+
let wall_clock_now = T::duration_since_epoch();
1186+
let now = T::now();
1187+
let last_updated = if wall_clock_now > duration_since_epoch {
1188+
now - (wall_clock_now - duration_since_epoch)
1189+
} else { now };
11801190
Ok(Self {
11811191
min_liquidity_offset_msat,
11821192
max_liquidity_offset_msat,
1183-
last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
1193+
last_updated,
11841194
})
11851195
}
11861196
}

0 commit comments

Comments
 (0)