This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ethcore: fix pow difficulty validation #9328
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b96c802
ethcore: fix pow difficulty validation
andresilva 1cec79b
ethcore: validate difficulty is not zero
andresilva 7c6f350
ethcore: add issue link to regression test
andresilva 06d763b
ethcore: fix tests
andresilva 359ddca
ethcore: move difficulty_to_boundary to ethash crate
andresilva 35a2ca4
Merge branch 'master' into andre/fix-pow-boundary
andresilva 1a9889c
ethcore: reuse difficulty_to_boundary and boundary_to_difficulty
andresilva 4cd8cae
ethcore: fix grumbles in difficulty_to_boundary_aux
andresilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,29 @@ 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 { | ||
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<T: Into<U512>>(difficulty: T) -> ethereum_types::U256 { | ||
let difficulty = difficulty.into(); | ||
|
||
assert!(!difficulty.is_zero()); | ||
|
||
if difficulty == U512::one() { | ||
U256::max_value().into() | ||
} else { | ||
// d > 1, so result should never overflow 256 bits | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: s/d/difficulty |
||
U256::from((U512::one() << 256) / difficulty) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_lru() { | ||
use tempdir::TempDir; | ||
|
@@ -155,6 +180,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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,19 +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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! I didn't know that we have it in more than 1 place |
||
if *difficulty <= U256::one() { | ||
U256::max_value().into() | ||
} else { | ||
(((U256::one() << 255) / *difficulty) << 1).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!( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we don't need that
.into()