Files
alkane-os/tests/difficulty.rs
T
noah a2d0365813
main-release-gate / verify-and-release (push) Failing after 1m43s
Convert pool difficulty to exact share targets
2026-10-15 01:00:43 -06:00

74 lines
2.2 KiB
Rust

// SPDX-License-Identifier: MIT
use alkane_os::difficulty::{DifficultyError, PoolDifficulty};
fn target(decimal: &str) -> String {
let number = serde_json::from_str(decimal).unwrap();
PoolDifficulty::from_json_number(&number)
.unwrap()
.target_le()
.iter()
.rev()
.map(|byte| format!("{byte:02x}"))
.collect()
}
#[test]
fn exact_share_targets_match_integer_reference() {
for (difficulty, expected) in [
(
"1",
"00000000ffff0000000000000000000000000000000000000000000000000000",
),
(
"2",
"000000007fff8000000000000000000000000000000000000000000000000000",
),
(
"0.5",
"00000001fffe0000000000000000000000000000000000000000000000000000",
),
(
"1.25",
"00000000cccc0000000000000000000000000000000000000000000000000000",
),
(
"2048.5",
"00000000001ffde021fde021fde021fde021fde021fde021fde021fde021fde0",
),
(
"0.000001",
"000f4230bdc00000000000000000000000000000000000000000000000000000",
),
(
"1000000000000000000",
"000000000000000000000012724b5f7471d95d3bbefee5e602a7b17261180d8e",
),
] {
assert_eq!(target(difficulty), expected, "difficulty {difficulty}");
}
assert_eq!(target("1.2e3"), target("1200"));
assert_eq!(target("1e-19"), "ff".repeat(32));
}
#[test]
fn invalid_or_unrepresentable_difficulty_is_rejected() {
let zero = serde_json::from_str("0").unwrap();
assert_eq!(
PoolDifficulty::from_json_number(&zero),
Err(DifficultyError::Invalid)
);
let negative = serde_json::from_str("-1").unwrap();
assert_eq!(
PoolDifficulty::from_json_number(&negative),
Err(DifficultyError::OutOfRange)
);
for value in ["1e20", "1e-20", "1.00000000000000000001"] {
let number = serde_json::from_str(value).unwrap();
assert_eq!(
PoolDifficulty::from_json_number(&number),
Err(DifficultyError::OutOfRange),
"difficulty {value}"
);
}
}