Decouple timestamp open-block-assignment/verification to Engine (#8305)

This commit is contained in:
Wei Tang
2018-04-05 16:11:21 +08:00
committed by André Silva
parent 811d165458
commit ff0ce70169
6 changed files with 33 additions and 15 deletions

View File

@@ -50,6 +50,17 @@ impl<M: Machine> Engine<M> for InstantSeal<M>
fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> {
Ok(())
}
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
use std::{time, cmp};
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
cmp::max(now.as_secs(), parent_timestamp)
}
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
header_timestamp >= parent_timestamp
}
}
#[cfg(test)]

View File

@@ -325,6 +325,19 @@ pub trait Engine<M: Machine>: Sync + Send {
fn supports_warp(&self) -> bool {
self.snapshot_components().is_some()
}
/// Return a new open block header timestamp based on the parent timestamp.
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
use std::{time, cmp};
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
cmp::max(now.as_secs() as u64, parent_timestamp + 1)
}
/// Check whether the parent timestamp is valid.
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
header_timestamp > parent_timestamp
}
}
/// Common type alias for an engine coupled with an Ethereum-like state machine.