From fbc9f0d7fbf3f120ed117f1bd0b685d6ffb03597 Mon Sep 17 00:00:00 2001 From: keorn Date: Thu, 5 Jan 2017 21:16:13 +0100 Subject: [PATCH] Add block rewards to more Engines (#4055) * add block rewards * imports --- ethcore/src/engines/authority_round.rs | 18 ++++++++++++++++++ ethcore/src/engines/tendermint/mod.rs | 15 +++++++++++++++ ethcore/src/engines/tendermint/params.rs | 6 +++++- json/src/spec/authority_round.rs | 6 +++++- json/src/spec/tendermint.rs | 10 +++++++--- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 72dacddf4..dca84e3a2 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -37,6 +37,7 @@ use service::ClientIoMessage; use transaction::SignedTransaction; use env_info::EnvInfo; use builtin::Builtin; +use state::CleanupMode; /// `AuthorityRound` params. #[derive(Debug, PartialEq)] @@ -49,6 +50,8 @@ pub struct AuthorityRoundParams { pub authorities: Vec
, /// Number of authorities. pub authority_n: usize, + /// Block reward. + pub block_reward: U256, /// Starting step, pub start_step: Option, } @@ -60,6 +63,7 @@ impl From for AuthorityRoundParams { step_duration: Duration::from_secs(p.step_duration.into()), authority_n: p.authorities.len(), authorities: p.authorities.into_iter().map(Into::into).collect::>(), + block_reward: p.block_reward.map_or_else(U256::zero, Into::into), start_step: p.start_step.map(Into::into), } } @@ -249,6 +253,20 @@ impl Engine for AuthorityRound { Seal::None } + /// Apply the block reward on finalisation of the block. + fn on_close_block(&self, block: &mut ExecutedBlock) { + let reward = self.our_params.block_reward; + let fields = block.fields_mut(); + + // Bestow block reward + fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty); + + // Commit state so that we can actually figure out the state root. + if let Err(e) = fields.state.commit() { + warn!("Encountered error on state commit: {}", e); + } + } + /// Check the number of seal fields. fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { if header.seal().len() != self.seal_fields() { diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index e124445e9..becc9d9c0 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -45,6 +45,7 @@ use views::HeaderView; use evm::Schedule; use io::{IoService, IoChannel}; use service::ClientIoMessage; +use state::CleanupMode; use self::message::*; use self::transition::TransitionHandler; use self::params::TendermintParams; @@ -469,6 +470,20 @@ impl Engine for Tendermint { Ok(()) } + /// Apply the block reward on finalisation of the block. + fn on_close_block(&self, block: &mut ExecutedBlock) { + let reward = self.our_params.block_reward; + let fields = block.fields_mut(); + + // Bestow block reward + fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty); + + // Commit state so that we can actually figure out the state root. + if let Err(e) = fields.state.commit() { + warn!("Encountered error on state commit: {}", e); + } + } + fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { let seal_length = header.seal().len(); if seal_length == self.seal_fields() { diff --git a/ethcore/src/engines/tendermint/params.rs b/ethcore/src/engines/tendermint/params.rs index cf723713b..e31182ae9 100644 --- a/ethcore/src/engines/tendermint/params.rs +++ b/ethcore/src/engines/tendermint/params.rs @@ -18,7 +18,7 @@ use ethjson; use super::transition::TendermintTimeouts; -use util::{Address, U256}; +use util::{Address, Uint, U256}; use time::Duration; /// `Tendermint` params. @@ -32,6 +32,8 @@ pub struct TendermintParams { pub authority_n: usize, /// Timeout durations for different steps. pub timeouts: TendermintTimeouts, + /// Block reward. + pub block_reward: U256, } impl Default for TendermintParams { @@ -42,6 +44,7 @@ impl Default for TendermintParams { gas_limit_bound_divisor: 0x0400.into(), authorities: authorities, authority_n: val_n, + block_reward: U256::zero(), timeouts: TendermintTimeouts::default(), } } @@ -67,6 +70,7 @@ impl From for TendermintParams { precommit: p.timeout_precommit.map_or(dt.precommit, to_duration), commit: p.timeout_commit.map_or(dt.commit, to_duration), }, + block_reward: p.block_reward.map_or_else(U256::zero, Into::into), } } } diff --git a/json/src/spec/authority_round.rs b/json/src/spec/authority_round.rs index d2cedb551..b302cc692 100644 --- a/json/src/spec/authority_round.rs +++ b/json/src/spec/authority_round.rs @@ -30,6 +30,9 @@ pub struct AuthorityRoundParams { pub step_duration: Uint, /// Valid authorities pub authorities: Vec
, + /// Block reward. + #[serde(rename="blockReward")] + pub block_reward: Option, /// Starting step. Determined automatically if not specified. /// To be used for testing only. #[serde(rename="startStep")] @@ -49,12 +52,13 @@ mod tests { use spec::authority_round::AuthorityRound; #[test] - fn basic_authority_deserialization() { + fn authority_round_deserialization() { let s = r#"{ "params": { "gasLimitBoundDivisor": "0x0400", "stepDuration": "0x02", "authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"], + "blockReward": "0x50", "startStep" : 24 } }"#; diff --git a/json/src/spec/tendermint.rs b/json/src/spec/tendermint.rs index 6858602da..b821b945c 100644 --- a/json/src/spec/tendermint.rs +++ b/json/src/spec/tendermint.rs @@ -39,6 +39,9 @@ pub struct TendermintParams { /// Commit step timeout in milliseconds. #[serde(rename="timeoutCommit")] pub timeout_commit: Option, + /// Block reward. + #[serde(rename="blockReward")] + pub block_reward: Option, } /// Tendermint engine deserialization. @@ -54,11 +57,12 @@ mod tests { use spec::tendermint::Tendermint; #[test] - fn basic_authority_deserialization() { + fn tendermint_deserialization() { let s = r#"{ "params": { - "gasLimitBoundDivisor": "0x0400", - "authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"] + "gasLimitBoundDivisor": "0x400", + "authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"], + "blockReward": "0x50" } }"#;