Skip to content

Commit

Permalink
WIP add EmitterTy to specify emit time or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
thealmarty committed Jul 27, 2021
1 parent 949d7a5 commit b34d3a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
1 change: 0 additions & 1 deletion plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ test-suite plutus-core-test
tasty-golden -any,
tasty-hedgehog -any,
tasty-hunit -any,
tasty-hunit -any,
text -any,
transformers -any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ runCekNoEmit
-> Term Name uni fun ()
-> (Either (CekEvaluationException uni fun) (Term Name uni fun ()), cost)
runCekNoEmit params mode term =
case runCek params mode False term of
case runCek params mode NoEmit term of
(errOrRes, cost', _) -> (errOrRes, cost')

-- | Unsafely evaluate a term using the CEK machine with logging disabled and keep track of costing.
Expand All @@ -99,7 +99,7 @@ evaluateCek
-> Term Name uni fun ()
-> (Either (CekEvaluationException uni fun) (Term Name uni fun ()), [String])
evaluateCek params term =
case runCek params restrictingEnormous True term of
case runCek params restrictingEnormous Emit term of
(errOrRes, _, logs) -> (errOrRes, logs)

-- | Evaluate a term using the CEK machine with logging disabled.
Expand All @@ -110,6 +110,16 @@ evaluateCekNoEmit
-> Either (CekEvaluationException uni fun) (Term Name uni fun ())
evaluateCekNoEmit params = fst . runCekNoEmit params restrictingEnormous

-- | Evaluate a term using the CEK machine with timestamped logging enabled.
evaluateCekWithTime
:: ( uni `Everywhere` ExMemoryUsage, Ix fun, PrettyUni uni fun)
=> MachineParameters CekMachineCosts CekValue uni fun
-> Term Name uni fun ()
-> (Either (CekEvaluationException uni fun) (Term Name uni fun ()), [String])
evaluateCekWithTime params term =
case runCek params restrictingEnormous EmitWithTimestamp term of
(errOrRes, _, logs) -> (errOrRes, logs)

-- | Evaluate a term using the CEK machine with logging enabled. May throw a 'CekMachineException'.
unsafeEvaluateCek
:: ( GShow uni, Typeable uni
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module UntypedPlutusCore.Evaluation.Machine.Cek.Internal
, ExBudgetMode(..)
, CekCarryingM (..)
, CekM
, EmitterTy (..)
, ErrorWithCause(..)
, EvaluationError(..)
, ExBudgetCategory(..)
Expand Down Expand Up @@ -392,6 +393,14 @@ type CekEvaluationException uni fun = CekEvaluationExceptionCarrying (Term Name
-- | The set of constraints we need to be able to print things in universes, which we need in order to throw exceptions.
type PrettyUni uni fun = (GShow uni, Closed uni, Pretty fun, Typeable uni, Typeable fun, Everywhere uni PrettyConst)

-- | Describe whether to emit or not. And if emitting, should it include timestamp?
-- The timestamp is for profiling purposes.
-- Don't emit timestamps if you are running tests.
data EmitterTy
= NoEmit
| Emit
| EmitWithTimestamp

{- Note [Throwing exceptions in ST]
This note represents MPJ's best understanding right now, might be wrong.
Expand Down Expand Up @@ -564,12 +573,16 @@ runCekM
(PrettyUni uni fun)
=> MachineParameters CekMachineCosts CekValue uni fun
-> ExBudgetMode cost uni fun
-> Bool
-> EmitterTy
-> (forall s. GivenCekReqs uni fun s => CekM uni fun s a)
-> (Either (CekEvaluationException uni fun) a, cost, [String])
runCekM (MachineParameters costs runtime) (ExBudgetMode getExBudgetInfo) emitting a = runST $ do
exBudgetMode <- getExBudgetInfo
mayLogsRef <- if emitting then Just <$> newSTRef DList.empty else pure Nothing
mayLogsRef <-
case emitting of
Emit -> Just <$> newSTRef DList.empty
NoEmit -> pure Nothing
EmitWithTimestamp -> Just <$> newSTRef DList.empty
let ?cekRuntime = runtime
?cekEmitter = mayLogsRef
?cekBudgetSpender = _exBudgetModeSpender exBudgetMode
Expand Down Expand Up @@ -790,7 +803,7 @@ runCek
:: ( uni `Everywhere` ExMemoryUsage, Ix fun, PrettyUni uni fun)
=> MachineParameters CekMachineCosts CekValue uni fun
-> ExBudgetMode cost uni fun
-> Bool
-> EmitterTy
-> Term Name uni fun ()
-> (Either (CekEvaluationException uni fun) (Term Name uni fun ()), cost, [String])
runCek params mode emitting term =
Expand Down
9 changes: 5 additions & 4 deletions plutus-tx/src/PlutusTx/Evaluation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ module PlutusTx.Evaluation
)
where

import qualified PlutusCore as PLC
import qualified PlutusCore as PLC
import PlutusCore.Default
import PlutusCore.Name

import UntypedPlutusCore
import UntypedPlutusCore.Evaluation.Machine.Cek hiding (evaluateCek, unsafeEvaluateCek)
import qualified UntypedPlutusCore.Evaluation.Machine.Cek as Cek
import UntypedPlutusCore.Evaluation.Machine.Cek hiding (evaluateCek, unsafeEvaluateCek)
import qualified UntypedPlutusCore.Evaluation.Machine.Cek as Cek
import UntypedPlutusCore.Evaluation.Machine.Cek.Internal (EmitterTy (Emit))

-- | Evaluate a program in the CEK machine with the usual string dynamic builtins.
evaluateCek
Expand All @@ -42,5 +43,5 @@ evaluateCekTrace
=> Program Name uni fun ()
-> ([String], TallyingSt fun, Either (CekEvaluationException uni fun) (Term Name uni fun ()))
evaluateCekTrace (Program _ _ t) =
case runCek PLC.defaultCekParameters Cek.tallying True t of
case runCek PLC.defaultCekParameters Cek.tallying Emit t of
(errOrRes, st, logs) -> (logs, st, errOrRes)

0 comments on commit b34d3a2

Please sign in to comment.