Skip to content

Commit

Permalink
refactor(applying): unify approach for protocol params access (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle authored Apr 12, 2024
1 parent 619cb82 commit a0c409b
Show file tree
Hide file tree
Showing 45 changed files with 2,046 additions and 1,510 deletions.
25 changes: 12 additions & 13 deletions pallas-applying/src/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils::{
get_network_id_value, get_payment_part, get_shelley_address, get_val_size_in_words,
mk_alonzo_vk_wits_check_list, values_are_equal, verify_signature,
AlonzoError::*,
AlonzoProtParams, FeePolicy, UTxOs,
AlonzoProtParams, UTxOs,
ValidationError::{self, *},
ValidationResult,
};
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn validate_alonzo_tx(
network_id: &u8,
) -> ValidationResult {
let tx_body: &TransactionBody = &mtx.transaction_body;
let size: &u64 = &get_alonzo_comp_tx_size(tx_body).ok_or(Alonzo(UnknownTxSize))?;
let size: &u32 = &get_alonzo_comp_tx_size(tx_body).ok_or(Alonzo(UnknownTxSize))?;
check_ins_not_empty(tx_body)?;
check_ins_and_collateral_in_utxos(tx_body, utxos)?;
check_tx_validity_interval(tx_body, mtx, block_slot)?;
Expand Down Expand Up @@ -131,7 +131,7 @@ fn check_upper_bound(

fn check_fee(
tx_body: &TransactionBody,
size: &u64,
size: &u32,
mtx: &MintedTx,
utxos: &UTxOs,
prot_pps: &AlonzoProtParams,
Expand All @@ -147,11 +147,10 @@ fn check_fee(
// minimum fee.
fn check_min_fee(
tx_body: &TransactionBody,
size: &u64,
size: &u32,
prot_pps: &AlonzoProtParams,
) -> ValidationResult {
let fee_policy: &FeePolicy = &prot_pps.fee_policy;
if tx_body.fee < fee_policy.summand + fee_policy.multiplier * size {
if tx_body.fee < (prot_pps.minfee_b + prot_pps.minfee_a * size) as u64 {
return Err(Alonzo(FeeBelowMin));
}
Ok(())
Expand Down Expand Up @@ -185,7 +184,7 @@ fn check_collaterals_number(
collaterals: &[TransactionInput],
prot_pps: &AlonzoProtParams,
) -> ValidationResult {
let number_collateral: u64 = collaterals.len() as u64;
let number_collateral: u32 = collaterals.len() as u32;
if number_collateral == 0 {
Err(Alonzo(CollateralMissing))
} else if number_collateral > prot_pps.max_collateral_inputs {
Expand Down Expand Up @@ -222,7 +221,7 @@ fn check_collaterals_assets(
utxos: &UTxOs,
prot_pps: &AlonzoProtParams,
) -> ValidationResult {
let fee_percentage: u64 = tx_body.fee * prot_pps.collateral_percent;
let fee_percentage: u64 = tx_body.fee * prot_pps.collateral_percentage as u64;
match &tx_body.collateral {
Some(collaterals) => {
for collateral in collaterals {
Expand Down Expand Up @@ -317,7 +316,7 @@ fn compute_min_lovelace(output: &TransactionOutput, prot_pps: &AlonzoProtParams)
Some(_) => 37, // utxoEntrySizeWithoutVal (27) + dataHashSize (10)
None => 27, // utxoEntrySizeWithoutVal
};
prot_pps.coins_per_utxo_word * output_entry_size
prot_pps.ada_per_utxo_byte * output_entry_size
}

// The size of the value in each of the outputs should not be greater than the
Expand All @@ -327,7 +326,7 @@ fn check_output_val_size(
prot_pps: &AlonzoProtParams,
) -> ValidationResult {
for output in tx_body.outputs.iter() {
if get_val_size_in_words(&output.amount) > prot_pps.max_val_size {
if get_val_size_in_words(&output.amount) > prot_pps.max_value_size as u64 {
return Err(Alonzo(MaxValSizeExceeded));
}
}
Expand Down Expand Up @@ -364,8 +363,8 @@ fn check_tx_network_id(tx_body: &TransactionBody, network_id: &u8) -> Validation
}

// The transaction size does not exceed the protocol limit.
fn check_tx_size(size: &u64, prot_pps: &AlonzoProtParams) -> ValidationResult {
if *size > prot_pps.max_tx_size {
fn check_tx_size(size: &u32, prot_pps: &AlonzoProtParams) -> ValidationResult {
if *size > prot_pps.max_transaction_size {
return Err(Alonzo(MaxTxSizeExceeded));
}
Ok(())
Expand All @@ -384,7 +383,7 @@ fn check_tx_ex_units(mtx: &MintedTx, prot_pps: &AlonzoProtParams) -> ValidationR
mem += ex_units.mem;
steps += ex_units.steps;
}
if mem > prot_pps.max_tx_ex_mem || steps > prot_pps.max_tx_ex_steps {
if mem > prot_pps.max_tx_ex_units.mem || steps > prot_pps.max_tx_ex_units.steps {
return Err(Alonzo(TxExUnitsExceeded));
}
}
Expand Down
25 changes: 12 additions & 13 deletions pallas-applying/src/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::utils::{
get_val_size_in_words, is_byron_address, lovelace_diff_or_fail, mk_alonzo_vk_wits_check_list,
values_are_equal, verify_signature,
BabbageError::*,
BabbageProtParams, FeePolicy, UTxOs,
BabbageProtParams, UTxOs,
ValidationError::{self, *},
ValidationResult,
};
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn validate_babbage_tx(
network_id: &u8,
) -> ValidationResult {
let tx_body: &MintedTransactionBody = &mtx.transaction_body.clone();
let size: &u64 = &get_babbage_tx_size(tx_body).ok_or(Babbage(UnknownTxSize))?;
let size: &u32 = &get_babbage_tx_size(tx_body).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)?;
Expand Down Expand Up @@ -139,7 +139,7 @@ fn check_upper_bound(tx_body: &MintedTransactionBody, block_slot: u64) -> Valida

fn check_fee(
tx_body: &MintedTransactionBody,
size: &u64,
size: &u32,
mtx: &MintedTx,
utxos: &UTxOs,
prot_pps: &BabbageProtParams,
Expand All @@ -155,11 +155,10 @@ fn check_fee(
// minimum fee.
fn check_min_fee(
tx_body: &MintedTransactionBody,
size: &u64,
size: &u32,
prot_pps: &BabbageProtParams,
) -> ValidationResult {
let fee_policy: &FeePolicy = &prot_pps.fee_policy;
if tx_body.fee < fee_policy.summand + fee_policy.multiplier * size {
if tx_body.fee < (prot_pps.minfee_b + prot_pps.minfee_a * size) as u64 {
return Err(Babbage(FeeBelowMin));
}
Ok(())
Expand Down Expand Up @@ -200,7 +199,7 @@ fn check_collaterals_number(
) -> ValidationResult {
if collaterals.is_empty() {
Err(Babbage(CollateralMissing))
} else if collaterals.len() > prot_pps.max_collateral_inputs as usize {
} else if collaterals.len() as u32 > prot_pps.max_collateral_inputs {
Err(Babbage(TooManyCollaterals))
} else {
Ok(())
Expand Down Expand Up @@ -265,7 +264,7 @@ fn check_collaterals_assets(
// The balance between collateral inputs and output contains only lovelace.
let paid_collateral: u64 =
lovelace_diff_or_fail(&coll_input, &coll_return, &Babbage(NonLovelaceCollateral))?;
let fee_percentage: u64 = tx_body.fee * prot_pps.collateral_percent;
let fee_percentage: u64 = tx_body.fee * prot_pps.collateral_percentage as u64;
// The balance is not lower than the minimum allowed.
if paid_collateral * 100 < fee_percentage {
return Err(Babbage(CollateralMinLovelace));
Expand Down Expand Up @@ -357,7 +356,7 @@ fn check_min_lovelace(
}

fn compute_min_lovelace(val: &Value, prot_pps: &BabbageProtParams) -> u64 {
prot_pps.coins_per_utxo_word * (get_val_size_in_words(val) + 160)
prot_pps.ada_per_utxo_byte * (get_val_size_in_words(val) + 160)
}

// The size of the value in each of the outputs should not be greater than the
Expand All @@ -371,7 +370,7 @@ fn check_output_val_size(
PseudoTransactionOutput::Legacy(output) => &output.amount,
PseudoTransactionOutput::PostAlonzo(output) => &output.value,
};
if get_val_size_in_words(val) > prot_pps.max_val_size {
if get_val_size_in_words(val) > prot_pps.max_value_size as u64 {
return Err(Babbage(MaxValSizeExceeded));
}
}
Expand Down Expand Up @@ -409,8 +408,8 @@ fn check_tx_network_id(tx_body: &MintedTransactionBody, network_id: &u8) -> Vali
Ok(())
}

fn check_tx_size(size: &u64, prot_pps: &BabbageProtParams) -> ValidationResult {
if *size > prot_pps.max_tx_size {
fn check_tx_size(size: &u32, prot_pps: &BabbageProtParams) -> ValidationResult {
if *size > prot_pps.max_transaction_size {
return Err(Babbage(MaxTxSizeExceeded));
}
Ok(())
Expand All @@ -427,7 +426,7 @@ fn check_tx_ex_units(mtx: &MintedTx, prot_pps: &BabbageProtParams) -> Validation
mem += ex_units.mem;
steps += ex_units.steps;
}
if mem > prot_pps.max_tx_ex_mem || steps > prot_pps.max_tx_ex_steps {
if mem > prot_pps.max_tx_ex_units.mem || steps > prot_pps.max_tx_ex_units.steps {
return Err(Babbage(TxExUnitsExceeded));
}
}
Expand Down
2 changes: 1 addition & 1 deletion pallas-applying/src/byron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn check_fees(tx: &Tx, size: &u64, utxos: &UTxOs, prot_pps: &ByronProtParams) ->
outputs_balance += output.amount
}
let total_balance: u64 = inputs_balance - outputs_balance;
let min_fees: u64 = prot_pps.fee_policy.summand + prot_pps.fee_policy.multiplier * size;
let min_fees: u64 = prot_pps.summand + prot_pps.multiplier * size;
if total_balance < min_fees {
Err(Byron(FeesBelowMin))
} else {
Expand Down
11 changes: 6 additions & 5 deletions pallas-applying/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ use pallas_traverse::{Era, MultiEraTx};
use shelley_ma::validate_shelley_ma_tx;

pub use utils::{
Environment, MultiEraProtParams, UTxOs, ValidationError::TxAndProtParamsDiffer,
Environment, MultiEraProtocolParameters, UTxOs,
ValidationError::{TxAndProtParamsDiffer, UnknownProtParams},
ValidationResult,
};

pub fn validate(metx: &MultiEraTx, utxos: &UTxOs, env: &Environment) -> ValidationResult {
match env.prot_params() {
MultiEraProtParams::Byron(bpp) => match metx {
MultiEraProtocolParameters::Byron(bpp) => match metx {
MultiEraTx::Byron(mtxp) => validate_byron_tx(mtxp, utxos, bpp, env.prot_magic()),
_ => Err(TxAndProtParamsDiffer),
},
MultiEraProtParams::Shelley(spp) => match metx {
MultiEraProtocolParameters::Shelley(spp) => match metx {
MultiEraTx::AlonzoCompatible(mtx, Era::Shelley)
| MultiEraTx::AlonzoCompatible(mtx, Era::Allegra)
| MultiEraTx::AlonzoCompatible(mtx, Era::Mary) => validate_shelley_ma_tx(
Expand All @@ -36,13 +37,13 @@ pub fn validate(metx: &MultiEraTx, utxos: &UTxOs, env: &Environment) -> Validati
),
_ => Err(TxAndProtParamsDiffer),
},
MultiEraProtParams::Alonzo(app) => match metx {
MultiEraProtocolParameters::Alonzo(app) => match metx {
MultiEraTx::AlonzoCompatible(mtx, Era::Alonzo) => {
validate_alonzo_tx(mtx, utxos, app, env.block_slot(), env.network_id())
}
_ => Err(TxAndProtParamsDiffer),
},
MultiEraProtParams::Babbage(bpp) => match metx {
MultiEraProtocolParameters::Babbage(bpp) => match metx {
MultiEraTx::Babbage(mtx) => validate_babbage_tx(
mtx,
utxos,
Expand Down
16 changes: 7 additions & 9 deletions pallas-applying/src/shelley_ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::utils::{
add_minted_value, add_values, aux_data_from_alonzo_minted_tx, empty_value,
get_alonzo_comp_tx_size, get_lovelace_from_alonzo_val, get_payment_part, get_shelley_address,
get_val_size_in_words, mk_alonzo_vk_wits_check_list, values_are_equal, verify_signature,
FeePolicy,
ShelleyMAError::*,
ShelleyProtParams, UTxOs,
ValidationError::{self, *},
Expand Down Expand Up @@ -32,7 +31,7 @@ pub fn validate_shelley_ma_tx(
) -> ValidationResult {
let tx_body: &TransactionBody = &mtx.transaction_body;
let tx_wits: &MintedWitnessSet = &mtx.transaction_witness_set;
let size: &u64 = &get_alonzo_comp_tx_size(tx_body).ok_or(ShelleyMA(UnknownTxSize))?;
let size: &u32 = &get_alonzo_comp_tx_size(tx_body).ok_or(ShelleyMA(UnknownTxSize))?;
check_ins_not_empty(tx_body)?;
check_ins_in_utxos(tx_body, utxos)?;
check_ttl(tx_body, block_slot)?;
Expand Down Expand Up @@ -75,8 +74,8 @@ fn check_ttl(tx_body: &TransactionBody, block_slot: &u64) -> ValidationResult {
}
}

fn check_tx_size(size: &u64, prot_pps: &ShelleyProtParams) -> ValidationResult {
if *size > prot_pps.max_tx_size {
fn check_tx_size(size: &u32, prot_pps: &ShelleyProtParams) -> ValidationResult {
if *size > prot_pps.max_transaction_size {
return Err(ShelleyMA(MaxTxSizeExceeded));
}
Ok(())
Expand Down Expand Up @@ -104,10 +103,10 @@ fn check_min_lovelace(

fn compute_min_lovelace(output: &TransactionOutput, prot_pps: &ShelleyProtParams) -> u64 {
match &output.amount {
Value::Coin(_) => prot_pps.min_lovelace,
Value::Coin(_) => prot_pps.min_utxo_value,
Value::Multiasset(lovelace, _) => {
let utxo_entry_size: u64 = 27 + get_val_size_in_words(&output.amount);
let coins_per_utxo_word: u64 = prot_pps.min_lovelace / 27;
let coins_per_utxo_word: u64 = prot_pps.min_utxo_value / 27;
max(*lovelace, utxo_entry_size * coins_per_utxo_word)
}
}
Expand Down Expand Up @@ -174,11 +173,10 @@ fn get_produced(tx_body: &TransactionBody, era: &Era) -> Result<Value, Validatio

fn check_fees(
tx_body: &TransactionBody,
size: &u64,
size: &u32,
prot_pps: &ShelleyProtParams,
) -> ValidationResult {
let fee_policy: &FeePolicy = &prot_pps.fee_policy;
if tx_body.fee < fee_policy.summand + fee_policy.multiplier * size {
if tx_body.fee < (prot_pps.minfee_b + prot_pps.minfee_a * size) as u64 {
return Err(ShelleyMA(FeesBelowMin));
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions pallas-applying/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ pub use validation::*;

pub type UTxOs<'b> = HashMap<MultiEraInput<'b>, MultiEraOutput<'b>>;

pub fn get_alonzo_comp_tx_size(tx_body: &TransactionBody) -> Option<u64> {
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 u64),
Ok(()) => Some(buff.len() as u32),
Err(_) => None,
}
}

pub fn get_babbage_tx_size(tx_body: &MintedTransactionBody) -> Option<u64> {
pub fn get_babbage_tx_size(tx_body: &MintedTransactionBody) -> Option<u32> {
let mut buff: Vec<u8> = Vec::new();
match encode(tx_body, &mut buff) {
Ok(()) => Some(buff.len() as u64),
Ok(()) => Some(buff.len() as u32),
Err(_) => None,
}
}
Expand Down
Loading

0 comments on commit a0c409b

Please sign in to comment.