Test MAX_ATTAINABLE_* constant values against Core values
What changed, and why it matters
This commit only adds a new unit test. It does not change any production code, so it cannot introduce a security vulnerability or fix one. The test verifies that certain difficulty-target constants in the rust-bitcoin library match the values used in Bitcoin Core after a compact/lossy conversion.
No security action needed. This is a test-only change. Reviewers may optionally verify the copied Core constants and the test assertions are correct.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a single test function, target_attainable_constants_from_original, in bitcoin/src/pow.rs. It defines plain Target constants copied from Bitcoin Core’s chainparams.cpp for mainnet, testnet, regtest, and signet, then asserts that converting each to a CompactTarget via to_compact_lossy() and back via Target::from_compact() equals the library’s existing MAX_ATTAINABLE_* constants. No runtime logic is modified.
Changed components
bitcoin/src/pow.rs (tests only)Inspect captured patch +30 / −0
diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs
index 03a4fe21..95f1cea7 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -1895,6 +1895,36 @@ mod tests {
assert_eq!(got, want)
}
+ #[test]
+ fn target_attainable_constants_from_original() {
+ // The plain target values for the various nets from Bitcoin Core with no conversions.
+ // https://github.com/bitcoin/bitcoin/blob/8105bce5b384c72cf08b25b7c5343622754e7337/src/kernel/chainparams.cpp#L88
+ const MAX_MAINNET: Target = Target(U256(u128::MAX >> 32, u128::MAX));
+ // https://github.com/bitcoin/bitcoin/blob/8105bce5b384c72cf08b25b7c5343622754e7337/src/kernel/chainparams.cpp#L208
+ const MAX_TESTNET: Target = Target(U256(u128::MAX >> 32, u128::MAX));
+ // https://github.com/bitcoin/bitcoin/blob/8105bce5b384c72cf08b25b7c5343622754e7337/src/kernel/chainparams.cpp#L411
+ const MAX_REGTEST: Target = Target(U256(u128::MAX >> 1, u128::MAX));
+ // https://github.com/bitcoin/bitcoin/blob/8105bce5b384c72cf08b25b7c5343622754e7337/src/kernel/chainparams.cpp#L348
+ const MAX_SIGNET: Target = Target(U256(0x3_77aeu128 << 88, 0));
+
+ assert_eq!(
+ Target::MAX_ATTAINABLE_MAINNET,
+ Target::from_compact(MAX_MAINNET.to_compact_lossy())
+ );
+ assert_eq!(
+ Target::MAX_ATTAINABLE_TESTNET,
+ Target::from_compact(MAX_TESTNET.to_compact_lossy())
+ );
+ assert_eq!(
+ Target::MAX_ATTAINABLE_REGTEST,
+ Target::from_compact(MAX_REGTEST.to_compact_lossy())
+ );
+ assert_eq!(
+ Target::MAX_ATTAINABLE_SIGNET,
+ Target::from_compact(MAX_SIGNET.to_compact_lossy())
+ );
+ }
+
#[test]
fn target_difficulty_float() {
let params = Params::new(crate::Network::Bitcoin);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.