2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-17 12:00:34 +01:00
|
|
|
extern crate ethash;
|
|
|
|
|
|
|
|
use self::ethash::{quick_get_difficulty, EthashManager, H256 as EH256};
|
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-14 17:24:57 +01:00
|
|
|
use evm::Factory;
|
2016-01-31 10:52:07 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
use tests::helpers::*;
|
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,
|
2016-01-17 12:00:34 +01:00
|
|
|
pow: EthashManager,
|
2016-01-17 01:18:35 +01:00
|
|
|
factory: Factory,
|
2016-01-16 18:30:27 +01:00
|
|
|
u64_params: RwLock<HashMap<String, u64>>,
|
|
|
|
u256_params: RwLock<HashMap<String, U256>>,
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ethash {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Create a new boxed instance of Ethash engine
|
2016-01-08 12:15:59 +01:00
|
|
|
pub fn new_boxed(spec: Spec) -> Box<Engine> {
|
2016-01-17 12:00:34 +01:00
|
|
|
Box::new(Ethash {
|
|
|
|
spec: spec,
|
|
|
|
pow: EthashManager::new(),
|
2016-01-14 17:24:57 +01:00
|
|
|
// TODO [todr] should this return any specific factory?
|
2016-01-17 01:18:35 +01:00
|
|
|
factory: Factory::default(),
|
2016-01-16 18:30:27 +01:00
|
|
|
u64_params: RwLock::new(HashMap::new()),
|
2016-01-17 01:18:35 +01:00
|
|
|
u256_params: RwLock::new(HashMap::new())
|
2016-01-17 12:00:34 +01:00
|
|
|
})
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
2016-01-15 01:53:52 +01:00
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
fn u64_param(&self, name: &str) -> u64 {
|
2016-01-19 11:10:38 +01:00
|
|
|
*self.u64_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
|
|
|
|
self.spec().engine_params.get(name).map_or(0u64, |a| decode(&a)))
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn u256_param(&self, name: &str) -> U256 {
|
2016-01-19 11:10:38 +01:00
|
|
|
*self.u256_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
|
|
|
|
self.spec().engine_params.get(name).map_or(x!(0), |a| decode(&a)))
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
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.
|
2016-01-27 17:22:01 +01:00
|
|
|
fn seal_rlp(&self) -> Bytes { encode(&H64::new()).to_vec() }
|
2016-01-10 22:55:07 +01:00
|
|
|
|
|
|
|
/// 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-14 17:24:57 +01:00
|
|
|
|
|
|
|
fn vm_factory(&self) -> &Factory {
|
|
|
|
&self.factory
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
fn schedule(&self, env_info: &EnvInfo) -> Schedule {
|
2016-01-28 20:34:06 +01:00
|
|
|
trace!(target: "client", "Creating schedule. param={:?}, fCML={}", self.spec().engine_params.get("frontierCompatibilityModeLimit"), self.u64_param("frontierCompatibilityModeLimit"));
|
2016-01-16 18:30:27 +01:00
|
|
|
match env_info.number < self.u64_param("frontierCompatibilityModeLimit") {
|
2016-01-28 20:34:06 +01:00
|
|
|
true => {
|
|
|
|
Schedule::new_frontier()
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
Schedule::new_homestead()
|
|
|
|
},
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-01-15 01:53:52 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
};
|
2016-01-15 23:32:17 +01:00
|
|
|
|
|
|
|
// info!("ethash: populate_from_parent #{}: difficulty={} and gas_limit={}", header.number, header.difficulty, header.gas_limit);
|
2016-01-15 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
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-26 19:18:22 +01:00
|
|
|
fn on_close_block(&self, block: &mut ExecutedBlock) {
|
2016-01-19 11:10:38 +01:00
|
|
|
let reward = self.spec().engine_params.get("blockReward").map_or(U256::from(0u64), |a| decode(&a));
|
2016-01-10 17:09:02 +01:00
|
|
|
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() {
|
2016-01-14 22:38:49 +01:00
|
|
|
fields.state.add_balance(u.author(), &(reward * U256::from(8 + u.number() - current_number) / U256::from(8)));
|
2016-01-10 21:55:03 +01:00
|
|
|
}
|
2016-01-14 22:38:49 +01:00
|
|
|
fields.state.commit();
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
2016-01-26 18:02:49 +01:00
|
|
|
// check the seal fields.
|
|
|
|
try!(UntrustedRlp::new(&header.seal[0]).as_val::<H256>());
|
2016-01-27 14:44:02 +01:00
|
|
|
try!(UntrustedRlp::new(&header.seal[1]).as_val::<H64>());
|
2016-01-26 18:02:49 +01:00
|
|
|
|
2016-02-03 12:18:12 +01:00
|
|
|
// TODO: consider removing these lines.
|
2016-01-10 19:47:32 +01:00
|
|
|
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
|
|
|
|
if header.difficulty < min_difficulty {
|
2016-02-03 12:18:12 +01:00
|
|
|
return Err(From::from(BlockError::DifficultyOutOfBounds(OutOfBounds { min: Some(min_difficulty), max: None, found: header.difficulty })))
|
2016-01-10 19:47:32 +01:00
|
|
|
}
|
2016-02-03 12:18:12 +01:00
|
|
|
|
2016-01-17 12:00:34 +01:00
|
|
|
let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(quick_get_difficulty(
|
|
|
|
&Ethash::to_ethash(header.bare_hash()),
|
2016-01-27 14:44:02 +01:00
|
|
|
header.nonce().low_u64(),
|
2016-01-17 12:00:34 +01:00
|
|
|
&Ethash::to_ethash(header.mix_hash()))));
|
|
|
|
if difficulty < header.difficulty {
|
2016-02-03 12:18:12 +01:00
|
|
|
return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty), max: None, found: difficulty })));
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-17 12:00:34 +01:00
|
|
|
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
2016-01-27 14:44:02 +01:00
|
|
|
let result = self.pow.compute_light(header.number as u64, &Ethash::to_ethash(header.bare_hash()), header.nonce().low_u64());
|
2016-01-17 12:00:34 +01:00
|
|
|
let mix = Ethash::from_ethash(result.mix_hash);
|
|
|
|
let difficulty = Ethash::boundary_to_difficulty(&Ethash::from_ethash(result.value));
|
|
|
|
if mix != header.mix_hash() {
|
2016-02-03 12:18:12 +01:00
|
|
|
return Err(From::from(BlockError::MismatchedH256SealElement(Mismatch { expected: mix, found: header.mix_hash() })));
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
|
|
|
if difficulty < header.difficulty {
|
2016-02-03 12:18:12 +01:00
|
|
|
return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty), max: None, found: difficulty })));
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
2016-01-10 19:47:32 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:03:48 +01:00
|
|
|
fn verify_block_family(&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-02-04 23:48:29 +01:00
|
|
|
fn verify_transaction_basic(&self, t: &SignedTransaction, header: &Header) -> result::Result<(), Error> {
|
2016-01-16 18:30:27 +01:00
|
|
|
if header.number() >= self.u64_param("frontierCompatibilityModeLimit") {
|
|
|
|
try!(t.check_low_s());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-01-17 23:07:58 +01:00
|
|
|
|
2016-02-04 17:23:53 +01:00
|
|
|
fn verify_transaction(&self, t: &SignedTransaction, _header: &Header) -> Result<(), Error> {
|
2016-01-17 23:07:58 +01:00
|
|
|
t.sender().map(|_|()) // Perform EC recovery and cache sender
|
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 11:15:39 +01:00
|
|
|
#[allow(wrong_self_convention)] // to_ethash should take self
|
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");
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
let min_difficulty = self.u256_param("minimumDifficulty");
|
|
|
|
let difficulty_bound_divisor = self.u256_param("difficultyBoundDivisor");
|
|
|
|
let duration_limit = self.u64_param("durationLimit");
|
|
|
|
let frontier_limit = self.u64_param("frontierCompatibilityModeLimit");
|
2016-01-09 19:10:05 +01:00
|
|
|
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-28 20:34:06 +01:00
|
|
|
trace!(target: "ethash", "Calculating difficulty parent.difficulty={}, header.timestamp={}, parent.timestamp={}", parent.difficulty, header.timestamp, parent.timestamp);
|
|
|
|
//block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)
|
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-28 20:46:33 +01:00
|
|
|
} else {
|
2016-01-28 20:34:06 +01:00
|
|
|
parent.difficulty - parent.difficulty / From::from(2048) * From::from(min(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-17 12:00:34 +01:00
|
|
|
|
|
|
|
fn boundary_to_difficulty(boundary: &H256) -> U256 {
|
|
|
|
U256::from((U512::one() << 256) / x!(U256::from(boundary.as_slice())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_ethash(hash: H256) -> EH256 {
|
|
|
|
unsafe { mem::transmute(hash) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_ethash(hash: EH256) -> H256 {
|
|
|
|
unsafe { mem::transmute(hash) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
2016-01-27 14:44:02 +01:00
|
|
|
fn nonce(&self) -> H64 {
|
2016-01-17 12:00:34 +01:00
|
|
|
decode(&self.seal()[1])
|
|
|
|
}
|
|
|
|
fn mix_hash(&self) -> H256 {
|
|
|
|
decode(&self.seal()[0])
|
|
|
|
}
|
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();
|
2016-01-31 10:52:07 +01:00
|
|
|
let mut db_result = get_temp_journal_db();
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut db = db_result.take();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-01-10 22:55:07 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-02-03 19:34:51 +01:00
|
|
|
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-14 22:38:49 +01:00
|
|
|
#[test]
|
|
|
|
fn on_close_block_with_uncle() {
|
|
|
|
use super::*;
|
|
|
|
let engine = new_morden().to_engine().unwrap();
|
|
|
|
let genesis_header = engine.spec().genesis_header();
|
2016-01-31 10:52:07 +01:00
|
|
|
let mut db_result = get_temp_journal_db();
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut db = db_result.take();
|
|
|
|
engine.spec().ensure_db_good(&mut db);
|
2016-01-14 22:38:49 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-02-03 19:34:51 +01:00
|
|
|
let mut b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
|
2016-01-14 22:38:49 +01:00
|
|
|
let mut uncle = Header::new();
|
|
|
|
let uncle_author = address_from_hex("ef2d6d194084c2de36e0dabfce45d046b37d1106");
|
|
|
|
uncle.author = uncle_author.clone();
|
|
|
|
b.push_uncle(uncle).unwrap();
|
|
|
|
|
|
|
|
let b = b.close();
|
|
|
|
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("478eae0e571ba000").unwrap());
|
|
|
|
assert_eq!(b.state().balance(&uncle_author), U256::from_str("3cb71f51fc558000").unwrap());
|
|
|
|
}
|
|
|
|
|
2016-01-10 19:47:32 +01:00
|
|
|
// TODO: difficulty test
|