2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2017-07-10 12:57:40 +02:00
|
|
|
use std::path::Path;
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::cmp;
|
2017-10-05 15:34:30 +02:00
|
|
|
use std::collections::BTreeMap;
|
2017-09-26 14:19:08 +02:00
|
|
|
use std::sync::Arc;
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::{KECCAK_EMPTY_LIST_RLP};
|
2017-09-25 19:45:33 +02:00
|
|
|
use ethash::{quick_get_difficulty, slow_hash_block_number, EthashManager, OptimizeFor};
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::{H256, H64};
|
2017-10-08 18:17:59 +02:00
|
|
|
use util::Address;
|
2017-09-05 12:14:03 +02:00
|
|
|
use unexpected::{OutOfBounds, Mismatch};
|
2016-01-08 21:33:41 +01:00
|
|
|
use block::*;
|
2017-09-26 14:19:08 +02:00
|
|
|
use error::{BlockError, Error};
|
2017-12-05 15:57:45 +01:00
|
|
|
use header::{Header, BlockNumber};
|
2017-10-05 15:34:30 +02:00
|
|
|
use engines::{self, Engine};
|
2016-04-09 19:20:35 +02:00
|
|
|
use ethjson;
|
2017-03-22 14:41:46 +01:00
|
|
|
use rlp::{self, UntrustedRlp};
|
2017-09-26 14:19:08 +02:00
|
|
|
use machine::EthereumMachine;
|
2017-09-02 13:14:21 +02:00
|
|
|
use semantic_version::SemanticVersion;
|
2017-01-16 14:29:51 +01:00
|
|
|
|
2017-04-24 13:14:50 +02:00
|
|
|
/// Number of blocks in an ethash snapshot.
|
|
|
|
// make dependent on difficulty incrment divisor?
|
2017-08-22 11:24:56 +02:00
|
|
|
const SNAPSHOT_BLOCKS: u64 = 5000;
|
|
|
|
/// Maximum number of blocks allowed in an ethash snapshot.
|
|
|
|
const MAX_SNAPSHOT_BLOCKS: u64 = 30000;
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
const DEFAULT_EIP649_DELAY: u64 = 3_000_000;
|
2017-04-24 13:14:50 +02:00
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Ethash params.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct EthashParams {
|
|
|
|
/// Minimum difficulty.
|
|
|
|
pub minimum_difficulty: U256,
|
|
|
|
/// Difficulty bound divisor.
|
|
|
|
pub difficulty_bound_divisor: U256,
|
2016-09-28 14:21:59 +02:00
|
|
|
/// Difficulty increment divisor.
|
|
|
|
pub difficulty_increment_divisor: u64,
|
2017-03-27 11:42:40 +02:00
|
|
|
/// Metropolis difficulty increment divisor.
|
|
|
|
pub metropolis_difficulty_increment_divisor: u64,
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Block duration.
|
|
|
|
pub duration_limit: u64,
|
2016-05-03 17:23:53 +02:00
|
|
|
/// Homestead transition block number.
|
2016-10-15 14:39:15 +02:00
|
|
|
pub homestead_transition: u64,
|
2016-09-28 14:21:59 +02:00
|
|
|
/// Transition block for a change of difficulty params (currently just bound_divisor).
|
|
|
|
pub difficulty_hardfork_transition: u64,
|
|
|
|
/// Difficulty param after the difficulty transition.
|
|
|
|
pub difficulty_hardfork_bound_divisor: U256,
|
|
|
|
/// Block on which there is no additional difficulty from the exponential bomb.
|
|
|
|
pub bomb_defuse_transition: u64,
|
2017-03-27 11:42:40 +02:00
|
|
|
/// Number of first block where EIP-100 rules begin.
|
|
|
|
pub eip100b_transition: u64,
|
2016-11-04 23:09:23 +01:00
|
|
|
/// Number of first block where ECIP-1010 begins.
|
|
|
|
pub ecip1010_pause_transition: u64,
|
|
|
|
/// Number of first block where ECIP-1010 ends.
|
2016-11-11 11:56:58 +01:00
|
|
|
pub ecip1010_continue_transition: u64,
|
2017-06-19 18:56:37 +02:00
|
|
|
/// Total block number for one ECIP-1017 era.
|
|
|
|
pub ecip1017_era_rounds: u64,
|
2017-10-08 18:17:59 +02:00
|
|
|
/// Number of first block where MCIP-3 begins.
|
|
|
|
pub mcip3_transition: u64,
|
|
|
|
/// MCIP-3 Block reward coin-base for miners.
|
|
|
|
pub mcip3_miner_reward: U256,
|
|
|
|
/// MCIP-3 Block reward ubi-base for basic income.
|
|
|
|
pub mcip3_ubi_reward: U256,
|
|
|
|
/// MCIP-3 contract address for universal basic income.
|
|
|
|
pub mcip3_ubi_contract: Address,
|
|
|
|
/// MCIP-3 Block reward dev-base for dev funds.
|
|
|
|
pub mcip3_dev_reward: U256,
|
|
|
|
/// MCIP-3 contract address for the developer funds.
|
|
|
|
pub mcip3_dev_contract: Address,
|
2017-09-26 14:19:08 +02:00
|
|
|
/// Block reward in base units.
|
|
|
|
pub block_reward: U256,
|
2017-09-15 21:07:54 +02:00
|
|
|
/// EIP-649 transition block.
|
|
|
|
pub eip649_transition: u64,
|
|
|
|
/// EIP-649 bomb delay.
|
|
|
|
pub eip649_delay: u64,
|
|
|
|
/// EIP-649 base reward.
|
|
|
|
pub eip649_reward: Option<U256>,
|
2018-01-05 13:49:07 +01:00
|
|
|
/// EXPIP-2 block height
|
|
|
|
pub expip2_transition: u64,
|
|
|
|
/// EXPIP-2 duration limit
|
|
|
|
pub expip2_duration_limit: u64,
|
2016-04-09 19:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ethjson::spec::EthashParams> for EthashParams {
|
|
|
|
fn from(p: ethjson::spec::EthashParams) -> Self {
|
|
|
|
EthashParams {
|
|
|
|
minimum_difficulty: p.minimum_difficulty.into(),
|
|
|
|
difficulty_bound_divisor: p.difficulty_bound_divisor.into(),
|
2016-09-28 14:21:59 +02:00
|
|
|
difficulty_increment_divisor: p.difficulty_increment_divisor.map_or(10, Into::into),
|
2017-03-27 11:42:40 +02:00
|
|
|
metropolis_difficulty_increment_divisor: p.metropolis_difficulty_increment_divisor.map_or(9, Into::into),
|
2017-06-09 12:24:23 +02:00
|
|
|
duration_limit: p.duration_limit.map_or(0, Into::into),
|
2016-10-15 14:39:15 +02:00
|
|
|
homestead_transition: p.homestead_transition.map_or(0, Into::into),
|
2016-11-11 11:56:58 +01:00
|
|
|
difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(u64::max_value(), Into::into),
|
2016-09-28 14:21:59 +02:00
|
|
|
difficulty_hardfork_bound_divisor: p.difficulty_hardfork_bound_divisor.map_or(p.difficulty_bound_divisor.into(), Into::into),
|
2016-11-11 11:56:58 +01:00
|
|
|
bomb_defuse_transition: p.bomb_defuse_transition.map_or(u64::max_value(), Into::into),
|
2017-03-27 11:42:40 +02:00
|
|
|
eip100b_transition: p.eip100b_transition.map_or(u64::max_value(), Into::into),
|
2016-11-11 11:56:58 +01:00
|
|
|
ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(u64::max_value(), Into::into),
|
|
|
|
ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(u64::max_value(), Into::into),
|
2017-06-19 18:56:37 +02:00
|
|
|
ecip1017_era_rounds: p.ecip1017_era_rounds.map_or(u64::max_value(), Into::into),
|
2017-10-08 18:17:59 +02:00
|
|
|
mcip3_transition: p.mcip3_transition.map_or(u64::max_value(), Into::into),
|
|
|
|
mcip3_miner_reward: p.mcip3_miner_reward.map_or_else(Default::default, Into::into),
|
|
|
|
mcip3_ubi_reward: p.mcip3_ubi_reward.map_or(U256::from(0), Into::into),
|
|
|
|
mcip3_ubi_contract: p.mcip3_ubi_contract.map_or_else(Address::new, Into::into),
|
|
|
|
mcip3_dev_reward: p.mcip3_dev_reward.map_or(U256::from(0), Into::into),
|
|
|
|
mcip3_dev_contract: p.mcip3_dev_contract.map_or_else(Address::new, Into::into),
|
2017-09-26 14:19:08 +02:00
|
|
|
block_reward: p.block_reward.map_or_else(Default::default, Into::into),
|
2017-09-15 21:07:54 +02:00
|
|
|
eip649_transition: p.eip649_transition.map_or(u64::max_value(), Into::into),
|
|
|
|
eip649_delay: p.eip649_delay.map_or(DEFAULT_EIP649_DELAY, Into::into),
|
|
|
|
eip649_reward: p.eip649_reward.map(Into::into),
|
2018-01-05 13:49:07 +01:00
|
|
|
expip2_transition: p.expip2_transition.map_or(u64::max_value(), Into::into),
|
|
|
|
expip2_duration_limit: p.expip2_duration_limit.map_or(30, Into::into),
|
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
|
|
|
ethash_params: EthashParams,
|
2016-01-17 12:00:34 +01:00
|
|
|
pow: EthashManager,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: EthereumMachine,
|
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
|
2017-09-25 19:45:33 +02:00
|
|
|
pub fn new<T: Into<Option<OptimizeFor>>>(
|
|
|
|
cache_dir: &Path,
|
2017-07-10 12:57:40 +02:00
|
|
|
ethash_params: EthashParams,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: EthereumMachine,
|
2017-09-25 19:45:33 +02:00
|
|
|
optimize_for: T,
|
2017-07-10 12:57:40 +02:00
|
|
|
) -> Arc<Self> {
|
2017-04-13 20:24:21 +02:00
|
|
|
Arc::new(Ethash {
|
2017-07-10 12:57:40 +02:00
|
|
|
ethash_params,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine,
|
2017-09-25 19:45:33 +02:00
|
|
|
pow: EthashManager::new(cache_dir.as_ref(), optimize_for.into()),
|
2017-04-13 20:24:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
// TODO [rphmeier]
|
|
|
|
//
|
|
|
|
// for now, this is different than Ethash's own epochs, and signal
|
|
|
|
// "consensus epochs".
|
|
|
|
// in this sense, `Ethash` is epochless: the same `EpochVerifier` can be used
|
|
|
|
// for any block in the chain.
|
|
|
|
// in the future, we might move the Ethash epoch
|
|
|
|
// caching onto this mechanism as well.
|
2017-09-26 14:19:08 +02:00
|
|
|
impl engines::EpochVerifier<EthereumMachine> for Arc<Ethash> {
|
2017-04-13 20:24:21 +02:00
|
|
|
fn verify_light(&self, _header: &Header) -> Result<(), Error> { Ok(()) }
|
|
|
|
fn verify_heavy(&self, header: &Header) -> Result<(), Error> {
|
2017-09-26 14:19:08 +02:00
|
|
|
self.verify_block_unordered(header)
|
2016-02-08 21:07:14 +01:00
|
|
|
}
|
2016-01-08 12:15:59 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl Engine<EthereumMachine> for Arc<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) }
|
2017-09-26 14:19:08 +02:00
|
|
|
fn machine(&self) -> &EthereumMachine { &self.machine }
|
|
|
|
|
|
|
|
// Two fields - nonce and mix.
|
2016-01-10 22:55:07 +01:00
|
|
|
fn seal_fields(&self) -> usize { 2 }
|
|
|
|
|
|
|
|
/// Additional engine-specific information for the user/developer concerning `header`.
|
2016-11-04 17:35:02 +01:00
|
|
|
fn extra_info(&self, header: &Header) -> BTreeMap<String, String> {
|
2017-08-27 12:47:00 +02:00
|
|
|
if header.seal().len() == self.seal_fields() {
|
|
|
|
map![
|
|
|
|
"nonce".to_owned() => format!("0x{}", header.nonce().hex()),
|
|
|
|
"mixHash".to_owned() => format!("0x{}", header.mix_hash().hex())
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
BTreeMap::default()
|
|
|
|
}
|
2016-05-19 00:41:41 +02:00
|
|
|
}
|
2016-01-14 17:24:57 +01:00
|
|
|
|
2017-12-05 15:57:45 +01:00
|
|
|
fn maximum_uncle_count(&self, _block: BlockNumber) -> usize { 2 }
|
2017-11-10 00:56:02 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn populate_from_parent(&self, header: &mut Header, parent: &Header) {
|
2016-08-29 11:35:24 +02:00
|
|
|
let difficulty = self.calculate_difficulty(header, parent);
|
|
|
|
header.set_difficulty(difficulty);
|
2016-01-15 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn on_new_block(
|
|
|
|
&self,
|
2017-09-26 14:19:08 +02:00
|
|
|
_block: &mut ExecutedBlock,
|
2017-06-28 13:17:36 +02:00
|
|
|
_begins_epoch: bool,
|
|
|
|
) -> Result<(), Error> {
|
2017-05-30 11:52:33 +02:00
|
|
|
Ok(())
|
2016-07-16 13:02:56 +02: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).
|
2017-05-30 11:52:33 +02:00
|
|
|
fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> {
|
2017-08-03 15:55:58 +02:00
|
|
|
use std::ops::Shr;
|
2017-09-26 14:19:08 +02:00
|
|
|
use parity_machine::{LiveBlock, WithBalances};
|
|
|
|
|
|
|
|
let author = *LiveBlock::header(&*block).author();
|
|
|
|
let number = LiveBlock::header(&*block).number();
|
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
// Applies EIP-649 reward.
|
2017-09-26 14:19:08 +02:00
|
|
|
let reward = if number >= self.ethash_params.eip649_transition {
|
|
|
|
self.ethash_params.eip649_reward.unwrap_or(self.ethash_params.block_reward)
|
2017-09-15 21:07:54 +02:00
|
|
|
} else {
|
2017-09-26 14:19:08 +02:00
|
|
|
self.ethash_params.block_reward
|
2017-09-15 21:07:54 +02:00
|
|
|
};
|
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
// Applies ECIP-1017 eras.
|
2017-06-19 18:56:37 +02:00
|
|
|
let eras_rounds = self.ethash_params.ecip1017_era_rounds;
|
2017-09-26 14:19:08 +02:00
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, reward, number);
|
|
|
|
|
|
|
|
let n_uncles = LiveBlock::uncles(&*block).len();
|
2016-01-10 21:55:03 +01:00
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
// Bestow block rewards.
|
|
|
|
let mut result_block_reward = reward + reward.shr(5) * U256::from(n_uncles);
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut uncle_rewards = Vec::with_capacity(n_uncles);
|
2017-07-18 12:14:06 +02:00
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
if number >= self.ethash_params.mcip3_transition {
|
|
|
|
result_block_reward = self.ethash_params.mcip3_miner_reward;
|
|
|
|
let ubi_contract = self.ethash_params.mcip3_ubi_contract;
|
|
|
|
let ubi_reward = self.ethash_params.mcip3_ubi_reward;
|
|
|
|
let dev_contract = self.ethash_params.mcip3_dev_contract;
|
|
|
|
let dev_reward = self.ethash_params.mcip3_dev_reward;
|
2017-09-26 14:19:08 +02:00
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
self.machine.add_balance(block, &author, &result_block_reward)?;
|
|
|
|
self.machine.add_balance(block, &ubi_contract, &ubi_reward)?;
|
|
|
|
self.machine.add_balance(block, &dev_contract, &dev_reward)?;
|
|
|
|
} else {
|
|
|
|
self.machine.add_balance(block, &author, &result_block_reward)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bestow uncle rewards.
|
2017-09-26 14:19:08 +02:00
|
|
|
for u in LiveBlock::uncles(&*block) {
|
|
|
|
let uncle_author = u.author();
|
|
|
|
let result_uncle_reward = if eras == 0 {
|
|
|
|
(reward * U256::from(8 + u.number() - number)).shr(3)
|
2017-06-19 18:56:37 +02:00
|
|
|
} else {
|
2017-09-26 14:19:08 +02:00
|
|
|
reward.shr(5)
|
|
|
|
};
|
2017-07-18 12:14:06 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
uncle_rewards.push((*uncle_author, result_uncle_reward));
|
2016-01-10 21:55:03 +01:00
|
|
|
}
|
2016-09-21 12:49:11 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
for &(ref a, ref reward) in &uncle_rewards {
|
|
|
|
self.machine.add_balance(block, a, reward)?;
|
2017-08-03 15:55:58 +02:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
// Note and trace.
|
2017-09-26 14:19:08 +02:00
|
|
|
self.machine.note_rewards(block, &[(author, result_block_reward)], &uncle_rewards)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_local_seal(&self, header: &Header) -> Result<(), Error> {
|
|
|
|
self.verify_block_basic(header)
|
|
|
|
.and_then(|_| self.verify_block_unordered(header))
|
2016-01-08 19:12:19 +01:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify_block_basic(&self, header: &Header) -> Result<(), Error> {
|
2016-01-26 18:02:49 +01:00
|
|
|
// check the seal fields.
|
2016-08-29 11:35:24 +02:00
|
|
|
if header.seal().len() != self.seal_fields() {
|
2016-02-08 21:07:14 +01:00
|
|
|
return Err(From::from(BlockError::InvalidSealArity(
|
2016-08-29 11:35:24 +02:00
|
|
|
Mismatch { expected: self.seal_fields(), found: header.seal().len() }
|
2016-02-08 21:07:14 +01:00
|
|
|
)));
|
|
|
|
}
|
2016-12-27 12:53:56 +01:00
|
|
|
UntrustedRlp::new(&header.seal()[0]).as_val::<H256>()?;
|
|
|
|
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-08-29 11:35:24 +02:00
|
|
|
if header.difficulty() < &min_difficulty {
|
|
|
|
return Err(From::from(BlockError::DifficultyOutOfBounds(OutOfBounds { min: Some(min_difficulty), max: None, found: header.difficulty().clone() })))
|
2016-01-10 19:47:32 +01:00
|
|
|
}
|
2016-02-03 12:18:12 +01:00
|
|
|
|
2016-11-05 10:39:15 +01:00
|
|
|
let difficulty = Ethash::boundary_to_difficulty(&H256(quick_get_difficulty(
|
|
|
|
&header.bare_hash().0,
|
2016-02-29 19:30:13 +01:00
|
|
|
header.nonce().low_u64(),
|
2016-11-05 10:39:15 +01:00
|
|
|
&header.mix_hash().0
|
2016-03-02 00:34:38 +01:00
|
|
|
)));
|
2016-08-29 11:35:24 +02:00
|
|
|
if &difficulty < header.difficulty() {
|
|
|
|
return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty().clone()), max: None, found: difficulty })));
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
2016-07-16 13:02:56 +02:00
|
|
|
|
2016-08-29 11:35:24 +02:00
|
|
|
if header.gas_limit() > &0x7fffffffffffffffu64.into() {
|
|
|
|
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: None, max: Some(0x7fffffffffffffffu64.into()), found: header.gas_limit().clone() })));
|
2016-07-19 09:25:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-09 19:10:05 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify_block_unordered(&self, header: &Header) -> Result<(), Error> {
|
2016-08-29 11:35:24 +02:00
|
|
|
if header.seal().len() != self.seal_fields() {
|
2016-02-09 12:23:35 +01:00
|
|
|
return Err(From::from(BlockError::InvalidSealArity(
|
2016-08-29 11:35:24 +02:00
|
|
|
Mismatch { expected: self.seal_fields(), found: header.seal().len() }
|
2016-02-09 12:23:35 +01:00
|
|
|
)));
|
|
|
|
}
|
2016-11-05 10:39:15 +01:00
|
|
|
let result = self.pow.compute_light(header.number() as u64, &header.bare_hash().0, header.nonce().low_u64());
|
|
|
|
let mix = H256(result.mix_hash);
|
|
|
|
let difficulty = Ethash::boundary_to_difficulty(&H256(result.value));
|
2017-09-25 19:45:33 +02:00
|
|
|
trace!(target: "miner", "num: {}, seed: {}, h: {}, non: {}, mix: {}, res: {}" , header.number() as u64, H256(slow_hash_block_number(header.number() as u64)), header.bare_hash(), header.nonce().low_u64(), H256(result.mix_hash), H256(result.value));
|
2016-01-17 12:00:34 +01:00
|
|
|
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
|
|
|
}
|
2016-08-29 11:35:24 +02:00
|
|
|
if &difficulty < header.difficulty() {
|
|
|
|
return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty().clone()), max: None, found: difficulty })));
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
2016-01-10 19:47:32 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify_block_family(&self, header: &Header, parent: &Header) -> 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.
|
2016-08-29 11:35:24 +02:00
|
|
|
let expected_difficulty = self.calculate_difficulty(header, parent);
|
|
|
|
if header.difficulty() != &expected_difficulty {
|
|
|
|
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty().clone() })))
|
2016-01-10 19:47:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn epoch_verifier<'a>(&self, _header: &Header, _proof: &'a [u8]) -> engines::ConstructedVerifier<'a, EthereumMachine> {
|
2017-07-10 12:57:40 +02:00
|
|
|
engines::ConstructedVerifier::Trusted(Box::new(self.clone()))
|
2017-04-13 20:24:21 +02:00
|
|
|
}
|
2017-04-19 20:31:53 +02:00
|
|
|
|
|
|
|
fn snapshot_components(&self) -> Option<Box<::snapshot::SnapshotComponents>> {
|
2017-08-22 11:24:56 +02:00
|
|
|
Some(Box::new(::snapshot::PowSnapshot::new(SNAPSHOT_BLOCKS, MAX_SNAPSHOT_BLOCKS)))
|
2017-04-19 20:31:53 +02:00
|
|
|
}
|
2017-06-19 18:56:37 +02:00
|
|
|
}
|
|
|
|
|
2016-01-09 19:10:05 +01:00
|
|
|
impl Ethash {
|
2016-08-29 11:35:24 +02:00
|
|
|
fn calculate_difficulty(&self, header: &Header, parent: &Header) -> U256 {
|
2017-09-15 21:07:54 +02:00
|
|
|
const EXP_DIFF_PERIOD: u64 = 100_000;
|
2016-08-29 11:35:24 +02:00
|
|
|
if header.number() == 0 {
|
2016-01-09 19:10:05 +01:00
|
|
|
panic!("Can't calculate genesis block difficulty");
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:18:28 +02:00
|
|
|
let parent_has_uncles = parent.uncles_hash() != &KECCAK_EMPTY_LIST_RLP;
|
2017-03-27 11:42:40 +02:00
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
let min_difficulty = self.ethash_params.minimum_difficulty;
|
2017-07-03 16:49:06 +02:00
|
|
|
|
2016-09-28 14:21:59 +02:00
|
|
|
let difficulty_hardfork = header.number() >= self.ethash_params.difficulty_hardfork_transition;
|
2017-07-03 16:49:06 +02:00
|
|
|
let difficulty_bound_divisor = if difficulty_hardfork {
|
|
|
|
self.ethash_params.difficulty_hardfork_bound_divisor
|
|
|
|
} else {
|
|
|
|
self.ethash_params.difficulty_bound_divisor
|
2016-09-28 14:21:59 +02:00
|
|
|
};
|
2017-07-03 16:49:06 +02:00
|
|
|
|
2018-01-05 13:49:07 +01:00
|
|
|
let expip2_hardfork = header.number() >= self.ethash_params.expip2_transition;
|
|
|
|
let duration_limit = if expip2_hardfork {
|
|
|
|
self.ethash_params.expip2_duration_limit
|
|
|
|
} else {
|
|
|
|
self.ethash_params.duration_limit
|
|
|
|
};
|
|
|
|
|
2016-10-15 14:39:15 +02:00
|
|
|
let frontier_limit = self.ethash_params.homestead_transition;
|
2016-04-09 19:20:35 +02:00
|
|
|
|
2016-08-29 11:35:24 +02:00
|
|
|
let mut target = if header.number() < frontier_limit {
|
|
|
|
if header.timestamp() >= parent.timestamp() + duration_limit {
|
2017-03-27 11:42:40 +02:00
|
|
|
*parent.difficulty() - (*parent.difficulty() / difficulty_bound_divisor)
|
2016-06-23 14:29:16 +02:00
|
|
|
} else {
|
2017-03-27 11:42:40 +02:00
|
|
|
*parent.difficulty() + (*parent.difficulty() / difficulty_bound_divisor)
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
2018-01-05 13:49:07 +01:00
|
|
|
} else {
|
2016-08-29 11:35:24 +02:00
|
|
|
trace!(target: "ethash", "Calculating difficulty parent.difficulty={}, header.timestamp={}, parent.timestamp={}", parent.difficulty(), header.timestamp(), parent.timestamp());
|
2016-01-28 20:34:06 +01:00
|
|
|
//block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)
|
2017-03-27 11:42:40 +02:00
|
|
|
let (increment_divisor, threshold) = if header.number() < self.ethash_params.eip100b_transition {
|
|
|
|
(self.ethash_params.difficulty_increment_divisor, 1)
|
|
|
|
} else if parent_has_uncles {
|
|
|
|
(self.ethash_params.metropolis_difficulty_increment_divisor, 2)
|
|
|
|
} else {
|
|
|
|
(self.ethash_params.metropolis_difficulty_increment_divisor, 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
let diff_inc = (header.timestamp() - parent.timestamp()) / increment_divisor;
|
|
|
|
if diff_inc <= threshold {
|
|
|
|
*parent.difficulty() + *parent.difficulty() / difficulty_bound_divisor * (threshold - diff_inc).into()
|
2016-01-28 20:46:33 +01:00
|
|
|
} else {
|
2017-07-29 17:12:07 +02:00
|
|
|
let multiplier = cmp::min(diff_inc - threshold, 99).into();
|
2017-07-03 16:49:06 +02:00
|
|
|
parent.difficulty().saturating_sub(
|
|
|
|
*parent.difficulty() / difficulty_bound_divisor * multiplier
|
|
|
|
)
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
};
|
2017-07-29 17:12:07 +02:00
|
|
|
target = cmp::max(min_difficulty, target);
|
2016-09-28 14:21:59 +02:00
|
|
|
if header.number() < self.ethash_params.bomb_defuse_transition {
|
2016-11-04 23:09:23 +01:00
|
|
|
if header.number() < self.ethash_params.ecip1010_pause_transition {
|
2017-09-15 21:07:54 +02:00
|
|
|
let mut number = header.number();
|
|
|
|
if number >= self.ethash_params.eip649_transition {
|
|
|
|
number = number.saturating_sub(self.ethash_params.eip649_delay);
|
|
|
|
}
|
|
|
|
let period = (number / EXP_DIFF_PERIOD) as usize;
|
2016-11-04 23:09:23 +01:00
|
|
|
if period > 1 {
|
2017-07-29 17:12:07 +02:00
|
|
|
target = cmp::max(min_difficulty, target + (U256::from(1) << (period - 2)));
|
2016-11-04 23:09:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if header.number() < self.ethash_params.ecip1010_continue_transition {
|
|
|
|
let fixed_difficulty = ((self.ethash_params.ecip1010_pause_transition / EXP_DIFF_PERIOD) - 2) as usize;
|
2017-07-29 17:12:07 +02:00
|
|
|
target = cmp::max(min_difficulty, target + (U256::from(1) << fixed_difficulty));
|
2016-11-04 23:09:23 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let period = ((parent.number() + 1) / EXP_DIFF_PERIOD) as usize;
|
|
|
|
let delay = ((self.ethash_params.ecip1010_continue_transition - self.ethash_params.ecip1010_pause_transition) / EXP_DIFF_PERIOD) as usize;
|
2017-07-29 17:12:07 +02:00
|
|
|
target = cmp::max(min_difficulty, target + (U256::from(1) << (period - delay - 2)));
|
2016-09-28 14:21:59 +02:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
2017-09-26 14:19:08 +02:00
|
|
|
/// Get the nonce field of the header.
|
2016-03-01 00:02:48 +01:00
|
|
|
pub fn nonce(&self) -> H64 {
|
2016-09-01 14:29:59 +02:00
|
|
|
rlp::decode(&self.seal()[1])
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
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-09-01 14:29:59 +02:00
|
|
|
rlp::decode(&self.seal()[0])
|
2016-01-17 12:00:34 +01:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
}
|
2016-03-01 01:15:00 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn ecip1017_eras_block_reward(era_rounds: u64, mut reward: U256, block_number:u64) -> (u64, U256) {
|
|
|
|
let eras = if block_number != 0 && block_number % era_rounds == 0 {
|
|
|
|
block_number / era_rounds - 1
|
|
|
|
} else {
|
|
|
|
block_number / era_rounds
|
|
|
|
};
|
2017-11-16 05:34:04 +01:00
|
|
|
let mut divi = U256::from(1);
|
2017-09-26 14:19:08 +02:00
|
|
|
for _ in 0..eras {
|
2017-11-16 05:25:05 +01:00
|
|
|
reward = reward * U256::from(4);
|
2017-11-16 05:34:04 +01:00
|
|
|
divi = divi * U256::from(5);
|
2017-11-16 05:25:05 +01:00
|
|
|
}
|
2017-11-16 05:34:04 +01:00
|
|
|
reward = reward / divi;
|
2017-09-26 14:19:08 +02:00
|
|
|
(eras, reward)
|
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 {
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::str::FromStr;
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::{H64, H256};
|
2016-10-24 18:35:25 +02:00
|
|
|
use util::*;
|
2016-02-08 21:07:14 +01:00
|
|
|
use block::*;
|
|
|
|
use tests::helpers::*;
|
2016-10-24 18:35:25 +02:00
|
|
|
use error::{BlockError, Error};
|
|
|
|
use header::Header;
|
2017-07-10 12:57:40 +02:00
|
|
|
use spec::Spec;
|
2017-10-08 18:17:59 +02:00
|
|
|
use super::super::{new_morden, new_mcip3_test, new_homestead_test_machine};
|
2017-09-26 14:19:08 +02:00
|
|
|
use super::{Ethash, EthashParams, ecip1017_eras_block_reward};
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp;
|
2016-02-08 21:07:14 +01:00
|
|
|
|
2017-07-10 12:57:40 +02:00
|
|
|
fn test_spec() -> Spec {
|
|
|
|
new_morden(&::std::env::temp_dir())
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
#[test]
|
|
|
|
fn on_close_block() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = test_spec();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2016-04-09 19:20:35 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2017-06-28 13:17:36 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2016-02-08 21:07:14 +01:00
|
|
|
let b = b.close();
|
2017-02-26 13:10:50 +01:00
|
|
|
assert_eq!(b.state().balance(&Address::zero()).unwrap(), U256::from_str("4563918244f40000").unwrap());
|
2016-02-08 21:07:14 +01:00
|
|
|
}
|
2016-01-09 19:10:05 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
#[test]
|
|
|
|
fn has_valid_ecip1017_eras_block_reward() {
|
|
|
|
let eras_rounds = 5000000;
|
|
|
|
|
|
|
|
let start_reward: U256 = "4563918244F40000".parse().unwrap();
|
|
|
|
|
|
|
|
let block_number = 0;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
|
|
|
assert_eq!(0, eras);
|
|
|
|
assert_eq!(U256::from_str("4563918244F40000").unwrap(), reward);
|
|
|
|
|
|
|
|
let block_number = 5000000;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
|
|
|
assert_eq!(0, eras);
|
|
|
|
assert_eq!(U256::from_str("4563918244F40000").unwrap(), reward);
|
|
|
|
|
|
|
|
let block_number = 10000000;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
|
|
|
assert_eq!(1, eras);
|
|
|
|
assert_eq!(U256::from_str("3782DACE9D900000").unwrap(), reward);
|
|
|
|
|
|
|
|
let block_number = 20000000;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
|
|
|
assert_eq!(3, eras);
|
|
|
|
assert_eq!(U256::from_str("2386F26FC1000000").unwrap(), reward);
|
|
|
|
|
|
|
|
let block_number = 80000000;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
|
|
|
assert_eq!(15, eras);
|
|
|
|
assert_eq!(U256::from_str("271000000000000").unwrap(), reward);
|
2017-12-05 15:57:45 +01:00
|
|
|
|
2017-11-24 12:31:48 +01:00
|
|
|
let block_number = 250000000;
|
|
|
|
let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
|
2017-11-24 13:17:26 +01:00
|
|
|
assert_eq!(49, eras);
|
|
|
|
assert_eq!(U256::from_str("51212FFBAF0A").unwrap(), reward);
|
2017-09-26 14:19:08 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
#[test]
|
|
|
|
fn on_close_block_with_uncle() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = test_spec();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2016-04-09 19:20:35 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2017-06-28 13:17:36 +02:00
|
|
|
let mut b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2016-02-08 21:07:14 +01:00
|
|
|
let mut uncle = Header::new();
|
2016-08-08 11:18:48 +02:00
|
|
|
let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
|
2016-08-29 11:35:24 +02:00
|
|
|
uncle.set_author(uncle_author);
|
2016-02-08 21:07:14 +01:00
|
|
|
b.push_uncle(uncle).unwrap();
|
|
|
|
|
|
|
|
let b = b.close();
|
2017-02-26 13:10:50 +01:00
|
|
|
assert_eq!(b.state().balance(&Address::zero()).unwrap(), "478eae0e571ba000".into());
|
|
|
|
assert_eq!(b.state().balance(&uncle_author).unwrap(), "3cb71f51fc558000".into());
|
2016-02-08 21:07:14 +01:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:17:59 +02:00
|
|
|
#[test]
|
|
|
|
fn has_valid_mcip3_era_block_rewards() {
|
|
|
|
let spec = new_mcip3_test();
|
|
|
|
let engine = &*spec.engine;
|
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
|
|
|
let b = b.close();
|
|
|
|
|
|
|
|
let ubi_contract: Address = "00efdd5883ec628983e9063c7d969fe268bbf310".into();
|
|
|
|
let dev_contract: Address = "00756cf8159095948496617f5fb17ed95059f536".into();
|
|
|
|
assert_eq!(b.state().balance(&Address::zero()).unwrap(), U256::from_str("d8d726b7177a80000").unwrap());
|
|
|
|
assert_eq!(b.state().balance(&ubi_contract).unwrap(), U256::from_str("2b5e3af16b1880000").unwrap());
|
|
|
|
assert_eq!(b.state().balance(&dev_contract).unwrap(), U256::from_str("c249fdd327780000").unwrap());
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:07:14 +01:00
|
|
|
#[test]
|
|
|
|
fn has_valid_metadata() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-08 21:07:14 +01:00
|
|
|
assert!(!engine.name().is_empty());
|
|
|
|
assert!(engine.version().major >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_return_schedule() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2017-04-19 14:30:00 +02:00
|
|
|
let schedule = engine.schedule(10000000);
|
2016-02-08 21:07:14 +01:00
|
|
|
assert!(schedule.stack_limit > 0);
|
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
let schedule = engine.schedule(100);
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-08 21:07:14 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_basic(&header);
|
2016-02-08 21:07:14 +01:00
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-08 21:43:53 +01:00
|
|
|
let mut header: Header = Header::default();
|
2017-06-28 14:16:53 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).into_vec(), rlp::encode(&H64::zero()).into_vec()]);
|
2016-02-08 21:43:53 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_basic(&header);
|
2016-02-08 21:43:53 +01:00
|
|
|
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-08 22:54:33 +01:00
|
|
|
let mut header: Header = Header::default();
|
2017-06-28 14:16:53 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).into_vec(), rlp::encode(&H64::zero()).into_vec()]);
|
2016-02-08 22:54:33 +01:00
|
|
|
header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_basic(&header);
|
2016-02-08 22:54:33 +01:00
|
|
|
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-09 12:23:35 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_unordered(&header);
|
2016-02-09 12:23:35 +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-09 12:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_do_seal256_verification_fail() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-09 12:23:35 +01:00
|
|
|
let mut header: Header = Header::default();
|
2017-06-28 14:16:53 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H256::zero()).into_vec(), rlp::encode(&H64::zero()).into_vec()]);
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_unordered(&header);
|
2016-02-09 12:23:35 +01:00
|
|
|
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-09 12:58:32 +01:00
|
|
|
let mut header: Header = Header::default();
|
2017-06-28 14:16:53 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H256::from("b251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d")).into_vec(), rlp::encode(&H64::zero()).into_vec()]);
|
2016-02-09 12:58:32 +01:00
|
|
|
header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_unordered(&header);
|
2016-02-09 12:58:32 +01:00
|
|
|
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().engine;
|
2016-02-09 13:20:18 +01:00
|
|
|
let header: Header = Header::default();
|
|
|
|
let parent_header: Header = Header::default();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_family(&header, &parent_header);
|
2016-02-09 13:20:18 +01:00
|
|
|
|
|
|
|
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() {
|
2017-07-10 12:57:40 +02:00
|
|
|
let engine = test_spec().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);
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_family(&header, &parent_header);
|
2016-02-09 13:20:18 +01:00
|
|
|
|
|
|
|
match verify_result {
|
|
|
|
Err(Error::Block(BlockError::InvalidDifficulty(_))) => {},
|
|
|
|
Err(_) => { panic!("should be invalid difficulty fail (got {:?})", verify_result); },
|
|
|
|
_ => { panic!("Should be error, got Ok"); },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-04 23:09:23 +01:00
|
|
|
#[test]
|
|
|
|
fn difficulty_frontier() {
|
2017-09-26 14:19:08 +02:00
|
|
|
let machine = new_homestead_test_machine();
|
2016-11-04 23:09:23 +01:00
|
|
|
let ethparams = get_default_ethash_params();
|
2017-09-26 14:19:08 +02:00
|
|
|
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
|
2016-11-04 23:09:23 +01:00
|
|
|
|
|
|
|
let mut parent_header = Header::default();
|
|
|
|
parent_header.set_number(1000000);
|
|
|
|
parent_header.set_difficulty(U256::from_str("b69de81a22b").unwrap());
|
|
|
|
parent_header.set_timestamp(1455404053);
|
|
|
|
let mut header = Header::default();
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(1455404058);
|
|
|
|
|
|
|
|
let difficulty = ethash.calculate_difficulty(&header, &parent_header);
|
|
|
|
assert_eq!(U256::from_str("b6b4bbd735f").unwrap(), difficulty);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn difficulty_homestead() {
|
2017-09-26 14:19:08 +02:00
|
|
|
let machine = new_homestead_test_machine();
|
2016-11-04 23:09:23 +01:00
|
|
|
let ethparams = get_default_ethash_params();
|
2017-09-26 14:19:08 +02:00
|
|
|
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
|
2016-11-04 23:09:23 +01:00
|
|
|
|
|
|
|
let mut parent_header = Header::default();
|
|
|
|
parent_header.set_number(1500000);
|
|
|
|
parent_header.set_difficulty(U256::from_str("1fd0fd70792b").unwrap());
|
|
|
|
parent_header.set_timestamp(1463003133);
|
|
|
|
let mut header = Header::default();
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(1463003177);
|
|
|
|
|
|
|
|
let difficulty = ethash.calculate_difficulty(&header, &parent_header);
|
|
|
|
assert_eq!(U256::from_str("1fc50f118efe").unwrap(), difficulty);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn difficulty_classic_bomb_delay() {
|
2017-09-26 14:19:08 +02:00
|
|
|
let machine = new_homestead_test_machine();
|
2016-11-04 23:09:23 +01:00
|
|
|
let ethparams = EthashParams {
|
|
|
|
ecip1010_pause_transition: 3000000,
|
|
|
|
..get_default_ethash_params()
|
|
|
|
};
|
2017-09-26 14:19:08 +02:00
|
|
|
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
|
2016-11-04 23:09:23 +01:00
|
|
|
|
|
|
|
let mut parent_header = Header::default();
|
|
|
|
parent_header.set_number(3500000);
|
|
|
|
parent_header.set_difficulty(U256::from_str("6F62EAF8D3C").unwrap());
|
|
|
|
parent_header.set_timestamp(1452838500);
|
|
|
|
let mut header = Header::default();
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 20);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("6F55FE9B74B").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 5);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("6F71D75632D").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 80);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("6F02746B3A5").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_difficulty_bomb_continue() {
|
2017-09-26 14:19:08 +02:00
|
|
|
let machine = new_homestead_test_machine();
|
2016-11-04 23:09:23 +01:00
|
|
|
let ethparams = EthashParams {
|
|
|
|
ecip1010_pause_transition: 3000000,
|
|
|
|
ecip1010_continue_transition: 5000000,
|
|
|
|
..get_default_ethash_params()
|
|
|
|
};
|
2017-09-26 14:19:08 +02:00
|
|
|
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
|
2016-11-04 23:09:23 +01:00
|
|
|
|
|
|
|
let mut parent_header = Header::default();
|
|
|
|
parent_header.set_number(5000102);
|
|
|
|
parent_header.set_difficulty(U256::from_str("14944397EE8B").unwrap());
|
|
|
|
parent_header.set_timestamp(1513175023);
|
|
|
|
let mut header = Header::default();
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 6);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("1496E6206188").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
parent_header.set_number(5100123);
|
|
|
|
parent_header.set_difficulty(U256::from_str("14D24B39C7CF").unwrap());
|
|
|
|
parent_header.set_timestamp(1514609324);
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 41);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("14CA9C5D9227").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
parent_header.set_number(6150001);
|
|
|
|
parent_header.set_difficulty(U256::from_str("305367B57227").unwrap());
|
|
|
|
parent_header.set_timestamp(1529664575);
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 105);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("309D09E0C609").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
parent_header.set_number(8000000);
|
|
|
|
parent_header.set_difficulty(U256::from_str("1180B36D4CE5B6A").unwrap());
|
|
|
|
parent_header.set_timestamp(1535431724);
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(parent_header.timestamp() + 420);
|
|
|
|
assert_eq!(
|
|
|
|
U256::from_str("5126FFD5BCBB9E7").unwrap(),
|
|
|
|
ethash.calculate_difficulty(&header, &parent_header)
|
|
|
|
);
|
|
|
|
}
|
2017-01-16 14:29:51 +01:00
|
|
|
|
2017-02-22 18:24:22 +01:00
|
|
|
#[test]
|
|
|
|
fn difficulty_max_timestamp() {
|
2017-09-26 14:19:08 +02:00
|
|
|
let machine = new_homestead_test_machine();
|
2017-02-22 18:24:22 +01:00
|
|
|
let ethparams = get_default_ethash_params();
|
2017-09-26 14:19:08 +02:00
|
|
|
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
|
2017-02-22 18:24:22 +01:00
|
|
|
|
|
|
|
let mut parent_header = Header::default();
|
|
|
|
parent_header.set_number(1000000);
|
|
|
|
parent_header.set_difficulty(U256::from_str("b69de81a22b").unwrap());
|
|
|
|
parent_header.set_timestamp(1455404053);
|
|
|
|
let mut header = Header::default();
|
|
|
|
header.set_number(parent_header.number() + 1);
|
|
|
|
header.set_timestamp(u64::max_value());
|
|
|
|
|
|
|
|
let difficulty = ethash.calculate_difficulty(&header, &parent_header);
|
|
|
|
assert_eq!(U256::from(12543204905719u64), difficulty);
|
|
|
|
}
|
2016-01-14 22:38:49 +01:00
|
|
|
}
|