From e82cf0f5a8b6771583e93fa79ed41bee7050687d Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Fri, 31 Jan 2020 21:09:46 -0500 Subject: [PATCH] Add BlockHeaderHash (de)serialization roundtrip proptest --- zebra-chain/src/block.rs | 24 +++++++++++++++++++++++- zebra-chain/src/types.rs | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index 86326a7fa..fab67517e 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -11,6 +11,9 @@ use std::{ io::{self, Read}, }; +#[cfg(test)] +use proptest_derive::Arbitrary; + use crate::merkle_tree::MerkleTreeRootHash; use crate::note_commitment_tree::SaplingNoteTreeRootHash; use crate::serialization::{ @@ -32,6 +35,7 @@ use crate::transaction::Transaction; /// for now I want to call it a `BlockHeaderHash` because that's /// more explicit. #[derive(Copy, Clone, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub struct BlockHeaderHash(pub [u8; 32]); impl fmt::Debug for BlockHeaderHash { @@ -184,8 +188,10 @@ impl ZcashDeserialize for Block { #[cfg(test)] mod tests { + use std::io::{Cursor, Write}; + use chrono::NaiveDateTime; - use std::io::Write; + use proptest::prelude::*; use crate::sha256d_writer::Sha256dWriter; @@ -226,4 +232,20 @@ mod tests { "BlockHeaderHash(\"35be4a0f97803879ed642d4e10a146c3fba8727a1dca8079e3f107221be1e7e4\")" ); } + + proptest! { + + #[test] + fn blockheaderhash_roundtrip(hash in any::()) { + let mut bytes = Cursor::new(Vec::new()); + hash.zcash_serialize(&mut bytes)?; + + bytes.set_position(0); + let other_hash = BlockHeaderHash::zcash_deserialize(&mut bytes)?; + + prop_assert_eq![hash, other_hash]; + } + + + } } diff --git a/zebra-chain/src/types.rs b/zebra-chain/src/types.rs index af56c88cd..9c3523014 100644 --- a/zebra-chain/src/types.rs +++ b/zebra-chain/src/types.rs @@ -183,6 +183,5 @@ mod proptests { prop_assert_eq![script, other_script]; } - } }