diff --git a/pallas-applying/src/alonzo.rs b/pallas-applying/src/alonzo.rs index aac55ff..109d934 100644 --- a/pallas-applying/src/alonzo.rs +++ b/pallas-applying/src/alonzo.rs @@ -36,16 +36,16 @@ pub fn validate_alonzo_tx( network_id: &u8, ) -> ValidationResult { let tx_body: &TransactionBody = &mtx.transaction_body; - let size: &u32 = &get_alonzo_comp_tx_size(tx_body).ok_or(Alonzo(UnknownTxSize))?; + let size: u32 = get_alonzo_comp_tx_size(mtx); check_ins_not_empty(tx_body)?; check_ins_and_collateral_in_utxos(tx_body, utxos)?; check_tx_validity_interval(tx_body, mtx, block_slot)?; - check_fee(tx_body, size, mtx, utxos, prot_pps)?; + check_fee(tx_body, &size, mtx, utxos, prot_pps)?; check_preservation_of_value(tx_body, utxos)?; check_min_lovelace(tx_body, prot_pps)?; check_output_val_size(tx_body, prot_pps)?; check_network_id(tx_body, network_id)?; - check_tx_size(size, prot_pps)?; + check_tx_size(&size, prot_pps)?; check_tx_ex_units(mtx, prot_pps)?; check_witness_set(mtx, utxos)?; check_languages(mtx, prot_pps)?; diff --git a/pallas-applying/src/babbage.rs b/pallas-applying/src/babbage.rs index a060451..ea4b11b 100644 --- a/pallas-applying/src/babbage.rs +++ b/pallas-applying/src/babbage.rs @@ -38,16 +38,16 @@ pub fn validate_babbage_tx( network_id: &u8, ) -> ValidationResult { let tx_body: &MintedTransactionBody = &mtx.transaction_body.clone(); - let size: &u32 = &get_babbage_tx_size(tx_body).ok_or(Babbage(UnknownTxSize))?; + let size: u32 = get_babbage_tx_size(mtx).ok_or(Babbage(UnknownTxSize))?; check_ins_not_empty(tx_body)?; check_all_ins_in_utxos(tx_body, utxos)?; check_tx_validity_interval(tx_body, block_slot)?; - check_fee(tx_body, size, mtx, utxos, prot_pps)?; + check_fee(tx_body, &size, mtx, utxos, prot_pps)?; check_preservation_of_value(tx_body, utxos)?; check_min_lovelace(tx_body, prot_pps)?; check_output_val_size(tx_body, prot_pps)?; check_network_id(tx_body, network_id)?; - check_tx_size(size, prot_pps)?; + check_tx_size(&size, prot_pps)?; check_tx_ex_units(mtx, prot_pps)?; check_minting(tx_body, mtx)?; check_well_formedness(tx_body, mtx)?; diff --git a/pallas-applying/src/byron.rs b/pallas-applying/src/byron.rs index c95c165..ac413ab 100644 --- a/pallas-applying/src/byron.rs +++ b/pallas-applying/src/byron.rs @@ -12,10 +12,7 @@ use crate::utils::{ use pallas_addresses::byron::{ AddrAttrs, AddrType, AddressId, AddressPayload, ByronAddress, SpendingData, }; -use pallas_codec::{ - minicbor::{encode, Encoder}, - utils::CborWrap, -}; +use pallas_codec::{minicbor::Encoder, utils::CborWrap}; use pallas_crypto::{ hash::Hash, key::ed25519::{PublicKey, Signature}, @@ -32,13 +29,13 @@ pub fn validate_byron_tx( prot_magic: &u32, ) -> ValidationResult { let tx: &Tx = &mtxp.transaction; - let size: &u64 = &get_tx_size(tx)?; + let size: u64 = get_tx_size(mtxp); check_ins_not_empty(tx)?; check_outs_not_empty(tx)?; check_ins_in_utxos(tx, utxos)?; check_outs_have_lovelace(tx)?; - check_fees(tx, size, utxos, prot_pps)?; - check_size(size, prot_pps)?; + check_fees(tx, &size, utxos, prot_pps)?; + check_size(&size, prot_pps)?; check_witnesses(mtxp, utxos, prot_magic) } @@ -126,12 +123,8 @@ fn check_size(size: &u64, prot_pps: &ByronProtParams) -> ValidationResult { Ok(()) } -fn get_tx_size(tx: &Tx) -> Result { - let mut buff: Vec = Vec::new(); - match encode(tx, &mut buff) { - Ok(()) => Ok(buff.len() as u64), - Err(_) => Err(Byron(UnknownTxSize)), - } +fn get_tx_size(mtxp: &MintedTxPayload) -> u64 { + (mtxp.transaction.raw_cbor().len() + mtxp.witness.raw_cbor().len()) as u64 } pub enum TaggedSignature<'a> { diff --git a/pallas-applying/src/shelley_ma.rs b/pallas-applying/src/shelley_ma.rs index 5c28064..dbae94e 100644 --- a/pallas-applying/src/shelley_ma.rs +++ b/pallas-applying/src/shelley_ma.rs @@ -31,14 +31,14 @@ pub fn validate_shelley_ma_tx( ) -> ValidationResult { let tx_body: &TransactionBody = &mtx.transaction_body; let tx_wits: &MintedWitnessSet = &mtx.transaction_witness_set; - let size: &u32 = &get_alonzo_comp_tx_size(tx_body).ok_or(ShelleyMA(UnknownTxSize))?; + let size: u32 = get_alonzo_comp_tx_size(mtx); check_ins_not_empty(tx_body)?; check_ins_in_utxos(tx_body, utxos)?; check_ttl(tx_body, block_slot)?; - check_tx_size(size, prot_pps)?; + check_tx_size(&size, prot_pps)?; check_min_lovelace(tx_body, prot_pps, era)?; check_preservation_of_value(tx_body, utxos, era)?; - check_fees(tx_body, size, prot_pps)?; + check_fees(tx_body, &size, prot_pps)?; check_network_id(tx_body, network_id)?; check_metadata(tx_body, mtx)?; check_witnesses(tx_body, tx_wits, utxos)?; diff --git a/pallas-applying/src/utils.rs b/pallas-applying/src/utils.rs index 5c375af..69390a4 100644 --- a/pallas-applying/src/utils.rs +++ b/pallas-applying/src/utils.rs @@ -7,15 +7,15 @@ pub use environment::*; use pallas_addresses::{Address, ShelleyAddress, ShelleyPaymentPart}; use pallas_codec::{ minicbor::encode, - utils::{Bytes, KeepRaw, KeyValuePairs}, + utils::{Bytes, KeepRaw, KeyValuePairs, Nullable}, }; use pallas_crypto::key::ed25519::{PublicKey, Signature}; use pallas_primitives::{ alonzo::{ AssetName, AuxiliaryData, Coin, MintedTx as AlonzoMintedTx, Multiasset, NativeScript, - NetworkId, PlutusScript, PolicyId, TransactionBody, VKeyWitness, Value, + NetworkId, PlutusScript, PolicyId, VKeyWitness, Value, }, - babbage::{MintedTransactionBody, MintedTx as BabbageMintedTx, PlutusV2Script}, + babbage::{MintedTx as BabbageMintedTx, PlutusV2Script}, }; use pallas_traverse::{MultiEraInput, MultiEraOutput}; use std::collections::HashMap; @@ -24,17 +24,23 @@ pub use validation::*; pub type UTxOs<'b> = HashMap, MultiEraOutput<'b>>; -pub fn get_alonzo_comp_tx_size(tx_body: &TransactionBody) -> Option { - let mut buff: Vec = Vec::new(); - match encode(tx_body, &mut buff) { - Ok(()) => Some(buff.len() as u32), - Err(_) => None, +pub fn get_alonzo_comp_tx_size(mtx: &AlonzoMintedTx) -> u32 { + match &mtx.auxiliary_data { + Nullable::Some(aux_data) => { + (aux_data.raw_cbor().len() + + mtx.transaction_body.raw_cbor().len() + + mtx.transaction_witness_set.raw_cbor().len()) as u32 + } + _ => { + (mtx.transaction_body.raw_cbor().len() + mtx.transaction_witness_set.raw_cbor().len()) + as u32 + } } } -pub fn get_babbage_tx_size(tx_body: &MintedTransactionBody) -> Option { +pub fn get_babbage_tx_size(mtx: &BabbageMintedTx) -> Option { let mut buff: Vec = Vec::new(); - match encode(tx_body, &mut buff) { + match encode(mtx, &mut buff) { Ok(()) => Some(buff.len() as u32), Err(_) => None, }