Skip to content

Commit

Permalink
Removed PlutusTx.Evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
bezirg committed Nov 30, 2021
1 parent 5b902f1 commit 1c80617
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 86 deletions.
2 changes: 1 addition & 1 deletion plutus-core/executables/uplc/Main.hs
Expand Up @@ -188,7 +188,7 @@ runEval (EvalOptions inp ifmt printMode budgetMode traceMode outputMode timingMo
case budgetM of
SomeBudgetMode bm -> evalWithTiming term >>= handleResults term
where
evaluate = Cek.runCek cekparams bm emitM . fromRight (error "input contains free variables") . runExcept @UPLC.FreeVariableError . UPLC.deBruijnTerm
evaluate = Cek.runCekDeBruijn cekparams bm emitM . fromRight (error "input contains free variables") . runExcept @UPLC.FreeVariableError . UPLC.deBruijnTerm
evalWithTiming t = case timingMode of
NoTiming -> pure $ evaluate t
Timing n -> do
Expand Down
Expand Up @@ -8,6 +8,7 @@ module UntypedPlutusCore.Evaluation.Machine.Cek
(
-- * Running the machine
runCek
, runCekDeBruijn
, runCekNoEmit
, unsafeRunCekNoEmit
, evaluateCek
Expand Down Expand Up @@ -86,6 +87,26 @@ allow one to specify an 'ExBudgetMode'. I.e. such functions are only for fully e
(and possibly returning logs). See also haddocks of 'enormousBudget'.
-}

-- | Evaluate a term using the CEK machine with logging enabled and keep track of costing.
-- A wrapper around the internal runCek to debruijn input and undebrijn output.
-- TODO: remove once we expose a direct debruijn api.
runCek
:: ( uni `Everywhere` ExMemoryUsage, Ix fun, PrettyUni uni fun, Monoid cost)
=> MachineParameters CekMachineCosts CekValue uni fun
-> ExBudgetMode cost uni fun
-> EmitterMode uni fun
-> Term Name uni fun ()
-> (Either (CekEvaluationException uni fun) (Term Name uni fun ()), cost, [Text])
runCek params mode emitMode term =
-- translating input
case runExcept @FreeVariableError $ deBruijnTerm term of
Left _ -> (error "freevarI", mempty, mempty)
Right dbt -> do
-- Don't use 'let': https://github.com/input-output-hk/plutus/issues/3876
case runCekDeBruijn params mode emitMode dbt of
-- translating back the output
(res, cost', emit) -> (unDeBruijnResult res, cost', emit)

-- | Evaluate a term using the CEK machine with logging disabled and keep track of costing.
runCekNoEmit
:: ( uni `Everywhere` ExMemoryUsage, Ix fun, PrettyUni uni fun, Monoid cost)
Expand All @@ -99,7 +120,7 @@ runCekNoEmit params mode term =
Left _ -> (error "freevarI", mempty)
Right dbt -> do
-- Don't use 'let': https://github.com/input-output-hk/plutus/issues/3876
case runCek params mode noEmitter dbt of
case runCekDeBruijn params mode noEmitter dbt of
-- translating back the output
(res, cost', _) -> (unDeBruijnResult res, cost')

Expand Down Expand Up @@ -132,7 +153,7 @@ evaluateCek emitMode params term =
Left _ -> (error "freevarI", mempty)
Right dbt ->
-- Don't use 'let': https://github.com/input-output-hk/plutus/issues/3876
case runCek params restrictingEnormous emitMode dbt of
case runCekDeBruijn params restrictingEnormous emitMode dbt of
-- translating back the output
(res, _, logs) -> (unDeBruijnResult res, logs)

Expand Down
Expand Up @@ -44,7 +44,7 @@ module UntypedPlutusCore.Evaluation.Machine.Cek.Internal
, StepKind(..)
, PrettyUni
, extractEvaluationResult
, runCek
, runCekDeBruijn
)
where

Expand Down Expand Up @@ -737,14 +737,14 @@ enterComputeCek = computeCek (toWordArray 0) where

