Skip to content

Commit 497fd65

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 497fd65

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lightning/src/routing/scoring.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,10 +1177,22 @@ 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+
// We write `last_updated` as wallclock time even though its ultimately an `Instant` (which
1182+
// is a time from a monotonic clock usually represented as an offset against boot time).
1183+
// Thus, we have to construct an `Instant` by subtracting the difference in wallclock time
1184+
// from the one that was written. However, because `Instant` can panic if we construct one
1185+
// in the future, we must handle wallclock time jumping backwards, which we do by simply
1186+
// using `Instant::now()` in that case.
1187+
let wall_clock_now = T::duration_since_epoch();
1188+
let now = T::now();
1189+
let last_updated = if wall_clock_now > duration_since_epoch {
1190+
now - (wall_clock_now - duration_since_epoch)
1191+
} else { now };
11801192
Ok(Self {
11811193
min_liquidity_offset_msat,
11821194
max_liquidity_offset_msat,
1183-
last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
1195+
last_updated,
11841196
})
11851197
}
11861198
}

0 commit comments

Comments
 (0)