Protect against timing underflows (#1111)

This commit is contained in:
Age Manning 2020-05-06 13:55:18 +10:00 committed by GitHub
parent 611a0c7d19
commit 4afcf721b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -392,8 +392,17 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
// For disconnected peers, lower their reputation by 1 for every hour they
// stay disconnected. This helps us slowly forget disconnected peers.
// In the same way, slowly allow banned peers back again.
let dc_hours = (now - since).as_secs() / 3600;
let last_dc_hours = (self.last_updated - since).as_secs() / 3600;
let dc_hours = now
.checked_duration_since(since)
.unwrap_or_else(|| Duration::from_secs(0))
.as_secs()
/ 3600;
let last_dc_hours = self
.last_updated
.checked_duration_since(since)
.unwrap_or_else(|| Duration::from_secs(0))
.as_secs()
/ 3600;
if dc_hours > last_dc_hours {
// this should be 1 most of the time
let rep_dif = (dc_hours - last_dc_hours)