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

Commit

Permalink
Engine backports (#4718)
Browse files Browse the repository at this point in the history
* custom dev presets

* add registrar field

* use constructor for dev registrar

* fix test
  • Loading branch information
keorn authored and arkpar committed Mar 6, 2017
1 parent 1be865b commit 6119cbc
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 21 deletions.
7 changes: 6 additions & 1 deletion ethcore/res/instant_seal.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions ethcore/src/engines/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct AuthorityRoundParams {
pub step_duration: Duration,
/// Block reward.
pub block_reward: U256,
/// Namereg contract address.
pub registrar: Address,
/// Starting step,
pub start_step: Option<u64>,
/// Valid validators.
Expand All @@ -62,6 +64,7 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
step_duration: Duration::from_secs(p.step_duration.into()),
validators: p.validators,
block_reward: p.block_reward.map_or_else(U256::zero, Into::into),
registrar: p.registrar.map_or_else(Address::new, Into::into),
start_step: p.start_step.map(Into::into),
}
}
Expand All @@ -73,6 +76,7 @@ pub struct AuthorityRound {
params: CommonParams,
gas_limit_bound_divisor: U256,
block_reward: U256,
registrar: Address,
step_duration: Duration,
builtins: BTreeMap<Address, Builtin>,
transition_service: IoService<()>,
Expand Down Expand Up @@ -112,6 +116,7 @@ impl AuthorityRound {
params: params,
gas_limit_bound_divisor: our_params.gas_limit_bound_divisor,
block_reward: our_params.block_reward,
registrar: our_params.registrar,
step_duration: our_params.step_duration,
builtins: builtins,
transition_service: IoService::<()>::start()?,
Expand Down Expand Up @@ -180,11 +185,16 @@ impl IoHandler<()> for TransitionHandler {

impl Engine for AuthorityRound {
fn name(&self) -> &str { "AuthorityRound" }

fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }

/// Two fields - consensus step and the corresponding proposer signature.
fn seal_fields(&self) -> usize { 2 }

fn params(&self) -> &CommonParams { &self.params }

fn additional_params(&self) -> HashMap<String, String> { hash_map!["registrar".to_owned() => self.registrar.hex()] }

fn builtins(&self) -> &BTreeMap<Address, Builtin> { &self.builtins }

fn step(&self) {
Expand Down
12 changes: 9 additions & 3 deletions ethcore/src/engines/instant_seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::collections::BTreeMap;
use util::Address;
use util::{Address, HashMap};
use builtin::Builtin;
use engines::{Engine, Seal};
use env_info::EnvInfo;
Expand All @@ -26,14 +26,16 @@ use block::ExecutedBlock;
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
pub struct InstantSeal {
params: CommonParams,
registrar: Address,
builtins: BTreeMap<Address, Builtin>,
}

impl InstantSeal {
/// Returns new instance of InstantSeal with default VM Factory
pub fn new(params: CommonParams, builtins: BTreeMap<Address, Builtin>) -> Self {
pub fn new(params: CommonParams, registrar: Address, builtins: BTreeMap<Address, Builtin>) -> Self {
InstantSeal {
params: params,
registrar: registrar,
builtins: builtins,
}
}
Expand All @@ -48,6 +50,10 @@ impl Engine for InstantSeal {
&self.params
}

fn additional_params(&self) -> HashMap<String, String> {
hash_map!["registrar".to_owned() => self.registrar.hex()]
}

fn builtins(&self) -> &BTreeMap<Address, Builtin> {
&self.builtins
}
Expand Down Expand Up @@ -76,9 +82,9 @@ mod tests {
fn instant_can_seal() {
let spec = Spec::new_instant();
let engine = &*spec.engine;
let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db();
let db = spec.ensure_db_good(db_result.take(), &Default::default()).unwrap();
let genesis_header = spec.genesis_header();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close_and_lock();
Expand Down
8 changes: 8 additions & 0 deletions ethcore/src/engines/tendermint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct Tendermint {
authority: RwLock<Address>,
/// Password used for signing messages.
password: RwLock<Option<String>>,
registrar: Address,
/// Blockchain height.
height: AtomicUsize,
/// Consensus round.
Expand Down Expand Up @@ -119,6 +120,7 @@ impl Tendermint {
block_reward: our_params.block_reward,
authority: RwLock::new(Address::default()),
password: RwLock::new(None),
registrar: our_params.registrar,
height: AtomicUsize::new(1),
round: AtomicUsize::new(0),
step: RwLock::new(Step::Propose),
Expand Down Expand Up @@ -376,14 +378,20 @@ impl Tendermint {

impl Engine for Tendermint {
fn name(&self) -> &str { "Tendermint" }

fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }

/// (consensus round, proposal signature, authority signatures)
fn seal_fields(&self) -> usize { 3 }

fn params(&self) -> &CommonParams { &self.params }

fn additional_params(&self) -> HashMap<String, String> { hash_map!["registrar".to_owned() => self.registrar.hex()] }

fn builtins(&self) -> &BTreeMap<Address, Builtin> { &self.builtins }

fn maximum_uncle_count(&self) -> usize { 0 }

fn maximum_uncle_age(&self) -> usize { 0 }

/// Additional engine-specific information for the user/developer concerning `header`.
Expand Down
5 changes: 4 additions & 1 deletion ethcore/src/engines/tendermint/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use ethjson;
use super::transition::TendermintTimeouts;
use util::{U256, Uint};
use util::{U256, Uint, Address, FixedHash};
use time::Duration;

/// `Tendermint` params.
Expand All @@ -32,6 +32,8 @@ pub struct TendermintParams {
pub timeouts: TendermintTimeouts,
/// Block reward.
pub block_reward: U256,
/// Namereg contract address.
pub registrar: Address,
}

fn to_duration(ms: ethjson::uint::Uint) -> Duration {
Expand All @@ -52,6 +54,7 @@ impl From<ethjson::spec::TendermintParams> for TendermintParams {
commit: p.timeout_commit.map_or(dt.commit, to_duration),
},
block_reward: p.block_reward.map_or_else(U256::zero, Into::into),
registrar: p.registrar.map_or_else(Address::new, Into::into),
}
}
}
2 changes: 1 addition & 1 deletion ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Spec {
fn engine(engine_spec: ethjson::spec::Engine, params: CommonParams, builtins: BTreeMap<Address, Builtin>) -> Arc<Engine> {
match engine_spec {
ethjson::spec::Engine::Null => Arc::new(NullEngine::new(params, builtins)),
ethjson::spec::Engine::InstantSeal => Arc::new(InstantSeal::new(params, builtins)),
ethjson::spec::Engine::InstantSeal(instant) => Arc::new(InstantSeal::new(params, instant.params.registrar.map_or_else(Address::new, Into::into), builtins)),
ethjson::spec::Engine::Ethash(ethash) => Arc::new(ethereum::Ethash::new(params, From::from(ethash.params), builtins)),
ethjson::spec::Engine::BasicAuthority(basic_authority) => Arc::new(BasicAuthority::new(params, From::from(basic_authority.params), builtins)),
ethjson::spec::Engine::AuthorityRound(authority_round) => AuthorityRound::new(params, From::from(authority_round.params), builtins).expect("Failed to start AuthorityRound consensus engine."),
Expand Down
3 changes: 3 additions & 0 deletions json/src/spec/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! Authority params deserialization.

use uint::Uint;
use hash::Address;
use super::ValidatorSet;

/// Authority params deserialization.
Expand All @@ -33,6 +34,8 @@ pub struct AuthorityRoundParams {
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
/// Address of the registrar contract.
pub registrar: Option<Address>,
/// Starting step. Determined automatically if not specified.
/// To be used for testing only.
#[serde(rename="startStep")]
Expand Down
10 changes: 4 additions & 6 deletions json/src/spec/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Engine deserialization.

use super::{Ethash, BasicAuthority, AuthorityRound, Tendermint};
use super::{Ethash, InstantSeal, BasicAuthority, AuthorityRound, Tendermint};

/// Engine deserialization.
#[derive(Debug, PartialEq, Deserialize)]
Expand All @@ -26,7 +26,7 @@ pub enum Engine {
Null,
/// Instantly sealing engine.
#[serde(rename="instantSeal")]
InstantSeal,
InstantSeal(InstantSeal),
/// Ethash engine.
Ethash(Ethash),
/// BasicAuthority engine.
Expand Down Expand Up @@ -55,12 +55,10 @@ mod tests {
assert_eq!(Engine::Null, deserialized);

let s = r#"{
"instantSeal": null
"instantSeal": { "params": {} }
}"#;

let deserialized: Engine = serde_json::from_str(s).unwrap();
assert_eq!(Engine::InstantSeal, deserialized);

let _deserialized: Engine = serde_json::from_str(s).unwrap();

let s = r#"{
"Ethash": {
Expand Down
50 changes: 50 additions & 0 deletions json/src/spec/instant_seal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Instant params deserialization.

use hash::Address;

/// Instant params deserialization.
#[derive(Debug, PartialEq, Deserialize)]
pub struct InstantSealParams {
/// Address of the registrar contract.
pub registrar: Option<Address>,
}

/// Instant engine deserialization.
#[derive(Debug, PartialEq, Deserialize)]
pub struct InstantSeal {
/// Instant Seal params.
pub params: InstantSealParams,
}

#[cfg(test)]
mod tests {
use serde_json;
use spec::instant_seal::InstantSeal;

#[test]
fn instant_seal_deserialization() {
let s = r#"{
"params": {
"registrar": "0xc6d9d2cd449a754c494264e1809c50e34d64562b"
}
}"#;

let _deserialized: InstantSeal = serde_json::from_str(s).unwrap();
}
}
2 changes: 2 additions & 0 deletions json/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod engine;
pub mod state;
pub mod ethash;
pub mod validator_set;
pub mod instant_seal;
pub mod basic_authority;
pub mod authority_round;
pub mod tendermint;
Expand All @@ -40,6 +41,7 @@ pub use self::engine::Engine;
pub use self::state::State;
pub use self::ethash::{Ethash, EthashParams};
pub use self::validator_set::ValidatorSet;
pub use self::instant_seal::{InstantSeal, InstantSealParams};
pub use self::basic_authority::{BasicAuthority, BasicAuthorityParams};
pub use self::authority_round::{AuthorityRound, AuthorityRoundParams};
pub use self::tendermint::{Tendermint, TendermintParams};
3 changes: 3 additions & 0 deletions json/src/spec/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! Tendermint params deserialization.

use uint::Uint;
use hash::Address;
use super::ValidatorSet;

/// Tendermint params deserialization.
Expand All @@ -42,6 +43,8 @@ pub struct TendermintParams {
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
/// Address of the registrar contract.
pub registrar: Option<Address>,
}

/// Tendermint engine deserialization.
Expand Down
37 changes: 28 additions & 9 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use ethcore_rpc::NetworkSettings;
use cache::CacheConfig;
use helpers::{to_duration, to_mode, to_block_id, to_u256, to_pending_set, to_price, replace_home, replace_home_for_db,
geth_ipc_path, parity_ipc_path, to_bootnodes, to_addresses, to_address, to_gas_limit, to_queue_strategy};
use params::{ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras};
use params::{SpecType, ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras};
use ethcore_logger::Config as LogConfig;
use dir::{self, Directories, default_hypervisor_path, default_local_path, default_data_path};
use dapps::Configuration as DappsConfiguration;
Expand Down Expand Up @@ -102,7 +102,6 @@ impl Configuration {
let vm_type = self.vm_type()?;
let mode = match self.args.flag_mode.as_ref() { "last" => None, mode => Some(to_mode(&mode, self.args.flag_mode_timeout, self.args.flag_mode_alarm)?), };
let update_policy = self.update_policy()?;
let miner_options = self.miner_options()?;
let logger_config = self.logger_config();
let http_conf = self.http_config()?;
let ipc_conf = self.ipc_config()?;
Expand Down Expand Up @@ -307,6 +306,12 @@ impl Configuration {

let verifier_settings = self.verifier_settings();

// Special presets are present for the dev chain.
let (gas_pricer, miner_options) = match spec {
SpecType::Dev => (GasPricerConfig::Fixed(0.into()), self.miner_options(0)?),
_ => (self.gas_pricer_config()?, self.miner_options(self.args.flag_reseal_min_period)?),
};

let run_cmd = RunCmd {
cache_config: cache_config,
dirs: dirs,
Expand All @@ -321,7 +326,7 @@ impl Configuration {
net_conf: net_conf,
network_id: network_id,
acc_conf: self.accounts_config()?,
gas_pricer: self.gas_pricer_config()?,
gas_pricer: gas_pricer,
miner_extras: self.miner_extras()?,
update_policy: update_policy,
mode: mode,
Expand Down Expand Up @@ -459,7 +464,7 @@ impl Configuration {
Ok(cfg)
}

fn miner_options(&self) -> Result<MinerOptions, String> {
fn miner_options(&self, reseal_min_period: u64) -> Result<MinerOptions, String> {
let reseal = self.args.flag_reseal_on_txs.parse::<ResealPolicy>()?;

let options = MinerOptions {
Expand All @@ -475,7 +480,7 @@ impl Configuration {
tx_queue_gas_limit: to_gas_limit(&self.args.flag_tx_queue_gas)?,
tx_queue_strategy: to_queue_strategy(&self.args.flag_tx_queue_strategy)?,
pending_set: to_pending_set(&self.args.flag_relay_set)?,
reseal_min_period: Duration::from_millis(self.args.flag_reseal_min_period),
reseal_min_period: Duration::from_millis(reseal_min_period),
work_queue_size: self.args.flag_work_queue_size,
enable_resubmission: !self.args.flag_remove_solved,
tx_queue_banning: match self.args.flag_tx_time_limit {
Expand Down Expand Up @@ -1093,13 +1098,14 @@ mod tests {
let conf3 = parse(&["parity", "--tx-queue-strategy", "gas"]);

// then
assert_eq!(conf0.miner_options().unwrap(), mining_options);
let min_period = conf0.args.flag_reseal_min_period;
assert_eq!(conf0.miner_options(min_period).unwrap(), mining_options);
mining_options.tx_queue_strategy = PrioritizationStrategy::GasFactorAndGasPrice;
assert_eq!(conf1.miner_options().unwrap(), mining_options);
assert_eq!(conf1.miner_options(min_period).unwrap(), mining_options);
mining_options.tx_queue_strategy = PrioritizationStrategy::GasPriceOnly;
assert_eq!(conf2.miner_options().unwrap(), mining_options);
assert_eq!(conf2.miner_options(min_period).unwrap(), mining_options);
mining_options.tx_queue_strategy = PrioritizationStrategy::GasAndGasPrice;
assert_eq!(conf3.miner_options().unwrap(), mining_options);
assert_eq!(conf3.miner_options(min_period).unwrap(), mining_options);
}

#[test]
Expand Down Expand Up @@ -1291,4 +1297,17 @@ mod tests {
let conf = Configuration::parse(&args).unwrap();
assert!(conf.init_reserved_nodes().is_ok());
}

#[test]
fn test_dev_chain() {
let args = vec!["parity", "--chain", "dev"];
let conf = parse(&args);
match conf.into_command().unwrap().cmd {
Cmd::Run(c) => {
assert_eq!(c.gas_pricer, GasPricerConfig::Fixed(0.into()));
assert_eq!(c.miner_options.reseal_min_period, Duration::from_millis(0));
},
_ => panic!("Should be Cmd::Run"),
}
}
}

0 comments on commit 6119cbc

Please sign in to comment.