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

Add block rewards to more Engines #4055

Merged
merged 2 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ethcore/src/engines/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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>,
}
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions ethcore/src/engines/tendermint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 5 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::{Address, U256};
use util::{Address, Uint, U256};
use time::Duration;

/// `Tendermint` params.
Expand All @@ -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 {
Expand All @@ -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(),
}
}
Expand All @@ -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),
}
}
}
6 changes: 5 additions & 1 deletion json/src/spec/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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
}
}"#;
Expand Down
10 changes: 7 additions & 3 deletions json/src/spec/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
}
}"#;

Expand Down