{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
module Test.Cardano.Ledger.Plutus (
PlutusArgs (..),
ScriptTestContext (..),
alwaysSucceedsPlutus,
alwaysFailsPlutus,
decodeHexPlutus,
mkCostModelConst,
zeroTestingCostModel,
zeroTestingCostModelV1,
zeroTestingCostModelV2,
zeroTestingCostModelV3,
testingCostModel,
testingCostModelV1,
testingCostModelV2,
testingCostModelV3,
testingEvaluationContext,
testingCostModels,
zeroTestingCostModels,
plutusLanguageOf,
showPlcVersion,
) where
import Cardano.Ledger.Binary.Plain (decodeFullFromHexText)
import Cardano.Ledger.Plutus.CostModels (
CostModel,
CostModels,
costModelInitParamCount,
getCostModelEvaluationContext,
mkCostModel,
mkCostModels,
)
import Cardano.Ledger.Plutus.Language (
Language (..),
Plutus (..),
PlutusBinary (..),
)
import Data.Int (Int64)
import Data.List (intercalate)
import qualified Data.Map.Strict as Map
import qualified Data.Text as Text (Text)
import GHC.Stack
import Numeric.Natural (Natural)
import qualified PlutusCore.Version as PLC
import qualified PlutusLedgerApi.Common as P
import qualified PlutusLedgerApi.Test.Examples as P (
alwaysFailingNAryFunction,
alwaysSucceedingNAryFunction,
)
import qualified PlutusLedgerApi.Test.V1.EvaluationContext as PV1
import qualified PlutusLedgerApi.Test.V2.EvaluationContext as PV2
import qualified PlutusLedgerApi.Test.V3.EvaluationContext as PV3
import qualified PlutusLedgerApi.Test.V4.EvaluationContext as PV4
import PlutusLedgerApi.V1 as PV1
import Test.Cardano.Ledger.Plutus.ScriptTestContext (
PlutusArgs (..),
ScriptTestContext (..),
)
mkCostModelConst :: HasCallStack => Language -> Int64 -> CostModel
mkCostModelConst :: HasCallStack => Language -> Int64 -> CostModel
mkCostModelConst Language
lang = Language -> [Int64] -> CostModel
forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
lang ([Int64] -> CostModel) -> (Int64 -> [Int64]) -> Int64 -> CostModel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int64 -> [Int64]
forall a. Int -> a -> [a]
replicate (Language -> Int
costModelInitParamCount Language
lang)
mkCostModel' :: (Integral i, Show i, HasCallStack) => Language -> [i] -> CostModel
mkCostModel' :: forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
lang [i]
params =
case Language -> [Int64] -> Either CostModelApplyError CostModel
mkCostModel Language
lang ([Int64] -> Either CostModelApplyError CostModel)
-> [Int64] -> Either CostModelApplyError CostModel
forall a b. (a -> b) -> a -> b
$ (i -> Int64) -> [i] -> [Int64]
forall a b. (a -> b) -> [a] -> [b]
map i -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral [i]
params of
Left CostModelApplyError
err ->
String -> CostModel
forall a. HasCallStack => String -> a
error (String -> CostModel) -> String -> CostModel
forall a b. (a -> b) -> a -> b
$
String
"CostModel parameters are not well-formed for "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Language -> String
forall a. Show a => a -> String
show Language
lang
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ CostModelApplyError -> String
forall a. Show a => a -> String
show CostModelApplyError
err
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [i] -> String
forall a. Show a => a -> String
show [i]
params
Right CostModel
costModel -> CostModel
costModel
zeroTestingCostModels :: HasCallStack => [Language] -> CostModels
zeroTestingCostModels :: HasCallStack => [Language] -> CostModels
zeroTestingCostModels [Language]
langs =
Map Language CostModel -> CostModels
mkCostModels (Map Language CostModel -> CostModels)
-> Map Language CostModel -> CostModels
forall a b. (a -> b) -> a -> b
$ [(Language, CostModel)] -> Map Language CostModel
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Language
lang, HasCallStack => Language -> CostModel
Language -> CostModel
zeroTestingCostModel Language
lang) | Language
lang <- [Language]
langs]
zeroTestingCostModel :: HasCallStack => Language -> CostModel
zeroTestingCostModel :: HasCallStack => Language -> CostModel
zeroTestingCostModel Language
lang = HasCallStack => Language -> Int64 -> CostModel
Language -> Int64 -> CostModel
mkCostModelConst Language
lang Int64
0
zeroTestingCostModelV1 :: HasCallStack => CostModel
zeroTestingCostModelV1 :: HasCallStack => CostModel
zeroTestingCostModelV1 = HasCallStack => Language -> CostModel
Language -> CostModel
zeroTestingCostModel Language
PlutusV1
zeroTestingCostModelV2 :: HasCallStack => CostModel
zeroTestingCostModelV2 :: HasCallStack => CostModel
zeroTestingCostModelV2 = HasCallStack => Language -> CostModel
Language -> CostModel
zeroTestingCostModel Language
PlutusV2
zeroTestingCostModelV3 :: HasCallStack => CostModel
zeroTestingCostModelV3 :: HasCallStack => CostModel
zeroTestingCostModelV3 = HasCallStack => Language -> CostModel
Language -> CostModel
zeroTestingCostModel Language
PlutusV3
testingCostModels :: HasCallStack => [Language] -> CostModels
testingCostModels :: HasCallStack => [Language] -> CostModels
testingCostModels [Language]
langs =
Map Language CostModel -> CostModels
mkCostModels (Map Language CostModel -> CostModels)
-> Map Language CostModel -> CostModels
forall a b. (a -> b) -> a -> b
$ [(Language, CostModel)] -> Map Language CostModel
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Language
lang, HasCallStack => Language -> CostModel
Language -> CostModel
testingCostModel Language
lang) | Language
lang <- [Language]
langs]
testingCostModel :: HasCallStack => Language -> CostModel
testingCostModel :: HasCallStack => Language -> CostModel
testingCostModel = \case
Language
PlutusV1 -> CostModel
HasCallStack => CostModel
testingCostModelV1
Language
PlutusV2 -> CostModel
HasCallStack => CostModel
testingCostModelV2
Language
PlutusV3 -> CostModel
HasCallStack => CostModel
testingCostModelV3
Language
PlutusV4 -> CostModel
HasCallStack => CostModel
testingCostModelV4
testingCostModelV1 :: HasCallStack => CostModel
testingCostModelV1 :: HasCallStack => CostModel
testingCostModelV1 = Language -> [Int64] -> CostModel
forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
PlutusV1 ([Int64] -> CostModel) -> [Int64] -> CostModel
forall a b. (a -> b) -> a -> b
$ (ParamName, Int64) -> Int64
forall a b. (a, b) -> b
snd ((ParamName, Int64) -> Int64) -> [(ParamName, Int64)] -> [Int64]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ParamName, Int64)]
HasCallStack => [(ParamName, Int64)]
PV1.costModelParamsForTesting
testingCostModelV2 :: HasCallStack => CostModel
testingCostModelV2 :: HasCallStack => CostModel
testingCostModelV2 = Language -> [Int64] -> CostModel
forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
PlutusV2 ([Int64] -> CostModel) -> [Int64] -> CostModel
forall a b. (a -> b) -> a -> b
$ (ParamName, Int64) -> Int64
forall a b. (a, b) -> b
snd ((ParamName, Int64) -> Int64) -> [(ParamName, Int64)] -> [Int64]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ParamName, Int64)]
HasCallStack => [(ParamName, Int64)]
PV2.costModelParamsForTesting
testingCostModelV3 :: HasCallStack => CostModel
testingCostModelV3 :: HasCallStack => CostModel
testingCostModelV3 = Language -> [Int64] -> CostModel
forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
PlutusV3 ([Int64] -> CostModel) -> [Int64] -> CostModel
forall a b. (a -> b) -> a -> b
$ (ParamName, Int64) -> Int64
forall a b. (a, b) -> b
snd ((ParamName, Int64) -> Int64) -> [(ParamName, Int64)] -> [Int64]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ParamName, Int64)]
HasCallStack => [(ParamName, Int64)]
PV3.costModelParamsForTesting
testingCostModelV4 :: HasCallStack => CostModel
testingCostModelV4 :: HasCallStack => CostModel
testingCostModelV4 = Language -> [Int64] -> CostModel
forall i.
(Integral i, Show i, HasCallStack) =>
Language -> [i] -> CostModel
mkCostModel' Language
PlutusV4 ([Int64] -> CostModel) -> [Int64] -> CostModel
forall a b. (a -> b) -> a -> b
$ (ParamName, Int64) -> Int64
forall a b. (a, b) -> b
snd ((ParamName, Int64) -> Int64) -> [(ParamName, Int64)] -> [Int64]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ParamName, Int64)]
HasCallStack => [(ParamName, Int64)]
PV4.costModelParamsForTesting
testingEvaluationContext :: Language -> PV1.EvaluationContext
testingEvaluationContext :: Language -> EvaluationContext
testingEvaluationContext = CostModel -> EvaluationContext
getCostModelEvaluationContext (CostModel -> EvaluationContext)
-> (Language -> CostModel) -> Language -> EvaluationContext
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Language -> CostModel
Language -> CostModel
testingCostModel
alwaysSucceedsPlutus :: Natural -> Plutus l
alwaysSucceedsPlutus :: forall (l :: Language). Natural -> Plutus l
alwaysSucceedsPlutus Natural
n = PlutusBinary -> Plutus l
forall (l :: Language). PlutusBinary -> Plutus l
Plutus (ShortByteString -> PlutusBinary
PlutusBinary (Natural -> ShortByteString
P.alwaysSucceedingNAryFunction Natural
n))
alwaysFailsPlutus :: Natural -> Plutus l
alwaysFailsPlutus :: forall (l :: Language). Natural -> Plutus l
alwaysFailsPlutus Natural
n = PlutusBinary -> Plutus l
forall (l :: Language). PlutusBinary -> Plutus l
Plutus (ShortByteString -> PlutusBinary
PlutusBinary (Natural -> ShortByteString
P.alwaysFailingNAryFunction Natural
n))
decodeHexPlutus :: HasCallStack => Text.Text -> Plutus l
decodeHexPlutus :: forall (l :: Language). HasCallStack => Text -> Plutus l
decodeHexPlutus =
(DecoderError -> Plutus l)
-> (ShortByteString -> Plutus l)
-> Either DecoderError ShortByteString
-> Plutus l
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Plutus l
forall a. HasCallStack => String -> a
error (String -> Plutus l)
-> (DecoderError -> String) -> DecoderError -> Plutus l
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall a. Show a => a -> String
show) (PlutusBinary -> Plutus l
forall (l :: Language). PlutusBinary -> Plutus l
Plutus (PlutusBinary -> Plutus l)
-> (ShortByteString -> PlutusBinary) -> ShortByteString -> Plutus l
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> PlutusBinary
PlutusBinary) (Either DecoderError ShortByteString -> Plutus l)
-> (Text -> Either DecoderError ShortByteString)
-> Text
-> Plutus l
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either DecoderError ShortByteString
forall a. FromCBOR a => Text -> Either DecoderError a
decodeFullFromHexText
plutusLanguageOf :: Language -> Either String P.PlutusLedgerLanguage
plutusLanguageOf :: Language -> Either String PlutusLedgerLanguage
plutusLanguageOf Language
l =
case [PlutusLedgerLanguage
pl | PlutusLedgerLanguage
pl <- [PlutusLedgerLanguage
forall a. Bounded a => a
minBound .. PlutusLedgerLanguage
forall a. Bounded a => a
maxBound], PlutusLedgerLanguage -> String
forall a. Show a => a -> String
show PlutusLedgerLanguage
pl String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== Language -> String
forall a. Show a => a -> String
show Language
l] of
[PlutusLedgerLanguage
pl] -> PlutusLedgerLanguage -> Either String PlutusLedgerLanguage
forall a b. b -> Either a b
Right PlutusLedgerLanguage
pl
[PlutusLedgerLanguage]
_ -> String -> Either String PlutusLedgerLanguage
forall a b. a -> Either a b
Left (String -> Either String PlutusLedgerLanguage)
-> String -> Either String PlutusLedgerLanguage
forall a b. (a -> b) -> a -> b
$ String
"No plutus ledger language named " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Language -> String
forall a. Show a => a -> String
show Language
l
showPlcVersion :: PLC.Version -> String
showPlcVersion :: Version -> String
showPlcVersion (PLC.Version Natural
major Natural
minor Natural
patch) = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"." ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Natural -> String) -> [Natural] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Natural -> String
forall a. Show a => a -> String
show [Natural
major, Natural
minor, Natural
patch]