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)]
pub struct Work(u128);
impl From<Work> for u128 {
fn from(work: Work) -> Self {
work.0
impl Work {
pub fn as_u128(self) -> u128 {
self.0
}
}

View File

@ -51,15 +51,7 @@ impl Arbitrary for Work {
// In the Zcash protocol, a Work is converted from an ExpandedDifficulty.
// But some randomised difficulties are impractically large, and will
// never appear in any real-world block. So we just use a random Work value.
(any::<u128>())
.prop_filter_map("zero Work values are invalid", |w| {
if w == 0 {
None
} else {
Some(Work(w))
}
})
.boxed()
(1..std::u128::MAX).prop_map(Work).boxed()
}
type Strategy = BoxedStrategy<Self>;

View File

@ -84,11 +84,11 @@ use proptest::prelude::*;
fn round_trip_work_expanded() {
zebra_test::init();
proptest!(|(work in 1..std::u128::MAX)| {
let work: U256 = work.into();
proptest!(|(work in any::<Work>())| {
let work: U256 = work.as_u128().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 = work_after.as_u128();
let work_after = U256::from(work_after);
prop_assert_eq!(work, work_after);
});