Stubbing out an Arbitrary impl for variant V1 of the Transaction enum

This commit is contained in:
Deirdre Connolly 2020-01-14 16:41:33 -05:00 committed by Deirdre Connolly
parent 1ee79b79d3
commit dfb28b7854
2 changed files with 93 additions and 16 deletions

View File

@ -1,7 +1,40 @@
use crate::serialization::{ZcashDeserialize, ZcashSerialize}; use std::io::Cursor;
use chrono::{TimeZone, Utc};
use proptest::{
collection::{vec, SizeRange},
option,
prelude::*,
};
use crate::{
serialization::{ZcashDeserialize, ZcashSerialize},
types::LockTime,
};
use super::*; use super::*;
impl Arbitrary for Transaction {
type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy {
(
vec(any::<TransparentInput>(), 0..10),
vec(any::<TransparentOutput>(), 0..10),
any::<LockTime>(),
)
.prop_map(|(inputs, outputs, lock_time)| Transaction::V1 {
inputs: inputs,
outputs: outputs,
lock_time: lock_time,
})
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}
#[test] #[test]
fn librustzcash_tx_deserialize_and_round_trip() { fn librustzcash_tx_deserialize_and_round_trip() {
// Copied from librustzcash // Copied from librustzcash
@ -154,3 +187,28 @@ fn librustzcash_tx_deserialize_and_round_trip() {
assert_eq!(&data[..], &data2[..]); assert_eq!(&data[..], &data2[..]);
} }
#[cfg(test)]
proptest! {
#[test]
fn transaction_roundtrip(mut random in vec(any::<u8>(), 1982)) {
// Standard header and version group id.
let mut data = vec![0x04, 0x00, 0x00, 0x80, 0x85, 0x20, 0x2f, 0x89];
data.append(&mut random);
// println!("{:?}", data);
let tx = 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];
}
}

View File

@ -9,6 +9,8 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{DateTime, TimeZone, Utc}; use chrono::{DateTime, TimeZone, Utc};
use hex; use hex;
#[cfg(test)] #[cfg(test)]
use proptest::prelude::*;
#[cfg(test)]
use proptest_derive::Arbitrary; use proptest_derive::Arbitrary;
use crate::serialization::{ use crate::serialization::{
@ -83,6 +85,23 @@ impl ZcashDeserialize for LockTime {
} }
} }
#[cfg(test)]
impl Arbitrary for LockTime {
type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy {
prop_oneof![
(0u32..500_000_000_u32).prop_map(|n| LockTime::Height(BlockHeight(n))),
Just(LockTime::Time(
Utc.timestamp(Utc::now().timestamp() as i64, 0)
))
]
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}
/// An encoding of a Bitcoin script. /// An encoding of a Bitcoin script.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))] #[cfg_attr(test, derive(Arbitrary))]
@ -129,31 +148,31 @@ mod tests {
} }
#[cfg(test)] #[cfg(test)]
mod proptest { mod proptests {
use std::io::Cursor; use std::io::Cursor;
use chrono::{TimeZone, Utc};
use proptest::prelude::*; use proptest::prelude::*;
use proptest_derive::Arbitrary;
use super::{BlockHeight, LockTime, Script}; use super::{BlockHeight, LockTime, Script};
use crate::serialization::{ZcashDeserialize, ZcashSerialize}; use crate::serialization::{ZcashDeserialize, ZcashSerialize};
impl Arbitrary for LockTime { // impl Arbitrary for LockTime {
type Parameters = (); // type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy { // fn arbitrary_with(_args: ()) -> Self::Strategy {
prop_oneof![ // prop_oneof![
(0u32..500_000_000_u32).prop_map(|n| LockTime::Height(BlockHeight(n))), // (0u32..500_000_000_u32).prop_map(|n| LockTime::Height(BlockHeight(n))),
Just(LockTime::Time( // Just(LockTime::Time(
Utc.timestamp(Utc::now().timestamp() as i64, 0) // Utc.timestamp(Utc::now().timestamp() as i64, 0)
)) // ))
] // ]
.boxed() // .boxed()
} // }
type Strategy = BoxedStrategy<Self>; // type Strategy = BoxedStrategy<Self>;
} // }
proptest! { proptest! {