Common engine method for bestowing rewards created

This commit is contained in:
Anton Gavrilov 2017-08-04 11:28:26 +02:00
parent 30434325a2
commit 655ed93ee8
4 changed files with 30 additions and 46 deletions

View File

@ -27,12 +27,10 @@ use block::*;
use builtin::Builtin; use builtin::Builtin;
use client::{Client, EngineClient}; use client::{Client, EngineClient};
use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier}; use engines::{Call, Engine, Seal, EngineError, ConstructedVerifier};
use trace::{Tracer, ExecutiveTracer, RewardType};
use error::{Error, TransactionError, BlockError}; use error::{Error, TransactionError, BlockError};
use ethjson; use ethjson;
use header::{Header, BlockNumber}; use header::{Header, BlockNumber};
use spec::CommonParams; use spec::CommonParams;
use state::CleanupMode;
use transaction::UnverifiedTransaction; use transaction::UnverifiedTransaction;
use super::signer::EngineSigner; use super::signer::EngineSigner;
@ -547,26 +545,7 @@ impl Engine for AuthorityRound {
/// Apply the block reward on finalisation of the block. /// Apply the block reward on finalisation of the block.
fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> {
let tracing_enabled = block.tracing_enabled(); ::engines::common::bestow_block_reward(block, self)
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
} }
/// Check the number of seal fields. /// Check the number of seal fields.

View File

@ -394,13 +394,14 @@ pub trait Engine : Sync + Send {
/// Common engine utilities /// Common engine utilities
pub mod common { pub mod common {
use std::sync::Arc; use std::sync::Arc;
use block::ExecutedBlock; use block::{ExecutedBlock, IsBlock};
use error::Error; use error::Error;
use transaction::SYSTEM_ADDRESS; use transaction::SYSTEM_ADDRESS;
use executive::Executive; use executive::Executive;
use vm::{CallType, ActionParams, ActionValue, EnvInfo, LastHashes}; use vm::{CallType, ActionParams, ActionValue, EnvInfo, LastHashes};
use trace::{NoopTracer, NoopVMTracer}; use trace::{NoopTracer, NoopVMTracer, Tracer, ExecutiveTracer, RewardType};
use state::Substate; use state::Substate;
use state::CleanupMode;
use util::*; use util::*;
use super::Engine; use super::Engine;
@ -469,4 +470,28 @@ pub mod common {
} }
Ok(()) Ok(())
} }
/// Trace rewards on closing block
pub fn bestow_block_reward<E: Engine + ?Sized>(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
}
} }

View File

@ -72,6 +72,7 @@ impl Engine for NullEngine {
fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> {
if self.params.block_reward == U256::zero() { if self.params.block_reward == U256::zero() {
// we don't have to apply reward in this case
return Ok(()) return Ok(())
} }

View File

@ -40,8 +40,6 @@ use account_provider::AccountProvider;
use block::*; use block::*;
use spec::CommonParams; use spec::CommonParams;
use engines::{Engine, Seal, EngineError, ConstructedVerifier}; use engines::{Engine, Seal, EngineError, ConstructedVerifier};
use trace::{Tracer, ExecutiveTracer, RewardType};
use state::CleanupMode;
use io::IoService; use io::IoService;
use super::signer::EngineSigner; use super::signer::EngineSigner;
use super::validator_set::{ValidatorSet, SimpleList}; use super::validator_set::{ValidatorSet, SimpleList};
@ -542,26 +540,7 @@ impl Engine for Tendermint {
/// Apply the block reward on finalisation of the block. /// Apply the block reward on finalisation of the block.
fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{ fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error>{
let tracing_enabled = block.tracing_enabled(); ::engines::common::bestow_block_reward(block, self)
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
} }
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {