Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: possible overflow in difficulty calculation (fixes #3923) #4090

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion base_layer/core/src/proof_of_work/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +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());
result.low_u64().into()
}

Expand All @@ -131,7 +132,7 @@ pub mod util {
}
#[cfg(test)]
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
mod test {
use crate::proof_of_work::difficulty::Difficulty;
use crate::proof_of_work::difficulty::{Difficulty, util::big_endian_difficulty};

#[test]
fn add_difficulty() {
Expand All @@ -148,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;
CjS77 marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}