Skip to content

Commit

Permalink
Test if transaction is rejected by script verifier
Browse files Browse the repository at this point in the history
Mock the state service to always return an error, this causes the script
verifier to fail getting the transparent unspent transaction output, and
should make the transaction validation fail.
  • Loading branch information
jvff committed Jun 15, 2021
1 parent a468929 commit 0b60f21
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions zebra-consensus/src/transaction/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, io, sync::Arc};

use tower::{service_fn, ServiceExt};

Expand All @@ -13,7 +13,7 @@ use zebra_chain::{

use super::{check, Request, Verifier};

use crate::{error::TransactionError, script};
use crate::{error::TransactionError, script, BoxError};
use color_eyre::eyre::Report;

#[test]
Expand Down Expand Up @@ -241,3 +241,45 @@ async fn v5_transaction_is_accepted_after_nu5_activation() {
assert_eq!(result, Ok(expected_hash));
}
}

#[tokio::test]
// TODO: Remove `should_panic` once the NU5 activation heights for testnet and mainnet have been
// defined.
#[should_panic]
async fn v5_transaction_is_rejected_based_on_script() {
let network = Network::Mainnet;
let blocks = zebra_test::vectors::MAINNET_BLOCKS.iter();

let state_service = service_fn(|_| async {
Err(Box::new(io::Error::new(
io::ErrorKind::Other,
"Pretending the UTXO was not found",
)) as BoxError)
});

let script_verifier = script::Verifier::new(state_service);
let verifier = Verifier::new(network, script_verifier);

let transaction = fake_v5_transactions_for_network(network, blocks)
.rev()
.find(|transaction| {
!transaction.is_coinbase()
&& transaction.inputs().is_empty()
&& transaction.joinsplit_count() == 0
&& transaction.sapling_spends_per_anchor().next().is_none()
&& transaction.sapling_outputs().next().is_none()
})
.expect("At least one fake V5 coinbase transaction in the test vectors");

let result = verifier
.oneshot(Request::Block {
transaction: Arc::new(transaction),
known_utxos: Arc::new(HashMap::new()),
height: NetworkUpgrade::Nu5
.activation_height(network)
.expect("NU5 activation height is specified"),
})
.await;

assert!(result.is_err());
}

0 comments on commit 0b60f21

Please sign in to comment.