Transaction::V1 round trip proptest

This commit is contained in:
Deirdre Connolly 2020-01-20 13:16:21 -05:00 committed by Deirdre Connolly
parent dfb28b7854
commit b0c0055915
3 changed files with 18 additions and 16 deletions

View File

@ -2,6 +2,9 @@ use std::fmt;
use hex; use hex;
#[cfg(test)]
use proptest_derive::Arbitrary;
use crate::{serialization::ZcashSerialize, sha256d_writer::Sha256dWriter}; use crate::{serialization::ZcashSerialize, sha256d_writer::Sha256dWriter};
use super::Transaction; 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 /// TODO: I'm pretty sure this is also a SHA256d hash but I haven't
/// confirmed it yet. /// confirmed it yet.
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct TransactionHash(pub [u8; 32]); pub struct TransactionHash(pub [u8; 32]);
impl From<Transaction> for TransactionHash { impl From<Transaction> for TransactionHash {

View File

@ -3,8 +3,8 @@ use std::io::Cursor;
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc};
use proptest::{ use proptest::{
collection::{vec, SizeRange}, arbitrary::{any, Arbitrary},
option, collection::vec,
prelude::*, prelude::*,
}; };
@ -15,6 +15,7 @@ use crate::{
use super::*; use super::*;
#[cfg(test)]
impl Arbitrary for Transaction { impl Arbitrary for Transaction {
type Parameters = (); type Parameters = ();
@ -192,23 +193,14 @@ fn librustzcash_tx_deserialize_and_round_trip() {
proptest! { proptest! {
#[test] #[test]
fn transaction_roundtrip(mut random in vec(any::<u8>(), 1982)) { fn transaction_roundtrip(tx in any::<Transaction>()) {
// Standard header and version group id. let mut data = Vec::new();
let mut data = vec![0x04, 0x00, 0x00, 0x80, 0x85, 0x20, 0x2f, 0x89];
data.append(&mut random);
// 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); prop_assert_eq![tx, tx2];
let mut data2 = Vec::new();
tx.zcash_serialize(&mut data2).expect("tx should serialize");
assert_ne!(&data[..], &data2[..]);
prop_assert_ne![data, data2];
} }
} }

View File

@ -1,5 +1,8 @@
//! Transaction types. //! Transaction types.
#[cfg(test)]
use proptest_derive::Arbitrary;
use crate::types::Script; use crate::types::Script;
use super::TransactionHash; use super::TransactionHash;
@ -8,6 +11,7 @@ use super::TransactionHash;
/// ///
/// A particular transaction output reference. /// A particular transaction output reference.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct OutPoint { pub struct OutPoint {
/// References the transaction that contains the UTXO being spent. /// References the transaction that contains the UTXO being spent.
pub hash: TransactionHash, pub hash: TransactionHash,
@ -19,6 +23,7 @@ pub struct OutPoint {
/// A transparent input to a transaction. /// A transparent input to a transaction.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct TransparentInput { pub struct TransparentInput {
/// The previous output transaction reference. /// The previous output transaction reference.
pub previous_output: OutPoint, 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 /// that spends my UTXO and sends 1 ZEC to you and 1 ZEC back to me
/// (just like receiving change). /// (just like receiving change).
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct TransparentOutput { pub struct TransparentOutput {
/// Transaction value. /// Transaction value.
// At https://en.bitcoin.it/wiki/Protocol_documentation#tx, this is an i64. // At https://en.bitcoin.it/wiki/Protocol_documentation#tx, this is an i64.