-- See Note [Compilation peculiarities].
-- | Evaluate a term using the CEK machine and keep track of costing, logging is optional.
runCek
runCekDeBruijn
:: ( uni `Everywhere` ExMemoryUsage, Ix fun, PrettyUni uni fun)
=> MachineParameters CekMachineCosts CekValue uni fun
-> ExBudgetMode cost uni fun
-> EmitterMode uni fun
-> Term NamedDeBruijn uni fun ()
-> (Either (CekEvaluationExceptionD uni fun) (Term NamedDeBruijn uni fun ()), cost, [Text])
runCek params mode emitMode term =
runCekDeBruijn params mode emitMode term =
runCekM params mode emitMode $ do
spendBudgetCek BStartup (cekStartupCost ?cekCosts)
enterComputeCek NoFrame Env.empty term
12 changes: 7 additions & 5 deletions plutus-ledger-api/src/Plutus/V1/Ledger/Api.hs
Expand Up @@ -124,8 +124,7 @@ import Plutus.V1.Ledger.Credential
import Plutus.V1.Ledger.Crypto
import Plutus.V1.Ledger.DCert
import Plutus.V1.Ledger.Interval hiding (singleton)
import Plutus.V1.Ledger.Scripts hiding (mkTermToEvaluate)
import Plutus.V1.Ledger.Scripts qualified as Scripts
import Plutus.V1.Ledger.Scripts as Scripts
import Plutus.V1.Ledger.Time
import Plutus.V1.Ledger.TxId
import Plutus.V1.Ledger.Value
Expand Down Expand Up @@ -198,7 +197,10 @@ mkTermToEvaluate :: (MonadError EvaluationError m) => SerializedScript -> [PLC.D
mkTermToEvaluate bs args = do
s@(Script (UPLC.Program _ v _)) <- liftEither $ first CodecError $ CBOR.deserialiseOrFail $ fromStrict $ fromShort bs
unless (v == PLC.defaultVersion ()) $ throwError $ IncompatibleVersionError v
pure $ UPLC._progTerm $ Scripts.mkTermToEvaluate $ Scripts.applyArguments s args
let t = UPLC._progTerm $ unScript $ Scripts.applyArguments s args
-- add fake names to keep the api working on NamedDeBruijn
pure $ UPLC.termMapNames UPLC.fakeNameDeBruijn t



-- | Evaluates a script, with a cost model and a budget that restricts how many
Expand All @@ -221,7 +223,7 @@ evaluateScriptRestricting verbose cmdata budget p args = swap $ runWriter @LogOu
Nothing -> throwError CostModelParameterMismatch

let (res, UPLC.RestrictingSt (PLC.ExRestrictingBudget final), logs) =
UPLC.runCek
UPLC.runCekDeBruijn
(toMachineParameters model)
(UPLC.restricting $ PLC.ExRestrictingBudget budget)
(if verbose == Verbose then UPLC.logEmitter else UPLC.noEmitter)
Expand All @@ -248,7 +250,7 @@ evaluateScriptCounting verbose cmdata p args = swap $ runWriter @LogOutput $ run
Nothing -> throwError CostModelParameterMismatch

let (res, UPLC.CountingSt final, logs) =
UPLC.runCek
UPLC.runCekDeBruijn
(toMachineParameters model)
UPLC.counting
(if verbose == Verbose then UPLC.logEmitter else UPLC.noEmitter)
Expand Down
10 changes: 3 additions & 7 deletions plutus-ledger-api/src/Plutus/V1/Ledger/Scripts.hs
Expand Up @@ -33,7 +33,6 @@ module Plutus.V1.Ledger.Scripts(
applyValidator,
applyMintingPolicyScript,
applyStakeValidatorScript,
mkTermToEvaluate,
applyArguments,
-- * Script wrappers
mkValidatorScript,
Expand Down Expand Up @@ -81,11 +80,11 @@ import Plutus.V1.Ledger.Orphans ()
import PlutusCore qualified as PLC
import PlutusCore.Data qualified as PLC
import PlutusCore.Evaluation.Machine.ExBudget qualified as PLC
import PlutusCore.Evaluation.Machine.Exception (ErrorWithCause (..), EvaluationError (..))
import PlutusCore.MkPlc qualified as PLC
import PlutusTx (CompiledCode, FromData (..), ToData (..), UnsafeFromData (..), getPlc, makeLift)
import PlutusTx.Builtins as Builtins
import PlutusTx.Builtins.Internal as BI
import PlutusTx.Evaluation (ErrorWithCause (..), EvaluationError (..))
import PlutusTx.Prelude
import Prettyprinter
import Prettyprinter.Extras
Expand Down Expand Up @@ -182,14 +181,11 @@ applyArguments (Script (UPLC.Program a v t)) args =
applied = PLC.mkIterApp () t termArgs
in Script (UPLC.Program a v applied)

mkTermToEvaluate :: Script -> UPLC.Program UPLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun ()
mkTermToEvaluate = UPLC.programMapNames UPLC.fakeNameDeBruijn . unScript

-- | Evaluate a script, returning the trace log.
evaluateScript :: forall m . (MonadError ScriptError m) => Script -> m (PLC.ExBudget, [Text])
evaluateScript s = do
let p = mkTermToEvaluate s
(result, UPLC.TallyingSt _ budget, logOut) = UPLC.runCek PLC.defaultCekParameters UPLC.tallying UPLC.logEmitter t
let t = UPLC.termMapNames UPLC.fakeNameDeBruijn $ UPLC._progTerm $ unScript s
(result, UPLC.TallyingSt _ budget, logOut) = UPLC.runCekDeBruijn PLC.defaultCekParameters UPLC.tallying UPLC.logEmitter t
case result of
Right _ -> Haskell.pure (budget, logOut)
Left errWithCause@(ErrorWithCause err cause) -> throwError $ case err of
Expand Down
3 changes: 1 addition & 2 deletions plutus-tx-plugin/test/Budget/Lib.hs
Expand Up @@ -12,7 +12,6 @@ import Common
import PlutusCore qualified as PLC
import PlutusCore.Evaluation.Machine.ExBudget qualified as PLC
import PlutusTx.Code (CompiledCode, getPlc)
import PlutusTx.Evaluation qualified as PlutusTx
import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.Evaluation.Machine.Cek qualified as UPLC

Expand Down Expand Up @@ -41,5 +40,5 @@ measureBudget compiledCode =
in case programE of
Left _ -> Nothing
Right program ->
let (_, UPLC.TallyingSt _ budget, _) = PlutusTx.evaluateCekTrace program
let (_, UPLC.TallyingSt _ budget) = UPLC.runCekNoEmit PLC.defaultCekParameters UPLC.tallying $ program ^. UPLC.progTerm
in Just budget
9 changes: 4 additions & 5 deletions plutus-tx-plugin/test/Lib.hs
Expand Up @@ -12,21 +12,20 @@ module Lib where

import Common
import Control.Exception
import Control.Lens.Combinators (_1)
import Control.Lens
import Control.Monad.Except
import Data.Text (Text)
import Flat (Flat)

import PlcTestUtils

import PlutusPrelude (view)
import PlutusTx.Code
import PlutusTx.Evaluation

import PlutusCore qualified as PLC
import PlutusCore.Pretty

import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.Evaluation.Machine.Cek

instance (PLC.Closed uni, uni `PLC.Everywhere` Flat, uni `PLC.Everywhere` PrettyConst, PLC.GShow uni, Pretty fun, Flat fun) =>
ToUPlc (CompiledCodeIn uni fun a) uni fun where
Expand All @@ -43,7 +42,7 @@ runPlcCek :: ToUPlc a PLC.DefaultUni PLC.DefaultFun => [a] -> ExceptT SomeExcept
runPlcCek values = do
ps <- traverse toUPlc values
let p = foldl1 UPLC.applyProgram ps
either (throwError . SomeException) pure $ evaluateCek p
either (throwError . SomeException) pure $ evaluateCekNoEmit PLC.defaultCekParameters (p ^. UPLC.progTerm)

runPlcCekTrace ::
ToUPlc a PLC.DefaultUni PLC.DefaultFun =>
Expand All @@ -52,7 +51,7 @@ runPlcCekTrace ::
runPlcCekTrace values = do
ps <- traverse toUPlc values
let p = foldl1 UPLC.applyProgram ps
let (logOut, TallyingSt tally _, result) = evaluateCekTrace p
let (result, TallyingSt tally _, logOut) = runCek PLC.defaultCekParameters tallying logEmitter (p ^. UPLC.progTerm)
res <- either (throwError . SomeException) pure result
pure (logOut, tally, res)

Expand Down
1 change: 0 additions & 1 deletion plutus-tx/plutus-tx.cabal
Expand Up @@ -40,7 +40,6 @@ library
PlutusTx.Coverage
PlutusTx.TH
PlutusTx.Prelude
PlutusTx.Evaluation
PlutusTx.Applicative
PlutusTx.Base
PlutusTx.Bool
Expand Down
60 changes: 0 additions & 60 deletions plutus-tx/src/PlutusTx/Evaluation.hs

This file was deleted.

0 comments on commit 1c80617

Please sign in to comment.