2016-01-09 12:30:41 +01:00
|
|
|
use common::*;
|
2016-01-08 21:33:41 +01:00
|
|
|
use block::*;
|
2016-01-09 12:30:41 +01:00
|
|
|
use spec::*;
|
|
|
|
use engine::*;
|
2016-01-11 17:01:42 +01:00
|
|
|
use evm::Schedule;
|
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" }
|
2016-01-10 21:55:03 +01:00
|
|
|
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
|
2016-01-10 22:55:07 +01:00
|
|
|
// Two fields - mix
|
|
|
|
fn seal_fields(&self) -> usize { 2 }
|
|
|
|
// Two empty data items in RLP.
|
|
|
|
fn seal_rlp(&self) -> Bytes { encode(&H64::new()) }
|
|
|
|
|
|
|
|
/// Additional engine-specific information for the user/developer concerning `header`.
|
|
|
|
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
|
2016-01-08 12:15:59 +01:00
|
|
|
fn spec(&self) -> &Spec { &self.spec }
|
2016-01-11 16:28:30 +01:00
|
|
|
fn schedule(&self, _env_info: &EnvInfo) -> Schedule { Schedule::new_frontier() }
|
2016-01-08 19:12:19 +01:00
|
|
|
|
|
|
|
/// Apply the block reward on finalisation of the block.
|
2016-01-10 21:55:03 +01:00
|
|
|
/// 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) {
|
2016-01-10 17:09:02 +01:00
|
|
|
let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
|
|
|
|
let fields = block.fields();
|
2016-01-10 21:55:03 +01:00
|
|
|
|
|
|
|
// 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-09 19:10:05 +01:00
|
|
|
|
2016-01-10 19:47:32 +01:00
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
2016-01-10 19:47:32 +01:00
|
|
|
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
|
|
|
|
if header.difficulty < min_difficulty {
|
|
|
|
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: min_difficulty, found: header.difficulty })))
|
|
|
|
}
|
|
|
|
// TODO: Verify seal (quick)
|
2016-01-09 19:10:05 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
2016-01-10 19:47:32 +01:00
|
|
|
// TODO: Verify seal (full)
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn verify_block_final(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
2016-01-10 19:47:32 +01:00
|
|
|
// Check difficulty is correct given the two timestamps.
|
|
|
|
let expected_difficulty = self.calculate_difficuty(header, parent);
|
|
|
|
if header.difficulty != expected_difficulty {
|
|
|
|
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty })))
|
|
|
|
}
|
|
|
|
let gas_limit_divisor = decode(self.spec().engine_params.get("gasLimitBoundDivisor").unwrap());
|
|
|
|
let min_gas = parent.gas_limit - parent.gas_limit / gas_limit_divisor;
|
|
|
|
let max_gas = parent.gas_limit + parent.gas_limit / gas_limit_divisor;
|
|
|
|
if header.gas_limit <= min_gas || header.gas_limit >= max_gas {
|
2016-01-12 11:44:16 +01:00
|
|
|
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: Some(min_gas), max: Some(max_gas), found: header.gas_limit })));
|
2016-01-10 19:47:32 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> result::Result<(), Error> { Ok(()) }
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ethash {
|
|
|
|
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
|
|
|
|
const EXP_DIFF_PERIOD: u64 = 100000;
|
2016-01-11 12:45:35 +01:00
|
|
|
if header.number == 0 {
|
2016-01-09 19:10:05 +01:00
|
|
|
panic!("Can't calculate genesis block difficulty");
|
|
|
|
}
|
|
|
|
|
|
|
|
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
|
|
|
|
let difficulty_bound_divisor = decode(self.spec().engine_params.get("difficultyBoundDivisor").unwrap());
|
2016-01-11 12:45:35 +01:00
|
|
|
let duration_limit: u64 = decode(self.spec().engine_params.get("durationLimit").unwrap());
|
2016-01-09 19:10:05 +01:00
|
|
|
let frontier_limit = decode(self.spec().engine_params.get("frontierCompatibilityModeLimit").unwrap());
|
|
|
|
let mut target = if header.number < frontier_limit {
|
|
|
|
if header.timestamp >= parent.timestamp + duration_limit {
|
|
|
|
parent.difficulty - (parent.difficulty / difficulty_bound_divisor)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parent.difficulty + (parent.difficulty / difficulty_bound_divisor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-01-11 12:45:35 +01:00
|
|
|
let diff_inc = (header.timestamp - parent.timestamp) / 10;
|
|
|
|
if diff_inc <= 1 {
|
|
|
|
parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc)
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-01-11 12:45:35 +01:00
|
|
|
parent.difficulty - parent.difficulty / From::from(2048) * From::from(max(diff_inc - 1, 99))
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
target = max(min_difficulty, target);
|
2016-01-11 12:45:35 +01:00
|
|
|
let period = ((parent.number + 1) / EXP_DIFF_PERIOD) as usize;
|
2016-01-09 19:10:05 +01:00
|
|
|
if period > 1 {
|
|
|
|
target = max(min_difficulty, target + (U256::from(1) << (period - 2)));
|
|
|
|
}
|
|
|
|
target
|
|
|
|
}
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
2016-01-08 22:04:21 +01:00
|
|
|
|
2016-01-09 12:30:41 +01:00
|
|
|
#[test]
|
2016-01-09 22:45:27 +01:00
|
|
|
fn on_close_block() {
|
2016-01-09 18:20:31 +01:00
|
|
|
use super::*;
|
|
|
|
let engine = new_morden().to_engine().unwrap();
|
2016-01-09 12:30:41 +01:00
|
|
|
let genesis_header = engine.spec().genesis_header();
|
|
|
|
let mut db = OverlayDB::new_temp();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-01-10 22:55:07 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
|
|
|
let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
|
2016-01-10 14:05:39 +01:00
|
|
|
let b = b.close();
|
2016-01-10 21:55:03 +01:00
|
|
|
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
|
2016-01-09 14:41:04 +01:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
|
2016-01-10 19:47:32 +01:00
|
|
|
// TODO: difficulty test
|