Skip to content

Commit

Permalink
feat(applying): check non-empty set of inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle committed Nov 16, 2023
1 parent 9ee47a4 commit 81ebe3c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
14 changes: 11 additions & 3 deletions pallas-applying/src/shelley.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
//! Utilities required for Shelley-era transaction validation.
use crate::types::{ShelleyProtParams, UTxOs, ValidationResult};
use crate::types::{ShelleyProtParams, UTxOs, ValidationError, ValidationResult};

use pallas_primitives::alonzo::MintedTx;
use pallas_primitives::alonzo::{MintedTx, TransactionBody};

// TODO: implement each of the validation rules.
pub fn validate_shelley_tx(
_mtxp: &MintedTx,
mtx: &MintedTx,
_utxos: &UTxOs,
_prot_pps: &ShelleyProtParams,
_prot_magic: &u32,
) -> ValidationResult {
let tx_body: &TransactionBody = &mtx.transaction_body;
check_ins_not_empty(tx_body)
}

fn check_ins_not_empty(tx_body: &TransactionBody) -> ValidationResult {
if tx_body.inputs.is_empty() {
return Err(ValidationError::TxInsEmpty);
}
Ok(())
}
44 changes: 40 additions & 4 deletions pallas-applying/tests/shelley.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use pallas_applying::{
types::{Environment, MultiEraProtParams, ShelleyProtParams},
types::{Environment, MultiEraProtParams, ShelleyProtParams, ValidationError},
validate, UTxOs,
};
use pallas_codec::minicbor::{
decode::{Decode, Decoder},
encode,
};
use pallas_primitives::alonzo::{MintedTx, TransactionBody};
use pallas_traverse::{Era, MultiEraTx};

#[cfg(test)]
Expand All @@ -12,14 +17,15 @@ mod byron_tests {
hex::decode(input).unwrap()
}

fn tx_from_cbor<'a>(tx_cbor: &'a Vec<u8>) -> MultiEraTx<'a> {
MultiEraTx::decode_for_era(Era::Shelley, &tx_cbor[..]).unwrap()
fn minted_tx_from_cbor<'a>(tx_cbor: &'a Vec<u8>) -> MintedTx<'a> {
pallas_codec::minicbor::decode::<MintedTx>(&tx_cbor[..]).unwrap()
}

#[test]
fn successful_mainnet_tx() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let metx: MultiEraTx = tx_from_cbor(&cbor_bytes);
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams),
prot_magic: 764824073,
Expand All @@ -30,4 +36,34 @@ mod byron_tests {
Err(err) => assert!(false, "Unexpected error ({:?}).", err),
}
}

#[test]
// Identical to sucessful_mainnet_tx, except that all inputs are removed.
fn empty_ins() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
// Clear the set of inputs in the transaction.
let mut tx_body: TransactionBody = (*mtx.transaction_body).clone();
tx_body.inputs = Vec::new();
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_body, &mut tx_buf) {
Ok(_) => (),
Err(err) => assert!(false, "Unable to encode Tx ({:?}).", err),
};
mtx.transaction_body =
Decode::decode(&mut Decoder::new(&tx_buf.as_slice()), &mut ()).unwrap();
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams),
prot_magic: 764824073,
};
let utxos: UTxOs = UTxOs::new();
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Inputs set should not be empty."),
Err(err) => match err {
ValidationError::TxInsEmpty => (),
_ => assert!(false, "Unexpected error ({:?}).", err),
},
}
}
}

0 comments on commit 81ebe3c

Please sign in to comment.