moved cost_mdls language determination into eval_redeemer
This commit is contained in:
parent
472cea6c41
commit
6619a0e431
|
@ -1,5 +1,5 @@
|
|||
use pallas_primitives::{
|
||||
babbage::{CostMdls, Language, MintedTx, Redeemer, TransactionInput, TransactionOutput},
|
||||
babbage::{CostMdls, MintedTx, Redeemer, TransactionInput, TransactionOutput},
|
||||
Fragment,
|
||||
};
|
||||
use pallas_traverse::{Era, MultiEraTx};
|
||||
|
@ -16,19 +16,12 @@ pub fn eval_tx(
|
|||
tx: &MintedTx,
|
||||
utxos: &[ResolvedInput],
|
||||
cost_mdls: &CostMdls,
|
||||
version: &Language,
|
||||
slot_config: &SlotConfig,
|
||||
) -> anyhow::Result<Vec<Redeemer>> {
|
||||
let redeemers = tx.transaction_witness_set.redeemer.as_ref();
|
||||
|
||||
let lookup_table = eval::get_script_and_datum_lookup_table(tx, utxos);
|
||||
|
||||
let costs_maybe = match version {
|
||||
Language::PlutusV1 => cost_mdls.plutus_v1.as_ref(),
|
||||
Language::PlutusV2 => cost_mdls.plutus_v2.as_ref(),
|
||||
};
|
||||
|
||||
if let Some(costs) = costs_maybe {
|
||||
match redeemers {
|
||||
Some(rs) => {
|
||||
let mut collected_redeemers = vec![];
|
||||
|
@ -39,27 +32,19 @@ pub fn eval_tx(
|
|||
slot_config,
|
||||
redeemer,
|
||||
&lookup_table,
|
||||
version,
|
||||
costs,
|
||||
cost_mdls,
|
||||
)?)
|
||||
}
|
||||
Ok(collected_redeemers)
|
||||
}
|
||||
None => Ok(vec![]),
|
||||
}
|
||||
} else {
|
||||
Err(anyhow::Error::msg(format!(
|
||||
"Missing cost model for version: {:?}",
|
||||
version
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_tx_raw(
|
||||
tx_bytes: &[u8],
|
||||
utxos_bytes: &[(Vec<u8>, Vec<u8>)],
|
||||
cost_mdls_bytes: &[u8],
|
||||
version_bytes: u32,
|
||||
slot_config: (u64, u64),
|
||||
) -> Result<Vec<Vec<u8>>, Error> {
|
||||
let multi_era_tx = MultiEraTx::decode(Era::Babbage, tx_bytes)
|
||||
|
@ -82,14 +67,8 @@ pub fn eval_tx_raw(
|
|||
slot_length: slot_config.1,
|
||||
};
|
||||
|
||||
let version = match version_bytes {
|
||||
1 => Language::PlutusV1,
|
||||
2 => Language::PlutusV2,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
match multi_era_tx {
|
||||
MultiEraTx::Babbage(tx) => match eval_tx(&tx, &utxos, &cost_mdls, &version, &sc) {
|
||||
MultiEraTx::Babbage(tx) => match eval_tx(&tx, &utxos, &cost_mdls, &sc) {
|
||||
Ok(redeemers) => Ok(redeemers
|
||||
.iter()
|
||||
.map(|r| r.encode_fragment().unwrap())
|
||||
|
@ -112,7 +91,7 @@ pub fn eval_tx_raw(
|
|||
mod tests {
|
||||
use pallas_codec::utils::MaybeIndefArray;
|
||||
use pallas_primitives::{
|
||||
babbage::{CostMdls, Language, TransactionInput, TransactionOutput},
|
||||
babbage::{CostMdls, TransactionInput, TransactionOutput},
|
||||
Fragment,
|
||||
};
|
||||
use pallas_traverse::{Era, MultiEraTx};
|
||||
|
@ -343,8 +322,7 @@ mod tests {
|
|||
.unwrap();
|
||||
match multi_era_tx {
|
||||
MultiEraTx::Babbage(tx) => {
|
||||
let redeemers =
|
||||
eval_tx(&tx, &utxos, &cost_mdl, &Language::PlutusV2, &slot_config).unwrap();
|
||||
let redeemers = eval_tx(&tx, &utxos, &cost_mdl, &slot_config).unwrap();
|
||||
|
||||
assert_eq!(redeemers.len(), 1)
|
||||
}
|
||||
|
@ -578,8 +556,7 @@ mod tests {
|
|||
.unwrap();
|
||||
match multi_era_tx {
|
||||
MultiEraTx::Babbage(tx) => {
|
||||
let redeemers =
|
||||
eval_tx(&tx, &utxos, &cost_mdl, &Language::PlutusV2, &slot_config).unwrap();
|
||||
let redeemers = eval_tx(&tx, &utxos, &cost_mdl, &slot_config).unwrap();
|
||||
|
||||
println!("{:?}", redeemers.len());
|
||||
}
|
||||
|
@ -649,8 +626,7 @@ mod tests {
|
|||
.unwrap();
|
||||
match multi_era_tx {
|
||||
MultiEraTx::Babbage(tx) => {
|
||||
let redeemers =
|
||||
eval_tx(&tx, &utxos, &cost_mdl, &Language::PlutusV1, &slot_config).unwrap();
|
||||
let redeemers = eval_tx(&tx, &utxos, &cost_mdl, &slot_config).unwrap();
|
||||
|
||||
println!("{:?}", redeemers.len());
|
||||
}
|
||||
|
|
|
@ -7,9 +7,9 @@ use pallas_addresses::{Address, ScriptHash, StakePayload};
|
|||
use pallas_codec::utils::{KeyValuePairs, MaybeIndefArray};
|
||||
use pallas_crypto::hash::Hash;
|
||||
use pallas_primitives::babbage::{
|
||||
Certificate, DatumHash, DatumOption, ExUnits, Language, Mint, MintedTx, PlutusV1Script,
|
||||
PlutusV2Script, PolicyId, Redeemer, RedeemerTag, RewardAccount, Script, StakeCredential,
|
||||
TransactionInput, TransactionOutput, Value, Withdrawals,
|
||||
Certificate, CostMdls, DatumHash, DatumOption, ExUnits, Language, Mint, MintedTx,
|
||||
PlutusV1Script, PlutusV2Script, PolicyId, Redeemer, RedeemerTag, RewardAccount, Script,
|
||||
StakeCredential, TransactionInput, TransactionOutput, Value, Withdrawals,
|
||||
};
|
||||
use pallas_traverse::{ComputeHash, OriginalHash};
|
||||
use std::{collections::HashMap, convert::TryInto, ops::Deref, vec};
|
||||
|
@ -556,8 +556,7 @@ pub fn eval_redeemer(
|
|||
slot_config: &SlotConfig,
|
||||
redeemer: &Redeemer,
|
||||
lookup_table: &DataLookupTable,
|
||||
version: &Language,
|
||||
costs: &[i64],
|
||||
cost_mdls: &CostMdls,
|
||||
) -> anyhow::Result<Redeemer> {
|
||||
let purpose = get_script_purpose(
|
||||
redeemer,
|
||||
|
@ -583,11 +582,17 @@ pub fn eval_redeemer(
|
|||
prog.into()
|
||||
};
|
||||
|
||||
let costs = if let Some(costs) = &cost_mdls.plutus_v1 {
|
||||
costs
|
||||
} else {
|
||||
return Err(anyhow::Error::msg("PlutusV1 cost model not found."));
|
||||
};
|
||||
|
||||
let result = program
|
||||
.apply_data(datum)
|
||||
.apply_data(redeemer.data.clone())
|
||||
.apply_data(script_context.to_plutus_data())
|
||||
.eval_with_params(version, costs);
|
||||
.eval_with_params(&Language::PlutusV1, &costs);
|
||||
|
||||
match result.0 {
|
||||
Ok(_) => {}
|
||||
|
@ -618,11 +623,17 @@ pub fn eval_redeemer(
|
|||
prog.into()
|
||||
};
|
||||
|
||||
let costs = if let Some(costs) = &cost_mdls.plutus_v2 {
|
||||
costs
|
||||
} else {
|
||||
return Err(anyhow::Error::msg("PlutusV2 cost model not found."));
|
||||
};
|
||||
|
||||
let result = program
|
||||
.apply_data(datum)
|
||||
.apply_data(redeemer.data.clone())
|
||||
.apply_data(script_context.to_plutus_data())
|
||||
.eval();
|
||||
.eval_with_params(&Language::PlutusV2, &costs);
|
||||
|
||||
match result.0 {
|
||||
Ok(_) => {}
|
||||
|
@ -655,10 +666,16 @@ pub fn eval_redeemer(
|
|||
prog.into()
|
||||
};
|
||||
|
||||
let costs = if let Some(costs) = &cost_mdls.plutus_v1 {
|
||||
costs
|
||||
} else {
|
||||
return Err(anyhow::Error::msg("PlutusV1 cost model not found."));
|
||||
};
|
||||
|
||||
let result = program
|
||||
.apply_data(redeemer.data.clone())
|
||||
.apply_data(script_context.to_plutus_data())
|
||||
.eval();
|
||||
.eval_with_params(&Language::PlutusV1, &costs);
|
||||
|
||||
match result.0 {
|
||||
Ok(_) => {}
|
||||
|
@ -689,10 +706,16 @@ pub fn eval_redeemer(
|
|||
prog.into()
|
||||
};
|
||||
|
||||
let costs = if let Some(costs) = &cost_mdls.plutus_v2 {
|
||||
costs
|
||||
} else {
|
||||
return Err(anyhow::Error::msg("PlutusV2 cost model not found."));
|
||||
};
|
||||
|
||||
let result = program
|
||||
.apply_data(redeemer.data.clone())
|
||||
.apply_data(script_context.to_plutus_data())
|
||||
.eval();
|
||||
.eval_with_params(&Language::PlutusV2, &costs);
|
||||
|
||||
match result.0 {
|
||||
Ok(_) => {}
|
||||
|
|
Loading…
Reference in New Issue