sidan_csl_rs/core/utils/
phase_two.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use pallas_primitives::conway::{CostModels, MintedTx, Redeemer};
use uplc::machine::cost_model::ExBudget;
use uplc::tx::error::Error;
use uplc::tx::{eval, iter_redeemers, DataLookupTable, ResolvedInput, SlotConfig};

pub enum PhaseTwoEvalResult {
    Success(Redeemer),
    Error(Redeemer, Error),
}

pub fn eval_phase_two(
    tx: &MintedTx,
    utxos: &[ResolvedInput],
    cost_mdls: Option<&CostModels>,
    slot_config: &SlotConfig,
) -> Result<Vec<PhaseTwoEvalResult>, Error> {
    let redeemers = tx.transaction_witness_set.redeemer.as_ref();

    let lookup_table = DataLookupTable::from_transaction(tx, utxos);

    match redeemers {
        Some(rs) => {
            let mut results = Vec::new();
            for (key, data, ex_units) in iter_redeemers(rs) {
                let remaining_budget = ExBudget {
                    cpu: i64::MAX,
                    mem: i64::MAX,
                };
                let redeemer = Redeemer {
                    tag: key.tag,
                    index: key.index,
                    data: data.clone(),
                    ex_units,
                };

                let eval_result = eval::eval_redeemer(
                    tx,
                    utxos,
                    slot_config,
                    &redeemer,
                    &lookup_table,
                    cost_mdls,
                    &remaining_budget,
                );

                match eval_result {
                    Ok(redeemer) => results.push(PhaseTwoEvalResult::Success(redeemer)),
                    Err(error) => results.push(PhaseTwoEvalResult::Error(redeemer, error)),
                }
            }

            Ok(results)
        }
        None => Ok(vec![]),
    }
}

// fn trim_cost_modes(cost_mdls: Option<&CostModels>) -> Option<CostModels> {
//     match cost_mdls {
//         None => None,
//         Some(mdls) => {
//             Some(CostModels {
//                 plutus_v1: mdls.plutus_v1.clone(),
//                 plutus_v2: mdls.plutus_v2.clone(),
//                 plutus_v3: match &mdls.plutus_v3 {
//                     None => None,
//                     Some(mdls_vec) => Some(mdls_vec[0..251].to_vec())
//                 }
//             })
//         }
//     }
// }