Skip to content

Commit 0d0adc7

Browse files
committed
Use SupportedLanguage for script generation
Make generators more era agnostic
1 parent ecd0f67 commit 0d0adc7

File tree

6 files changed

+80
-65
lines changed

6 files changed

+80
-65
lines changed

eras/alonzo/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
### `testlib`
2020

21+
* Change type signature of `genPlutusScript`, `genNativeScript`, `genAlonzoScript`, `alwaysSucceedsLang` and `alwaysFailsLang`.
2122
* Remove `TxInfoLanguage` and `mkTxInfoLanguage`
2223
* Added `Era` module with `AlonzoEraTest` class
2324

eras/alonzo/impl/cardano-ledger-alonzo.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ library
7676
-Wunused-packages
7777

7878
build-depends:
79+
FailT,
7980
aeson >=2.2,
8081
base >=4.14 && <5,
8182
base64-bytestring,
@@ -101,7 +102,6 @@ library
101102
text,
102103
transformers,
103104
validation-selective,
104-
FailT,
105105

106106
if !impl(ghc >=9.2)
107107
ghc-options: -Wno-name-shadowing

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Arbitrary.hs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ import Cardano.Ledger.Alonzo (AlonzoEra)
3838
import Cardano.Ledger.Alonzo.Core
3939
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis (..))
4040
import Cardano.Ledger.Alonzo.PParams (AlonzoPParams (AlonzoPParams), OrdExUnits (OrdExUnits))
41-
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError)
41+
import Cardano.Ledger.Alonzo.Plutus.Context (
42+
EraPlutusContext (ContextError),
43+
SupportedLanguage (..),
44+
mkSupportedPlutusScript,
45+
supportedLanguages,
46+
)
4247
import Cardano.Ledger.Alonzo.Plutus.Evaluate (CollectError)
4348
import Cardano.Ledger.Alonzo.Plutus.TxInfo (AlonzoContextError)
4449
import Cardano.Ledger.Alonzo.Rules (
@@ -78,11 +83,11 @@ import Cardano.Ledger.Plutus.Language (
7883
PlutusLanguage,
7984
asSLanguage,
8085
plutusLanguage,
81-
withSLanguage,
8286
)
8387
import Cardano.Ledger.Shelley.Rules (PredicateFailure, ShelleyUtxowPredFailure)
8488
import Data.Functor.Identity (Identity)
8589
import Data.List.NonEmpty (NonEmpty ((:|)))
90+
import qualified Data.List.NonEmpty as NE (toList)
8691
import qualified Data.Map.Strict as Map
8792
import qualified Data.MapExtras as Map (fromElems)
8893
import qualified Data.Set as Set
@@ -196,41 +201,39 @@ instance
196201
genEraLanguage :: forall era. AlonzoEraScript era => Gen Language
197202
genEraLanguage = choose (minBound, eraMaxLanguage @era)
198203

204+
instance EraPlutusContext era => Arbitrary (SupportedLanguage era) where
205+
arbitrary = elements $ NE.toList (supportedLanguages @era)
206+
199207
instance
200-
( AlonzoEraScript era
208+
( EraPlutusContext era
201209
, Script era ~ AlonzoScript era
202210
, NativeScript era ~ Timelock era
203211
) =>
204212
Arbitrary (AlonzoScript era)
205213
where
206-
arbitrary = genEraLanguage @era >>= genAlonzoScript
214+
arbitrary = arbitrary >>= genAlonzoScript
207215

208216
genAlonzoScript ::
209-
( AlonzoEraScript era
217+
( EraPlutusContext era
210218
, Script era ~ AlonzoScript era
211219
, NativeScript era ~ Timelock era
212220
) =>
213-
Language ->
221+
SupportedLanguage era ->
214222
Gen (AlonzoScript era)
215223
genAlonzoScript lang =
216224
frequency
217-
[ (2, genPlutusScript lang)
218-
, (8, genNativeScript)
225+
[ (2, fromPlutusScript <$> genPlutusScript lang)
226+
, (8, fromNativeScript <$> genNativeScript)
219227
]
220228

221229
genNativeScript ::
222-
( AlonzoEraScript era
223-
, NativeScript era ~ Timelock era
224-
) =>
225-
Gen (AlonzoScript era)
226-
genNativeScript = TimelockScript <$> arbitrary
230+
Arbitrary (NativeScript era) =>
231+
Gen (NativeScript era)
232+
genNativeScript = arbitrary
227233

228234
genPlutusScript ::
229-
( AlonzoEraScript era
230-
, Script era ~ AlonzoScript era
231-
) =>
232-
Language ->
233-
Gen (AlonzoScript era)
235+
SupportedLanguage era ->
236+
Gen (PlutusScript era)
234237
genPlutusScript lang =
235238
frequency
236239
[ (5, alwaysSucceedsLang lang <$> elements [1, 2, 3])
@@ -448,13 +451,21 @@ alwaysFails ::
448451
Script era
449452
alwaysFails n = mkPlutusScript' (alwaysFailsPlutus @l n)
450453

451-
alwaysSucceedsLang :: (HasCallStack, AlonzoEraScript era) => Language -> Natural -> Script era
452-
alwaysSucceedsLang lang n =
453-
withSLanguage lang $ \slang -> mkPlutusScript' $ asSLanguage slang (alwaysSucceedsPlutus n)
454+
alwaysSucceedsLang ::
455+
SupportedLanguage era ->
456+
Natural ->
457+
PlutusScript era
458+
alwaysSucceedsLang supportedLanguage n =
459+
case supportedLanguage of
460+
SupportedLanguage slang -> mkSupportedPlutusScript $ asSLanguage slang (alwaysSucceedsPlutus n)
454461

455-
alwaysFailsLang :: (HasCallStack, AlonzoEraScript era) => Language -> Natural -> Script era
456-
alwaysFailsLang lang n =
457-
withSLanguage lang $ \slang -> mkPlutusScript' $ asSLanguage slang (alwaysFailsPlutus n)
462+
alwaysFailsLang ::
463+
SupportedLanguage era ->
464+
Natural ->
465+
PlutusScript era
466+
alwaysFailsLang supportedLanguage n =
467+
case supportedLanguage of
468+
SupportedLanguage slang -> mkSupportedPlutusScript $ asSLanguage slang (alwaysFailsPlutus n)
458469

459470
-- | Partial version of `mkPlutusScript`
460471
mkPlutusScript' ::

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary/TxWitsSpec.hs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
module Test.Cardano.Ledger.Alonzo.Binary.TxWitsSpec (spec) where
1212

13-
import Cardano.Ledger.Allegra.Scripts (Timelock)
1413
import Cardano.Ledger.Alonzo.Core
14+
import Cardano.Ledger.Alonzo.Plutus.Context
1515
import Cardano.Ledger.Alonzo.Scripts
1616
import Cardano.Ledger.Alonzo.TxWits (AlonzoTxWits (..))
1717
import Cardano.Ledger.BaseTypes
@@ -20,17 +20,17 @@ import Cardano.Ledger.Binary.Decoding
2020
import Cardano.Ledger.Binary.Encoding
2121
import Cardano.Ledger.Plutus.Language
2222
import Data.List (isPrefixOf)
23+
import qualified Data.List.NonEmpty as NE (toList)
2324
import qualified Data.Map as Map
24-
import qualified Data.Maybe as Maybe (mapMaybe)
2525
import Test.Cardano.Ledger.Alonzo.Arbitrary
2626
import Test.Cardano.Ledger.Alonzo.Binary.Annotator ()
2727
import Test.Cardano.Ledger.Common
2828

2929
spec ::
3030
forall era.
31-
( AlonzoEraScript era
32-
, Script era ~ AlonzoScript era
33-
, NativeScript era ~ Timelock era
31+
( EraPlutusContext era
32+
, Arbitrary (NativeScript era)
33+
, DecCBOR (Annotator (NativeScript era))
3434
) =>
3535
Spec
3636
spec = do
@@ -60,9 +60,8 @@ emptyFieldsProps = do
6060

6161
plutusScriptsProp ::
6262
forall era.
63-
( AlonzoEraScript era
63+
( EraPlutusContext era
6464
, DecCBOR (Annotator (NativeScript era))
65-
, Script era ~ AlonzoScript era
6665
) =>
6766
Spec
6867
plutusScriptsProp = do
@@ -71,7 +70,7 @@ plutusScriptsProp = do
7170
[ distinctProp
7271
, duplicateProp
7372
]
74-
<*> [minBound .. eraMaxLanguage @era]
73+
<*> NE.toList (supportedLanguages @era)
7574
where
7675
distinctProp lang =
7776
forAllShow (genEncoding lang False) (showEnc @era) $
@@ -85,26 +84,23 @@ plutusScriptsProp = do
8584
enc
8685
"Final number of elements"
8786

88-
genEncoding :: Language -> Bool -> Gen Encoding
89-
genEncoding lang duplicate = do
90-
sc <- genPlutusScript @era lang
91-
let scs
92-
| duplicate = [sc, sc]
93-
| otherwise = [sc]
94-
let plutusBins = withSLanguage lang $ \slang ->
95-
Maybe.mapMaybe
96-
(\x -> plutusBinary <$> (toPlutusScript x >>= toPlutusSLanguage slang))
97-
scs
98-
pure $ encCBOR $ Map.singleton (keys lang) (encCBOR plutusBins)
99-
keys PlutusV1 = 3 :: Int
100-
keys PlutusV2 = 6
101-
keys PlutusV3 = 7
87+
genEncoding :: SupportedLanguage era -> Bool -> Gen Encoding
88+
genEncoding supportedLanguage@(SupportedLanguage slang) duplicate = do
89+
plutusScript <- genPlutusScript supportedLanguage
90+
let plutusScripts
91+
| duplicate = [plutusScript, plutusScript]
92+
| otherwise = [plutusScript]
93+
pure $ encCBOR $ Map.singleton (keys slang) (encCBOR (plutusScriptBinary <$> plutusScripts))
94+
keys :: SLanguage l -> Int
95+
keys SPlutusV1 = 3
96+
keys SPlutusV2 = 6
97+
keys SPlutusV3 = 7
10298

10399
nativeScriptsProp ::
104100
forall era.
105101
( AlonzoEraScript era
106-
, Script era ~ AlonzoScript era
107-
, NativeScript era ~ Timelock era
102+
, Arbitrary (NativeScript era)
103+
, DecCBOR (Annotator (NativeScript era))
108104
) =>
109105
Spec
110106
nativeScriptsProp = do
@@ -127,13 +123,12 @@ nativeScriptsProp = do
127123

128124
genEncoding :: Bool -> Gen Encoding
129125
genEncoding duplicate = do
130-
sc <- genNativeScript @era
131-
let scs
132-
| duplicate = [sc, sc]
133-
| otherwise = [sc]
126+
nativeScript <- genNativeScript @era
127+
let nativeScripts
128+
| duplicate = [nativeScript, nativeScript]
129+
| otherwise = [nativeScript]
134130

135-
let natives = Maybe.mapMaybe getNativeScript scs
136-
pure $ encCBOR $ Map.singleton (1 :: Int) (encCBOR natives)
131+
pure $ encCBOR $ Map.singleton (1 :: Int) (encCBOR nativeScripts)
137132

138133
expectDeserialiseSuccess ::
139134
forall era.

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Translation/TranslatableGen.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import Cardano.Ledger.Alonzo.Plutus.Context (
2020
LedgerTxInfo (..),
2121
PlutusTxInfo,
2222
SupportedLanguage (..),
23-
supportedLanguages,
2423
toPlutusTxInfo,
2524
)
2625
import Cardano.Ledger.Alonzo.TxWits (Redeemers)
@@ -30,7 +29,6 @@ import Cardano.Ledger.State (UTxO (..))
3029
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
3130
import Cardano.Slotting.Slot (EpochSize (..))
3231
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
33-
import qualified Data.List.NonEmpty as NE (toList)
3432
import qualified Data.Map.Strict as Map
3533
import qualified Data.Set as Set
3634
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
@@ -76,8 +74,8 @@ genTranslationInstance ::
7674
Gen (TranslationInstance era)
7775
genTranslationInstance = do
7876
protVer <- arbitrary
79-
supportedLanguage <- elements $ NE.toList (supportedLanguages @era)
80-
tx <- tgTx @era supportedLanguage
77+
supportedLanguage :: SupportedLanguage era <- arbitrary
78+
tx <- tgTx supportedLanguage
8179
utxo <- tgUtxo supportedLanguage tx
8280
let lti =
8381
LedgerTxInfo

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/Functions.hs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
module Test.Cardano.Ledger.Generic.Functions where
1313

1414
import Cardano.Ledger.Address (Addr (..))
15+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext, mkSupportedLanguageM)
1516
import Cardano.Ledger.Alonzo.Scripts (plutusScriptLanguage)
1617
import Cardano.Ledger.Alonzo.Tx (AlonzoTx (..), IsValid (..))
1718
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxOut (..))
@@ -48,6 +49,7 @@ import qualified Cardano.Ledger.UMap as UM
4849
import Cardano.Ledger.Val (Val ((<+>), (<->)), inject)
4950
import Cardano.Slotting.EpochInfo.API (epochInfoSize)
5051
import Control.Monad.Reader (runReader)
52+
import Control.Monad.Trans.Fail.String (errorFail)
5153
import Data.Default (Default (def))
5254
import qualified Data.Foldable as Fold (fold, toList)
5355
import qualified Data.List as List
@@ -300,24 +302,32 @@ primaryLanguage Alonzo = Just PlutusV1
300302
primaryLanguage _ = Nothing
301303
{-# NOINLINE primaryLanguage #-}
302304

305+
alwaysSucceedsLang' :: forall era. EraPlutusContext era => Language -> Natural -> Script era
306+
alwaysSucceedsLang' l =
307+
fromPlutusScript . alwaysSucceedsLang (errorFail (mkSupportedLanguageM @era l))
308+
309+
alwaysFailsLang' :: forall era. EraPlutusContext era => Language -> Natural -> Script era
310+
alwaysFailsLang' l =
311+
fromPlutusScript . alwaysFailsLang (errorFail (mkSupportedLanguageM @era l))
312+
303313
alwaysTrue :: forall era. Proof era -> Maybe Language -> Natural -> Script era
304-
alwaysTrue Conway (Just l) n = alwaysSucceedsLang @era l n
314+
alwaysTrue Conway (Just l) n = alwaysSucceedsLang' @era l n
305315
alwaysTrue p@Conway Nothing _ = fromNativeScript $ Scriptic.allOf [] p
306-
alwaysTrue Babbage (Just l) n = alwaysSucceedsLang @era l n
316+
alwaysTrue Babbage (Just l) n = alwaysSucceedsLang' @era l n
307317
alwaysTrue p@Babbage Nothing _ = fromNativeScript $ Scriptic.allOf [] p
308-
alwaysTrue Alonzo (Just l) n = alwaysSucceedsLang @era l n
318+
alwaysTrue Alonzo (Just l) n = alwaysSucceedsLang' @era l n
309319
alwaysTrue p@Alonzo Nothing _ = fromNativeScript $ Scriptic.allOf [] p
310320
alwaysTrue p@Mary _ n = always n p
311321
alwaysTrue p@Allegra _ n = always n p
312322
alwaysTrue p@Shelley _ n = always n p
313323
{-# NOINLINE alwaysTrue #-}
314324

315325
alwaysFalse :: forall era. Proof era -> Maybe Language -> Natural -> Script era
316-
alwaysFalse Conway (Just l) n = alwaysFailsLang @era l n
326+
alwaysFalse Conway (Just l) n = alwaysFailsLang' @era l n
317327
alwaysFalse p@Conway Nothing _ = fromNativeScript $ Scriptic.anyOf [] p
318-
alwaysFalse Babbage (Just l) n = alwaysFailsLang @era l n
328+
alwaysFalse Babbage (Just l) n = alwaysFailsLang' @era l n
319329
alwaysFalse p@Babbage Nothing _ = fromNativeScript $ Scriptic.anyOf [] p
320-
alwaysFalse Alonzo (Just l) n = alwaysFailsLang @era l n
330+
alwaysFalse Alonzo (Just l) n = alwaysFailsLang' @era l n
321331
alwaysFalse p@Alonzo Nothing _ = fromNativeScript $ Scriptic.anyOf [] p
322332
alwaysFalse p@Mary _ n = never n p
323333
alwaysFalse p@Allegra _ n = never n p

0 commit comments

Comments
 (0)