diff --git a/zebra-chain/src/work/difficulty.rs b/zebra-chain/src/work/difficulty.rs index 085aeffe..d7204c3c 100644 --- a/zebra-chain/src/work/difficulty.rs +++ b/zebra-chain/src/work/difficulty.rs @@ -123,6 +123,12 @@ impl fmt::Debug for ExpandedDifficulty { #[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)] pub struct Work(u128); +impl From for u128 { + fn from(work: Work) -> Self { + work.0 + } +} + impl fmt::Debug for Work { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // There isn't a standard way to represent alternate formats for the diff --git a/zebra-state/src/tests.rs b/zebra-state/src/tests.rs index f3067cf3..f4c027df 100644 --- a/zebra-state/src/tests.rs +++ b/zebra-state/src/tests.rs @@ -1,4 +1,4 @@ -use std::{mem, sync::Arc}; +use std::{convert::TryFrom, mem, sync::Arc}; use primitive_types::U256; use zebra_chain::{ @@ -6,6 +6,7 @@ use zebra_chain::{ transaction::Transaction, transparent, work::difficulty::ExpandedDifficulty, + work::difficulty::Work, }; use super::*; @@ -77,6 +78,22 @@ static BLOCK_LOCATOR_CASES: &[(u32, u32)] = &[ (10000, 9901), ]; +use proptest::prelude::*; + +#[test] +fn round_trip_work_expanded() { + zebra_test::init(); + + proptest!(|(work in 1..std::u128::MAX)| { + let work: U256 = work.into(); + let expanded = work_to_expanded(work); + let work_after = Work::try_from(expanded).unwrap(); + let work_after = u128::from(work_after); + let work_after = U256::from(work_after); + prop_assert_eq!(work, work_after); + }); +} + /// Check that the block locator heights are sensible. #[test] fn test_block_locator_heights() {