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

Commit

Permalink
Add registrar fields (#4716)
Browse files Browse the repository at this point in the history
* 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 32286ad
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 13 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
10 changes: 9 additions & 1 deletion 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)

/// (consensus view, 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

0 comments on commit 32286ad

Please sign in to comment.