Support millisecond timestamp for instant seal engine (#9469)

* Support millisecond timestamp for instant seal engine

* Forgot to checkin instant_seal mod

* Fix instant seal config

* Fix json crate compile

* Fix private_spec.json

* Option<bool> -> bool
This commit is contained in:
Wei Tang
2018-09-06 17:38:00 +08:00
committed by Andronik Ordian
parent 39a12622ae
commit 5752869824
7 changed files with 71 additions and 12 deletions

View File

@@ -17,17 +17,33 @@
use engines::{Engine, Seal};
use parity_machine::{Machine, Transactions, TotalScoredHeader};
/// `InstantSeal` params.
#[derive(Debug, PartialEq)]
pub struct InstantSealParams {
/// Whether to use millisecond timestamp
pub millisecond_timestamp: bool,
}
impl From<::ethjson::spec::InstantSealParams> for InstantSealParams {
fn from(p: ::ethjson::spec::InstantSealParams) -> Self {
InstantSealParams {
millisecond_timestamp: p.millisecond_timestamp,
}
}
}
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
/// Only seals blocks which have transactions.
pub struct InstantSeal<M> {
params: InstantSealParams,
machine: M,
}
impl<M> InstantSeal<M> {
/// Returns new instance of InstantSeal over the given state machine.
pub fn new(machine: M) -> Self {
pub fn new(params: InstantSealParams, machine: M) -> Self {
InstantSeal {
machine: machine,
params, machine,
}
}
}
@@ -56,8 +72,12 @@ impl<M: Machine> Engine<M> for InstantSeal<M>
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)
let dur = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
let mut now = dur.as_secs();
if self.params.millisecond_timestamp {
now = now * 1000 + dur.subsec_millis() as u64;
}
cmp::max(now, parent_timestamp)
}
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {

View File

@@ -589,7 +589,7 @@ impl Spec {
match engine_spec {
ethjson::spec::Engine::Null(null) => Arc::new(NullEngine::new(null.params.into(), machine)),
ethjson::spec::Engine::Ethash(ethash) => Arc::new(::ethereum::Ethash::new(spec_params.cache_dir, ethash.params.into(), machine, spec_params.optimization_setting)),
ethjson::spec::Engine::InstantSeal => Arc::new(InstantSeal::new(machine)),
ethjson::spec::Engine::InstantSeal(instant_seal) => Arc::new(InstantSeal::new(instant_seal.params.into(), machine)),
ethjson::spec::Engine::BasicAuthority(basic_authority) => Arc::new(BasicAuthority::new(basic_authority.params.into(), machine)),
ethjson::spec::Engine::AuthorityRound(authority_round) => AuthorityRound::new(authority_round.params.into(), machine)
.expect("Failed to start AuthorityRound consensus engine."),