fix(applying): fix tx size calculation (#443)

This commit is contained in:
Maico Leberle 2024-05-14 07:40:52 -03:00 committed by GitHub
parent 810088a378
commit f18af0b9b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 31 additions and 32 deletions

View file

@ -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)?;

View file

@ -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)?;

View file

@ -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<u64, ValidationError> {
let mut buff: Vec<u8> = 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> {

View file

@ -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)?;

View file

@ -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<MultiEraInput<'b>, MultiEraOutput<'b>>;
pub fn get_alonzo_comp_tx_size(tx_body: &TransactionBody) -> Option<u32> {
let mut buff: Vec<u8> = 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<u32> {
pub fn get_babbage_tx_size(mtx: &BabbageMintedTx) -> Option<u32> {
let mut buff: Vec<u8> = Vec::new();
match encode(tx_body, &mut buff) {
match encode(mtx, &mut buff) {
Ok(()) => Some(buff.len() as u32),
Err(_) => None,
}