Add block rewards to more Engines (#4055)

* add block rewards

* imports
This commit is contained in:
keorn 2017-01-05 21:16:13 +01:00 committed by Gav Wood
parent b4e713efdc
commit fbc9f0d7fb
5 changed files with 50 additions and 5 deletions

View File

@ -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<Address>,
/// Number of authorities.
pub authority_n: usize,
/// Block reward.
pub block_reward: U256,
/// Starting step,
pub start_step: Option<u64>,
}
@ -60,6 +63,7 @@ impl From<ethjson::spec::AuthorityRoundParams> 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::<Vec<_>>(),
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() {

View File

@ -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() {

View File

@ -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<ethjson::spec::TendermintParams> 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),
}
}
}

View File

@ -30,6 +30,9 @@ pub struct AuthorityRoundParams {
pub step_duration: Uint,
/// Valid authorities
pub authorities: Vec<Address>,
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
/// 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
}
}"#;

View File

@ -39,6 +39,9 @@ pub struct TendermintParams {
/// Commit step timeout in milliseconds.
#[serde(rename="timeoutCommit")]
pub timeout_commit: Option<Uint>,
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
}
/// 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"
}
}"#;