Skip to content

Commit d9bbd32

Browse files
authored
[Evaluation] Add 'CekResult' and improve 'dischargeCekValue' (#7272)
This adds `CekResult` and `CekReport` so that we never run into very confusing issues with excessive laziness invalidating benchmarking results like it happened in #3876. Plus, 3-tuples are an anti-pattern anyway, so it's good to move from them. Also fixes a bug in `dischargeCekValue` where it previously wouldn't discharge under `Constr` or `Case` due to a catch-all clause that we forgot to update when introducing SOPs. It doesn't matter on-chain (we only care there if something is a `()` or success in general, not whether `Constr` was discharges correctly), but since we use the CEK machine as a sorta normalizer in tests, we still want it to return the correct answer.
1 parent 7569ae0 commit d9bbd32

File tree

27 files changed

+236
-159
lines changed

27 files changed

+236
-159
lines changed

cardano-constitution/test/Cardano/Constitution/Validator/Data/GoldenTests.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- editorconfig-checker-disable-file
2+
{-# LANGUAGE GADTs #-}
3+
24
module Cardano.Constitution.Validator.Data.GoldenTests
35
( tests
46
) where
@@ -7,6 +9,7 @@ import Cardano.Constitution.Config
79
import Cardano.Constitution.Data.Validator
810
import Cardano.Constitution.Validator.TestsCommon
911
import Helpers.TestBuilders
12+
import PlutusCore.Default as UPLC
1013
import PlutusCore.Evaluation.Machine.ExBudget
1114
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
1215
import PlutusCore.Pretty (prettyPlcReadableSimple)
@@ -89,5 +92,5 @@ runForBudget v ctx =
8992
in case UPLC.runCekDeBruijn defaultCekParametersForTesting counting noEmitter vPs of
9093
-- Here, we guard against the case that a ConstitutionValidator **FAILS EARLY** (for some reason),
9194
-- resulting in misleading low budget costs.
92-
(Left _, _, _) -> error "For safety, we only compare budget of succesful executions."
93-
(Right _ , UPLC.CountingSt budget, _) -> budget
95+
UPLC.CekReport (UPLC.CekSuccessConstant (UPLC.Some (UPLC.ValueOf UPLC.DefaultUniUnit ()))) (UPLC.CountingSt budget) _ -> budget
96+
_ -> error "For safety, we only compare budgets of successful executions."

cardano-constitution/test/Cardano/Constitution/Validator/Data/TestsCommon.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ unsafeRunCekRes = unsafeFromRight . runCekRes
6262
runCekRes :: (t ~ Term NamedDeBruijn DefaultUni DefaultFun ())
6363
=> t -> Either (CekEvaluationException NamedDeBruijn DefaultUni DefaultFun) t
6464
runCekRes t =
65-
(\(res,_,_) -> res) $
65+
UPLC.cekResultToEither . UPLC._cekReportResult $
6666
UPLC.runCekDeBruijn defaultCekParametersForTesting restrictingEnormous noEmitter t
6767

6868
liftCode110 :: Lift DefaultUni a => a -> CompiledCode a

cardano-constitution/test/Cardano/Constitution/Validator/GoldenTests.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
-- editorconfig-checker-disable-file
2+
{-# LANGUAGE GADTs #-}
3+
24
module Cardano.Constitution.Validator.GoldenTests
35
( tests
46
) where
@@ -7,6 +9,7 @@ import Cardano.Constitution.Config
79
import Cardano.Constitution.Validator
810
import Cardano.Constitution.Validator.TestsCommon
911
import Helpers.TestBuilders
12+
import PlutusCore.Default as UPLC
1013
import PlutusCore.Evaluation.Machine.ExBudget
1114
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
1215
import PlutusCore.Pretty (prettyPlcReadableSimple)
@@ -89,5 +92,5 @@ runForBudget v ctx =
8992
in case UPLC.runCekDeBruijn defaultCekParametersForTesting counting noEmitter vPs of
9093
-- Here, we guard against the case that a ConstitutionValidator **FAILS EARLY** (for some reason),
9194
-- resulting in misleading low budget costs.
92-
(Left _, _, _) -> error "For safety, we only compare budget of succesful executions."
93-
(Right _ , UPLC.CountingSt budget, _) -> budget
95+
UPLC.CekReport (UPLC.CekSuccessConstant (UPLC.Some (UPLC.ValueOf UPLC.DefaultUniUnit ()))) (UPLC.CountingSt budget) _ -> budget
96+
_ -> error "For safety, we only compare budget of succesful executions."

cardano-constitution/test/Cardano/Constitution/Validator/TestsCommon.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ unsafeRunCekRes = unsafeFromRight . runCekRes
6262
runCekRes :: (t ~ Term NamedDeBruijn DefaultUni DefaultFun ())
6363
=> t -> Either (CekEvaluationException NamedDeBruijn DefaultUni DefaultFun) t
6464
runCekRes t =
65-
(\(res,_,_) -> res) $
65+
UPLC.cekResultToEither . UPLC._cekReportResult $
6666
UPLC.runCekDeBruijn defaultCekParametersForTesting restrictingEnormous noEmitter t
6767

6868
liftCode110 :: Lift DefaultUni a => a -> CompiledCode a

plutus-benchmark/common/PlutusBenchmark/Common.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ getConfig limit = do
7979
getCostsCek :: UPLC.Program UPLC.NamedDeBruijn DefaultUni DefaultFun () -> (Integer, Integer)
8080
getCostsCek (UPLC.Program _ _ prog) =
8181
case Cek.runCekDeBruijn PLC.defaultCekParametersForTesting Cek.tallying Cek.noEmitter prog of
82-
(_res, Cek.TallyingSt _ budget, _logs) ->
82+
Cek.CekReport _res (Cek.TallyingSt _ budget) _logs ->
8383
let ExBudget (ExCPU cpu)(ExMemory mem) = budget
8484
in (fromSatInt cpu, fromSatInt mem)
8585

@@ -119,12 +119,11 @@ evaluateCekLikeInProd
119119
-> Either
120120
(UPLC.CekEvaluationException UPLC.NamedDeBruijn UPLC.DefaultUni UPLC.DefaultFun)
121121
(UPLC.Term UPLC.NamedDeBruijn UPLC.DefaultUni UPLC.DefaultFun ())
122-
evaluateCekLikeInProd evalCtx term = do
123-
let (getRes, _, _) =
124-
let -- The validation benchmarks were all created from PlutusV1 scripts
125-
pv = LedgerApi.ledgerLanguageIntroducedIn LedgerApi.PlutusV1
126-
in LedgerApi.evaluateTerm UPLC.restrictingEnormous pv LedgerApi.Quiet evalCtx term
127-
getRes
122+
evaluateCekLikeInProd evalCtx term =
123+
let -- The validation benchmarks were all created from PlutusV1 scripts
124+
pv = LedgerApi.ledgerLanguageIntroducedIn LedgerApi.PlutusV1
125+
in Cek.cekResultToEither . Cek._cekReportResult $
126+
LedgerApi.evaluateTerm UPLC.restrictingEnormous pv LedgerApi.Quiet evalCtx term
128127

129128
-- | Evaluate a term and either throw if evaluation fails or discard the result and return '()'.
130129
-- Useful for benchmarking.

plutus-benchmark/lists/exe/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import UntypedPlutusCore.Evaluation.Machine.Cek qualified as Cek
2020

2121
getBudgetUsage :: Term -> Maybe Integer
2222
getBudgetUsage term =
23-
case (\ (fstT,sndT,_) -> (fstT,sndT) ) $
23+
case (\(Cek.CekReport fstT sndT _) -> (Cek.cekResultToEither fstT, sndT)) $
2424
Cek.runCekDeBruijn PLC.defaultCekParametersForTesting Cek.counting Cek.noEmitter term
2525
of
2626
(Left _, _) -> Nothing
@@ -29,7 +29,7 @@ getBudgetUsage term =
2929

3030
getCekSteps :: Term -> Maybe Integer
3131
getCekSteps term =
32-
case (\ (fstT,sndT,_) -> (fstT,sndT) ) $
32+
case (\(Cek.CekReport fstT sndT _) -> (Cek.cekResultToEither fstT, sndT)) $
3333
Cek.runCekDeBruijn PLC.unitCekParameters Cek.tallying Cek.noEmitter term
3434
of
3535
(Left _, _) -> Nothing

plutus-benchmark/nofib/exe/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ evaluateWithCek
208208
-> UPLC.EvaluationResult (UPLC.Term UPLC.NamedDeBruijn DefaultUni DefaultFun ())
209209
evaluateWithCek =
210210
UPLC.unsafeSplitStructuralOperational
211-
. (\(fstT,_,_) -> fstT)
211+
. UPLC.cekResultToEither
212+
. UPLC._cekReportResult
212213
. UPLC.runCekDeBruijn PLC.defaultCekParametersForTesting UPLC.restrictingEnormous UPLC.noEmitter
213214

214215
writeFlatNamed :: UPLC.Program UPLC.NamedDeBruijn DefaultUni DefaultFun () -> IO ()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Changed
2+
3+
- In #7272 made the CEK machine return a `CekReport` and fixed a bug with `dischargeCekValue` not dicharging under `Constr` and `Case`.

plutus-core/executables/plutus/AnyProgram/Run.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ runPlc (PLC.Program _ _ t)
6161
runUplc :: (?opts :: Opts, Typeable a)
6262
=> UPLC.UnrestrictedProgram NamedDeBruijn DefaultUni DefaultFun a -> IO ()
6363
runUplc (UPLC.UnrestrictedProgram (UPLC.Program _ _ t)) =
64-
case UPLC.runCekDeBruijn defaultCekParametersForTesting exBudgetMode logEmitter t of
64+
case (\(UPLC.CekReport res cost logs) -> (UPLC.cekResultToEither res, cost, logs)) $
65+
UPLC.runCekDeBruijn defaultCekParametersForTesting exBudgetMode logEmitter t of
6566
(Left errorWithCause, _, logs) -> do
6667
for_ logs (printE . unpack)
6768
failE $ show errorWithCause

plutus-core/plutus-core/src/PlutusCore/Builtin/KnownType.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ makeKnownOrFail x = case makeKnown x of
385385
readKnownSelf
386386
:: (ReadKnown val a, BuiltinErrorToEvaluationError structural operational)
387387
=> val -> Either (ErrorWithCause (EvaluationError structural operational) val) a
388-
readKnownSelf val = fromRightM (flip throwErrorWithCause val . builtinErrorToEvaluationError) $ readKnown val
388+
readKnownSelf val =
389+
fromRightM (flip throwErrorWithCause val . builtinErrorToEvaluationError) $ readKnown val
389390
{-# INLINE readKnownSelf #-}
390391

391392
instance MakeKnownIn uni val a => MakeKnownIn uni val (BuiltinResult a) where

0 commit comments

Comments
 (0)