From 4f68662e53513b78d0b10b3cb10bae97f0a4d6a3 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 10 Jan 2016 21:55:03 +0100 Subject: [PATCH] Proper rewarding; needs consensus test doing though. --- src/block.rs | 4 ++-- src/env_info.rs | 4 ++-- src/ethereum/ethash.rs | 20 ++++++++++++-------- src/header.rs | 9 +++++---- src/spec.rs | 8 ++++---- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/block.rs b/src/block.rs index 1c8f787ad..475e2c390 100644 --- a/src/block.rs +++ b/src/block.rs @@ -148,9 +148,9 @@ impl<'engine> OpenBlock<'engine> { pub fn env_info(&self) -> EnvInfo { // TODO: memoise. EnvInfo { - number: self.block.header.number.clone(), + number: self.block.header.number, author: self.block.header.author.clone(), - timestamp: self.block.header.timestamp.clone(), + timestamp: self.block.header.timestamp, difficulty: self.block.header.difficulty.clone(), last_hashes: self.last_hashes.clone(), gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)), diff --git a/src/env_info.rs b/src/env_info.rs index 919db314d..e7c8ae261 100644 --- a/src/env_info.rs +++ b/src/env_info.rs @@ -7,11 +7,11 @@ pub type LastHashes = Vec; /// Information concerning the execution environment for a message-call/contract-creation. pub struct EnvInfo { /// The block number. - pub number: U256, + pub number: usize, /// The block author. pub author: Address, /// The block timestamp. - pub timestamp: U256, + pub timestamp: usize, /// The block difficulty. pub difficulty: U256, /// The block gas limit. diff --git a/src/ethereum/ethash.rs b/src/ethereum/ethash.rs index 5922832b3..1c955a91f 100644 --- a/src/ethereum/ethash.rs +++ b/src/ethereum/ethash.rs @@ -17,20 +17,24 @@ impl Ethash { impl Engine for Ethash { fn name(&self) -> &str { "Ethash" } + fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) } fn spec(&self) -> &Spec { &self.spec } fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() } /// Apply the block reward on finalisation of the block. + /// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current). fn on_close_block(&self, block: &mut Block) { let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64)); let fields = block.fields(); - fields.state.add_balance(&fields.header.author, &reward); -/* - let uncle_authors = block.uncles.iter().map(|u| u.author().clone()).collect(); - for a in uncle_authors { - block.state_mut().addBalance(a, _blockReward * (8 + i.number() - m_currentBlock.number()) / 8); - r += _blockReward / 32; - }*/ + + // Bestow block reward + fields.state.add_balance(&fields.header.author, &(reward + reward / U256::from(32) * U256::from(fields.uncles.len()))); + + // Bestow uncle rewards + let current_number = fields.header.number(); + for u in fields.uncles.iter() { + fields.state.add_balance(u.author(), &(reward * U256::from((8 + u.number() - current_number) / 8))); + } } } @@ -43,5 +47,5 @@ fn on_close_block() { engine.spec().ensure_db_good(&mut db); let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]); let b = b.close(); - assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244F40000").unwrap()); + assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap()); } \ No newline at end of file diff --git a/src/header.rs b/src/header.rs index a1666ef04..b90538009 100644 --- a/src/header.rs +++ b/src/header.rs @@ -11,8 +11,8 @@ use basic_types::*; pub struct Header { // TODO: make all private. pub parent_hash: H256, - pub timestamp: U256, - pub number: U256, + pub timestamp: usize, + pub number: usize, pub author: Address, pub transactions_root: H256, @@ -41,8 +41,8 @@ impl Header { pub fn new() -> Header { Header { parent_hash: ZERO_H256.clone(), - timestamp: BAD_U256.clone(), - number: ZERO_U256.clone(), + timestamp: 0, + number: 0, author: ZERO_ADDRESS.clone(), transactions_root: ZERO_H256.clone(), @@ -64,6 +64,7 @@ impl Header { pub fn author(&self) -> &Address { &self.author } pub fn extra_data(&self) -> &Bytes { &self.extra_data } pub fn seal(&self) -> &Vec { &self.seal } + pub fn number(&self) -> usize { self.number } // TODO: seal_at, set_seal_at &c. diff --git a/src/spec.rs b/src/spec.rs index 377b9c0df..2ba1fa10c 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -60,7 +60,7 @@ pub struct Spec { pub difficulty: U256, pub gas_limit: U256, pub gas_used: U256, - pub timestamp: U256, + pub timestamp: usize, pub extra_data: Bytes, pub genesis_state: HashMap, pub seal_fields: usize, @@ -92,8 +92,8 @@ impl Spec { pub fn genesis_header(&self) -> Header { Header { parent_hash: self.parent_hash.clone(), - timestamp: self.timestamp.clone(), - number: U256::from(0u8), + timestamp: self.timestamp, + number: 0, author: self.author.clone(), transactions_root: SHA3_NULL_RLP.clone(), uncles_hash: RlpStream::new_list(0).out().sha3(), @@ -181,7 +181,7 @@ impl Spec { difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(), gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(), gas_used: U256::from(0u8), - timestamp: U256::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(), + timestamp: usize::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(), extra_data: genesis["extraData"].as_string().unwrap()[2..].from_hex().unwrap(), genesis_state: state, seal_fields: seal_fields,