From 33b9d6904c699a0cdd5f14f126b977ce2d4317d6 Mon Sep 17 00:00:00 2001 From: Shing Him Ng Date: Wed, 13 Nov 2024 08:18:23 -0600 Subject: [PATCH] Remove From and Into test impls The removed impls have been replaced with test-specific functions for converting between Rust primitives and U256 --- bitcoin/src/blockdata/block.rs | 5 ++-- bitcoin/src/pow.rs | 47 ++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index c4825b601..f6a039042 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -369,6 +369,7 @@ mod tests { use super::*; use crate::consensus::encode::{deserialize, serialize}; use crate::{CompactTarget, Network, TestnetVersion}; + use crate::pow::test_utils::{u128_to_work, u64_to_work}; #[test] fn test_coinbase_and_bip34() { @@ -422,7 +423,7 @@ mod tests { let prevhash = hex!("4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000"); let merkle = hex!("bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914c"); - let work = Work::from(0x100010001_u128); + let work = u128_to_work(0x100010001_u128); let decode: Result = deserialize(&some_block); let bad_decode: Result = deserialize(&cutoff_block); @@ -468,7 +469,7 @@ mod tests { let prevhash = hex!("2aa2f2ca794ccbd40c16e2f3333f6b8b683f9e7179b2c4d74906000000000000"); let merkle = hex!("10bc26e70a2f672ad420a6153dd0c28b40a6002c55531bfc99bf8994a8e8f67e"); - let work = Work::from(0x257c3becdacc64_u64); + let work = u64_to_work(0x257c3becdacc64_u64); assert!(decode.is_ok()); let real_decode = decode.unwrap(); diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index 30f4de2cd..8698854f2 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -1077,21 +1077,36 @@ impl kani::Arbitrary for U256 { } } +/// In test code, U256s are a pain to work with, so we just convert Rust primitives in many places +#[cfg(test)] +pub mod test_utils { + use crate::pow::{Target, Work, U256}; + + /// Converts a `u64` to a [`Work`] + pub fn u64_to_work(u: u64) -> Work { + Work(U256::from(u)) + } + + /// Converts a `u128` to a [`Work`] + pub fn u128_to_work(u: u128) -> Work { + Work(U256::from(u)) + } + + /// Converts a `u32` to a [`Target`] + pub fn u32_to_target(u: u32) -> Target { + Target(U256::from(u)) + } + + /// Converts a `u64` to a [`Target`] + pub fn u64_to_target(u: u64) -> Target { + Target(U256::from(u)) + } +} + #[cfg(test)] mod tests { use super::*; - - impl From for Target { - fn from(x: u64) -> Self { Self(U256::from(x)) } - } - - impl From for Target { - fn from(x: u32) -> Self { Self(U256::from(x)) } - } - - impl> From for Work { - fn from(x: T) -> Self { Self(U256::from(x)) } - } + use crate::pow::test_utils::{u128_to_work, u32_to_target, u64_to_target}; impl U256 { fn bit_at(&self, index: usize) -> bool { @@ -1102,9 +1117,7 @@ mod tests { let word = if index < 128 { self.1 } else { self.0 }; (word & (1 << (index % 128))) != 0 } - } - impl U256 { /// Constructs a new U256 from a big-endian array of u64's fn from_array(a: [u64; 4]) -> Self { let mut ret = U256::ZERO; @@ -1865,7 +1878,7 @@ mod tests { ]; for (n_bits, target) in tests { - let want = Target::from(target); + let want = u64_to_target(target); let got = Target::from_compact(CompactTarget::from_consensus(n_bits)); assert_eq!(got, want); } @@ -1926,7 +1939,7 @@ mod tests { #[test] fn roundtrip_target_work() { - let target = Target::from(0xdeadbeef_u32); + let target = u32_to_target(0xdeadbeef_u32); let work = target.to_work(); let back = work.to_target(); assert_eq!(back, target) @@ -1947,7 +1960,7 @@ mod tests { for &(chainwork, core_log2) in tests { // Core log2 in the logs is rounded to 6 decimal places. - let log2 = (Work::from(chainwork).log2() * 1e6).round() / 1e6; + let log2 = (u128_to_work(chainwork).log2() * 1e6).round() / 1e6; assert_eq!(log2, core_log2) }