Skip to content

Commit fbb316a

Browse files
committed
[Test] Add 'fix id'
1 parent 5458fe5 commit fbb316a

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

plutus-core/plutus-core/src/PlutusCore/Evaluation/Machine/ExBudget.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ module PlutusCore.Evaluation.Machine.ExBudget
140140
, ExBudgetBuiltin(..)
141141
, ExRestrictingBudget(..)
142142
, LowerInitialCharacter
143+
, largeBudget
143144
, enormousBudget
144145
) where
145146

@@ -206,6 +207,12 @@ newtype ExRestrictingBudget = ExRestrictingBudget
206207
deriving newtype (Semigroup, Monoid)
207208
deriving newtype (Pretty, PrettyBy config, NFData)
208209

210+
-- | When we want to just evaluate the program that is intended to run out of budget we use the
211+
-- 'Restricting' mode with this big budget designed to make the CEK machine terminate in a
212+
-- fraction of a second on the reference machine.
213+
largeBudget :: ExRestrictingBudget
214+
largeBudget = ExRestrictingBudget $ ExBudget (2 * 10 ^ (11 :: Int)) (10 ^ (10 :: Int))
215+
209216
-- | When we want to just evaluate the program we use the 'Restricting' mode with an enormous
210217
-- budget, so that evaluation costs of on-chain budgeting are reflected accurately in benchmarks.
211218
enormousBudget :: ExRestrictingBudget

plutus-core/plutus-ir/test/PlutusIR/Generators/QuickCheck/Tests.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import PlutusIR.Generators.QuickCheck
1414

1515
import PlutusCore.Builtin (fromValue)
1616
import PlutusCore.Default
17-
import PlutusCore.Evaluation.Machine.ExBudget
1817
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults (defaultCekParametersForTesting)
1918
import PlutusCore.Name.Unique
2019
import PlutusCore.Pretty
@@ -25,7 +24,7 @@ import PlutusCore.Version (latestVersion)
2524
import PlutusIR
2625
import PlutusIR.Test ()
2726
import UntypedPlutusCore qualified as UPLC
28-
import UntypedPlutusCore.Evaluation.Machine.Cek (restricting, runCekNoEmit,
27+
import UntypedPlutusCore.Evaluation.Machine.Cek (restrictingLarge, runCekNoEmit,
2928
unsafeSplitStructuralOperational)
3029

3130
import Control.Exception
@@ -203,11 +202,10 @@ noStructuralErrors term =
203202
-- Throw on a structural evaluation error and succeed on both an operational evaluation error and
204203
-- evaluation success.
205204
void . evaluate . unsafeSplitStructuralOperational . fst $ do
206-
let -- The numbers are picked so that evaluation of the arbitrarily generated term always
207-
-- finishes in reasonable time even if evaluation loops (in which case we'll get an
208-
-- out-of-budget failure).
209-
budgeting = restricting . ExRestrictingBudget $ ExBudget 1000000000 1000000000
210-
runCekNoEmit defaultCekParametersForTesting budgeting term
205+
-- Using 'restrictingLarge' so that evaluation of the arbitrarily generated term always finishes
206+
-- in reasonable time even if evaluation loops (in which case we'll get an out-of-budget
207+
-- failure).
208+
runCekNoEmit defaultCekParametersForTesting restrictingLarge term
211209

212210
-- | Test that evaluation of well-typed terms doesn't fail with a structural error.
213211
prop_noStructuralErrors :: Property

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Evaluation/Machine/Cek.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module UntypedPlutusCore.Evaluation.Machine.Cek
3232
, counting
3333
, tallying
3434
, restricting
35+
, restrictingLarge
3536
, restrictingEnormous
3637
, enormousBudget
3738
-- * Emitter modes

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Evaluation/Machine/Cek/ExBudgetMode.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module UntypedPlutusCore.Evaluation.Machine.Cek.ExBudgetMode
2020
, enormousBudget
2121
, tallying
2222
, restricting
23+
, restrictingLarge
2324
, restrictingEnormous
2425
)
2526
where
@@ -159,6 +160,10 @@ restricting (ExRestrictingBudget initB@(ExBudget cpuInit memInit)) = ExBudgetMod
159160
final = RestrictingSt . ExRestrictingBudget <$> remaining
160161
pure $ ExBudgetInfo spender final cumulative
161162

