use arbitrary for creating work
This commit is contained in:
parent
b287ea58c2
commit
a5861e5ad9
|
@ -123,9 +123,9 @@ impl fmt::Debug for ExpandedDifficulty {
|
||||||
#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)]
|
#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub struct Work(u128);
|
pub struct Work(u128);
|
||||||
|
|
||||||
impl From<Work> for u128 {
|
impl Work {
|
||||||
fn from(work: Work) -> Self {
|
pub fn as_u128(self) -> u128 {
|
||||||
work.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,15 +51,7 @@ impl Arbitrary for Work {
|
||||||
// In the Zcash protocol, a Work is converted from an ExpandedDifficulty.
|
// In the Zcash protocol, a Work is converted from an ExpandedDifficulty.
|
||||||
// But some randomised difficulties are impractically large, and will
|
// But some randomised difficulties are impractically large, and will
|
||||||
// never appear in any real-world block. So we just use a random Work value.
|
// never appear in any real-world block. So we just use a random Work value.
|
||||||
(any::<u128>())
|
(1..std::u128::MAX).prop_map(Work).boxed()
|
||||||
.prop_filter_map("zero Work values are invalid", |w| {
|
|
||||||
if w == 0 {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Work(w))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.boxed()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Strategy = BoxedStrategy<Self>;
|
type Strategy = BoxedStrategy<Self>;
|
||||||
|
|
|
@ -84,11 +84,11 @@ use proptest::prelude::*;
|
||||||
fn round_trip_work_expanded() {
|
fn round_trip_work_expanded() {
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
||||||
proptest!(|(work in 1..std::u128::MAX)| {
|
proptest!(|(work in any::<Work>())| {
|
||||||
let work: U256 = work.into();
|
let work: U256 = work.as_u128().into();
|
||||||
let expanded = work_to_expanded(work);
|
let expanded = work_to_expanded(work);
|
||||||
let work_after = Work::try_from(expanded).unwrap();
|
let work_after = Work::try_from(expanded).unwrap();
|
||||||
let work_after = u128::from(work_after);
|
let work_after = work_after.as_u128();
|
||||||
let work_after = U256::from(work_after);
|
let work_after = U256::from(work_after);
|
||||||
prop_assert_eq!(work, work_after);
|
prop_assert_eq!(work, work_after);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue