Skip to content

Commit ecd0f67

Browse files
committed
Switch TxInfo golden tests to use SupportedLanguage
1 parent 92f337c commit ecd0f67

File tree

7 files changed

+66
-104
lines changed

7 files changed

+66
-104
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+
* Remove `TxInfoLanguage` and `mkTxInfoLanguage`
2122
* Added `Era` module with `AlonzoEraTest` class
2223

2324
## 1.13.0.0

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ library testlib
143143

144144
build-depends:
145145
HUnit,
146-
QuickCheck,
147146
base,
148147
bytestring,
149148
cardano-data:{cardano-data, testlib},

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ module Test.Cardano.Ledger.Alonzo.Translation.Golden (
88
assertTranslationResultsMatchGolden,
99
) where
1010

11-
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError, LedgerTxInfo (..), toPlutusTxInfo)
12-
import Cardano.Ledger.Alonzo.Scripts (AlonzoEraScript)
11+
import Cardano.Ledger.Alonzo.Plutus.Context (
12+
LedgerTxInfo (..),
13+
SupportedLanguage (..),
14+
toPlutusTxInfo,
15+
)
1316
import Cardano.Ledger.Binary
1417
import Cardano.Ledger.Core
1518
import Control.Exception (throwIO)
1619
import qualified Data.ByteString.Lazy as BSL
1720
import Test.Cardano.Ledger.Alonzo.Binary.Annotator ()
1821
import Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen (
1922
TranslatableGen (..),
20-
TxInfoLanguage (..),
2123
epochInfo,
2224
systemStart,
2325
toVersionedTxInfo,
@@ -34,10 +36,7 @@ import Test.HUnit (Assertion, assertEqual)
3436
-- and serializes both arguments and result to golden/translations.cbor file
3537
generateGoldenFile ::
3638
forall era.
37-
( Show (ContextError era)
38-
, AlonzoEraScript era
39-
, TranslatableGen era
40-
) =>
39+
TranslatableGen era =>
4140
FilePath ->
4241
IO ()
4342
generateGoldenFile file = do
@@ -49,7 +48,6 @@ generateGoldenFile file = do
4948
assertTranslationResultsMatchGolden ::
5049
forall era.
5150
( TranslatableGen era
52-
, Show (ContextError era)
5351
, HasCallStack
5452
) =>
5553
IO FilePath ->
@@ -62,14 +60,13 @@ assertTranslationResultsMatchGolden file = do
6260
assertTranslationComparison ::
6361
forall era.
6462
( TranslatableGen era
65-
, Show (ContextError era)
6663
, HasCallStack
6764
) =>
6865
TranslationInstance era ->
6966
Assertion
70-
assertTranslationComparison (TranslationInstance protVer lang utxo tx expected) =
71-
case mkTxInfoLanguage @era lang of
72-
TxInfoLanguage slang -> do
67+
assertTranslationComparison (TranslationInstance protVer supportedLanguage utxo tx expected) =
68+
case supportedLanguage of
69+
SupportedLanguage slang -> do
7370
case toPlutusTxInfo slang lti of
7471
Left e -> error $ show e
7572
Right actual -> assertEqual errorMessage expected $ toVersionedTxInfo slang actual
@@ -84,9 +81,9 @@ assertTranslationComparison (TranslationInstance protVer lang utxo tx expected)
8481
}
8582
errorMessage =
8683
unlines
87-
[ "Unexpected txinfo with arguments: "
84+
[ "Unexpected TxInfo with arguments: "
8885
, " ProtVer: " <> show protVer
89-
, " language: " <> show lang
90-
, " utxo: " <> show utxo
91-
, " tx: " <> show tx
86+
, " Language: " <> show supportedLanguage
87+
, " UTxO: " <> show utxo
88+
, " Tx: " <> show tx
9289
]

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

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
module Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen (
1010
TranslatableGen (..),
11-
TxInfoLanguage (..),
1211
translationInstances,
1312
epochInfo,
1413
toVersionedTxInfo,
@@ -17,42 +16,36 @@ module Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen (
1716

1817
import Cardano.Ledger.Alonzo (AlonzoEra)
1918
import Cardano.Ledger.Alonzo.Plutus.Context (
20-
ContextError,
21-
EraPlutusTxInfo,
19+
EraPlutusContext,
2220
LedgerTxInfo (..),
2321
PlutusTxInfo,
22+
SupportedLanguage (..),
23+
supportedLanguages,
2424
toPlutusTxInfo,
2525
)
26-
import Cardano.Ledger.Alonzo.Scripts (AlonzoEraScript (eraMaxLanguage))
2726
import Cardano.Ledger.Alonzo.TxWits (Redeemers)
2827
import Cardano.Ledger.Core as Core
29-
import Cardano.Ledger.Plutus.Language (Language (..), SLanguage (..))
28+
import Cardano.Ledger.Plutus.Language (SLanguage (..))
3029
import Cardano.Ledger.State (UTxO (..))
3130
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
3231
import Cardano.Slotting.Slot (EpochSize (..))
3332
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
33+
import qualified Data.List.NonEmpty as NE (toList)
3434
import qualified Data.Map.Strict as Map
3535
import qualified Data.Set as Set
3636
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
37-
import GHC.Stack
3837
import Lens.Micro ((^.))
3938
import Test.Cardano.Ledger.Alonzo.Arbitrary ()
4039
import Test.Cardano.Ledger.Alonzo.Translation.TranslationInstance (
4140
TranslationInstance (..),
4241
VersionedTxInfo (..),
4342
)
44-
import Test.QuickCheck (Gen, arbitrary, elements, vectorOf)
45-
import Test.QuickCheck.Gen (Gen (MkGen))
46-
import Test.QuickCheck.Random (mkQCGen)
43+
import Test.Cardano.Ledger.Common
4744

48-
data TxInfoLanguage era where
49-
TxInfoLanguage :: EraPlutusTxInfo l era => SLanguage l -> TxInfoLanguage era
50-
51-
class EraTx era => TranslatableGen era where
45+
class (EraTx era, EraPlutusContext era, Arbitrary (Script era)) => TranslatableGen era where
5246
tgRedeemers :: Gen (Redeemers era)
53-
tgTx :: Language -> Gen (Core.Tx era)
54-
tgUtxo :: Language -> Core.Tx era -> Gen (UTxO era)
55-
mkTxInfoLanguage :: HasCallStack => Language -> TxInfoLanguage era
47+
tgTx :: SupportedLanguage era -> Gen (Core.Tx era)
48+
tgUtxo :: SupportedLanguage era -> Core.Tx era -> Gen (UTxO era)
5649

5750
instance TranslatableGen AlonzoEra where
5851
tgRedeemers = arbitrary
@@ -61,24 +54,14 @@ instance TranslatableGen AlonzoEra where
6154
let ins = tx ^. bodyTxL ^. inputsTxBodyL
6255
outs <- vectorOf (length ins) (arbitrary :: Gen (TxOut AlonzoEra))
6356
pure $ UTxO (Map.fromList $ Set.toList ins `zip` outs)
64-
mkTxInfoLanguage PlutusV1 = TxInfoLanguage SPlutusV1
65-
mkTxInfoLanguage lang =
66-
error $ "Language " ++ show lang ++ " is not supported in " ++ eraName @AlonzoEra
6757

6858
translationInstances ::
69-
forall era.
70-
( AlonzoEraScript era
71-
, TranslatableGen era
72-
, Show (ContextError era)
73-
) =>
59+
TranslatableGen era =>
7460
Int ->
7561
Int ->
7662
[TranslationInstance era]
7763
translationInstances size seed =
78-
generateWithSeed seed $ vectorOf size genTranslationInstance
79-
80-
generateWithSeed :: Int -> Gen a -> a
81-
generateWithSeed seed (MkGen g) = g (mkQCGen seed) 30
64+
runGen seed 30 $ vectorOf size genTranslationInstance
8265

8366
toVersionedTxInfo :: SLanguage l -> PlutusTxInfo l -> VersionedTxInfo
8467
toVersionedTxInfo slang txInfo =
@@ -89,16 +72,13 @@ toVersionedTxInfo slang txInfo =
8972

9073
genTranslationInstance ::
9174
forall era.
92-
( AlonzoEraScript era
93-
, TranslatableGen era
94-
, Show (ContextError era)
95-
) =>
75+
TranslatableGen era =>
9676
Gen (TranslationInstance era)
9777
genTranslationInstance = do
9878
protVer <- arbitrary
99-
lang <- elements [minBound .. eraMaxLanguage @era]
100-
tx <- tgTx @era lang
101-
utxo <- tgUtxo lang tx
79+
supportedLanguage <- elements $ NE.toList (supportedLanguages @era)
80+
tx <- tgTx @era supportedLanguage
81+
utxo <- tgUtxo supportedLanguage tx
10282
let lti =
10383
LedgerTxInfo
10484
{ ltiProtVer = protVer
@@ -107,11 +87,12 @@ genTranslationInstance = do
10787
, ltiUTxO = utxo
10888
, ltiTx = tx
10989
}
110-
case mkTxInfoLanguage @era lang of
111-
TxInfoLanguage slang -> do
90+
case supportedLanguage of
91+
SupportedLanguage slang -> do
11292
case toPlutusTxInfo slang lti of
11393
Left err -> error $ show err
114-
Right txInfo -> pure $ TranslationInstance protVer lang utxo tx $ toVersionedTxInfo slang txInfo
94+
Right txInfo ->
95+
pure $ TranslationInstance protVer supportedLanguage utxo tx $ toVersionedTxInfo slang txInfo
11596

11697
epochInfo :: EpochInfo (Either a)
11798
epochInfo = fixedEpochInfo (EpochSize 100) (mkSlotLength 1)

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Test.Cardano.Ledger.Alonzo.Translation.TranslationInstance (
1717
VersionedTxInfo (..),
1818
) where
1919

20+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext, SupportedLanguage)
2021
import Cardano.Ledger.BaseTypes (ProtVer)
2122
import Cardano.Ledger.Binary (
2223
DecCBOR (..),
@@ -35,11 +36,9 @@ import Cardano.Ledger.Binary.Coders (
3536
(<!),
3637
)
3738
import Cardano.Ledger.Core as Core
38-
import Cardano.Ledger.Plutus.Language (Language (..))
3939
import Cardano.Ledger.State (UTxO (..))
4040
import qualified Codec.Serialise as Cborg (Serialise (..))
4141
import qualified Data.ByteString.Lazy as BSL
42-
import Data.Typeable (Typeable)
4342
import GHC.Generics (Generic)
4443
import qualified PlutusLedgerApi.V1 as PV1
4544
import qualified PlutusLedgerApi.V2 as PV2
@@ -54,7 +53,7 @@ data VersionedTxInfo
5453
-- | Represents arguments passed to `alonzoTxInfo` along with the produced result.
5554
data TranslationInstance era = TranslationInstance
5655
{ tiProtVer :: ProtVer
57-
, tiLanguage :: Language
56+
, tiLanguage :: SupportedLanguage era
5857
, tiUtxo :: UTxO era
5958
, tiTx :: Core.Tx era
6059
, tiResult :: VersionedTxInfo
@@ -126,7 +125,7 @@ instance DecCBOR VersionedTxInfo where
126125
decCBOR = fromPlainDecoder Cborg.decode
127126

128127
instance
129-
( Typeable era
128+
( Era era
130129
, EncCBOR (PParams era)
131130
, EncCBOR (UTxO era)
132131
, EncCBOR (Core.Tx era)
@@ -143,10 +142,10 @@ instance
143142
!> To r
144143

145144
instance
146-
( Typeable era
147-
, DecCBOR (PParams era)
145+
( DecCBOR (PParams era)
148146
, DecCBOR (UTxO era)
149147
, DecCBOR (Core.Tx era)
148+
, EraPlutusContext era
150149
) =>
151150
DecCBOR (TranslationInstance era)
152151
where
@@ -161,10 +160,10 @@ instance
161160

162161
deserializeTranslationInstances ::
163162
forall era.
164-
( Era era
165-
, DecCBOR (PParams era)
163+
( DecCBOR (PParams era)
166164
, DecCBOR (UTxO era)
167165
, DecCBOR (Core.Tx era)
166+
, EraPlutusContext era
168167
) =>
169168
BSL.ByteString ->
170169
Either DecoderError [TranslationInstance era]

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/Translation/TranslatableGen.hs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Test.Cardano.Ledger.Babbage.Translation.TranslatableGen (
1313
) where
1414

1515
import Cardano.Ledger.Address (Addr (..))
16+
import Cardano.Ledger.Alonzo.Plutus.Context (SupportedLanguage (..))
1617
import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..), ExUnits (..))
1718
import Cardano.Ledger.Alonzo.Tx (AlonzoTx (..))
1819
import Cardano.Ledger.Alonzo.TxWits
@@ -22,7 +23,7 @@ import Cardano.Ledger.Babbage.TxBody (BabbageTxOut (..), TxBody (BabbageTxBody))
2223
import Cardano.Ledger.Binary (mkSized)
2324
import Cardano.Ledger.Credential (StakeReference (..))
2425
import Cardano.Ledger.Plutus.Data (Data (..), Datum (..))
25-
import Cardano.Ledger.Plutus.Language (Language (..), SLanguage (..))
26+
import Cardano.Ledger.Plutus.Language (SLanguage (..))
2627
import Cardano.Ledger.State (UTxO (..))
2728
import Cardano.Ledger.TxIn (TxIn (..))
2829
import qualified Data.Map.Strict as Map
@@ -31,10 +32,7 @@ import Data.Sequence.Strict (fromList)
3132
import qualified Data.Set as Set
3233
import Lens.Micro ((^.))
3334
import Test.Cardano.Ledger.Alonzo.Arbitrary (genScripts)
34-
import Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen (
35-
TranslatableGen (..),
36-
TxInfoLanguage (..),
37-
)
35+
import Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen (TranslatableGen (..))
3836
import Test.Cardano.Ledger.Babbage.Arbitrary ()
3937
import Test.Cardano.Ledger.Core.Arbitrary ()
4038
import Test.QuickCheck (
@@ -53,10 +51,6 @@ instance TranslatableGen BabbageEra where
5351
tgRedeemers = genRedeemers
5452
tgTx l = genTx @BabbageEra (genTxBody l)
5553
tgUtxo = utxoWithTx @BabbageEra
56-
mkTxInfoLanguage PlutusV1 = TxInfoLanguage SPlutusV1
57-
mkTxInfoLanguage PlutusV2 = TxInfoLanguage SPlutusV2
58-
mkTxInfoLanguage lang =
59-
error $ "Language " ++ show lang ++ " is not supported in " ++ eraName @BabbageEra
6054

6155
utxoWithTx ::
6256
forall era.
@@ -65,7 +59,7 @@ utxoWithTx ::
6559
, Arbitrary (Script era)
6660
, TxOut era ~ BabbageTxOut era
6761
) =>
68-
Language ->
62+
SupportedLanguage era ->
6963
Tx era ->
7064
Gen (UTxO era)
7165
utxoWithTx l tx = do
@@ -77,8 +71,6 @@ genTx ::
7771
forall era.
7872
( TranslatableGen era
7973
, Arbitrary (TxAuxData era)
80-
, Arbitrary (Script era)
81-
, AlonzoEraScript era
8274
, AlonzoTxWits era ~ TxWits era
8375
) =>
8476
Gen (TxBody era) ->
@@ -96,28 +88,28 @@ genTxOut ::
9688
, Arbitrary (Value era)
9789
, Arbitrary (Script era)
9890
) =>
99-
Language ->
91+
SupportedLanguage era ->
10092
Gen (BabbageTxOut era)
101-
genTxOut l = do
93+
genTxOut (SupportedLanguage slang) = do
10294
addr <- genNonByronAddr
10395
value <- scale (`div` 15) arbitrary
104-
script <- case l of
105-
PlutusV1 -> pure SNothing
96+
script <- case slang of
97+
SPlutusV1 -> pure SNothing
10698
_ -> arbitrary
107-
datum <- case l of
108-
PlutusV1 -> oneof [pure NoDatum, DatumHash <$> (arbitrary :: Gen DataHash)]
99+
datum <- case slang of
100+
SPlutusV1 -> oneof [pure NoDatum, DatumHash <$> (arbitrary :: Gen DataHash)]
109101
_ -> arbitrary
110102
pure $ BabbageTxOut addr value datum script
111103

112-
genTxBody :: Language -> Gen (TxBody BabbageEra)
113-
genTxBody l = do
104+
genTxBody :: SupportedLanguage BabbageEra -> Gen (TxBody BabbageEra)
105+
genTxBody l@(SupportedLanguage slang) = do
114106
let genTxOuts = fromList <$> listOf1 (mkSized (eraProtVerLow @BabbageEra) <$> genTxOut @BabbageEra l)
115107
let genTxIns = Set.fromList <$> listOf1 (arbitrary :: Gen TxIn)
116108
BabbageTxBody
117109
<$> genTxIns
118110
<*> arbitrary
119-
<*> ( case l of -- refinputs
120-
PlutusV1 -> pure Set.empty
111+
<*> ( case slang of -- refinputs
112+
SPlutusV1 -> pure Set.empty
121113
_ -> arbitrary
122114
)
123115
<*> genTxOuts
@@ -145,10 +137,7 @@ genNonByronAddr =
145137
]
146138

147139
genTxWits ::
148-
( TranslatableGen era
149-
, Arbitrary (Script era)
150-
, AlonzoEraScript era
151-
) =>
140+
TranslatableGen era =>
152141
Gen (AlonzoTxWits era)
153142
genTxWits =
154143
AlonzoTxWits

0 commit comments

Comments
 (0)