use arbitrary for creating work

This commit is contained in:
Jane Lusby 2020-11-11 17:55:17 -08:00 committed by Deirdre Connolly
parent b287ea58c2
commit a5861e5ad9
3 changed files with 7 additions and 15 deletions

View File

@ -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
} }
} }

View File

@ -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>;

View File

@ -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);
}); });