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},
machine::cost_model::ExBudget,
parser,
transaction_eval::eval_tx,
transaction_eval::script_context::{ResolvedInput, SlotConfig},
tx::{
self,
script_context::{ResolvedInput, SlotConfig},
},
};
mod args;
@ -52,7 +54,7 @@ fn main() -> anyhow::Result<()> {
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;
mod pretty;
pub mod program_builder;
pub mod transaction_eval;
pub mod tx;
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> {
PlutusData::decode_fragment(bytes)

View File

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

View File

@ -4,20 +4,20 @@ use pallas_primitives::{
};
use pallas_traverse::{Era, MultiEraTx};
use crate::Error;
use self::script_context::{ResolvedInput, SlotConfig};
use error::Error;
use script_context::{ResolvedInput, SlotConfig};
mod error;
mod eval;
pub mod script_context;
mod to_plutus_data;
pub fn eval_tx(
pub fn eval(
tx: &MintedTx,
utxos: &[ResolvedInput],
cost_mdls: Option<&CostMdls>,
slot_config: &SlotConfig,
) -> anyhow::Result<Vec<Redeemer>> {
) -> Result<Vec<Redeemer>, Error> {
let redeemers = tx.transaction_witness_set.redeemer.as_ref();
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],
utxos_bytes: &[(Vec<u8>, Vec<u8>)],
cost_mdls_bytes: &[u8],
@ -68,7 +68,7 @@ pub fn eval_tx_raw(
};
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
.iter()
.map(|r| r.encode_fragment().unwrap())
@ -96,7 +96,7 @@ mod tests {
};
use pallas_traverse::{Era, MultiEraTx};
use super::{eval_tx, ResolvedInput, SlotConfig};
use super::{eval, ResolvedInput, SlotConfig};
#[test]
fn test_eval() {
@ -322,7 +322,7 @@ mod tests {
.unwrap();
match multi_era_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)
}
@ -556,7 +556,7 @@ mod tests {
.unwrap();
match multi_era_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());
}
@ -626,7 +626,7 @@ mod tests {
.unwrap();
match multi_era_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());
}

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,
},
to_plutus_data::ToPlutusData,
Error,
};
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(
inputs: &[TransactionInput],
utxos: &[ResolvedInput],
) -> anyhow::Result<Vec<TxInInfo>> {
) -> Result<Vec<TxInInfo>, Error> {
let result = inputs
.iter()
.map(|input| {
@ -105,7 +106,7 @@ pub fn get_tx_in_info_v1(
fn get_tx_in_info_v2(
inputs: &[TransactionInput],
utxos: &[ResolvedInput],
) -> anyhow::Result<Vec<TxInInfo>> {
) -> Result<Vec<TxInInfo>, Error> {
let result = inputs
.iter()
.map(|input| {
@ -142,7 +143,7 @@ fn get_script_purpose(
mint: &Option<Mint>,
dcert: &Option<Vec<Certificate>>,
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
let tag = redeemer.tag.clone();
let index = redeemer.index;
@ -226,7 +227,7 @@ fn get_tx_info_v1(
tx: &MintedTx,
utxos: &[ResolvedInput],
slot_config: &SlotConfig,
) -> anyhow::Result<TxInfo> {
) -> Result<TxInfo, Error> {
let body = tx.transaction_body.clone();
if body.reference_inputs.is_some() {
@ -289,7 +290,7 @@ fn get_tx_info_v2(
tx: &MintedTx,
utxos: &[ResolvedInput],
slot_config: &SlotConfig,
) -> anyhow::Result<TxInfo> {
) -> Result<TxInfo, Error> {
let body = tx.transaction_body.clone();
let inputs = get_tx_in_info_v2(&body.inputs, utxos)?;
@ -557,7 +558,7 @@ pub fn eval_redeemer(
redeemer: &Redeemer,
lookup_table: &DataLookupTable,
cost_mdls_opt: Option<&CostMdls>,
) -> anyhow::Result<Redeemer> {
) -> Result<Redeemer, Error> {
let purpose = get_script_purpose(
redeemer,
&tx.transaction_body.inputs,