From aaf41182c02027707f124bcc72b5739148ddd275 Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Mon, 15 Nov 2021 20:27:37 +0000 Subject: [PATCH] WIP Property tests --- zebra-consensus/src/transaction/tests.rs | 3 + zebra-consensus/src/transaction/tests/prop.rs | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 zebra-consensus/src/transaction/tests/prop.rs diff --git a/zebra-consensus/src/transaction/tests.rs b/zebra-consensus/src/transaction/tests.rs index 9cd87c8b339..2db406db42b 100644 --- a/zebra-consensus/src/transaction/tests.rs +++ b/zebra-consensus/src/transaction/tests.rs @@ -30,6 +30,9 @@ use super::{check, Request, Verifier}; use crate::{error::TransactionError, script}; use color_eyre::eyre::Report; +#[cfg(test)] +mod prop; + #[test] fn v5_fake_transactions() -> Result<(), Report> { zebra_test::init(); diff --git a/zebra-consensus/src/transaction/tests/prop.rs b/zebra-consensus/src/transaction/tests/prop.rs new file mode 100644 index 00000000000..8e14886fa88 --- /dev/null +++ b/zebra-consensus/src/transaction/tests/prop.rs @@ -0,0 +1,80 @@ +use std::{collections::HashMap, sync::Arc}; + +use proptest::prelude::*; +use tower::ServiceExt; + +use zebra_chain::{ + parameters::Network, + transaction::{LockTime, Transaction}, + transparent, +}; + +use crate::{error::TransactionError, script, transaction}; + +proptest! { + #[test] + fn zero_lock_time_is_always_unlocked( + network in any::(), + (transaction, known_utxos) in Transaction::valid_transparent_transfer_strategy(), + ) { + zebra_test::init(); + + transaction.set_lock_time(LockTime::unlocked()); + + let transaction_id = transaction.unmined_id(); + + let result = validate(transaction, known_utxos, network); + + prop_assert!(result.is_ok()); + prop_assert_eq!(result.unwrap().tx_id(), transaction_id); + } + + // #[test] + // fn lock_time_is_ignored_because_of_sequence_numbers() { + // todo!(); + // } + + // #[test] + // fn transaction_is_rejected_based_on_lock_height() { + // todo!(); + // } + + // #[test] + // fn transaction_is_rejected_based_on_lock_time() { + // todo!(); + // } + + // #[test] + // fn transaction_with_lock_height_is_accepted() { + // todo!(); + // } + + // #[test] + // fn transaction_with_lock_time_is_accepted() { + // todo!(); + // } +} + +fn validate( + transaction: Transaction, + known_utxos: HashMap, + network: Network, +) -> Result { + zebra_test::RUNTIME.block_on(async { + // Initialize the verifier + let state_service = + tower::service_fn(|_| async { unreachable!("State service should not be called") }); + let script_verifier = script::Verifier::new(state_service); + let verifier = transaction::Verifier::new(network, script_verifier); + + // Test the transaction verifier + verifier + .clone() + .oneshot(transaction::Request::Block { + transaction: Arc::new(transaction), + known_utxos: Arc::new(known_utxos), + height, + }) + .await + }) +}