Skip to content

Commit 5c06d1d

Browse files
authored
Merge pull request #1603 from TheBlueMatt/2022-07-no-backwards-time
Avoid panicking on wallclock time going backwards across restart
2 parents 29e34c8 + 497fd65 commit 5c06d1d

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
@@ -1215,10 +1215,22 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
12151215
(2, max_liquidity_offset_msat, required),
12161216
(4, duration_since_epoch, required),
12171217
});
1218+
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
1219+
// We write `last_updated` as wallclock time even though its ultimately an `Instant` (which
1220+
// is a time from a monotonic clock usually represented as an offset against boot time).
1221+
// Thus, we have to construct an `Instant` by subtracting the difference in wallclock time
1222+
// from the one that was written. However, because `Instant` can panic if we construct one
1223+
// in the future, we must handle wallclock time jumping backwards, which we do by simply
1224+
// using `Instant::now()` in that case.
1225+
let wall_clock_now = T::duration_since_epoch();
1226+
let now = T::now();
1227+
let last_updated = if wall_clock_now > duration_since_epoch {
1228+
now - (wall_clock_now - duration_since_epoch)
1229+
} else { now };
12181230
Ok(Self {
12191231
min_liquidity_offset_msat,
12201232
max_liquidity_offset_msat,
1221-
last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
1233+
last_updated,
12221234
})
12231235
}
12241236
}

0 commit comments

Comments
 (0)