openethereum/src/ethereum/ethash.rs

51 lines
1.7 KiB
Rust
Raw Normal View History

use common::*;
2016-01-08 21:33:41 +01:00
use block::*;
use spec::*;
use engine::*;
2016-01-08 12:15:59 +01:00
/// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum
/// mainnet chains in the Olympic, Frontier and Homestead eras.
pub struct Ethash {
spec: Spec,
}
impl Ethash {
pub fn new_boxed(spec: Spec) -> Box<Engine> {
2016-01-08 12:27:00 +01:00
Box::new(Ethash{spec: spec})
2016-01-08 12:15:59 +01:00
}
}
2016-01-08 12:27:00 +01:00
impl Engine for Ethash {
2016-01-08 12:15:59 +01:00
fn name(&self) -> &str { "Ethash" }
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
2016-01-08 12:15:59 +01:00
fn spec(&self) -> &Spec { &self.spec }
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
2016-01-08 19:12:19 +01:00
/// 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).
2016-01-08 22:04:21 +01:00
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();
// 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)));
}
2016-01-08 19:12:19 +01:00
}
2016-01-08 12:15:59 +01:00
}
2016-01-08 22:04:21 +01:00
#[test]
2016-01-09 22:45:27 +01:00
fn on_close_block() {
use super::*;
let engine = new_morden().to_engine().unwrap();
let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
2016-01-10 14:05:39 +01:00
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());
}