Ethash populate_from_parent implementation.

This commit is contained in:
Gav Wood 2016-01-15 01:53:52 +01:00
parent 02e2b75407
commit 7c5c2bd9b8

View File

@ -14,6 +14,8 @@ impl Ethash {
pub fn new_boxed(spec: Spec) -> Box<Engine> { pub fn new_boxed(spec: Spec) -> Box<Engine> {
Box::new(Ethash{spec: spec}) Box::new(Ethash{spec: spec})
} }
fn u256_param(&self, name: &str) -> U256 { self.spec().engine_params.get(name).map(|a| decode(&a)).unwrap_or(U256::from(0u64)) }
} }
impl Engine for Ethash { impl Engine for Ethash {
@ -29,6 +31,20 @@ impl Engine for Ethash {
fn spec(&self) -> &Spec { &self.spec } fn spec(&self) -> &Spec { &self.spec }
fn schedule(&self, _env_info: &EnvInfo) -> Schedule { Schedule::new_frontier() } fn schedule(&self, _env_info: &EnvInfo) -> Schedule { Schedule::new_frontier() }
fn populate_from_parent(&self, header: &mut Header, parent: &Header) {
header.difficulty = self.calculate_difficuty(header, parent);
header.gas_limit = {
let gas_floor_target: U256 = x!(3141562);
let gas_limit = parent.gas_limit;
let bound_divisor = self.u256_param("gasLimitBoundDivisor");
if gas_limit < gas_floor_target {
min(gas_floor_target, gas_limit + gas_limit / bound_divisor - x!(1))
} else {
max(gas_floor_target, gas_limit - gas_limit / bound_divisor + x!(1) + (header.gas_used * x!(6) / x!(5)) / bound_divisor)
}
};
}
/// Apply the block reward on finalisation of the block. /// 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). /// 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) { fn on_close_block(&self, block: &mut Block) {
@ -45,7 +61,6 @@ impl Engine for Ethash {
} }
} }
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> { fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap()); let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
if header.difficulty < min_difficulty { if header.difficulty < min_difficulty {