diff --git a/ethcore/engines/authority-round/src/lib.rs b/ethcore/engines/authority-round/src/lib.rs index 08366bd67ac..89a0b1b60c9 100644 --- a/ethcore/engines/authority-round/src/lib.rs +++ b/ethcore/engines/authority-round/src/lib.rs @@ -248,18 +248,26 @@ impl Step { }) } - /// Finds the remaining duration of the current step. Returns `None` if there was a counter - /// under- or overflow. - fn opt_duration_remaining(&self) -> Option { - let next_step = self.load().checked_add(1)?; + /// Convert a given step number to the corresponding expected time + fn step_to_time(&self, step: u64) -> Option { let StepDurationInfo { transition_step, transition_timestamp, step_duration } = self.durations.iter() - .take_while(|info| info.transition_step < next_step) + .take_while(|info| info.transition_step < step) .last() - .expect("durations cannot be empty") + .expect("durations cannot be empty.") .clone(); - let next_time = transition_timestamp - .checked_add(next_step.checked_sub(transition_step)?.checked_mul(step_duration)?)?; + + let time = transition_timestamp + .checked_add(step.checked_sub(transition_step)?.checked_mul(step_duration)?)?; + Some(time) + } + + + /// Finds the remaining duration of the current step. Returns `None` if there was a counter + /// under- or overflow. + fn opt_duration_remaining(&self) -> Option { + let next_step = self.load().checked_add(1)?; + let next_time = self.step_to_time(next_step)?; Some(Duration::from_secs(next_time.saturating_sub(unix_now().as_secs()))) } @@ -318,13 +326,12 @@ impl Step { Err(None) // wait a bit for blocks in near future } else if given > current { - let d = self.durations.iter().take_while(|info| info.transition_step <= current).last() - .expect("Duration map has at least a 0 entry.") - .step_duration; + let current_time = self.step_to_time(current).ok_or(None)?; + let given_time = self.step_to_time(given).ok_or(None)?; Err(Some(OutOfBounds { min: None, - max: Some(d * current), - found: d * given, + max: Some(current_time), + found: given_time, })) } else { Ok(()) diff --git a/ethcore/verification/src/verification.rs b/ethcore/verification/src/verification.rs index 9802159aa84..eeebe2a7a72 100644 --- a/ethcore/verification/src/verification.rs +++ b/ethcore/verification/src/verification.rs @@ -374,10 +374,9 @@ fn verify_parent(header: &Header, parent: &Header, engine: &dyn Engine) -> Resul "Parent hash should already have been verified; qed"); if !engine.is_timestamp_valid(header.timestamp(), parent.timestamp()) { - let now = SystemTime::now(); - let min = CheckedSystemTime::checked_add(now, Duration::from_secs(parent.timestamp().saturating_add(1))) + let min = CheckedSystemTime::checked_add(UNIX_EPOCH, Duration::from_secs(parent.timestamp().saturating_add(1))) .ok_or(BlockError::TimestampOverflow)?; - let found = CheckedSystemTime::checked_add(now, Duration::from_secs(header.timestamp())) + let found = CheckedSystemTime::checked_add(UNIX_EPOCH, Duration::from_secs(header.timestamp())) .ok_or(BlockError::TimestampOverflow)?; return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: None, min: Some(min), found }.into()))) }