Skip to content

Commit 147050f

Browse files
committed
Move transaction-related golden tests from cardano-ledger-alonzo-test to cardano-ledger-alonzo
Consolidates golden tests from the separate `cardano-ledger-alonzo-test` package into the main `cardano-ledger-alonzo` impl package, following the same pattern already completed in other eras (allegra, mary, conway, babbage).
1 parent f7cd206 commit 147050f

14 files changed

Lines changed: 558 additions & 446 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ library testlib
175175
Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec.Invalid
176176
Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec.Valid
177177
Test.Cardano.Ledger.Alonzo.ImpTest
178+
Test.Cardano.Ledger.Alonzo.JSON.GoldenSpec
178179
Test.Cardano.Ledger.Alonzo.Translation.Golden
179180
Test.Cardano.Ledger.Alonzo.Translation.TranslatableGen
180181
Test.Cardano.Ledger.Alonzo.Translation.TranslationInstance
@@ -204,6 +205,7 @@ library testlib
204205
-Wno-pattern-namespace-specifier
205206
build-depends:
206207
HUnit,
208+
aeson,
207209
base,
208210
bytestring,
209211
cardano-data:{cardano-data, testlib},
@@ -255,6 +257,7 @@ test-suite tests
255257
Paths_cardano_ledger_alonzo
256258
Test.Cardano.Ledger.Alonzo.Binary.CanonicalSpec
257259
Test.Cardano.Ledger.Alonzo.Binary.CddlSpec
260+
Test.Cardano.Ledger.Alonzo.Binary.GoldenSpec
258261
Test.Cardano.Ledger.Alonzo.BinarySpec
259262
Test.Cardano.Ledger.Alonzo.GoldenSpec
260263
Test.Cardano.Ledger.Alonzo.GoldenTranslation
@@ -273,6 +276,11 @@ test-suite tests
273276
-rtsopts
274277
-with-rtsopts=-N
275278

279+
-- In ghc-9.14 the `pattern` namespace specifier is deprecated.
280+
if impl(ghc >=9.14)
281+
ghc-options:
282+
-Wno-pattern-namespace-specifier
283+
276284
build-depends:
277285
HUnit,
278286
QuickCheck,
@@ -283,10 +291,12 @@ test-suite tests
283291
cardano-ledger-alonzo:{cardano-ledger-alonzo, cddl, testlib},
284292
cardano-ledger-binary:{cardano-ledger-binary, testlib},
285293
cardano-ledger-core:{cardano-ledger-core, testlib},
286-
cardano-ledger-shelley:testlib,
294+
cardano-ledger-mary,
295+
cardano-ledger-shelley:{cardano-ledger-shelley, testlib},
287296
cardano-strict-containers,
288297
containers,
289298
microlens,
290299
microlens-mtl,
300+
plutus-ledger-api,
291301
testlib,
292302
text,
File renamed without changes.
File renamed without changes.

eras/alonzo/test-suite/golden/TagMismatchDescription.json renamed to eras/alonzo/impl/golden/TagMismatchDescription.json

File renamed without changes.
File renamed without changes.

eras/alonzo/test-suite/golden/mainnet-alonzo-genesis.json renamed to eras/alonzo/impl/golden/mainnet-alonzo-genesis.json

File renamed without changes.

eras/alonzo/impl/golden/tx.cbor

1.69 KB
Binary file not shown.

