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;
#[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<Transaction> for TransactionHash {

View File

@ -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::<u8>(), 1982)) {
fn transaction_roundtrip(tx in any::<Transaction>()) {
// 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];
}
}

View File

@ -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.