1+ {- FOURMOLU_DISABLE -}
2+
3+ {-# LANGUAGE RankNTypes #-}
4+
15-- NOTE: The contents of this module are originally copied from
26-- Cardano.Ledger.Alonzo.Plutus.Evaluate
37
@@ -7,17 +11,19 @@ module PSR.Evaluation.Ledger (evalTxExUnitsWithLogs, evalPwcExUnitsWithLogs) whe
711-- Imports
812--------------------------------------------------------------------------------
913
14+ -- import Cardano.Ledger.Plutus.TxInfo (transExUnits)
15+
1016import Cardano.Ledger.Alonzo.Core
1117import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (.. ), LedgerTxInfo (.. ))
1218import Cardano.Ledger.Alonzo.Plutus.Evaluate (TransactionScriptFailure (.. ))
1319import Cardano.Ledger.Alonzo.Scripts (ExUnits , lookupPlutusScript , plutusScriptLanguage , toAsIx )
1420import Cardano.Ledger.Alonzo.TxWits (unRedeemersL )
1521import Cardano.Ledger.Alonzo.UTxO (AlonzoScriptsNeeded (.. ))
16- import Cardano.Ledger.Plutus.CostModels (costModelsValid )
22+ import Cardano.Ledger.Plutus.CostModels (costModelsValid , getEvaluationContext )
1723import Cardano.Ledger.Plutus.Evaluate (
18- PlutusWithContext ,
19- evaluatePlutusWithContext ,
24+ PlutusWithContext (.. ),
2025 )
26+ import Cardano.Ledger.Plutus.Language (PlutusLanguage (.. ), PlutusRunnable )
2127import Cardano.Ledger.Plutus.TxInfo (exBudgetToExUnits )
2228import Cardano.Ledger.State (EraUTxO (.. ), ScriptsProvided (.. ), UTxO (.. ))
2329import Cardano.Slotting.EpochInfo (EpochInfo )
@@ -42,22 +48,54 @@ import Cardano.Ledger.Conway.Scripts qualified as Conway
4248-- Evaluators
4349--------------------------------------------------------------------------------
4450
51+ -- NOTE: There is also debugPlutusUnbounded in
52+ -- cardano-ledger:Cardano.Ledger.Plutus.Evaluate. It may be possible to use it
53+ -- to replace a lot of code from this module.
54+ --
55+ -- TODO: Explore "debugPlutusUnbounded".
56+
4557note :: e -> Maybe a -> Either e a
4658note _ (Just x) = Right x
4759note e Nothing = Left e
4860
61+ withRunnablePlutusWithContext ::
62+ PlutusWithContext ->
63+ -- | Handle the decoder failure
64+ (P. EvaluationError -> a ) ->
65+ (forall l . PlutusLanguage l => PlutusRunnable l -> PlutusArgs l -> a ) ->
66+ a
67+ withRunnablePlutusWithContext PlutusWithContext {pwcProtocolVersion, pwcScript, pwcArgs} onError f =
68+ case pwcScript of
69+ Right pr -> f pr pwcArgs
70+ Left plutus ->
71+ case decodePlutusRunnable pwcProtocolVersion plutus of
72+ Right pr -> f pr pwcArgs
73+ Left err -> onError (P. CodecError err)
74+
75+ evaluatePlutusWithContextBudget ::
76+ P. VerboseMode ->
77+ PlutusWithContext ->
78+ ([Text ], Either P. EvaluationError P. ExBudget )
79+ evaluatePlutusWithContextBudget mode pwc@ PlutusWithContext {.. } =
80+ withRunnablePlutusWithContext pwc (([] ,) . Left ) $
81+ -- NOTE: evaluatePlutusRunnableBudget is used for testing primarily and runs
82+ -- without any budget restrictions.
83+ evaluatePlutusRunnableBudget
84+ pwcProtocolVersion
85+ mode
86+ (getEvaluationContext pwcCostModel)
87+
4988evalPwcExUnitsWithLogs ::
5089 PlutusWithContext ->
5190 ExUnits ->
5291 PwcExecutionResult era
5392evalPwcExUnitsWithLogs pwc exUnits =
54- case evaluatePlutusWithContext P. Verbose pwc of
93+ case evaluatePlutusWithContextBudget P. Verbose pwc of
5594 (logs, Left err) -> Left $ ValidationFailure exUnits err logs pwc
5695 (logs, Right exBudget) ->
5796 note (IncompatibleBudget exBudget) $
5897 (pwc,logs,) <$> exBudgetToExUnits exBudget
5998
60- {- FOURMOLU_DISABLE -}
6199evalTxExUnitsWithLogs ::
62100 forall era .
63101 ( AlonzoEraTx era
@@ -83,7 +121,6 @@ evalTxExUnitsWithLogs ::
83121 -- Unlike `evalTxExUnits`, this function also returns evaluation logs, useful for
84122 -- debugging.
85123 RedeemerReportWithLogs era
86- {- FOURMOLU_ENABLE -}
87124evalTxExUnitsWithLogs ssi pp tx utxo epochInfo systemStart = Map. mapWithKey findAndCount rdmrs
88125 where
89126 keyedByPurpose (plutusPurpose, _) = hoistPlutusPurpose toAsIx plutusPurpose
0 commit comments