From b96c802c7a7a9c816dfb5facef588272acbd354d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Thu, 9 Aug 2018 13:03:53 +0100 Subject: [PATCH 1/7] ethcore: fix pow difficulty validation --- ethcore/src/ethereum/ethash.rs | 24 +++++++++++++++++++----- miner/src/work_notify.rs | 6 ++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index ca19983d3e1..fc682492454 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -21,7 +21,7 @@ use std::sync::Arc; use hash::{KECCAK_EMPTY_LIST_RLP}; use engines::block_reward::{self, RewardKind}; use ethash::{quick_get_difficulty, slow_hash_block_number, EthashManager, OptimizeFor}; -use ethereum_types::{H256, H64, U256, Address}; +use ethereum_types::{H256, H64, U512, U256, Address}; use unexpected::{OutOfBounds, Mismatch}; use block::*; use error::{BlockError, Error}; @@ -450,11 +450,12 @@ impl Ethash { /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. pub fn boundary_to_difficulty(boundary: &H256) -> U256 { - let d = U256::from(*boundary); - if d <= U256::one() { + let d = U512::from(&**boundary); + if d <= U512::one() { U256::max_value() } else { - ((U256::one() << 255) / d) << 1 + // d > 1, so result should never overflow 256 bits + U256::from((U512::one() << 256) / d) } } @@ -463,7 +464,9 @@ impl Ethash { if *difficulty <= U256::one() { U256::max_value().into() } else { - (((U256::one() << 255) / *difficulty) << 1).into() + let d = U512::from(difficulty); + // d > 1, so result should never overflow 256 bits + U256::from((U512::one() << 256) / d).into() } } } @@ -776,6 +779,17 @@ mod tests { assert_eq!(Ethash::difficulty_to_boundary(&U256::from(32)), H256::from_str("0800000000000000000000000000000000000000000000000000000000000000").unwrap()); } + #[test] + fn test_difficulty_to_boundary_regression() { + // the last bit was originally being truncated when performing the conversion + for difficulty in 1..9 { + assert_eq!(U256::from(difficulty), Ethash::boundary_to_difficulty(&Ethash::difficulty_to_boundary(&difficulty.into()))); + assert_eq!(H256::from(difficulty), Ethash::difficulty_to_boundary(&Ethash::boundary_to_difficulty(&difficulty.into()))); + assert_eq!(U256::from(difficulty), Ethash::boundary_to_difficulty(&Ethash::boundary_to_difficulty(&difficulty.into()).into())); + assert_eq!(H256::from(difficulty), Ethash::difficulty_to_boundary(&Ethash::difficulty_to_boundary(&difficulty.into()).into())); + } + } + #[test] fn difficulty_frontier() { let machine = new_homestead_test_machine(); diff --git a/miner/src/work_notify.rs b/miner/src/work_notify.rs index efae26ff114..f360ace25c5 100644 --- a/miner/src/work_notify.rs +++ b/miner/src/work_notify.rs @@ -28,7 +28,7 @@ use self::ethash::SeedHashCompute; use self::url::Url; use self::hyper::header::ContentType; -use ethereum_types::{H256, U256}; +use ethereum_types::{H256, U256, U512}; use parking_lot::Mutex; use futures::Future; @@ -72,7 +72,9 @@ fn difficulty_to_boundary(difficulty: &U256) -> H256 { if *difficulty <= U256::one() { U256::max_value().into() } else { - (((U256::one() << 255) / *difficulty) << 1).into() + let d = U512::from(difficulty); + // d > 1, so result should never overflow 256 bits + U256::from((U512::one() << 256) / d).into() } } From 1cec79bcf91d1e4747dda2aa7b56659aca868ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Thu, 9 Aug 2018 20:37:56 +0100 Subject: [PATCH 2/7] ethcore: validate difficulty is not zero --- ethcore/src/ethereum/ethash.rs | 8 ++++++-- json/src/spec/ethash.rs | 1 + miner/src/work_notify.rs | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index fc682492454..69e5e37a349 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -450,8 +450,10 @@ impl Ethash { /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. pub fn boundary_to_difficulty(boundary: &H256) -> U256 { + assert!(!boundary.is_zero()); + let d = U512::from(&**boundary); - if d <= U512::one() { + if d == U512::one() { U256::max_value() } else { // d > 1, so result should never overflow 256 bits @@ -461,7 +463,9 @@ impl Ethash { /// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. pub fn difficulty_to_boundary(difficulty: &U256) -> H256 { - if *difficulty <= U256::one() { + assert!(!difficulty.is_zero()); + + if *difficulty == U256::one() { U256::max_value().into() } else { let d = U512::from(difficulty); diff --git a/json/src/spec/ethash.rs b/json/src/spec/ethash.rs index 19fd0966273..fd6b9fca59f 100644 --- a/json/src/spec/ethash.rs +++ b/json/src/spec/ethash.rs @@ -24,6 +24,7 @@ use hash::Address; pub struct EthashParams { /// See main EthashParams docs. #[serde(rename="minimumDifficulty")] + #[serde(deserialize_with="uint::validate_non_zero")] pub minimum_difficulty: Uint, /// See main EthashParams docs. #[serde(rename="difficultyBoundDivisor")] diff --git a/miner/src/work_notify.rs b/miner/src/work_notify.rs index f360ace25c5..4e1655508ee 100644 --- a/miner/src/work_notify.rs +++ b/miner/src/work_notify.rs @@ -69,7 +69,9 @@ impl WorkPoster { /// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. fn difficulty_to_boundary(difficulty: &U256) -> H256 { - if *difficulty <= U256::one() { + assert!(!difficulty.is_zero()); + + if *difficulty == U256::one() { U256::max_value().into() } else { let d = U512::from(difficulty); From 7c6f350bc6f9c5dfdaca22c9f5071047a1e7f791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Thu, 9 Aug 2018 20:39:39 +0100 Subject: [PATCH 3/7] ethcore: add issue link to regression test --- ethcore/src/ethereum/ethash.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index 69e5e37a349..4c29adfbbcb 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -786,6 +786,7 @@ mod tests { #[test] fn test_difficulty_to_boundary_regression() { // the last bit was originally being truncated when performing the conversion + // https://github.com/paritytech/parity-ethereum/issues/8397 for difficulty in 1..9 { assert_eq!(U256::from(difficulty), Ethash::boundary_to_difficulty(&Ethash::difficulty_to_boundary(&difficulty.into()))); assert_eq!(H256::from(difficulty), Ethash::difficulty_to_boundary(&Ethash::boundary_to_difficulty(&difficulty.into()))); From 06d763b61949a87c27aa6fb21a1fa87da612b115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Thu, 9 Aug 2018 20:51:14 +0100 Subject: [PATCH 4/7] ethcore: fix tests --- ethcore/src/ethereum/ethash.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index 4c29adfbbcb..e31e94d7737 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -775,8 +775,6 @@ mod tests { #[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()); @@ -795,6 +793,18 @@ mod tests { } } + #[test] + #[should_panic] + fn test_difficulty_to_boundary_panics_on_zero() { + Ethash::difficulty_to_boundary(&U256::from(0)); + } + + #[test] + #[should_panic] + fn test_boundary_to_difficulty_panics_on_zero() { + Ethash::boundary_to_difficulty(&H256::from(0)); + } + #[test] fn difficulty_frontier() { let machine = new_homestead_test_machine(); From 359ddcace59b8e4fce661ac82e8587244ac70402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Thu, 9 Aug 2018 23:13:50 +0100 Subject: [PATCH 5/7] ethcore: move difficulty_to_boundary to ethash crate --- Cargo.lock | 1 + ethash/Cargo.toml | 11 +++--- ethash/src/lib.rs | 69 +++++++++++++++++++++++++++++++++- ethcore/src/ethereum/ethash.rs | 66 ++------------------------------ ethcore/src/miner/stratum.rs | 5 +-- miner/src/work_notify.rs | 17 +-------- rpc/src/v1/impls/eth.rs | 5 +-- 7 files changed, 84 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1760b3b922c..8f36587f698 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -470,6 +470,7 @@ version = "1.12.0" dependencies = [ "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethereum-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "keccak-hash 0.1.2 (git+https://github.com/paritytech/parity-common)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ethash/Cargo.toml b/ethash/Cargo.toml index df0f17e0f37..d127ef4ac66 100644 --- a/ethash/Cargo.toml +++ b/ethash/Cargo.toml @@ -6,13 +6,14 @@ authors = ["Parity Technologies "] [lib] [dependencies] -log = "0.3" -keccak-hash = { git = "https://github.com/paritytech/parity-common" } -primal = "0.2.3" -parking_lot = "0.6" crunchy = "0.1.0" -memmap = "0.6" either = "1.0.0" +ethereum-types = "0.3" +keccak-hash = { git = "https://github.com/paritytech/parity-common" } +log = "0.3" +memmap = "0.6" +parking_lot = "0.6" +primal = "0.2.3" [dev-dependencies] tempdir = "0.3" diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index 69b5a1d1155..e4adeb09d52 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -16,10 +16,11 @@ #![cfg_attr(feature = "benches", feature(test))] -extern crate primal; -extern crate parking_lot; extern crate either; +extern crate ethereum_types; extern crate memmap; +extern crate parking_lot; +extern crate primal; #[macro_use] extern crate crunchy; @@ -38,6 +39,7 @@ mod shared; pub use cache::{NodeCacheBuilder, OptimizeFor}; pub use compute::{ProofOfWork, quick_get_difficulty, slow_hash_block_number}; use compute::Light; +use ethereum_types::{U256, U512}; use keccak::H256; use parking_lot::Mutex; pub use seed_compute::SeedHashCompute; @@ -136,6 +138,32 @@ impl EthashManager { } } +/// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. +pub fn boundary_to_difficulty(boundary: ðereum_types::H256) -> U256 { + assert!(!boundary.is_zero()); + + let d = U512::from(&**boundary); + if d == U512::one() { + U256::max_value() + } else { + // d > 1, so result should never overflow 256 bits + U256::from((U512::one() << 256) / d) + } +} + +/// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. +pub fn difficulty_to_boundary(difficulty: &U256) -> ethereum_types::H256 { + assert!(!difficulty.is_zero()); + + if *difficulty == U256::one() { + U256::max_value().into() + } else { + let d = U512::from(difficulty); + // d > 1, so result should never overflow 256 bits + U256::from((U512::one() << 256) / d).into() + } +} + #[test] fn test_lru() { use tempdir::TempDir; @@ -155,6 +183,43 @@ fn test_lru() { assert_eq!(ethash.cache.lock().prev_epoch.unwrap(), 0); } +#[test] +fn test_difficulty_to_boundary() { + use ethereum_types::H256; + use std::str::FromStr; + + assert_eq!(difficulty_to_boundary(&U256::from(1)), H256::from(U256::max_value())); + assert_eq!(difficulty_to_boundary(&U256::from(2)), H256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap()); + assert_eq!(difficulty_to_boundary(&U256::from(4)), H256::from_str("4000000000000000000000000000000000000000000000000000000000000000").unwrap()); + assert_eq!(difficulty_to_boundary(&U256::from(32)), H256::from_str("0800000000000000000000000000000000000000000000000000000000000000").unwrap()); +} + +#[test] +fn test_difficulty_to_boundary_regression() { + use ethereum_types::H256; + + // the last bit was originally being truncated when performing the conversion + // https://github.com/paritytech/parity-ethereum/issues/8397 + for difficulty in 1..9 { + assert_eq!(U256::from(difficulty), boundary_to_difficulty(&difficulty_to_boundary(&difficulty.into()))); + assert_eq!(H256::from(difficulty), difficulty_to_boundary(&boundary_to_difficulty(&difficulty.into()))); + assert_eq!(U256::from(difficulty), boundary_to_difficulty(&boundary_to_difficulty(&difficulty.into()).into())); + assert_eq!(H256::from(difficulty), difficulty_to_boundary(&difficulty_to_boundary(&difficulty.into()).into())); + } +} + +#[test] +#[should_panic] +fn test_difficulty_to_boundary_panics_on_zero() { + difficulty_to_boundary(&U256::from(0)); +} + +#[test] +#[should_panic] +fn test_boundary_to_difficulty_panics_on_zero() { + boundary_to_difficulty(ðereum_types::H256::from(0)); +} + #[cfg(feature = "benches")] mod benchmarks { extern crate test; diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index e31e94d7737..16069c32751 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -20,8 +20,8 @@ use std::collections::BTreeMap; use std::sync::Arc; use hash::{KECCAK_EMPTY_LIST_RLP}; use engines::block_reward::{self, RewardKind}; -use ethash::{quick_get_difficulty, slow_hash_block_number, EthashManager, OptimizeFor}; -use ethereum_types::{H256, H64, U512, U256, Address}; +use ethash::{self, quick_get_difficulty, slow_hash_block_number, EthashManager, OptimizeFor}; +use ethereum_types::{H256, H64, U256, Address}; use unexpected::{OutOfBounds, Mismatch}; use block::*; use error::{BlockError, Error}; @@ -302,7 +302,7 @@ impl Engine for Arc { return Err(From::from(BlockError::DifficultyOutOfBounds(OutOfBounds { min: Some(min_difficulty), max: None, found: header.difficulty().clone() }))) } - let difficulty = Ethash::boundary_to_difficulty(&H256(quick_get_difficulty( + let difficulty = ethash::boundary_to_difficulty(&H256(quick_get_difficulty( &header.bare_hash().0, seal.nonce.low_u64(), &seal.mix_hash.0 @@ -324,7 +324,7 @@ impl Engine for Arc { let result = self.pow.compute_light(header.number() as u64, &header.bare_hash().0, seal.nonce.low_u64()); let mix = H256(result.mix_hash); - let difficulty = Ethash::boundary_to_difficulty(&H256(result.value)); + let difficulty = ethash::boundary_to_difficulty(&H256(result.value)); trace!(target: "miner", "num: {num}, seed: {seed}, h: {h}, non: {non}, mix: {mix}, res: {res}", num = header.number() as u64, seed = H256(slow_hash_block_number(header.number() as u64)), @@ -447,32 +447,6 @@ impl Ethash { } target } - - /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. - pub fn boundary_to_difficulty(boundary: &H256) -> U256 { - assert!(!boundary.is_zero()); - - let d = U512::from(&**boundary); - if d == U512::one() { - U256::max_value() - } else { - // d > 1, so result should never overflow 256 bits - U256::from((U512::one() << 256) / d) - } - } - - /// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. - pub fn difficulty_to_boundary(difficulty: &U256) -> H256 { - assert!(!difficulty.is_zero()); - - if *difficulty == U256::one() { - U256::max_value().into() - } else { - let d = U512::from(difficulty); - // d > 1, so result should never overflow 256 bits - U256::from((U512::one() << 256) / d).into() - } - } } fn ecip1017_eras_block_reward(era_rounds: u64, mut reward: U256, block_number:u64) -> (u64, U256) { @@ -773,38 +747,6 @@ mod tests { } } - #[test] - fn test_difficulty_to_boundary() { - 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()); - } - - #[test] - fn test_difficulty_to_boundary_regression() { - // the last bit was originally being truncated when performing the conversion - // https://github.com/paritytech/parity-ethereum/issues/8397 - for difficulty in 1..9 { - assert_eq!(U256::from(difficulty), Ethash::boundary_to_difficulty(&Ethash::difficulty_to_boundary(&difficulty.into()))); - assert_eq!(H256::from(difficulty), Ethash::difficulty_to_boundary(&Ethash::boundary_to_difficulty(&difficulty.into()))); - assert_eq!(U256::from(difficulty), Ethash::boundary_to_difficulty(&Ethash::boundary_to_difficulty(&difficulty.into()).into())); - assert_eq!(H256::from(difficulty), Ethash::difficulty_to_boundary(&Ethash::difficulty_to_boundary(&difficulty.into()).into())); - } - } - - #[test] - #[should_panic] - fn test_difficulty_to_boundary_panics_on_zero() { - Ethash::difficulty_to_boundary(&U256::from(0)); - } - - #[test] - #[should_panic] - fn test_boundary_to_difficulty_panics_on_zero() { - Ethash::boundary_to_difficulty(&H256::from(0)); - } - #[test] fn difficulty_frontier() { let machine = new_homestead_test_machine(); diff --git a/ethcore/src/miner/stratum.rs b/ethcore/src/miner/stratum.rs index ca74432790f..38c881bb1b3 100644 --- a/ethcore/src/miner/stratum.rs +++ b/ethcore/src/miner/stratum.rs @@ -22,8 +22,7 @@ use std::fmt; use client::{Client, ImportSealedBlock}; use ethereum_types::{H64, H256, clean_0x, U256}; -use ethereum::ethash::Ethash; -use ethash::SeedHashCompute; +use ethash::{self, SeedHashCompute}; #[cfg(feature = "work-notify")] use ethcore_miner::work_notify::NotifyWork; #[cfg(feature = "work-notify")] @@ -167,7 +166,7 @@ impl StratumJobDispatcher { /// Serializes payload for stratum service fn payload(&self, pow_hash: H256, difficulty: U256, number: u64) -> String { // TODO: move this to engine - let target = Ethash::difficulty_to_boundary(&difficulty); + let target = ethash::difficulty_to_boundary(&difficulty); let seed_hash = &self.seed_compute.lock().hash_block_number(number); let seed_hash = H256::from_slice(&seed_hash[..]); format!( diff --git a/miner/src/work_notify.rs b/miner/src/work_notify.rs index 4e1655508ee..52290198256 100644 --- a/miner/src/work_notify.rs +++ b/miner/src/work_notify.rs @@ -28,7 +28,7 @@ use self::ethash::SeedHashCompute; use self::url::Url; use self::hyper::header::ContentType; -use ethereum_types::{H256, U256, U512}; +use ethereum_types::{H256, U256}; use parking_lot::Mutex; use futures::Future; @@ -67,23 +67,10 @@ impl WorkPoster { } } -/// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. -fn difficulty_to_boundary(difficulty: &U256) -> H256 { - assert!(!difficulty.is_zero()); - - if *difficulty == U256::one() { - U256::max_value().into() - } else { - let d = U512::from(difficulty); - // d > 1, so result should never overflow 256 bits - U256::from((U512::one() << 256) / d).into() - } -} - impl NotifyWork for WorkPoster { fn notify(&self, pow_hash: H256, difficulty: U256, number: u64) { // TODO: move this to engine - let target = difficulty_to_boundary(&difficulty); + let target = ethash::difficulty_to_boundary(&difficulty); let seed_hash = &self.seed_compute.lock().hash_block_number(number); let seed_hash = H256::from_slice(&seed_hash[..]); let body = format!( diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 1e4ad87a554..67dc640d84d 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -24,10 +24,9 @@ use rlp::{self, Rlp}; use ethereum_types::{U256, H64, H256, Address}; use parking_lot::Mutex; -use ethash::SeedHashCompute; +use ethash::{self, SeedHashCompute}; use ethcore::account_provider::AccountProvider; use ethcore::client::{BlockChainClient, BlockId, TransactionId, UncleId, StateOrBlock, StateClient, StateInfo, Call, EngineInfo}; -use ethcore::ethereum::Ethash; use ethcore::filter::Filter as EthcoreFilter; use ethcore::header::{BlockNumber as EthBlockNumber}; use ethcore::log_entry::LogEntry; @@ -758,7 +757,7 @@ impl Eth for EthClient< })?; let (pow_hash, number, timestamp, difficulty) = work; - let target = Ethash::difficulty_to_boundary(&difficulty); + let target = ethash::difficulty_to_boundary(&difficulty); let seed_hash = self.seed_compute.lock().hash_block_number(number); let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(); From 1a9889c08684cc9742610dd3fd1368b70eb0ad9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Fri, 10 Aug 2018 10:55:31 +0100 Subject: [PATCH 6/7] ethcore: reuse difficulty_to_boundary and boundary_to_difficulty --- ethash/src/lib.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index e4adeb09d52..2d0fd6410bb 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -140,27 +140,24 @@ impl EthashManager { /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. pub fn boundary_to_difficulty(boundary: ðereum_types::H256) -> U256 { - assert!(!boundary.is_zero()); - - let d = U512::from(&**boundary); - if d == U512::one() { - U256::max_value() - } else { - // d > 1, so result should never overflow 256 bits - U256::from((U512::one() << 256) / d) - } + difficulty_to_boundary_aux(&**boundary) } /// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. pub fn difficulty_to_boundary(difficulty: &U256) -> ethereum_types::H256 { + difficulty_to_boundary_aux(difficulty).into() +} + +fn difficulty_to_boundary_aux>(difficulty: T) -> ethereum_types::U256 { + let difficulty = difficulty.into(); + assert!(!difficulty.is_zero()); - if *difficulty == U256::one() { + if difficulty == U512::one() { U256::max_value().into() } else { - let d = U512::from(difficulty); // d > 1, so result should never overflow 256 bits - U256::from((U512::one() << 256) / d).into() + U256::from((U512::one() << 256) / difficulty) } } From 4cd8cae6c010aed6d23c89179865523266aba035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Fri, 10 Aug 2018 13:52:01 +0100 Subject: [PATCH 7/7] ethcore: fix grumbles in difficulty_to_boundary_aux --- ethash/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index 2d0fd6410bb..29361ad5c51 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -154,9 +154,9 @@ fn difficulty_to_boundary_aux>(difficulty: T) -> ethereum_types::U assert!(!difficulty.is_zero()); if difficulty == U512::one() { - U256::max_value().into() + U256::max_value() } else { - // d > 1, so result should never overflow 256 bits + // difficulty > 1, so result should never overflow 256 bits U256::from((U512::one() << 256) / difficulty) } }