fixed #1261, overflow when calculating work (#1283)

* fixed #1261, overflow when calculating work

* tests for difficulty_to_boundary

* return !U256::zer() for boundary <= 1
This commit is contained in:
Marek Kotewicz 2016-06-15 16:33:38 +02:00 committed by Gav Wood
parent 6b852a0635
commit 549647b6f2

View File

@ -255,12 +255,21 @@ impl Ethash {
/// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`. /// Convert an Ethash boundary to its original difficulty. Basically just `f(x) = 2^256 / x`.
pub fn boundary_to_difficulty(boundary: &H256) -> U256 { pub fn boundary_to_difficulty(boundary: &H256) -> U256 {
U256::from((U512::one() << 256) / U256::from(boundary.as_slice()).into()) let d = U256::from(*boundary);
if d <= U256::one() {
U256::max_value()
} else {
((U256::one() << 255) / d) << 1
}
} }
/// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`. /// Convert an Ethash difficulty to the target boundary. Basically just `f(x) = 2^256 / x`.
pub fn difficulty_to_boundary(difficulty: &U256) -> H256 { pub fn difficulty_to_boundary(difficulty: &U256) -> H256 {
U256::from((U512::one() << 256) / difficulty.into()).into() if *difficulty <= U256::one() {
U256::max_value().into()
} else {
(((U256::one() << 255) / *difficulty) << 1).into()
}
} }
fn to_ethash(hash: H256) -> EH256 { fn to_ethash(hash: H256) -> EH256 {
@ -291,12 +300,11 @@ impl Header {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate ethash;
use common::*; use common::*;
use block::*; use block::*;
use tests::helpers::*; use tests::helpers::*;
use super::super::new_morden; use super::super::new_morden;
use super::Ethash;
#[test] #[test]
fn on_close_block() { fn on_close_block() {
@ -509,5 +517,15 @@ 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());
assert_eq!(Ethash::difficulty_to_boundary(&U256::from(32)), H256::from_str("0800000000000000000000000000000000000000000000000000000000000000").unwrap());
}
// TODO: difficulty test // TODO: difficulty test
} }