feat: rename transaction eval and add error enum

This commit is contained in:
rvcas 2022-09-18 15:35:10 -04:00
parent 68fc0f643e
commit 9e280f9cb5
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
8 changed files with 34 additions and 26 deletions

View File

@ -9,8 +9,10 @@ use uplc::{
ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term}, ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term},
machine::cost_model::ExBudget, machine::cost_model::ExBudget,
parser, parser,
transaction_eval::eval_tx, tx::{
transaction_eval::script_context::{ResolvedInput, SlotConfig}, self,
script_context::{ResolvedInput, SlotConfig},
},
}; };
mod args; mod args;
@ -52,7 +54,7 @@ fn main() -> anyhow::Result<()> {
slot_length, slot_length,
}; };
eval_tx(tx_babbage, &resolved_inputs, None, &slot_config)?; tx::eval(tx_babbage, &resolved_inputs, None, &slot_config)?;
} }
} }
}, },

View File

@ -6,11 +6,11 @@ pub mod machine;
pub mod parser; pub mod parser;
mod pretty; mod pretty;
pub mod program_builder; pub mod program_builder;
pub mod transaction_eval; pub mod tx;
pub use pallas_primitives::alonzo::PlutusData; pub use pallas_primitives::alonzo::PlutusData;
pub type Error = Box<dyn std::error::Error>;
use pallas_primitives::Fragment; use pallas_primitives::{Error, Fragment};
pub fn plutus_data(bytes: &[u8]) -> Result<PlutusData, Error> { pub fn plutus_data(bytes: &[u8]) -> Result<PlutusData, Error> {
PlutusData::decode_fragment(bytes) PlutusData::decode_fragment(bytes)

View File

@ -1,12 +1,10 @@
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use thiserror::Error;
use crate::ast::{NamedDeBruijn, Term, Type}; use crate::ast::{NamedDeBruijn, Term, Type};
use super::{ExBudget, Value}; use super::{ExBudget, Value};
#[derive(Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("Over budget mem: {} & cpu: {}", .0.mem, .0.cpu)] #[error("Over budget mem: {} & cpu: {}", .0.mem, .0.cpu)]
OutOfExError(ExBudget), OutOfExError(ExBudget),

View File

@ -4,20 +4,20 @@ use pallas_primitives::{
}; };
use pallas_traverse::{Era, MultiEraTx}; use pallas_traverse::{Era, MultiEraTx};
use crate::Error; use error::Error;
use script_context::{ResolvedInput, SlotConfig};
use self::script_context::{ResolvedInput, SlotConfig};
mod error;
mod eval; mod eval;
pub mod script_context; pub mod script_context;
mod to_plutus_data; mod to_plutus_data;
pub fn eval_tx( pub fn eval(
tx: &MintedTx, tx: &MintedTx,
utxos: &[ResolvedInput], utxos: &[ResolvedInput],
cost_mdls: Option<&CostMdls>, cost_mdls: Option<&CostMdls>,
slot_config: &SlotConfig, slot_config: &SlotConfig,
) -> anyhow::Result<Vec<Redeemer>> { ) -> Result<Vec<Redeemer>, Error> {
let redeemers = tx.transaction_witness_set.redeemer.as_ref(); let redeemers = tx.transaction_witness_set.redeemer.as_ref();
let lookup_table = eval::get_script_and_datum_lookup_table(tx, utxos); let lookup_table = eval::get_script_and_datum_lookup_table(tx, utxos);
@ -41,7 +41,7 @@ pub fn eval_tx(
} }
} }
pub fn eval_tx_raw( pub fn eval_raw(
tx_bytes: &[u8], tx_bytes: &[u8],
utxos_bytes: &[(Vec<u8>, Vec<u8>)], utxos_bytes: &[(Vec<u8>, Vec<u8>)],
cost_mdls_bytes: &[u8], cost_mdls_bytes: &[u8],
@ -68,7 +68,7 @@ pub fn eval_tx_raw(
}; };
match multi_era_tx { match multi_era_tx {
MultiEraTx::Babbage(tx) => match eval_tx(&tx, &utxos, Some(&cost_mdls), &sc) { MultiEraTx::Babbage(tx) => match eval(&tx, &utxos, Some(&cost_mdls), &sc) {
Ok(redeemers) => Ok(redeemers Ok(redeemers) => Ok(redeemers
.iter() .iter()
.map(|r| r.encode_fragment().unwrap()) .map(|r| r.encode_fragment().unwrap())
@ -96,7 +96,7 @@ mod tests {
}; };
use pallas_traverse::{Era, MultiEraTx}; use pallas_traverse::{Era, MultiEraTx};
use super::{eval_tx, ResolvedInput, SlotConfig}; use super::{eval, ResolvedInput, SlotConfig};
#[test] #[test]
fn test_eval() { fn test_eval() {
@ -322,7 +322,7 @@ mod tests {
.unwrap(); .unwrap();
match multi_era_tx { match multi_era_tx {
MultiEraTx::Babbage(tx) => { MultiEraTx::Babbage(tx) => {
let redeemers = eval_tx(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap(); let redeemers = eval(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap();
assert_eq!(redeemers.len(), 1) assert_eq!(redeemers.len(), 1)
} }
@ -556,7 +556,7 @@ mod tests {
.unwrap(); .unwrap();
match multi_era_tx { match multi_era_tx {
MultiEraTx::Babbage(tx) => { MultiEraTx::Babbage(tx) => {
let redeemers = eval_tx(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap(); let redeemers = eval(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap();
println!("{:?}", redeemers.len()); println!("{:?}", redeemers.len());
} }
@ -626,7 +626,7 @@ mod tests {
.unwrap(); .unwrap();
match multi_era_tx { match multi_era_tx {
MultiEraTx::Babbage(tx) => { MultiEraTx::Babbage(tx) => {
let redeemers = eval_tx(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap(); let redeemers = eval(&tx, &utxos, Some(&cost_mdl), &slot_config).unwrap();
println!("{:?}", redeemers.len()); println!("{:?}", redeemers.len());
} }

View File

@ -0,0 +1,7 @@
use crate::machine;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Runtime error")]
Machine(#[from] machine::Error),
}

View File

@ -20,6 +20,7 @@ use super::{
TxInfoV1, TxInfoV2, TxOut, TxInfoV1, TxInfoV2, TxOut,
}, },
to_plutus_data::ToPlutusData, to_plutus_data::ToPlutusData,
Error,
}; };
fn slot_to_begin_posix_time(slot: u64, sc: &SlotConfig) -> u64 { fn slot_to_begin_posix_time(slot: u64, sc: &SlotConfig) -> u64 {
@ -58,7 +59,7 @@ pub struct DataLookupTable {
pub fn get_tx_in_info_v1( pub fn get_tx_in_info_v1(
inputs: &[TransactionInput], inputs: &[TransactionInput],
utxos: &[ResolvedInput], utxos: &[ResolvedInput],
) -> anyhow::Result<Vec<TxInInfo>> { ) -> Result<Vec<TxInInfo>, Error> {
let result = inputs let result = inputs
.iter() .iter()
.map(|input| { .map(|input| {
@ -105,7 +106,7 @@ pub fn get_tx_in_info_v1(
fn get_tx_in_info_v2( fn get_tx_in_info_v2(
inputs: &[TransactionInput], inputs: &[TransactionInput],
utxos: &[ResolvedInput], utxos: &[ResolvedInput],
) -> anyhow::Result<Vec<TxInInfo>> { ) -> Result<Vec<TxInInfo>, Error> {
let result = inputs let result = inputs
.iter() .iter()
.map(|input| { .map(|input| {
@ -142,7 +143,7 @@ fn get_script_purpose(
mint: &Option<Mint>, mint: &Option<Mint>,
dcert: &Option<Vec<Certificate>>, dcert: &Option<Vec<Certificate>>,
wdrl: &Option<Withdrawals>, wdrl: &Option<Withdrawals>,
) -> anyhow::Result<ScriptPurpose> { ) -> Result<ScriptPurpose, Error> {
// sorting according to specs section 4.1: https://hydra.iohk.io/build/18583827/download/1/alonzo-changes.pdf // sorting according to specs section 4.1: https://hydra.iohk.io/build/18583827/download/1/alonzo-changes.pdf
let tag = redeemer.tag.clone(); let tag = redeemer.tag.clone();
let index = redeemer.index; let index = redeemer.index;
@ -226,7 +227,7 @@ fn get_tx_info_v1(
tx: &MintedTx, tx: &MintedTx,
utxos: &[ResolvedInput], utxos: &[ResolvedInput],
slot_config: &SlotConfig, slot_config: &SlotConfig,
) -> anyhow::Result<TxInfo> { ) -> Result<TxInfo, Error> {
let body = tx.transaction_body.clone(); let body = tx.transaction_body.clone();
if body.reference_inputs.is_some() { if body.reference_inputs.is_some() {
@ -289,7 +290,7 @@ fn get_tx_info_v2(
tx: &MintedTx, tx: &MintedTx,
utxos: &[ResolvedInput], utxos: &[ResolvedInput],
slot_config: &SlotConfig, slot_config: &SlotConfig,
) -> anyhow::Result<TxInfo> { ) -> Result<TxInfo, Error> {
let body = tx.transaction_body.clone(); let body = tx.transaction_body.clone();
let inputs = get_tx_in_info_v2(&body.inputs, utxos)?; let inputs = get_tx_in_info_v2(&body.inputs, utxos)?;
@ -557,7 +558,7 @@ pub fn eval_redeemer(
redeemer: &Redeemer, redeemer: &Redeemer,
lookup_table: &DataLookupTable, lookup_table: &DataLookupTable,
cost_mdls_opt: Option<&CostMdls>, cost_mdls_opt: Option<&CostMdls>,
) -> anyhow::Result<Redeemer> { ) -> Result<Redeemer, Error> {
let purpose = get_script_purpose( let purpose = get_script_purpose(
redeemer, redeemer,
&tx.transaction_body.inputs, &tx.transaction_body.inputs,