From 655ed93ee89d139e1f5cbd460b19cc77d2e5feac Mon Sep 17 00:00:00 2001 From: Anton Gavrilov Date: Fri, 4 Aug 2017 11:28:26 +0200 Subject: [PATCH] Common engine method for bestowing rewards created --- ethcore/src/engines/authority_round/mod.rs | 23 +---------------- ethcore/src/engines/mod.rs | 29 ++++++++++++++++++++-- ethcore/src/engines/null_engine.rs | 1 + ethcore/src/engines/tendermint/mod.rs | 23 +---------------- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 790280100..67cdc11d7 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -27,12 +27,10 @@ use block::*; use builtin::Builtin; use client::{Client, EngineClient}; use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier}; -use trace::{Tracer, ExecutiveTracer, RewardType}; use error::{Error, TransactionError, BlockError}; use ethjson; use header::{Header, BlockNumber}; use spec::CommonParams; -use state::CleanupMode; use transaction::UnverifiedTransaction; use super::signer::EngineSigner; @@ -547,26 +545,7 @@ impl Engine for AuthorityRound { /// Apply the block reward on finalisation of the block. fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { - let tracing_enabled = block.tracing_enabled(); - let fields = block.fields_mut(); - let mut tracer = ExecutiveTracer::default(); - // Bestow block reward - let reward = self.params().block_reward; - let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) - .map_err(::error::Error::from) - .and_then(|_| fields.state.commit()); - - if tracing_enabled { - let block_author = fields.header.author().clone(); - tracer.trace_reward(block_author, self.params().block_reward, RewardType::Block); - fields.traces.as_mut().map(|mut traces| traces.push(tracer.traces())); - } - - // Commit state so that we can actually figure out the state root. - if let Err(ref e) = res { - warn!("Encountered error on closing block: {}", e); - } - res + ::engines::common::bestow_block_reward(block, self) } /// Check the number of seal fields. diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index c11a1343f..c978cd5e3 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -394,13 +394,14 @@ pub trait Engine : Sync + Send { /// Common engine utilities pub mod common { use std::sync::Arc; - use block::ExecutedBlock; + use block::{ExecutedBlock, IsBlock}; use error::Error; use transaction::SYSTEM_ADDRESS; use executive::Executive; use vm::{CallType, ActionParams, ActionValue, EnvInfo, LastHashes}; - use trace::{NoopTracer, NoopVMTracer}; + use trace::{NoopTracer, NoopVMTracer, Tracer, ExecutiveTracer, RewardType}; use state::Substate; + use state::CleanupMode; use util::*; use super::Engine; @@ -469,4 +470,28 @@ pub mod common { } Ok(()) } + + /// Trace rewards on closing block + pub fn bestow_block_reward(block: &mut ExecutedBlock, engine: &E) -> Result<(), Error> { + let tracing_enabled = block.tracing_enabled(); + let fields = block.fields_mut(); + let mut tracer = ExecutiveTracer::default(); + // Bestow block reward + let reward = engine.params().block_reward; + let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) + .map_err(::error::Error::from) + .and_then(|_| fields.state.commit()); + + if tracing_enabled { + let block_author = fields.header.author().clone(); + tracer.trace_reward(block_author, engine.params().block_reward, RewardType::Block); + fields.traces.as_mut().map(|mut traces| traces.push(tracer.traces())); + } + + // Commit state so that we can actually figure out the state root. + if let Err(ref e) = res { + warn!("Encountered error on bestowing reward: {}", e); + } + res + } } diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 91abfe58f..378637a5e 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -72,6 +72,7 @@ impl Engine for NullEngine { fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { if self.params.block_reward == U256::zero() { + // we don't have to apply reward in this case return Ok(()) } diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index e1b001441..160ee7f0b 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -40,8 +40,6 @@ use account_provider::AccountProvider; use block::*; use spec::CommonParams; use engines::{Engine, Seal, EngineError, ConstructedVerifier}; -use trace::{Tracer, ExecutiveTracer, RewardType}; -use state::CleanupMode; use io::IoService; use super::signer::EngineSigner; use super::validator_set::{ValidatorSet, SimpleList}; @@ -542,26 +540,7 @@ impl Engine for Tendermint { /// Apply the block reward on finalisation of the block. fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{ - let tracing_enabled = block.tracing_enabled(); - let fields = block.fields_mut(); - let mut tracer = ExecutiveTracer::default(); - // Bestow block reward - let reward = self.params().block_reward; - let res = fields.state.add_balance(fields.header.author(), &reward, CleanupMode::NoEmpty) - .map_err(::error::Error::from) - .and_then(|_| fields.state.commit()); - - if tracing_enabled { - let block_author = fields.header.author().clone(); - tracer.trace_reward(block_author, self.params().block_reward, RewardType::Block); - fields.traces.as_mut().map(|mut traces| traces.push(tracer.traces())); - } - - // Commit state so that we can actually figure out the state root. - if let Err(ref e) = res { - warn!("Encountered error on closing block: {}", e); - } - res + ::engines::common::bestow_block_reward(block, self) } fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {