diff --git a/zebra-chain/src/transaction/hash.rs b/zebra-chain/src/transaction/hash.rs index 2c013734..bbba046e 100644 --- a/zebra-chain/src/transaction/hash.rs +++ b/zebra-chain/src/transaction/hash.rs @@ -2,6 +2,9 @@ use std::fmt; use hex; +#[cfg(test)] +use proptest_derive::Arbitrary; + use crate::{serialization::ZcashSerialize, sha256d_writer::Sha256dWriter}; use super::Transaction; @@ -11,6 +14,7 @@ use super::Transaction; /// TODO: I'm pretty sure this is also a SHA256d hash but I haven't /// confirmed it yet. #[derive(Copy, Clone, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub struct TransactionHash(pub [u8; 32]); impl From for TransactionHash { diff --git a/zebra-chain/src/transaction/tests.rs b/zebra-chain/src/transaction/tests.rs index 15d06b01..89c198f6 100644 --- a/zebra-chain/src/transaction/tests.rs +++ b/zebra-chain/src/transaction/tests.rs @@ -3,8 +3,8 @@ use std::io::Cursor; use chrono::{TimeZone, Utc}; use proptest::{ - collection::{vec, SizeRange}, - option, + arbitrary::{any, Arbitrary}, + collection::vec, prelude::*, }; @@ -15,6 +15,7 @@ use crate::{ use super::*; +#[cfg(test)] impl Arbitrary for Transaction { type Parameters = (); @@ -192,23 +193,14 @@ fn librustzcash_tx_deserialize_and_round_trip() { proptest! { #[test] - fn transaction_roundtrip(mut random in vec(any::(), 1982)) { + fn transaction_roundtrip(tx in any::()) { - // Standard header and version group id. - let mut data = vec![0x04, 0x00, 0x00, 0x80, 0x85, 0x20, 0x2f, 0x89]; - data.append(&mut random); + let mut data = Vec::new(); - // println!("{:?}", data); + tx.zcash_serialize(&mut data).expect("tx should serialize"); - let tx = Transaction::zcash_deserialize(&data[..]).expect("randomized tx should deserialize"); + let tx2 = Transaction::zcash_deserialize(&data[..]).expect("randomized tx should deserialize"); - println!("{:?}", tx); - - let mut data2 = Vec::new(); - tx.zcash_serialize(&mut data2).expect("tx should serialize"); - - assert_ne!(&data[..], &data2[..]); - - prop_assert_ne![data, data2]; + prop_assert_eq![tx, tx2]; } } diff --git a/zebra-chain/src/transaction/transparent.rs b/zebra-chain/src/transaction/transparent.rs index 9cfa7b15..d21e1fd7 100644 --- a/zebra-chain/src/transaction/transparent.rs +++ b/zebra-chain/src/transaction/transparent.rs @@ -1,5 +1,8 @@ //! Transaction types. +#[cfg(test)] +use proptest_derive::Arbitrary; + use crate::types::Script; use super::TransactionHash; @@ -8,6 +11,7 @@ use super::TransactionHash; /// /// A particular transaction output reference. #[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub struct OutPoint { /// References the transaction that contains the UTXO being spent. pub hash: TransactionHash, @@ -19,6 +23,7 @@ pub struct OutPoint { /// A transparent input to a transaction. #[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub struct TransparentInput { /// The previous output transaction reference. pub previous_output: OutPoint, @@ -45,6 +50,7 @@ pub struct TransparentInput { /// that spends my UTXO and sends 1 ZEC to you and 1 ZEC back to me /// (just like receiving change). #[derive(Clone, Debug, Eq, PartialEq)] +#[cfg_attr(test, derive(Arbitrary))] pub struct TransparentOutput { /// Transaction value. // At https://en.bitcoin.it/wiki/Protocol_documentation#tx, this is an i64.