163+
-- | 'restricting' instantiated at 'largeBudget'.
164+
restrictingLarge :: ThrowableBuiltins uni fun => ExBudgetMode RestrictingSt uni fun
165+
restrictingLarge = restricting largeBudget
166+
162167
-- | 'restricting' instantiated at 'enormousBudget'.
163168
restrictingEnormous :: ThrowableBuiltins uni fun => ExBudgetMode RestrictingSt uni fun
164169
restrictingEnormous = restricting enormousBudget

plutus-core/untyped-plutus-core/test/Evaluation/Builtins/Definition.hs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
{-# LANGUAGE CPP #-}
55
{-# LANGUAGE LambdaCase #-}
6+
{-# LANGUAGE NumericUnderscores #-}
67
{-# LANGUAGE OverloadedStrings #-}
78
{-# LANGUAGE PartialTypeSignatures #-}
89
{-# LANGUAGE TypeApplications #-}
@@ -32,7 +33,6 @@ import PlutusCore.Builtin
3233
import PlutusCore.Compiler.Erase (eraseTerm)
3334
import PlutusCore.Data
3435
import PlutusCore.Default
35-
import PlutusCore.Evaluation.Machine.ExBudget
3636
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
3737
import PlutusCore.Evaluation.Machine.MachineParameters
3838
import PlutusCore.Examples.Builtins
@@ -497,16 +497,33 @@ test_TrackCostsRetaining =
497497
]
498498
assertBool err $ expected > actual
499499

500+
typecheckAndEvalToOutOfEx :: Term TyName Name DefaultUni DefaultFun () -> Assertion
501+
typecheckAndEvalToOutOfEx term =
502+
let evalRestricting params = fst . runCekNoEmit params restrictingLarge
503+
in case typecheckAnd def evalRestricting defaultBuiltinCostModelForTesting term of
504+
Right (Left (ErrorWithCause (OperationalEvaluationError (CekOutOfExError _)) _)) ->
505+
pure ()
506+
err -> assertFailure $ "Expected a 'CekOutOfExError' but got: " ++ displayPlc err
507+
500508
test_SerialiseDataImpossible :: TestTree
501509
test_SerialiseDataImpossible =
502-
testCase "Serialising an impossible 'Data' object finishes" $ do
510+
testCase "Serialising an impossible 'Data' object runs out of budget and finishes" $ do
503511
let dataLoop :: Term TyName Name DefaultUni DefaultFun ()
504-
dataLoop = Apply () (Builtin () SerialiseData) $ mkConstant () loop where
505-
loop = List [loop]
506-
budgetMode = restricting . ExRestrictingBudget $ ExBudget 10000000000 10000000
507-
evalRestricting params = unsafeSplitStructuralOperational . fst . runCekNoEmit params budgetMode
508-
typecheckAnd def evalRestricting defaultBuiltinCostModelForTesting dataLoop @?=
509-
Right EvaluationFailure
512+
dataLoop =
513+
let loop = List [loop]
514+
in Apply () (Builtin () SerialiseData) $ mkConstant () loop
515+
typecheckAndEvalToOutOfEx dataLoop
516+
517+
test_fixId :: TestTree
518+
test_fixId =
519+
testCase "'fix id' runs out of budget and finishes" $ do
520+
let fixId :: Term TyName Name DefaultUni DefaultFun ()
521+
fixId =
522+
mkIterAppNoAnn (mkIterInstNoAnn Plc.fix [integer, integer])
523+
[ tyInst () Plc.idFun (TyFun () integer integer)
524+
, mkConstant @Integer () 42
525+
]
526+
typecheckAndEvalToOutOfEx fixId
510527

511528
-- | If the first char is an opening paren and the last chat is a closing paren, then remove them.
512529
-- This is useful for rendering a term-as-a-test-name in CLI, since currently we wrap readably
@@ -1195,6 +1212,7 @@ test_definition =
11951212
, test_TrackCostsRetaining
11961213
#endif
11971214
, test_SerialiseDataImpossible
1215+
, test_fixId
11981216
, runTestNestedHere
11991217
[ test_Integer
12001218
, test_String

0 commit comments

Comments
 (0)