eras/alonzo/impl/test/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import qualified Test.Cardano.Ledger.Alonzo.Binary.CddlSpec as CddlSpec
88
import qualified Test.Cardano.Ledger.Alonzo.Binary.CostModelsSpec as CostModelsSpec
99
import qualified Test.Cardano.Ledger.Alonzo.Binary.TxWitsSpec as TxWitsSpec
1010
import qualified Test.Cardano.Ledger.Alonzo.BinarySpec as BinarySpec
11-
import qualified Test.Cardano.Ledger.Alonzo.GoldenSpec as Golden
11+
import qualified Test.Cardano.Ledger.Alonzo.GoldenSpec as GoldenSpec
1212
import qualified Test.Cardano.Ledger.Alonzo.GoldenTranslation as GoldenTranslation
1313
import qualified Test.Cardano.Ledger.Alonzo.Imp as Imp
1414
import qualified Test.Cardano.Ledger.Alonzo.Imp.TxInfoSpec as TxInfoImp
@@ -27,7 +27,7 @@ main =
2727
roundTripJsonEraSpec @AlonzoEra
2828
roundTripJsonShelleyEraSpec @AlonzoEra
2929
GoldenTranslation.tests
30-
Golden.spec
30+
GoldenSpec.spec
3131
describe "Imp" $ do
3232
Imp.spec @AlonzoEra
3333
describe "CostModels" $ do
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
3+
module Test.Cardano.Ledger.Alonzo.Binary.GoldenSpec (spec) where
4+
5+
import Cardano.Ledger.Alonzo (AlonzoEra)
6+
import Cardano.Ledger.Alonzo.Core (eraProtVerLow)
7+
import Paths_cardano_ledger_alonzo (getDataFileName)
8+
import qualified Test.Cardano.Ledger.Alonzo.Binary.Golden as BinaryGolden
9+
import Test.Cardano.Ledger.Alonzo.Examples (ledgerExamples)
10+
import Test.Cardano.Ledger.Binary.Golden (cborAnnGoldenSpec)
11+
import Test.Cardano.Ledger.Common (Spec)
12+
import Test.Cardano.Ledger.Shelley.Examples (LedgerExamples (..))
13+
14+
spec :: Spec
15+
spec = do
16+
cborAnnGoldenSpec
17+
getDataFileName
18+
"golden/tx.cbor"
19+
(eraProtVerLow @AlonzoEra)
20+
(leTx ledgerExamples)
21+
BinaryGolden.spec @AlonzoEra
Lines changed: 243 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,253 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE PatternSynonyms #-}
14
{-# LANGUAGE ScopedTypeVariables #-}
25
{-# LANGUAGE TypeApplications #-}
36

47
module Test.Cardano.Ledger.Alonzo.GoldenSpec (spec) where
58

6-
import Cardano.Ledger.Alonzo
7-
import Paths_cardano_ledger_alonzo (getDataFileName)
8-
import qualified Test.Cardano.Ledger.Alonzo.Binary.Golden as Golden
9-
import Test.Cardano.Ledger.Alonzo.Era ()
9+
import Cardano.Ledger.Address (Addr)
10+
import Cardano.Ledger.Allegra.Scripts (
11+
pattern RequireTimeExpire,
12+
pattern RequireTimeStart,
13+
)
14+
import Cardano.Ledger.Alonzo (AlonzoEra)
15+
import Cardano.Ledger.Alonzo.Core (
16+
EraTxOut (..),
17+
PParams,
18+
dataHashTxOutL,
19+
emptyPParams,
20+
mkBasicTxOut,
21+
ppCostModelsL,
22+
)
23+
import Cardano.Ledger.Alonzo.PParams (LangDepView (..), getLanguageView)
24+
import Cardano.Ledger.Alonzo.TxBody (utxoEntrySize)
25+
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
26+
import Cardano.Ledger.Coin (Coin (..))
27+
import Cardano.Ledger.Core (hashScript)
28+
import Cardano.Ledger.Keys (KeyRole (..))
29+
import Cardano.Ledger.Mary (MaryEra)
30+
import Cardano.Ledger.Mary.Value (AssetName (..), PolicyID (..), valueFromList)
31+
import Cardano.Ledger.Plutus.Data (Data (..), hashData)
32+
import Cardano.Ledger.Plutus.Language (Language (..))
33+
import Cardano.Ledger.Shelley.Scripts (pattern RequireAllOf)
34+
import Cardano.Ledger.Slot (SlotNo (..))
35+
import qualified Data.ByteString.Base16 as B16
36+
import qualified Data.ByteString.Short as SBS
37+
import qualified Data.Sequence.Strict as StrictSeq
38+
import Data.Word (Word8)
39+
import Lens.Micro
40+
import qualified PlutusLedgerApi.V1 as PV1
41+
import qualified Test.Cardano.Ledger.Alonzo.Binary.GoldenSpec as BinaryGoldenSpec
42+
import qualified Test.Cardano.Ledger.Alonzo.JSON.GoldenSpec as JsonGoldenSpec
1043
import Test.Cardano.Ledger.Common
11-
import Test.Cardano.Ledger.Core.JSON (goldenJsonPParamsSpec, goldenJsonPParamsUpdateSpec)
44+
import Test.Cardano.Ledger.Core.KeyPair (mkAddr, mkKeyPair)
45+
import Test.Cardano.Ledger.Plutus (zeroTestingCostModels)
1246

1347
spec :: Spec
1448
spec =
1549
describe "Golden" $ do
16-
describe "JSON files" $ do
17-
beforeAll (getDataFileName "golden/pparams.json") $
18-
goldenJsonPParamsSpec @AlonzoEra
19-
beforeAll (getDataFileName "golden/pparams-update.json") $
20-
goldenJsonPParamsUpdateSpec @AlonzoEra
21-
describe "CBOR" $ Golden.spec @AlonzoEra
50+
describe "JSON" JsonGoldenSpec.spec
51+
describe "CBOR" BinaryGoldenSpec.spec
52+
goldenScriptIntegrity
53+
goldenUTxOEntryMinAda
54+
55+
goldenScriptIntegrity :: Spec
56+
goldenScriptIntegrity =
57+
describe "Script integrity hash" $ do
58+
it "PlutusV1" $ testScriptIntegrityHash exPP PlutusV1 exampleLangDepViewPV1
59+
it "PlutusV2" $ testScriptIntegrityHash exPP PlutusV2 exampleLangDepViewPV2
60+
where
61+
testScriptIntegrityHash ::
62+
HasCallStack =>
63+
PParams AlonzoEra ->
64+
Language ->
65+
LangDepView ->
66+
Expectation
67+
testScriptIntegrityHash pp lang view = getLanguageView pp lang `shouldBe` view
68+
69+
exampleLangDepViewPV1 :: LangDepView
70+
exampleLangDepViewPV1 = LangDepView b1 b2
71+
where
72+
b1 =
73+
fromRightError "invalid hex encoding of the language inside exampleLangDepViewPV1" $
74+
B16.decode "4100"
75+
b2 =
76+
fromRightError "invalid hex encoding of the cost model inside exampleLangDepViewPV1" $
77+
B16.decode $
78+
"58a89f0000000000000000000000000000000000000000000000000000000000"
79+
<> "0000000000000000000000000000000000000000000000000000000000000000"
80+
<> "0000000000000000000000000000000000000000000000000000000000000000"
81+
<> "0000000000000000000000000000000000000000000000000000000000000000"
82+
<> "0000000000000000000000000000000000000000000000000000000000000000"
83+
<> "000000000000000000ff"
84+
85+
exampleLangDepViewPV2 :: LangDepView
86+
exampleLangDepViewPV2 = LangDepView b1 b2
87+
where
88+
b1 =
89+
fromRightError "invalid hex encoding of the language inside exampleLangDepViewPV2" $
90+
B16.decode "01"
91+
b2 =
92+
fromRightError "invalid hex encoding of the cost model inside exampleLangDepViewPV2" $
93+
B16.decode $
94+
"98af000000000000000000000000000000000000000000000000000000000000"
95+
<> "0000000000000000000000000000000000000000000000000000000000000000"
96+
<> "0000000000000000000000000000000000000000000000000000000000000000"
97+
<> "0000000000000000000000000000000000000000000000000000000000000000"
98+
<> "0000000000000000000000000000000000000000000000000000000000000000"
99+
<> "0000000000000000000000000000000000"
100+
101+
exPP :: PParams AlonzoEra
102+
exPP =
103+
emptyPParams
104+
& ppCostModelsL .~ zeroTestingCostModels [PlutusV1, PlutusV2]
105+
106+
fromRightError :: (HasCallStack, Show a) => String -> Either a b -> b
107+
fromRightError errorMsg =
108+
either (\e -> error $ errorMsg ++ ": " ++ show e) id
109+
110+
-- | (heapWords of a DataHash) * coinsPerUTxOWordLocal is 344820
111+
goldenUTxOEntryMinAda :: Spec
112+
goldenUTxOEntryMinAda =
113+
describe "golden tests - UTxOEntryMinAda" $ do
114+
it "one policy, one (smallest) name, yes datum hash" $
115+
calcMinUTxO
116+
( mkBasicTxOut carlAddr (valueFromList (Coin 1407406) [(pid1, smallestName, 1)])
117+
& dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.List [])))
118+
)
119+
`shouldBe` Coin 1655136
120+
it "one policy, one (smallest) name, no datum hash" $
121+
calcMinUTxO
122+
(mkBasicTxOut bobAddr (valueFromList (Coin 1407406) [(pid1, smallestName, 1)]))
123+
`shouldBe` Coin 1310316
124+
it "one policy, one (small) name" $
125+
calcMinUTxO
126+
(mkBasicTxOut aliceAddr (valueFromList (Coin 1444443) [(pid1, smallName 1, 1)]))
127+
`shouldBe` Coin 1344798
128+
it "one policy, three (small) names" $
129+
calcMinUTxO
130+
( mkBasicTxOut
131+
aliceAddr
132+
( valueFromList
133+
(Coin 1555554)
134+
[ (pid1, smallName 1, 1)
135+
, (pid1, smallName 2, 1)
136+
, (pid1, smallName 3, 1)
137+
]
138+
)
139+
)
140+
`shouldBe` Coin 1448244
141+
it "one policy, one (largest) name" $
142+
calcMinUTxO
143+
(mkBasicTxOut carlAddr (valueFromList (Coin 1555554) [(pid1, largestName 65, 1)]))
144+
`shouldBe` Coin 1448244
145+
it "one policy, three (largest) name, with hash" $
146+
calcMinUTxO
147+
( mkBasicTxOut
148+
carlAddr
149+
( valueFromList
150+
(Coin 1962961)
151+
[ (pid1, largestName 65, 1)
152+
, (pid1, largestName 66, 1)
153+
, (pid1, largestName 67, 1)
154+
]
155+
)
156+
& dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.Constr 0 [PV1.Constr 0 []])))
157+
)
158+
`shouldBe` Coin 2172366
159+
it "two policies, one (smallest) name" $
160+
calcMinUTxO
161+
( mkBasicTxOut
162+
aliceAddr
163+
(valueFromList (Coin 1592591) [(pid1, smallestName, 1), (pid2, smallestName, 1)])
164+
)
165+
`shouldBe` Coin 1482726
166+
it "two policies, one (smallest) name, with hash" $
167+
calcMinUTxO
168+
( mkBasicTxOut
169+
aliceAddr
170+
(valueFromList (Coin 1592591) [(pid1, smallestName, 1), (pid2, smallestName, 1)])
171+
& dataHashTxOutL .~ SJust (hashData @AlonzoEra (Data (PV1.Constr 0 [])))
172+
)
173+
`shouldBe` Coin 1827546
174+
it "two policies, two (small) names" $
175+
calcMinUTxO
176+
(mkBasicTxOut bobAddr (valueFromList (Coin 1629628) [(pid1, smallName 1, 1), (pid2, smallName 2, 1)]))
177+
`shouldBe` Coin 1517208
178+
it "three policies, ninety-six (small) names" $
179+
calcMinUTxO
180+
( mkBasicTxOut
181+
aliceAddr
182+
( let f i c = (i, smallName c, 1)
183+
in valueFromList
184+
(Coin 7407400)
185+
[ f i c
186+
| (i, cs) <-
187+
[(pid1, [32 .. 63]), (pid2, [64 .. 95]), (pid3, [96 .. 127])]
188+
, c <- cs
189+
]
190+
)
191+
)
192+
`shouldBe` Coin 6896400
193+
it "utxo entry size of ada-only" $
194+
-- This value, 29, is helpful for comparing the alonzo protocol parameter utxoCostPerWord
195+
-- with the old parameter minUTxOValue.
196+
-- If we wish to keep the ada-only, no datum hash, minimum value nearly the same,
197+
-- we can divide minUTxOValue by 29 and round.
198+
utxoEntrySize @AlonzoEra (mkBasicTxOut aliceAddr mempty) `shouldBe` 29
199+
where
200+
calcMinUTxO :: TxOut AlonzoEra -> Coin
201+
calcMinUTxO tout = Coin (utxoEntrySize tout * coinsPerUTxOWordLocal)
202+
203+
-- \| ada cost of storing a word8 of data as a UTxO entry, assuming no change to minUTxOValue
204+
coinsPerUTxOWordLocal :: Integer
205+
coinsPerUTxOWordLocal = quot minUTxOValueShelleyMA utxoEntrySizeWithoutValLocal
206+
where
207+
utxoEntrySizeWithoutValLocal = 29
208+
Coin minUTxOValueShelleyMA = minUTxO
209+
210+
-- Helpers inlined from Test.Cardano.Ledger.Mary.Golden
211+
-- (cardano-ledger-shelley-ma-test cannot be a dep due to cyclic dependency via cardano-protocol-tpraos)
212+
213+
pid1 :: PolicyID
214+
pid1 =
215+
PolicyID $
216+
hashScript @MaryEra $
217+
RequireAllOf (StrictSeq.fromList [])
218+
219+
pid2 :: PolicyID
220+
pid2 =
221+
PolicyID $
222+
hashScript @MaryEra $
223+
RequireAllOf (StrictSeq.fromList [RequireTimeStart (SlotNo 1)])
224+
225+
pid3 :: PolicyID
226+
pid3 =
227+
PolicyID $
228+
hashScript @MaryEra $
229+
RequireAllOf (StrictSeq.fromList [RequireTimeExpire (SlotNo 1)])
230+
231+
smallestName :: AssetName
232+
smallestName = AssetName $ SBS.pack []
233+
234+
smallName :: Word8 -> AssetName
235+
smallName c = AssetName $ SBS.pack [c]
236+
237+
largestName :: Word8 -> AssetName
238+
largestName c = AssetName . SBS.pack $ c : [1 .. 31]
239+
240+
minUTxO :: Coin
241+
minUTxO = Coin $ 1000 * 1000
242+
243+
-- Addresses inlined from Test.Cardano.Ledger.Shelley.Examples.Cast
244+
-- (cardano-ledger-shelley-test cannot be a dep due to cyclic dependency via cardano-protocol-tpraos)
245+
246+
aliceAddr :: Addr
247+
aliceAddr = mkAddr (mkKeyPair @Payment 0) (mkKeyPair @Staking 1)
248+
249+
bobAddr :: Addr
250+
bobAddr = mkAddr (mkKeyPair @Payment 2) (mkKeyPair @Staking 3)
251+
252+
carlAddr :: Addr
253+
carlAddr = mkAddr (mkKeyPair @Payment 4) (mkKeyPair @Staking 5)

0 commit comments

Comments
 (0)