Add proptest for work roundtrip

This commit is contained in:
Jane Lusby 2020-11-11 14:41:49 -08:00 committed by Deirdre Connolly
parent a798074088
commit b287ea58c2
2 changed files with 24 additions and 1 deletions

View File

@ -123,6 +123,12 @@ 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 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

View File

@ -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() {