Skip to content

Commit

Permalink
Merge branch 'main' into fet/script-data-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Oct 22, 2024
2 parents 37f4b34 + 061a779 commit 074b277
Show file tree
Hide file tree
Showing 28 changed files with 971 additions and 1,065 deletions.
84 changes: 43 additions & 41 deletions pallas-applying/src/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::utils::{
add_minted_value, add_values, aux_data_from_alonzo_minted_tx, compute_native_script_hash,
compute_plutus_script_hash, empty_value, get_alonzo_comp_tx_size, get_lovelace_from_alonzo_val,
get_network_id_value, get_payment_part, get_shelley_address, get_val_size_in_words,
compute_plutus_v1_script_hash, 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,
AlonzoError::*,
AlonzoProtParams, UTxOs,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn check_min_fee(
fn presence_of_plutus_scripts(mtx: &MintedTx) -> bool {
let minted_witness_set: &MintedWitnessSet = &mtx.transaction_witness_set;
match &minted_witness_set.plutus_script {
Some(plutus_scripts) => !plutus_scripts.is_empty(),
Some(plutus_v1_scripts) => !plutus_v1_scripts.is_empty(),
None => false,
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ fn check_tx_outs_network_id(tx_body: &TransactionBody, network_id: &u8) -> Valid
// global network ID.
fn check_tx_network_id(tx_body: &TransactionBody, network_id: &u8) -> ValidationResult {
if let Some(tx_network_id) = tx_body.network_id {
if get_network_id_value(tx_network_id) != *network_id {
if u8::from(tx_network_id) != *network_id {
return Err(Alonzo(TxWrongNetworkID));
}
}
Expand Down Expand Up @@ -402,11 +402,11 @@ fn check_witness_set(mtx: &MintedTx, utxos: &UTxOs) -> ValidationResult {
Some(scripts) => scripts.clone().iter().map(|x| x.clone().unwrap()).collect(),
None => Vec::new(),
};
let plutus_scripts: Vec<PlutusScript> = match &tx_wits.plutus_script {
let plutus_v1_scripts: Vec<PlutusScript<1>> = match &tx_wits.plutus_script {
Some(scripts) => scripts.clone(),
None => Vec::new(),
};
check_needed_scripts_are_included(tx_body, utxos, &native_scripts, &plutus_scripts)?;
check_needed_scripts_are_included(tx_body, utxos, &native_scripts, &plutus_v1_scripts)?;
check_datums(tx_body, utxos, &tx_wits.plutus_data)?;
check_redeemers(tx_body, tx_wits, utxos)?;
check_required_signers(&tx_body.required_signers, vkey_wits, tx_hash)?;
Expand All @@ -420,21 +420,23 @@ fn check_needed_scripts_are_included(
tx_body: &TransactionBody,
utxos: &UTxOs,
native_scripts: &[NativeScript],
plutus_scripts: &[PlutusScript],
plutus_v1_scripts: &[PlutusScript<1>],
) -> ValidationResult {
let mut native_scripts: Vec<(bool, NativeScript)> =
native_scripts.iter().map(|x| (false, x.clone())).collect();
let mut plutus_scripts: Vec<(bool, PlutusScript)> =
plutus_scripts.iter().map(|x| (false, x.clone())).collect();
check_script_inputs(tx_body, &mut native_scripts, &mut plutus_scripts, utxos)?;
check_minting_policies(tx_body, &mut native_scripts, &mut plutus_scripts)?;
let mut plutus_v1_scripts: Vec<(bool, PlutusScript<1>)> = plutus_v1_scripts
.iter()
.map(|x| (false, x.clone()))
.collect();
check_script_inputs(tx_body, &mut native_scripts, &mut plutus_v1_scripts, utxos)?;
check_minting_policies(tx_body, &mut native_scripts, &mut plutus_v1_scripts)?;
for (native_script_covered, _) in native_scripts.iter() {
if !native_script_covered {
return Err(Alonzo(UnneededNativeScript));
}
}
for (plutus_script_covered, _) in plutus_scripts.iter() {
if !plutus_script_covered {
for (plutus_v1_script_covered, _) in plutus_v1_scripts.iter() {
if !plutus_v1_script_covered {
return Err(Alonzo(UnneededPlutusScript));
}
}
Expand Down Expand Up @@ -554,7 +556,7 @@ fn mk_plutus_script_redeemer_pointers(
for (index, input) in sorted_inputs.iter().enumerate() {
if let Some(script_hash) = get_script_hash_from_input(input, utxos) {
for plutus_script in plutus_scripts.iter() {
let hashed_script: PolicyId = compute_plutus_script_hash(plutus_script);
let hashed_script: PolicyId = compute_plutus_v1_script_hash(plutus_script);
if script_hash == hashed_script {
res.push(RedeemerPointer {
tag: RedeemerTag::Spend,
Expand All @@ -564,23 +566,22 @@ fn mk_plutus_script_redeemer_pointers(
}
}
}
match mint {
Some(minted_value) => {
let sorted_policies: Vec<PolicyId> = sort_policies(minted_value);
for (index, policy) in sorted_policies.iter().enumerate() {
for plutus_script in plutus_scripts.iter() {
let hashed_script: PolicyId = compute_plutus_script_hash(plutus_script);
if *policy == hashed_script {
res.push(RedeemerPointer {
tag: RedeemerTag::Mint,
index: index as u32,
})
}

if let Some(minted_value) = mint {
let sorted_policies: Vec<PolicyId> = sort_policies(minted_value);
for (index, policy) in sorted_policies.iter().enumerate() {
for plutus_script in plutus_scripts.iter() {
let hashed_script: PolicyId = compute_plutus_v1_script_hash(plutus_script);
if *policy == hashed_script {
res.push(RedeemerPointer {
tag: RedeemerTag::Mint,
index: index as u32,
})
}
}
}
None => (),
}

res
}
None => Vec::new(),
Expand Down Expand Up @@ -626,7 +627,7 @@ fn redeemer_pointers_coincide(
fn check_script_inputs(
tx_body: &TransactionBody,
native_scripts: &mut [(bool, NativeScript)],
plutus_scripts: &mut [(bool, PlutusScript)],
plutus_v1_scripts: &mut [(bool, PlutusScript<1>)],
utxos: &UTxOs,
) -> ValidationResult {
let mut inputs: Vec<(bool, ScriptHash)> = get_script_hashes(tx_body, utxos);
Expand All @@ -638,8 +639,8 @@ fn check_script_inputs(
*native_script_covered = true;
}
}
for (plutus_script_covered, plutus_script) in plutus_scripts.iter_mut() {
let hashed_script: PolicyId = compute_plutus_script_hash(plutus_script);
for (plutus_script_covered, plutus_v1_script) in plutus_v1_scripts.iter_mut() {
let hashed_script: PolicyId = compute_plutus_v1_script_hash(plutus_v1_script);
if *input_script_hash == hashed_script {
*input_script_covered = true;
*plutus_script_covered = true;
Expand Down Expand Up @@ -678,7 +679,7 @@ fn get_script_hash_from_input(input: &TransactionInput, utxos: &UTxOs) -> Option
fn check_minting_policies(
tx_body: &TransactionBody,
native_scripts: &mut [(bool, NativeScript)],
plutus_scripts: &mut [(bool, PlutusScript)],
plutus_v1_scripts: &mut [(bool, PlutusScript<1>)],
) -> ValidationResult {
match &tx_body.mint {
None => Ok(()),
Expand All @@ -693,8 +694,8 @@ fn check_minting_policies(
*native_script_covered = true;
}
}
for (plutus_script_covered, plutus_script) in plutus_scripts.iter_mut() {
let hashed_script: PolicyId = compute_plutus_script_hash(plutus_script);
for (plutus_script_covered, plutus_v1_script) in plutus_v1_scripts.iter_mut() {
let hashed_script: PolicyId = compute_plutus_v1_script_hash(plutus_v1_script);
if *policy == hashed_script {
*policy_covered = true;
*plutus_script_covered = true;
Expand Down Expand Up @@ -724,10 +725,11 @@ fn check_vkey_input_wits(
let tx_hash: &Vec<u8> = &Vec::from(mtx.transaction_body.original_hash().as_ref());
let mut inputs_and_collaterals: Vec<TransactionInput> = Vec::new();
inputs_and_collaterals.extend(tx_body.inputs.clone());
match &tx_body.collateral {
Some(collaterals) => inputs_and_collaterals.extend(collaterals.clone()),
None => (),

if let Some(collaterals) = &tx_body.collateral {
inputs_and_collaterals.extend(collaterals.clone());
}

for input in inputs_and_collaterals.iter() {
match utxos.get(&MultiEraInput::from_alonzo_compatible(input)) {
Some(multi_era_output) => {
Expand Down Expand Up @@ -923,18 +925,18 @@ fn check_minting(tx_body: &TransactionBody, mtx: &MintedTx) -> ValidationResult
.map(|x| x.clone().unwrap())
.collect(),
};
let plutus_script_wits: Vec<PlutusScript> =
let plutus_v1_script_wits: Vec<PlutusScript<1>> =
match &mtx.transaction_witness_set.plutus_script {
None => Vec::new(),
Some(plutus_script_wits) => plutus_script_wits.clone(),
Some(plutus_v1_script_wits) => plutus_v1_script_wits.clone(),
};
for (policy, _) in minted_value.iter() {
if native_script_wits
.iter()
.all(|native_script| compute_native_script_hash(native_script) != *policy)
&& plutus_script_wits
.iter()
.all(|plutus_script| compute_plutus_script_hash(plutus_script) != *policy)
&& plutus_v1_script_wits.iter().all(|plutus_v1_script| {
compute_plutus_v1_script_hash(plutus_v1_script) != *policy
})
{
return Err(Alonzo(MintingLacksPolicy));
}
Expand Down
31 changes: 15 additions & 16 deletions pallas-applying/src/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use crate::utils::{
add_minted_value, add_values, aux_data_from_babbage_minted_tx, compute_native_script_hash,
compute_plutus_script_hash, compute_plutus_v2_script_hash, empty_value, get_babbage_tx_size,
get_lovelace_from_alonzo_val, get_network_id_value, get_payment_part, get_shelley_address,
get_val_size_in_words, is_byron_address, lovelace_diff_or_fail, mk_alonzo_vk_wits_check_list,
values_are_equal, verify_signature,
compute_plutus_v1_script_hash, compute_plutus_v2_script_hash, empty_value, get_babbage_tx_size,
get_lovelace_from_alonzo_val, get_payment_part, get_shelley_address, 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, UTxOs,
ValidationError::{self, *},
Expand All @@ -16,15 +16,14 @@ use pallas_codec::{
minicbor::{encode, Encoder},
utils::{Bytes, KeepRaw},
};
use pallas_crypto::hash::Hash;
use pallas_primitives::{
alonzo::{RedeemerPointer, RedeemerTag},
babbage::{
AddrKeyhash, Language, Mint, MintedTransactionBody, MintedTransactionOutput, MintedTx,
MintedWitnessSet, NativeScript, PlutusData, PlutusV1Script, PlutusV2Script, PolicyId,
PseudoDatumOption, PseudoScript, PseudoTransactionOutput, Redeemer, RequiredSigners,
TransactionInput, VKeyWitness, Value,
Language, Mint, MintedTransactionBody, MintedTransactionOutput, MintedTx, MintedWitnessSet,
NativeScript, PseudoDatumOption, PseudoScript, PseudoTransactionOutput, Redeemer,
RequiredSigners, VKeyWitness, Value,
},
AddrKeyhash, Hash, PlutusData, PlutusScript, PolicyId, TransactionInput,
};
use pallas_traverse::{MultiEraInput, MultiEraOutput, OriginalHash};
use std::ops::Deref;
Expand Down Expand Up @@ -166,11 +165,11 @@ fn check_min_fee(

fn presence_of_plutus_scripts(mtx: &MintedTx) -> bool {
let minted_witness_set: &MintedWitnessSet = &mtx.transaction_witness_set;
let plutus_v1_scripts: &[PlutusV1Script] = &minted_witness_set
let plutus_v1_scripts: &[PlutusScript<1>] = &minted_witness_set
.plutus_v1_script
.clone()
.unwrap_or_default();
let plutus_v2_scripts: &[PlutusV2Script] = &minted_witness_set
let plutus_v2_scripts: &[PlutusScript<2>] = &minted_witness_set
.plutus_v2_script
.clone()
.unwrap_or_default();
Expand Down Expand Up @@ -401,7 +400,7 @@ fn check_tx_outs_network_id(tx_body: &MintedTransactionBody, network_id: &u8) ->
// global network ID.
fn check_tx_network_id(tx_body: &MintedTransactionBody, network_id: &u8) -> ValidationResult {
if let Some(tx_network_id) = tx_body.network_id {
if get_network_id_value(tx_network_id) != *network_id {
if u8::from(tx_network_id) != *network_id {
return Err(Babbage(TxWrongNetworkID));
}
}
Expand Down Expand Up @@ -449,12 +448,12 @@ fn check_minting(tx_body: &MintedTransactionBody, mtx: &MintedTx) -> ValidationR
.map(|x| x.clone().unwrap())
.collect(),
};
let v1_script_wits: Vec<PlutusV1Script> =
let v1_script_wits: Vec<PlutusScript<1>> =
match &mtx.transaction_witness_set.plutus_v1_script {
None => Vec::new(),
Some(v1_script_wits) => v1_script_wits.clone(),
};
let v2_script_wits: Vec<PlutusV2Script> =
let v2_script_wits: Vec<PlutusScript<2>> =
match &mtx.transaction_witness_set.plutus_v2_script {
None => Vec::new(),
Some(v2_script_wits) => v2_script_wits.clone(),
Expand All @@ -465,7 +464,7 @@ fn check_minting(tx_body: &MintedTransactionBody, mtx: &MintedTx) -> ValidationR
.all(|script| compute_native_script_hash(script) != *policy)
&& v1_script_wits
.iter()
.all(|script| compute_plutus_script_hash(script) != *policy)
.all(|script| compute_plutus_v1_script_hash(script) != *policy)
&& v2_script_wits
.iter()
.all(|script| compute_plutus_v2_script_hash(script) != *policy)
Expand Down Expand Up @@ -500,7 +499,7 @@ fn check_witness_set(mtx: &MintedTx, utxos: &UTxOs) -> ValidationResult {
Some(scripts) => scripts
.clone()
.iter()
.map(compute_plutus_script_hash)
.map(compute_plutus_v1_script_hash)
.collect(),
None => Vec::new(),
};
Expand Down
28 changes: 16 additions & 12 deletions pallas-applying/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use pallas_codec::{
use pallas_crypto::key::ed25519::{PublicKey, Signature};
use pallas_primitives::{
alonzo::{
AddrKeyhash, AssetName, AuxiliaryData, Coin, Epoch, GenesisDelegateHash, Genesishash,
MintedTx as AlonzoMintedTx, Multiasset, NativeScript, NetworkId, PlutusScript, PolicyId,
PoolKeyhash, PoolMetadata, Relay, RewardAccount, StakeCredential, TransactionIndex,
UnitInterval, VKeyWitness, Value, VrfKeyhash,
AuxiliaryData, MintedTx as AlonzoMintedTx, Multiasset, NativeScript, VKeyWitness, Value,
},
babbage::{MintedTx as BabbageMintedTx, PlutusV2Script},
babbage::MintedTx as BabbageMintedTx,
AddrKeyhash, AssetName, Coin, Epoch, GenesisDelegateHash, Genesishash, NetworkId, PlutusScript,
PolicyId, PoolKeyhash, PoolMetadata, Relay, RewardAccount, StakeCredential, TransactionIndex,
UnitInterval, VrfKeyhash,
};
use pallas_traverse::{time::Slot, MultiEraInput, MultiEraOutput};
use std::collections::HashMap;
Expand Down Expand Up @@ -267,11 +267,9 @@ pub fn get_lovelace_from_alonzo_val(val: &Value) -> Coin {
}
}

#[deprecated(since = "0.31.0", note = "use `u8::from(...)` instead")]
pub fn get_network_id_value(network_id: NetworkId) -> u8 {
match network_id {
NetworkId::One => 0,
NetworkId::Two => 1,
}
u8::from(network_id)
}

pub fn mk_alonzo_vk_wits_check_list(
Expand Down Expand Up @@ -337,13 +335,18 @@ pub fn compute_native_script_hash(script: &NativeScript) -> PolicyId {
pallas_crypto::hash::Hasher::<224>::hash(&payload)
}

pub fn compute_plutus_script_hash(script: &PlutusScript) -> PolicyId {
#[deprecated(since = "0.31.0", note = "use `compute_plutus_v1_script_hash` instead")]
pub fn compute_plutus_script_hash(script: &PlutusScript<1>) -> PolicyId {
compute_plutus_v1_script_hash(script)
}

pub fn compute_plutus_v1_script_hash(script: &PlutusScript<1>) -> PolicyId {
let mut payload: Vec<u8> = Vec::from(script.as_ref());
payload.insert(0, 1);
pallas_crypto::hash::Hasher::<224>::hash(&payload)
}

pub fn compute_plutus_v2_script_hash(script: &PlutusV2Script) -> PolicyId {
pub fn compute_plutus_v2_script_hash(script: &PlutusScript<2>) -> PolicyId {
let mut payload: Vec<u8> = Vec::from(script.as_ref());
payload.insert(0, 2);
pallas_crypto::hash::Hasher::<224>::hash(&payload)
Expand Down Expand Up @@ -375,7 +378,8 @@ pub struct DState {
pub inst_rewards: InstantaneousRewards,
}

// Essentially part of the `PoolRegistration` component of `Certificate` at alonzo/src/model.rs
// Essentially part of the `PoolRegistration` component of `Certificate` at
// alonzo/src/model.rs
#[derive(Clone, Debug)]
pub struct PoolParam {
pub vrf_keyhash: VrfKeyhash,
Expand Down
13 changes: 7 additions & 6 deletions pallas-applying/src/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
//! era.
use pallas_primitives::{
alonzo::{
Coin, CostMdls, ExUnitPrices, ExUnits, Nonce, ProtocolVersion, RationalNumber, UnitInterval,
Coin, CostModels, ExUnitPrices, ExUnits, Nonce, ProtocolVersion, RationalNumber,
UnitInterval,
},
babbage::CostMdls as BabbageCostMdls,
conway::{CostMdls as ConwayCostMdls, Epoch},
babbage::CostModels as BabbageCostModels,
conway::{CostModels as ConwayCostModels, Epoch},
};

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -100,7 +101,7 @@ pub struct AlonzoProtParams {
pub protocol_version: ProtocolVersion,
pub min_pool_cost: Coin,
pub ada_per_utxo_byte: Coin,
pub cost_models_for_script_languages: CostMdls,
pub cost_models_for_script_languages: CostModels,
pub execution_costs: ExUnitPrices,
pub max_tx_ex_units: ExUnits,
pub max_block_ex_units: ExUnits,
Expand Down Expand Up @@ -128,7 +129,7 @@ pub struct BabbageProtParams {
pub protocol_version: ProtocolVersion,
pub min_pool_cost: Coin,
pub ada_per_utxo_byte: Coin,
pub cost_models_for_script_languages: BabbageCostMdls,
pub cost_models_for_script_languages: BabbageCostModels,
pub execution_costs: ExUnitPrices,
pub max_tx_ex_units: ExUnits,
pub max_block_ex_units: ExUnits,
Expand Down Expand Up @@ -156,7 +157,7 @@ pub struct ConwayProtParams {
pub protocol_version: ProtocolVersion,
pub min_pool_cost: Coin,
pub ada_per_utxo_byte: Coin,
pub cost_models_for_script_languages: ConwayCostMdls,
pub cost_models_for_script_languages: ConwayCostModels,
pub execution_costs: ExUnitPrices,
pub max_tx_ex_units: ExUnits,
pub max_block_ex_units: ExUnits,
Expand Down
Loading

0 comments on commit 074b277

Please sign in to comment.