Merge rust-bitcoin/rust-bitcoin#3612: Remove From and Into test impls and create test-specific function for converting between Rust primitives and U256
33b9d6904c
Remove From and Into test impls (Shing Him Ng) Pull request description: Created a new `test-utils` module for these functions since it needs to be visible for tests in the `block` module that were also using the previous `From` and `Into` impls. Fixes #3160 ACKs for top commit: apoelstra: ACK 33b9d6904c699a0cdd5f14f126b977ce2d4317d6; successfully ran local tests tcharding: ACK33b9d6904c
Tree-SHA512: 33f2aff7f0a468f12a8d6cd3fe375563ea5ce2870ef23f483fb9d1eeec146e1f4da66ca9ea17eb05fba7693b995d3aa00a0fd9ff0d1e86a358b6b9d7f0ee9288
This commit is contained in:
commit
431581abe9
|
@ -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<Block, _> = deserialize(&some_block);
|
||||
let bad_decode: Result<Block, _> = 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();
|
||||
|
|
|
@ -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<u64> for Target {
|
||||
fn from(x: u64) -> Self { Self(U256::from(x)) }
|
||||
}
|
||||
|
||||
impl From<u32> for Target {
|
||||
fn from(x: u32) -> Self { Self(U256::from(x)) }
|
||||
}
|
||||
|
||||
impl<T: Into<u128>> From<T> 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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue