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-06-29 20:04:52 +02:00
|
|
|
use 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-04-09 19:20:35 +02:00
|
|
|
use spec::CommonParams;
|
2016-01-09 12:30:41 +01:00
|
|
|
use engine::*;
|
2016-05-19 00:44:49 +02:00
|
|
|
use evm::Schedule;
|
2016-04-09 19:20:35 +02:00
|
|
|
use ethjson;
|
|
|
|
|
|
|
|
/// Ethash params.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct EthashParams {
|
|
|
|
/// Gas limit divisor.
|
|
|
|
pub gas_limit_bound_divisor: U256,
|
|
|
|
/// Minimum difficulty.
|
|
|
|
pub minimum_difficulty: U256,
|
|
|
|
/// Difficulty bound divisor.
|
|
|
|
pub difficulty_bound_divisor: U256,
|
|
|
|
/// Block duration.
|
|
|
|
pub duration_limit: u64,
|
|
|
|
/// Block reward.
|
|
|
|
pub block_reward: U256,
|
|
|
|
/// Namereg contract address.
|
|
|
|
pub registrar: Address,
|
2016-05-03 17:23:53 +02:00
|
|
|
/// Homestead transition block number.
|
|
|
|
pub frontier_compatibility_mode_limit: u64,
|
2016-07-16 13:02:56 +02:00
|
|
|
/// DAO hard-fork transition block (X).
|
|
|
|
pub dao_hardfork_transition: u64,
|
|
|
|
/// DAO hard-fork refund contract address (C).
|
|
|
|
pub dao_hardfork_beneficiary: Address,
|
2016-07-19 09:25:51 +02:00
|
|
|
/// DAO hard-fork DAO accounts list (L)
|
2016-07-16 13:02:56 +02:00
|
|
|
pub dao_hardfork_accounts: Vec<Address>,
|
2016-04-09 19:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ethjson::spec::EthashParams> for EthashParams {
|
|
|
|
fn from(p: ethjson::spec::EthashParams) -> Self {
|
|
|
|
EthashParams {
|
|
|
|
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
|
|
|
|
minimum_difficulty: p.minimum_difficulty.into(),
|
|
|
|
difficulty_bound_divisor: p.difficulty_bound_divisor.into(),
|
|
|
|
duration_limit: p.duration_limit.into(),
|
|
|
|
block_reward: p.block_reward.into(),
|
2016-07-19 09:25:51 +02:00
|
|
|
registrar: p.registrar.map_or_else(Address::new, Into::into),
|
|
|
|
frontier_compatibility_mode_limit: p.frontier_compatibility_mode_limit.map_or(0, Into::into),
|
|
|
|
dao_hardfork_transition: p.dao_hardfork_transition.map_or(0x7fffffffffffffff, Into::into),
|
|
|
|
dao_hardfork_beneficiary: p.dao_hardfork_beneficiary.map_or_else(Address::new, Into::into),
|
|
|
|
dao_hardfork_accounts: p.dao_hardfork_accounts.unwrap_or_else(Vec::new).into_iter().map(Into::into).collect(),
|
2016-04-09 19:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2016-04-09 19:20:35 +02:00
|
|
|
params: CommonParams,
|
|
|
|
ethash_params: EthashParams,
|
|
|
|
builtins: BTreeMap<Address, Builtin>,
|
2016-01-17 12:00:34 +01:00
|
|
|
pow: EthashManager,
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ethash {
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Create a new instance of Ethash engine
|
|
|
|
pub fn new(params: CommonParams, ethash_params: EthashParams, builtins: BTreeMap<Address, Builtin>) -> Self {
|
2016-02-08 21:07:14 +01:00
|
|
|
Ethash {
|
2016-04-09 19:20:35 +02:00
|
|
|
params: params,
|
|
|
|
ethash_params: ethash_params,
|
|
|
|
builtins: builtins,
|
2016-02-08 21:07:14 +01:00
|
|
|
pow: EthashManager::new(),
|
|
|
|
}
|
|
|
|
}
|
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 }
|
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
fn params(&self) -> &CommonParams { &self.params }
|
|
|
|
|
|
|
|
fn builtins(&self) -> &BTreeMap<Address, Builtin> {
|
|
|
|
&self.builtins
|
|
|
|
}
|
|
|
|
|
2016-01-10 22:55:07 +01:00
|
|
|
/// Additional engine-specific information for the user/developer concerning `header`.
|
2016-05-19 00:41:41 +02:00
|
|
|
fn extra_info(&self, header: &Header) -> HashMap<String, String> {
|
|
|
|
hash_map!["nonce".to_owned() => format!("0x{}", header.nonce().hex()), "mixHash".to_owned() => format!("0x{}", header.mix_hash().hex())]
|
|
|
|
}
|
2016-01-14 17:24:57 +01:00
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
fn schedule(&self, env_info: &EnvInfo) -> Schedule {
|
2016-05-03 17:23:53 +02:00
|
|
|
trace!(target: "client", "Creating schedule. fCML={}", self.ethash_params.frontier_compatibility_mode_limit);
|
2016-04-09 19:20:35 +02:00
|
|
|
|
2016-05-03 17:23:53 +02:00
|
|
|
if env_info.number < self.ethash_params.frontier_compatibility_mode_limit {
|
2016-02-23 11:40:23 +01:00
|
|
|
Schedule::new_frontier()
|
|
|
|
} else {
|
2016-07-05 18:18:35 +02:00
|
|
|
Schedule::new_homestead()
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 19:12:19 +01:00
|
|
|
|
2016-06-23 14:29:16 +02:00
|
|
|
fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, gas_ceil_target: U256) {
|
2016-01-15 01:53:52 +01:00
|
|
|
header.difficulty = self.calculate_difficuty(header, parent);
|
|
|
|
header.gas_limit = {
|
|
|
|
let gas_limit = parent.gas_limit;
|
2016-04-09 19:20:35 +02:00
|
|
|
let bound_divisor = self.ethash_params.gas_limit_bound_divisor;
|
2016-01-15 01:53:52 +01:00
|
|
|
if gas_limit < gas_floor_target {
|
2016-05-31 16:59:01 +02:00
|
|
|
min(gas_floor_target, gas_limit + gas_limit / bound_divisor - 1.into())
|
2016-06-23 14:29:16 +02:00
|
|
|
} else if gas_limit > gas_ceil_target {
|
|
|
|
max(gas_ceil_target, gas_limit - gas_limit / bound_divisor + 1.into())
|
2016-01-15 01:53:52 +01:00
|
|
|
} else {
|
2016-06-24 11:40:25 +02:00
|
|
|
min(gas_ceil_target,
|
2016-06-23 14:29:16 +02:00
|
|
|
max(gas_floor_target,
|
|
|
|
gas_limit - gas_limit / bound_divisor + 1.into() +
|
|
|
|
(header.gas_used * 6.into() / 5.into()) / bound_divisor))
|
2016-01-15 01:53:52 +01:00
|
|
|
}
|
|
|
|
};
|
2016-07-16 13:02:56 +02:00
|
|
|
if header.number >= self.ethash_params.dao_hardfork_transition &&
|
|
|
|
header.number <= self.ethash_params.dao_hardfork_transition + 9 {
|
|
|
|
header.extra_data = b"dao-hard-fork"[..].to_owned();
|
2016-07-19 09:25:51 +02:00
|
|
|
}
|
2016-03-02 00:52:18 +01:00
|
|
|
header.note_dirty();
|
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-07-16 13:02:56 +02:00
|
|
|
fn on_new_block(&self, block: &mut ExecutedBlock) {
|
|
|
|
if block.fields().header.number == self.ethash_params.dao_hardfork_transition {
|
|
|
|
// TODO: enable trigger function maybe?
|
|
|
|
// if block.fields().header.gas_limit <= 4_000_000.into() {
|
|
|
|
let mut state = block.fields_mut().state;
|
2016-07-19 09:25:51 +02:00
|
|
|
for child in &self.ethash_params.dao_hardfork_accounts {
|
2016-07-16 13:02:56 +02:00
|
|
|
let b = state.balance(child);
|
|
|
|
state.transfer_balance(child, &self.ethash_params.dao_hardfork_beneficiary, &b);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-09 19:20:35 +02:00
|
|
|
let reward = self.ethash_params.block_reward;
|
2016-03-24 23:03:22 +01:00
|
|
|
let fields = block.fields_mut();
|
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.
|
2016-02-08 21:07:14 +01:00
|
|
|
if header.seal.len() != self.seal_fields() {
|
|
|
|
return Err(From::from(BlockError::InvalidSealArity(
|
|
|
|
Mismatch { expected: self.seal_fields(), found: header.seal.len() }
|
|
|
|
)));
|
|
|
|
}
|
2016-01-26 18:02:49 +01:00
|
|
|
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-04-09 19:20:35 +02:00
|
|
|
let min_difficulty = self.ethash_params.minimum_difficulty;
|
2016-01-10 19:47:32 +01:00
|
|
|
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(
|
2016-02-29 19:30:13 +01:00
|
|
|
&Ethash::to_ethash(header.bare_hash()),
|
|
|
|
header.nonce().low_u64(),
|
2016-03-02 00:34:38 +01:00
|
|
|
&Ethash::to_ethash(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-07-16 13:02:56 +02:00
|
|
|
|
|
|
|
if header.number >= self.ethash_params.dao_hardfork_transition &&
|
|
|
|
header.number <= self.ethash_params.dao_hardfork_transition + 9 &&
|
|
|
|
header.extra_data[..] != b"dao-hard-fork"[..] {
|
|
|
|
return Err(From::from(BlockError::ExtraDataOutOfBounds(OutOfBounds { min: None, max: None, found: 0 })));
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.gas_limit > 0x7fffffffffffffffu64.into() {
|
|
|
|
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: None, max: Some(0x7fffffffffffffffu64.into()), found: header.gas_limit })));
|
2016-07-19 09:25:51 +02: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-02-09 12:23:35 +01:00
|
|
|
if header.seal.len() != self.seal_fields() {
|
|
|
|
return Err(From::from(BlockError::InvalidSealArity(
|
|
|
|
Mismatch { expected: self.seal_fields(), found: header.seal.len() }
|
|
|
|
)));
|
|
|
|
}
|
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-02-09 13:20:18 +01:00
|
|
|
// we should not calculate difficulty for genesis blocks
|
|
|
|
if header.number() == 0 {
|
|
|
|
return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { min: Some(1), max: None, found: header.number() })));
|
|
|
|
}
|
|
|
|
|
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 })))
|
|
|
|
}
|
2016-04-09 19:20:35 +02:00
|
|
|
let gas_limit_divisor = self.ethash_params.gas_limit_bound_divisor;
|
2016-01-10 19:47:32 +01:00
|
|
|
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-02-23 11:40:23 +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-05-03 17:23:53 +02:00
|
|
|
if header.number() >= self.ethash_params.frontier_compatibility_mode_limit {
|
2016-01-16 18:30:27 +01:00
|
|
|
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-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", 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-04-09 19:20:35 +02:00
|
|
|
let min_difficulty = self.ethash_params.minimum_difficulty;
|
|
|
|
let difficulty_bound_divisor = self.ethash_params.difficulty_bound_divisor;
|
|
|
|
let duration_limit = self.ethash_params.duration_limit;
|
2016-05-03 17:23:53 +02:00
|
|
|
let frontier_limit = self.ethash_params.frontier_compatibility_mode_limit;
|
2016-04-09 19:20:35 +02:00
|
|
|
|
2016-01-09 19:10:05 +01:00
|
|
|
let mut target = if header.number < frontier_limit {
|
|
|
|
if header.timestamp >= parent.timestamp + duration_limit {
|
2016-02-23 11:40:23 +01:00
|
|
|
parent.difficulty - (parent.difficulty / difficulty_bound_divisor)
|
2016-06-23 14:29:16 +02:00
|
|
|
} else {
|
2016-01-09 19:10:05 +01:00
|
|
|
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-02-23 11:40:23 +01:00
|
|
|
|
2016-02-29 19:30:13 +01:00
|
|
|
/// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`.
|
|
|
|
pub fn boundary_to_difficulty(boundary: &H256) -> U256 {
|
2016-06-15 16:33:38 +02:00
|
|
|
let d = U256::from(*boundary);
|
|
|
|
if d <= U256::one() {
|
|
|
|
U256::max_value()
|
|
|
|
} else {
|
|
|
|
((U256::one() << 255) / d) << 1
|
|
|
|
}
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 19:30:13 +01:00
|
|
|
/// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`.
|
|
|
|
pub fn difficulty_to_boundary(difficulty: &U256) -> H256 {
|
2016-06-15 16:33:38 +02:00
|
|
|
if *difficulty <= U256::one() {
|
|
|
|
U256::max_value().into()
|
|
|
|
} else {
|
|
|
|
(((U256::one() << 255) / *difficulty) << 1).into()
|
|
|
|
}
|
2016-02-29 19:30:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:00:34 +01:00
|
|
|
fn to_ethash(hash: H256) -> EH256 {
|
|
|
|
unsafe { mem::transmute(hash) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_ethash(hash: EH256) -> H256 {
|
|
|
|
unsafe { mem::transmute(hash) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
2016-03-01 01:15:00 +01:00
|
|
|
/// Get the none field of the header.
|
2016-03-01 00:02:48 +01:00
|
|
|
pub fn nonce(&self) -> H64 {
|
2016-01-17 12:00:34 +01:00
|
|
|
decode(&self.seal()[1])
|
|
|
|
}
|
2016-03-01 01:15:00 +01:00
|
|
|
|
|
|
|
/// Get the mix hash field of the header.
|
2016-03-01 00:02:48 +01:00
|
|
|
pub fn mix_hash(&self) -> H256 {
|
2016-01-17 12:00:34 +01:00
|
|
|
decode(&self.seal()[0])
|
|
|
|
}
|
2016-03-01 01:15:00 +01:00
|
|
|
|
|
|
|
/// Set the nonce and mix hash fields of the header.
|
2016-03-01 00:02:48 +01:00
|
|
|
pub fn set_nonce_and_mix_hash(&mut self, nonce: &H64, mix_hash: &H256) {
|
|
|
|
self.seal = vec![encode(mix_hash).to_vec(), encode(nonce).to_vec()];
|
|
|
|
}
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
2016-01-08 22:04:21 +01:00
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use common::*;
|
|
|
|
use block::*;
|
|
|
|
use tests::helpers::*;
|
|
|
|
use super::super::new_morden;
|
2016-06-15 16:33:38 +02:00
|
|
|
use super::Ethash;
|
2016-02-08 21:07:14 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn on_close_block() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let spec = new_morden();
|
|
|
|
let engine = &spec.engine;
|
|
|
|
let genesis_header = spec.genesis_header();
|
2016-02-08 21:07:14 +01:00
|
|
|
let mut db_result = get_temp_journal_db();
|
|
|
|
let mut db = db_result.take();
|
2016-04-09 19:20:35 +02:00
|
|
|
spec.ensure_db_good(db.as_hashdb_mut());
|
2016-02-08 21:07:14 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-05-19 00:44:49 +02:00
|
|
|
let vm_factory = Default::default();
|
2016-07-06 10:40:45 +02:00
|
|
|
let b = OpenBlock::new(engine.deref(), &vm_factory, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
|
2016-02-08 21:07:14 +01:00
|
|
|
let b = b.close();
|
|
|
|
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
|
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
#[test]
|
|
|
|
fn on_close_block_with_uncle() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let spec = new_morden();
|
|
|
|
let engine = &spec.engine;
|
|
|
|
let genesis_header = spec.genesis_header();
|
2016-02-08 21:07:14 +01:00
|
|
|
let mut db_result = get_temp_journal_db();
|
|
|
|
let mut db = db_result.take();
|
2016-04-09 19:20:35 +02:00
|
|
|
spec.ensure_db_good(db.as_hashdb_mut());
|
2016-02-08 21:07:14 +01:00
|
|
|
let last_hashes = vec![genesis_header.hash()];
|
2016-05-19 00:44:49 +02:00
|
|
|
let vm_factory = Default::default();
|
2016-07-06 10:40:45 +02:00
|
|
|
let mut b = OpenBlock::new(engine.deref(), &vm_factory, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![]).unwrap();
|
2016-02-08 21:07:14 +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());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn has_valid_metadata() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-08 21:07:14 +01:00
|
|
|
assert!(!engine.name().is_empty());
|
|
|
|
assert!(engine.version().major >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_return_schedule() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-08 21:07:14 +01:00
|
|
|
let schedule = engine.schedule(&EnvInfo {
|
|
|
|
number: 10000000,
|
2016-05-31 16:59:01 +02:00
|
|
|
author: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
timestamp: 0,
|
2016-05-31 16:59:01 +02:00
|
|
|
difficulty: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
last_hashes: vec![],
|
2016-05-31 16:59:01 +02:00
|
|
|
gas_used: 0.into(),
|
2016-06-18 20:26:44 +02:00
|
|
|
gas_limit: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert!(schedule.stack_limit > 0);
|
|
|
|
|
|
|
|
let schedule = engine.schedule(&EnvInfo {
|
|
|
|
number: 100,
|
2016-05-31 16:59:01 +02:00
|
|
|
author: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
timestamp: 0,
|
2016-05-31 16:59:01 +02:00
|
|
|
difficulty: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
last_hashes: vec![],
|
2016-05-31 16:59:01 +02:00
|
|
|
gas_used: 0.into(),
|
2016-06-18 20:26:44 +02:00
|
|
|
gas_limit: 0.into(),
|
2016-02-08 21:07:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert!(!schedule.have_delegate_call);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-08 21:43:53 +01:00
|
|
|
fn can_do_seal_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
|
|
|
//let engine = Ethash::new_test(new_morden());
|
2016-02-08 21:07:14 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_basic(&header, None);
|
|
|
|
|
2016-02-08 21:43:53 +01:00
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
|
2016-02-09 12:58:32 +01:00
|
|
|
Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
2016-02-08 21:43:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_do_difficulty_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-08 21:43:53 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).to_vec(), rlp::encode(&H64::zero()).to_vec()]);
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_basic(&header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::DifficultyOutOfBounds(_))) => {},
|
2016-02-09 12:58:32 +01:00
|
|
|
Err(_) => { panic!("should be block difficulty error (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
2016-02-08 21:43:53 +01:00
|
|
|
}
|
2016-02-08 21:07:14 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:54:33 +01:00
|
|
|
#[test]
|
|
|
|
fn can_do_proof_of_work_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-08 22:54:33 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).to_vec(), rlp::encode(&H64::zero()).to_vec()]);
|
|
|
|
header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_basic(&header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidProofOfWork(_))) => {},
|
2016-02-09 12:58:32 +01:00
|
|
|
Err(_) => { panic!("should be invalid proof of work error (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
2016-02-08 22:54:33 +01:00
|
|
|
}
|
2016-02-09 12:23:35 +01:00
|
|
|
}
|
2016-02-08 22:54:33 +01:00
|
|
|
|
2016-02-09 12:23:35 +01:00
|
|
|
#[test]
|
|
|
|
fn can_do_seal_unordered_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 12:23:35 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_unordered(&header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
|
2016-02-09 12:58:32 +01:00
|
|
|
Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
2016-02-09 12:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_do_seal256_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 12:23:35 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).to_vec(), rlp::encode(&H64::zero()).to_vec()]);
|
|
|
|
let verify_result = engine.verify_block_unordered(&header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::MismatchedH256SealElement(_))) => {},
|
2016-02-09 12:58:32 +01:00
|
|
|
Err(_) => { panic!("should be invalid 256-bit seal fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
2016-02-09 12:23:35 +01:00
|
|
|
}
|
2016-02-08 22:54:33 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 12:58:32 +01:00
|
|
|
#[test]
|
|
|
|
fn can_do_proof_of_work_unordered_verification_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 12:58:32 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_seal(vec![rlp::encode(&H256::from("b251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d")).to_vec(), rlp::encode(&H64::zero()).to_vec()]);
|
|
|
|
header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_unordered(&header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidProofOfWork(_))) => {},
|
|
|
|
Err(_) => { panic!("should be invalid proof-of-work fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
|
|
|
}
|
2016-02-09 13:20:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_verify_block_family_genesis_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 13:20:18 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
let parent_header: Header = Header::default();
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_family(&header, &parent_header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::RidiculousNumber(_))) => {},
|
|
|
|
Err(_) => { panic!("should be invalid block number fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 12:58:32 +01:00
|
|
|
|
2016-02-09 13:20:18 +01:00
|
|
|
#[test]
|
|
|
|
fn can_verify_block_family_difficulty_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 13:20:18 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_number(2);
|
|
|
|
let mut parent_header: Header = Header::default();
|
|
|
|
parent_header.set_number(1);
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_family(&header, &parent_header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidDifficulty(_))) => {},
|
|
|
|
Err(_) => { panic!("should be invalid difficulty fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_verify_block_family_gas_fail() {
|
2016-04-09 19:20:35 +02:00
|
|
|
let engine = new_morden().engine;
|
2016-02-09 13:20:18 +01:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
header.set_number(2);
|
|
|
|
header.set_difficulty(U256::from_str("0000000000000000000000000000000000000000000000000000000000020000").unwrap());
|
|
|
|
let mut parent_header: Header = Header::default();
|
|
|
|
parent_header.set_number(1);
|
|
|
|
|
|
|
|
let verify_result = engine.verify_block_family(&header, &parent_header, None);
|
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidGasLimit(_))) => {},
|
|
|
|
Err(_) => { panic!("should be invalid difficulty fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
|
|
|
}
|
2016-02-09 12:58:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 16:33:38 +02:00
|
|
|
#[test]
|
|
|
|
fn test_difficulty_to_boundary() {
|
|
|
|
// result of f(0) is undefined, so do not assert the result
|
|
|
|
let _ = Ethash::difficulty_to_boundary(&U256::from(0));
|
|
|
|
assert_eq!(Ethash::difficulty_to_boundary(&U256::from(1)), H256::from(U256::max_value()));
|
|
|
|
assert_eq!(Ethash::difficulty_to_boundary(&U256::from(2)), H256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap());
|
|
|
|
assert_eq!(Ethash::difficulty_to_boundary(&U256::from(4)), H256::from_str("4000000000000000000000000000000000000000000000000000000000000000").unwrap());
|
|
|
|
assert_eq!(Ethash::difficulty_to_boundary(&U256::from(32)), H256::from_str("0800000000000000000000000000000000000000000000000000000000000000").unwrap());
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
// TODO: difficulty test
|
2016-01-14 22:38:49 +01:00
|
|
|
}
|