Skip to content

Commit f1802ff

Browse files
adithyaovtweag-ev-ak
authored andcommitted
Run the local plutus script without any budget constraints
1 parent 94a0170 commit f1802ff

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

lib/PSR/Evaluation/Ledger.hs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
1016
import Cardano.Ledger.Alonzo.Core
1117
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (..), LedgerTxInfo (..))
1218
import Cardano.Ledger.Alonzo.Plutus.Evaluate (TransactionScriptFailure (..))
1319
import Cardano.Ledger.Alonzo.Scripts (ExUnits, lookupPlutusScript, plutusScriptLanguage, toAsIx)
1420
import Cardano.Ledger.Alonzo.TxWits (unRedeemersL)
1521
import Cardano.Ledger.Alonzo.UTxO (AlonzoScriptsNeeded (..))
16-
import Cardano.Ledger.Plutus.CostModels (costModelsValid)
22+
import Cardano.Ledger.Plutus.CostModels (costModelsValid, getEvaluationContext)
1723
import Cardano.Ledger.Plutus.Evaluate (
18-
PlutusWithContext,
19-
evaluatePlutusWithContext,
24+
PlutusWithContext (..),
2025
)
26+
import Cardano.Ledger.Plutus.Language (PlutusLanguage (..), PlutusRunnable)
2127
import Cardano.Ledger.Plutus.TxInfo (exBudgetToExUnits)
2228
import Cardano.Ledger.State (EraUTxO (..), ScriptsProvided (..), UTxO (..))
2329
import 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+
4557
note :: e -> Maybe a -> Either e a
4658
note _ (Just x) = Right x
4759
note 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+
4988
evalPwcExUnitsWithLogs ::
5089
PlutusWithContext ->
5190
ExUnits ->
5291
PwcExecutionResult era
5392
evalPwcExUnitsWithLogs 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 -}
6199
evalTxExUnitsWithLogs ::
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 -}
87124
evalTxExUnitsWithLogs ssi pp tx utxo epochInfo systemStart = Map.mapWithKey findAndCount rdmrs
88125
where
89126
keyedByPurpose (plutusPurpose, _) = hoistPlutusPurpose toAsIx plutusPurpose

0 commit comments

Comments
 (0)