From 0fc5f6d861e8fb3a577c01a443109f551afdf385 Mon Sep 17 00:00:00 2001 From: jorgeantonio21 Date: Wed, 11 May 2022 12:44:00 +0100 Subject: [PATCH 1/4] attempt to tackle issue #3923" --- base_layer/core/src/proof_of_work/difficulty.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base_layer/core/src/proof_of_work/difficulty.rs b/base_layer/core/src/proof_of_work/difficulty.rs index b54d22d2c7..77190dae9d 100644 --- a/base_layer/core/src/proof_of_work/difficulty.rs +++ b/base_layer/core/src/proof_of_work/difficulty.rs @@ -118,6 +118,9 @@ pub mod util { /// This will provide the difficulty of the hash assuming the hash is big_endian pub(crate) fn big_endian_difficulty(hash: &[u8]) -> Difficulty { let scalar = U256::from_big_endian(hash); // Big endian so the hash has leading zeroes + if scalar <= U256::from(2).pow(U256::from(192)) { + return u64::MAX.into(); + } let result = U256::MAX / scalar; result.low_u64().into() } From 2a0cad4f41b9ffec9d80a68c64cec50f12e52a1b Mon Sep 17 00:00:00 2001 From: jorgeantonio21 Date: Wed, 11 May 2022 14:05:37 +0100 Subject: [PATCH 2/4] add PR changes --- base_layer/core/src/proof_of_work/difficulty.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/base_layer/core/src/proof_of_work/difficulty.rs b/base_layer/core/src/proof_of_work/difficulty.rs index 77190dae9d..eb1b06f169 100644 --- a/base_layer/core/src/proof_of_work/difficulty.rs +++ b/base_layer/core/src/proof_of_work/difficulty.rs @@ -118,10 +118,8 @@ pub mod util { /// This will provide the difficulty of the hash assuming the hash is big_endian pub(crate) fn big_endian_difficulty(hash: &[u8]) -> Difficulty { let scalar = U256::from_big_endian(hash); // Big endian so the hash has leading zeroes - if scalar <= U256::from(2).pow(U256::from(192)) { - return u64::MAX.into(); - } let result = U256::MAX / scalar; + let result = result.min(u64::MAX.into()); // if difficulty is less than result.low_u64().into() } From 2dbf566f690574775bee4671d94d3855f8fb2ed9 Mon Sep 17 00:00:00 2001 From: jorgeantonio21 Date: Wed, 11 May 2022 15:54:53 +0100 Subject: [PATCH 3/4] add test coverage for difficulty overflow in big_endian_difficulty method --- .../core/src/proof_of_work/difficulty.rs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/base_layer/core/src/proof_of_work/difficulty.rs b/base_layer/core/src/proof_of_work/difficulty.rs index eb1b06f169..a6f10ba94c 100644 --- a/base_layer/core/src/proof_of_work/difficulty.rs +++ b/base_layer/core/src/proof_of_work/difficulty.rs @@ -119,7 +119,7 @@ pub mod util { pub(crate) fn big_endian_difficulty(hash: &[u8]) -> Difficulty { let scalar = U256::from_big_endian(hash); // Big endian so the hash has leading zeroes let result = U256::MAX / scalar; - let result = result.min(u64::MAX.into()); // if difficulty is less than + let result = result.min(u64::MAX.into()); result.low_u64().into() } @@ -132,7 +132,7 @@ pub mod util { } #[cfg(test)] mod test { - use crate::proof_of_work::difficulty::Difficulty; + use crate::proof_of_work::difficulty::{Difficulty, util::big_endian_difficulty}; #[test] fn add_difficulty() { @@ -149,4 +149,25 @@ mod test { let d = Difficulty::from(1_000_000); assert_eq!("1,000,000", format!("{}", d)); } + + #[test] + fn high_target() { + let target: &[u8] = &[0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]; + let expected = Difficulty::from(1); + assert_eq!(big_endian_difficulty(target), expected); + } + + #[test] + fn max_difficulty() { + let target = u64::MAX; + let expected = u64::MAX; + assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected)); + } + + #[test] + fn stop_overflow() { + let target: u64 = 64; + let expected = u64::MAX; + assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected)); + } } From cc86d35e2f156c5604db76bc0eb19677e198c11d Mon Sep 17 00:00:00 2001 From: jorgeantonio21 Date: Thu, 12 May 2022 12:11:27 +0100 Subject: [PATCH 4/4] add changes to follow PR comments --- .../core/src/proof_of_work/difficulty.rs | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/base_layer/core/src/proof_of_work/difficulty.rs b/base_layer/core/src/proof_of_work/difficulty.rs index a6f10ba94c..092825511c 100644 --- a/base_layer/core/src/proof_of_work/difficulty.rs +++ b/base_layer/core/src/proof_of_work/difficulty.rs @@ -129,10 +129,41 @@ pub mod util { let result = U256::MAX / scalar; result.low_u64().into() } + + #[cfg(test)] + mod test { + use super::*; + + #[test] + fn high_target() { + let target: &[u8] = &[ + 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + ]; + let expected = Difficulty::from(1); + assert_eq!(big_endian_difficulty(target), expected); + } + + #[test] + fn max_difficulty() { + let target = U256::MAX / U256::from(u64::MAX); + let mut bytes = [0u8; 32]; + target.to_big_endian(&mut bytes); + assert_eq!(big_endian_difficulty(&bytes), Difficulty::from(u64::MAX)); + } + + #[test] + fn stop_overflow() { + let target: u64 = 64; + let expected = u64::MAX; + assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected)); + } + } } + #[cfg(test)] mod test { - use crate::proof_of_work::difficulty::{Difficulty, util::big_endian_difficulty}; + use crate::proof_of_work::difficulty::Difficulty; #[test] fn add_difficulty() { @@ -149,25 +180,4 @@ mod test { let d = Difficulty::from(1_000_000); assert_eq!("1,000,000", format!("{}", d)); } - - #[test] - fn high_target() { - let target: &[u8] = &[0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]; - let expected = Difficulty::from(1); - assert_eq!(big_endian_difficulty(target), expected); - } - - #[test] - fn max_difficulty() { - let target = u64::MAX; - let expected = u64::MAX; - assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected)); - } - - #[test] - fn stop_overflow() { - let target: u64 = 64; - let expected = u64::MAX; - assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected)); - } }