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 transaction::SignedTransaction;
use env_info::EnvInfo; use env_info::EnvInfo;
use builtin::Builtin; use builtin::Builtin;
use state::CleanupMode;
/// `AuthorityRound` params. /// `AuthorityRound` params.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -49,6 +50,8 @@ pub struct AuthorityRoundParams {
pub authorities: Vec<Address>, pub authorities: Vec<Address>,
/// Number of authorities. /// Number of authorities.
pub authority_n: usize, pub authority_n: usize,
/// Block reward.
pub block_reward: U256,
/// Starting step, /// Starting step,
pub start_step: Option<u64>, 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()), step_duration: Duration::from_secs(p.step_duration.into()),
authority_n: p.authorities.len(), authority_n: p.authorities.len(),
authorities: p.authorities.into_iter().map(Into::into).collect::<Vec<_>>(), 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), start_step: p.start_step.map(Into::into),
} }
} }
@ -249,6 +253,20 @@ impl Engine for AuthorityRound {
Seal::None 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. /// Check the number of seal fields.
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
if header.seal().len() != self.seal_fields() { if header.seal().len() != self.seal_fields() {

View File

@ -45,6 +45,7 @@ use views::HeaderView;
use evm::Schedule; use evm::Schedule;
use io::{IoService, IoChannel}; use io::{IoService, IoChannel};
use service::ClientIoMessage; use service::ClientIoMessage;
use state::CleanupMode;
use self::message::*; use self::message::*;
use self::transition::TransitionHandler; use self::transition::TransitionHandler;
use self::params::TendermintParams; use self::params::TendermintParams;
@ -469,6 +470,20 @@ impl Engine for Tendermint {
Ok(()) 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> { fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
let seal_length = header.seal().len(); let seal_length = header.seal().len();
if seal_length == self.seal_fields() { if seal_length == self.seal_fields() {

View File

@ -18,7 +18,7 @@
use ethjson; use ethjson;
use super::transition::TendermintTimeouts; use super::transition::TendermintTimeouts;
use util::{Address, U256}; use util::{Address, Uint, U256};
use time::Duration; use time::Duration;
/// `Tendermint` params. /// `Tendermint` params.
@ -32,6 +32,8 @@ pub struct TendermintParams {
pub authority_n: usize, pub authority_n: usize,
/// Timeout durations for different steps. /// Timeout durations for different steps.
pub timeouts: TendermintTimeouts, pub timeouts: TendermintTimeouts,
/// Block reward.
pub block_reward: U256,
} }
impl Default for TendermintParams { impl Default for TendermintParams {
@ -42,6 +44,7 @@ impl Default for TendermintParams {
gas_limit_bound_divisor: 0x0400.into(), gas_limit_bound_divisor: 0x0400.into(),
authorities: authorities, authorities: authorities,
authority_n: val_n, authority_n: val_n,
block_reward: U256::zero(),
timeouts: TendermintTimeouts::default(), timeouts: TendermintTimeouts::default(),
} }
} }
@ -67,6 +70,7 @@ impl From<ethjson::spec::TendermintParams> for TendermintParams {
precommit: p.timeout_precommit.map_or(dt.precommit, to_duration), precommit: p.timeout_precommit.map_or(dt.precommit, to_duration),
commit: p.timeout_commit.map_or(dt.commit, 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, pub step_duration: Uint,
/// Valid authorities /// Valid authorities
pub authorities: Vec<Address>, pub authorities: Vec<Address>,
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
/// Starting step. Determined automatically if not specified. /// Starting step. Determined automatically if not specified.
/// To be used for testing only. /// To be used for testing only.
#[serde(rename="startStep")] #[serde(rename="startStep")]
@ -49,12 +52,13 @@ mod tests {
use spec::authority_round::AuthorityRound; use spec::authority_round::AuthorityRound;
#[test] #[test]
fn basic_authority_deserialization() { fn authority_round_deserialization() {
let s = r#"{ let s = r#"{
"params": { "params": {
"gasLimitBoundDivisor": "0x0400", "gasLimitBoundDivisor": "0x0400",
"stepDuration": "0x02", "stepDuration": "0x02",
"authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"], "authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"],
"blockReward": "0x50",
"startStep" : 24 "startStep" : 24
} }
}"#; }"#;

View File

@ -39,6 +39,9 @@ pub struct TendermintParams {
/// Commit step timeout in milliseconds. /// Commit step timeout in milliseconds.
#[serde(rename="timeoutCommit")] #[serde(rename="timeoutCommit")]
pub timeout_commit: Option<Uint>, pub timeout_commit: Option<Uint>,
/// Block reward.
#[serde(rename="blockReward")]
pub block_reward: Option<Uint>,
} }
/// Tendermint engine deserialization. /// Tendermint engine deserialization.
@ -54,11 +57,12 @@ mod tests {
use spec::tendermint::Tendermint; use spec::tendermint::Tendermint;
#[test] #[test]
fn basic_authority_deserialization() { fn tendermint_deserialization() {
let s = r#"{ let s = r#"{
"params": { "params": {
"gasLimitBoundDivisor": "0x0400", "gasLimitBoundDivisor": "0x400",
"authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"] "authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"],
"blockReward": "0x50"
} }
}"#; }"#;