Skip to content

Commit

Permalink
WIP Property tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvff committed Nov 21, 2021
1 parent defd730 commit aaf4118
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions zebra-consensus/src/transaction/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
80 changes: 80 additions & 0 deletions zebra-consensus/src/transaction/tests/prop.rs
Original file line number Diff line number Diff line change
@@ -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::<Network>(),
(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<transparent::OutPoint, transparent::OrderedUtxo>,
network: Network,
) -> Result<transaction::Response, TransactionError> {
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
})
}

0 comments on commit aaf4118

Please sign in to comment.