@@ -220,24 +220,25 @@ mkTermToEvaluate bs args = do
220220 pure t
221221
222222-- | Evaluates a script, with a cost model and a budget that restricts how many
223- -- resources it can use according to the cost model. There's a default cost
224- -- model in 'UPLC.defaultBuiltinCostModel' and a budget called 'enormousBudget' in
225- -- 'UntypedPlutusCore.Evaluation.Machine.Cek.ExBudgetMode' which should be large
226- -- enough to evaluate any sensible program.
223+ -- resources it can use according to the cost model. Also returns the budget that
224+ -- was actually used.
225+ --
226+ -- Can be used to calculate budgets for scripts, but even in this case you must give
227+ -- a limit to guard against scripts that run for a long time or loop.
227228evaluateScriptRestricting
228229 :: VerboseMode -- ^ Whether to produce log output
229230 -> CostModelParams -- ^ The cost model to use
230231 -> ExBudget -- ^ The resource budget which must not be exceeded during evaluation
231232 -> SerializedScript -- ^ The script to evaluate
232233 -> [PLC. Data ] -- ^ The arguments to the script
233- -> (LogOutput , Either EvaluationError () )
234+ -> (LogOutput , Either EvaluationError ExBudget )
234235evaluateScriptRestricting verbose cmdata budget p args = swap $ runWriter @ LogOutput $ runExceptT $ do
235236 appliedTerm <- mkTermToEvaluate p args
236237 model <- case applyCostModelParams PLC. defaultCekCostModel cmdata of
237238 Just model -> pure model
238239 Nothing -> throwError CostModelParameterMismatch
239240
240- let (res, _ , logs) =
241+ let (res, UPLC. RestrictingSt ( PLC. ExRestrictingBudget final) , logs) =
241242 UPLC. runCek
242243 (toMachineParameters model)
243244 (UPLC. restricting $ PLC. ExRestrictingBudget budget)
@@ -246,9 +247,12 @@ evaluateScriptRestricting verbose cmdata budget p args = swap $ runWriter @LogOu
246247
247248 tell logs
248249 liftEither $ first CekError $ void res
250+ pure (budget `PLC.minusExBudget` final)
249251
250252-- | Evaluates a script, returning the minimum budget that the script would need
251- -- to evaluate successfully.
253+ -- to evaluate successfully. This will take as long as the script takes, if you need to
254+ -- limit the execution time of the script also, you can use 'evaluateScriptRestricting', which
255+ -- also returns the used budget.
252256evaluateScriptCounting
253257 :: VerboseMode -- ^ Whether to produce log output
254258 -> CostModelParams -- ^ The cost model to use
0 commit comments