Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
grumbles: err when max_time & max_threshold fails
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Jun 12, 2019
1 parent c6da351 commit eb7405a
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions ethcore/src/verification/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,27 @@ pub fn verify_header_params(header: &Header, engine: &EthEngine, is_full: bool,

if is_full {
const ACCEPTABLE_DRIFT: Duration = Duration::from_secs(15);
// this will resist overflow until `year 2037`
let now = SystemTime::now();
let now = now.duration_since(UNIX_EPOCH).expect("now is after UNIX_EPOCH; qed");
let max_time = now.checked_add(ACCEPTABLE_DRIFT)
.unwrap_or(Duration::from_secs(u64::max_value()));
let invalid_threshold = max_time.checked_add(ACCEPTABLE_DRIFT * 9)
.unwrap_or(Duration::from_secs(u64::max_value()));
let max_time = now.checked_add(ACCEPTABLE_DRIFT);
let invalid_threshold = max_time.and_then(|t| t.checked_add(ACCEPTABLE_DRIFT * 9));
let timestamp = Duration::from_secs(header.timestamp());

if timestamp > invalid_threshold {
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: Some(max_time), min: None, found: timestamp }.into())))
// System clock is unreliable or `year 2037` or later has been reached
if invalid_threshold.is_none() && max_time.is_none() {
return Err(From::from(BlockError::TimestampOverflow))
}

if let Some(invalid) = invalid_threshold {
if timestamp > invalid {
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: max_time, min: None, found: timestamp }.into())))
}
}

if timestamp > max_time {
return Err(From::from(BlockError::TemporarilyInvalid(OutOfBounds { max: Some(max_time), min: None, found: timestamp }.into())))
if let Some(max) = max_time {
if timestamp > max {
return Err(From::from(BlockError::TemporarilyInvalid(OutOfBounds { max: max_time, min: None, found: timestamp }.into())))
}
}
}

Expand Down

0 comments on commit eb7405a

Please sign in to comment.