Skip to content

Commit 0f51add

Browse files
committed
Make tests era-generic
1 parent 1ffc288 commit 0f51add

File tree

19 files changed

+299
-142
lines changed

19 files changed

+299
-142
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,11 @@ Next you'll want to add a datatype to represent the new era (e.g. `ConwayEra`).
356356
Then copy over the entire test suite from the previous era and substitute the
357357
era types for the newly added era type. Once the tests are in place, the type
358358
checker will guide you to add all the necessary type family and type class
359-
instances.
359+
instances. You might need to make some of the tests era-generic. See which tests
360+
don't yet take an era type annotation and modify those test if possible. The tests
361+
that can be made era-generic should be moved to the `testlib` of the era where
362+
they were first introduced, and any era-specific tests should reside in the test
363+
suite package of that era.
360364
361365
Add the `Cardano.Ledger.<era>.Core` module and re-export the `Core` module from
362366
the previous era. Use the `Core` module from the current era whenever you need

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ library testlib
153153
Test.Cardano.Ledger.Conway.ImpTest
154154
Test.Cardano.Ledger.Conway.Plutus.PlutusSpec
155155
Test.Cardano.Ledger.Conway.Proposals
156+
Test.Cardano.Ledger.Conway.Spec
156157
Test.Cardano.Ledger.Conway.SPORatifySpec
157158
Test.Cardano.Ledger.Conway.Translation.TranslatableGen
158159
Test.Cardano.Ledger.Conway.TreeDiff
@@ -249,7 +250,6 @@ test-suite tests
249250
other-modules:
250251
Paths_cardano_ledger_conway
251252
Test.Cardano.Ledger.Conway.GoldenTranslation
252-
Test.Cardano.Ledger.Conway.Spec
253253

254254
default-language: Haskell2010
255255
ghc-options:
@@ -269,6 +269,5 @@ test-suite tests
269269
base,
270270
cardano-ledger-alonzo:testlib,
271271
cardano-ledger-conway,
272-
cardano-ledger-core,
273-
cardano-ledger-core:testlib,
272+
cardano-ledger-core:{testlib, cardano-ledger-core},
274273
testlib,

eras/conway/impl/test/Main.hs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,30 @@
22

33
module Main where
44

5+
import Cardano.Ledger.Coin (Coin (..))
56
import Cardano.Ledger.Conway (ConwayEra)
6-
import qualified Test.Cardano.Ledger.Alonzo.Binary.CostModelsSpec as CostModelsSpec
7-
import qualified Test.Cardano.Ledger.Alonzo.Binary.TxWitsSpec as TxWitsSpec
7+
import Cardano.Ledger.Conway.Tx (tierRefScriptFee)
88
import Test.Cardano.Ledger.Common
9-
import qualified Test.Cardano.Ledger.Conway.Binary.CddlSpec as Cddl
10-
import qualified Test.Cardano.Ledger.Conway.Binary.Regression as Regression
11-
import qualified Test.Cardano.Ledger.Conway.BinarySpec as Binary
12-
import qualified Test.Cardano.Ledger.Conway.CommitteeRatifySpec as CommitteeRatify
13-
import qualified Test.Cardano.Ledger.Conway.DRepRatifySpec as DRepRatify
149
import qualified Test.Cardano.Ledger.Conway.GenesisSpec as Genesis
15-
import Test.Cardano.Ledger.Conway.GoldenSpec as Golden
1610
import qualified Test.Cardano.Ledger.Conway.GoldenTranslation as GoldenTranslation
1711
import qualified Test.Cardano.Ledger.Conway.GovActionReorderSpec as GovActionReorder
18-
import qualified Test.Cardano.Ledger.Conway.Imp as Imp
1912
import Test.Cardano.Ledger.Conway.Plutus.PlutusSpec as PlutusSpec
20-
import qualified Test.Cardano.Ledger.Conway.Proposals as Proposals
21-
import qualified Test.Cardano.Ledger.Conway.SPORatifySpec as SPORatifySpec
22-
import qualified Test.Cardano.Ledger.Conway.Spec as Spec
23-
import qualified Test.Cardano.Ledger.Conway.TxInfoSpec as TxInfo
24-
import Test.Cardano.Ledger.Core.JSON (roundTripJsonEraSpec)
13+
import qualified Test.Cardano.Ledger.Conway.Spec as ConwaySpec
2514

2615
main :: IO ()
27-
main =
28-
ledgerTestMain $
29-
describe "Conway" $ do
30-
GoldenTranslation.spec
31-
Golden.spec
32-
Spec.spec
33-
Proposals.spec
34-
Binary.spec
35-
Cddl.spec @ConwayEra
36-
DRepRatify.spec
37-
CommitteeRatify.spec
38-
SPORatifySpec.spec
39-
Genesis.spec
40-
GovActionReorder.spec
41-
roundTripJsonEraSpec @ConwayEra
42-
describe "Imp" $
43-
Imp.spec @ConwayEra
44-
describe "CostModels" $ do
45-
CostModelsSpec.spec @ConwayEra
46-
describe "TxWits" $ do
47-
TxWitsSpec.spec @ConwayEra
48-
describe "Plutus" $ do
49-
PlutusSpec.spec
50-
Regression.spec @ConwayEra
51-
TxInfo.spec
16+
main = ledgerTestMain $ do
17+
describe "Conway era-generic" $ ConwaySpec.spec @ConwayEra
18+
describe "Conway era-specific" $ do
19+
GoldenTranslation.spec
20+
Genesis.spec
21+
GovActionReorder.spec
22+
describe "Plutus" $ do
23+
PlutusSpec.spec
24+
describe "Various tests for functions defined in Conway" $ do
25+
prop "tierRefScriptFee is a linear function when growth is 1" $ \(Positive sizeIncrement) baseFee (NonNegative size) ->
26+
tierRefScriptFee 1 sizeIncrement baseFee size
27+
=== Coin (floor (fromIntegral size * baseFee))
28+
it "tierRefScriptFee" $ do
29+
let step = 25600
30+
map (tierRefScriptFee 1.5 step 15) [0, step .. 204800]
31+
`shouldBe` map Coin [0, 384000, 960000, 1824000, 3120000, 5064000, 7980000, 12354000, 18915000]

eras/conway/impl/test/Test/Cardano/Ledger/Conway/Spec.hs

Lines changed: 0 additions & 16 deletions
This file was deleted.

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/BinarySpec.hs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
55
{-# LANGUAGE TypeApplications #-}
6+
{-# LANGUAGE TypeOperators #-}
67

78
module Test.Cardano.Ledger.Conway.BinarySpec (spec) where
89

910
import Cardano.Ledger.Alonzo.TxWits (Redeemers, TxDats)
1011
import Cardano.Ledger.Binary
1112
import Cardano.Ledger.Conway
13+
import Cardano.Ledger.Conway.Core (AlonzoEraScript (..), AsIx)
1214
import Cardano.Ledger.Conway.Genesis
1315
import Cardano.Ledger.Conway.Governance
1416
import Cardano.Ledger.Core
17+
import Cardano.Ledger.Shelley.LedgerState (StashedAVVMAddresses)
1518
import Data.Default (def)
1619
import Data.Proxy
1720
import Data.Typeable (typeRep)
@@ -21,14 +24,29 @@ import Test.Cardano.Ledger.Common
2124
import Test.Cardano.Ledger.Conway.Arbitrary ()
2225
import Test.Cardano.Ledger.Conway.Binary.Annotator ()
2326
import Test.Cardano.Ledger.Conway.Binary.RoundTrip (roundTripConwayCommonSpec)
27+
import Test.Cardano.Ledger.Conway.Era (BabbageEraTest)
28+
import Test.Cardano.Ledger.Conway.ImpTest (ConwayEraImp)
2429
import Test.Cardano.Ledger.Conway.TreeDiff ()
2530
import Test.Cardano.Ledger.Core.Binary (specUpgrade)
2631
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec, txSizeSpec)
27-
import Test.Cardano.Ledger.Core.Binary.RoundTrip (roundTripEraSpec)
32+
import Test.Cardano.Ledger.Core.Binary.RoundTrip (RuleListEra, roundTripEraSpec)
2833

29-
spec :: Spec
34+
spec ::
35+
forall era.
36+
( BabbageEraTest (PreviousEra era)
37+
, ConwayEraImp era
38+
, DecCBOR (TxAuxData era)
39+
, DecCBOR (TxWits era)
40+
, DecCBOR (TxBody era)
41+
, DecCBOR (Tx era)
42+
, Arbitrary (PlutusPurpose AsIx era)
43+
, RuleListEra era
44+
, StashedAVVMAddresses era ~ ()
45+
, SafeToHash (TxWits era)
46+
) =>
47+
Spec
3048
spec = do
31-
specUpgrade @ConwayEra def
49+
specUpgrade @era def
3250
describe "RoundTrip" $ do
3351
roundTripCborSpec @GovActionId
3452
roundTripCborSpec @(GovPurposeId 'PParamUpdatePurpose)
@@ -37,14 +55,14 @@ spec = do
3755
roundTripCborSpec @(GovPurposeId 'ConstitutionPurpose)
3856
roundTripCborSpec @Vote
3957
roundTripCborSpec @Voter
40-
roundTripConwayCommonSpec @ConwayEra
58+
roundTripConwayCommonSpec @era
4159
-- ConwayGenesis only makes sense in Conway era
42-
roundTripEraSpec @ConwayEra @ConwayGenesis
60+
roundTripEraSpec @era @ConwayGenesis
4361
describe "DecCBOR instances equivalence" $ do
44-
Binary.decoderEquivalenceCoreEraTypesSpec @ConwayEra
45-
decoderEquivalenceLenientSpec @(TxDats ConwayEra)
46-
decoderEquivalenceLenientSpec @(Redeemers ConwayEra)
47-
Binary.txSizeSpec @ConwayEra
62+
Binary.decoderEquivalenceCoreEraTypesSpec @era
63+
decoderEquivalenceLenientSpec @(TxDats era)
64+
decoderEquivalenceLenientSpec @(Redeemers era)
65+
Binary.txSizeSpec @era
4866
where
4967
-- The expectation used in this spec allows for the deserialization to fail, in which case
5068
-- it only checks that it fails for both decoders.

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/CommitteeRatifySpec.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ import qualified Data.Set as Set
4040
import Lens.Micro ((&), (.~))
4141
import Test.Cardano.Ledger.Common
4242
import Test.Cardano.Ledger.Conway.Arbitrary ()
43+
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)
4344
import Test.Cardano.Ledger.Core.Arbitrary ()
4445

45-
spec :: Spec
46+
spec :: forall era. ConwayEraTest era => Spec
4647
spec = do
4748
describe "Committee Ratification" $ do
48-
acceptedProp @ConwayEra
49+
acceptedProp @era
4950
acceptedRatioProp
5051
allYesProp
5152
allNoProp

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/DRepRatifySpec.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ import qualified Data.Set as Set
4242
import Data.Word (Word64)
4343
import Test.Cardano.Ledger.Common
4444
import Test.Cardano.Ledger.Conway.Arbitrary ()
45+
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)
4546
import Test.Cardano.Ledger.Core.Arbitrary ()
4647
import Test.Cardano.Ledger.Core.Rational ((%!))
4748

48-
spec :: Spec
49+
spec :: forall era. ConwayEraTest era => Spec
4950
spec = do
5051
describe "DRep Ratification" $ do
51-
correctThresholdsProp @ConwayEra
52-
noStakeProp @ConwayEra
52+
correctThresholdsProp @era
53+
noStakeProp @era
5354
acceptedRatioProp
5455
allAbstainProp
5556
noVotesProp
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
12
{-# LANGUAGE ScopedTypeVariables #-}
23
{-# LANGUAGE TypeApplications #-}
34

45
module Test.Cardano.Ledger.Conway.GoldenSpec (spec) where
56

6-
import Cardano.Ledger.Conway
77
import Paths_cardano_ledger_conway (getDataFileName)
88
import Test.Cardano.Ledger.Common
9-
import Test.Cardano.Ledger.Conway.Era ()
9+
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)
1010
import Test.Cardano.Ledger.Core.JSON (goldenJsonPParamsSpec, goldenJsonPParamsUpdateSpec)
1111

12-
spec :: Spec
12+
spec :: forall era. ConwayEraTest era => Spec
1313
spec =
1414
describe "Golden" $ do
1515
beforeAll (getDataFileName "golden/pparams.json") $
16-
goldenJsonPParamsSpec @ConwayEra
16+
goldenJsonPParamsSpec @era
1717
beforeAll (getDataFileName "golden/pparams-update.json") $
18-
goldenJsonPParamsUpdateSpec @ConwayEra
18+
goldenJsonPParamsUpdateSpec @era

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Proposals.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
{-# LANGUAGE LambdaCase #-}
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE NamedFieldPuns #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
44

55
module Test.Cardano.Ledger.Conway.Proposals where
66

7-
import Cardano.Ledger.Conway
87
import Cardano.Ledger.Conway.Governance
98
import Control.DeepSeq (force)
109
import Control.Exception (AssertionFailed (..), evaluate)
@@ -22,13 +21,14 @@ import Test.Cardano.Ledger.Conway.Arbitrary (
2221
ProposalsForEnactment (..),
2322
ProposalsNewActions (..),
2423
)
24+
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)
2525

26-
spec :: Spec
26+
spec :: forall era. ConwayEraTest era => Spec
2727
spec = do
2828
describe "Proposals" $ do
2929
describe "Construction" $ do
3030
prop "Adding new nodes keeps Proposals consistent" $
31-
\(ProposalsNewActions ps actions :: ProposalsNewActions ConwayEra) ->
31+
\(ProposalsNewActions ps actions :: ProposalsNewActions era) ->
3232
let ps' =
3333
F.foldl'
3434
(\p action -> fromMaybe (error "Unable to add action") $ proposalsAddAction action p)
@@ -38,22 +38,22 @@ spec = do
3838
in actionsMap `shouldBe` (actionsMap `Map.intersection` proposalsActionsMap ps')
3939
describe "Removal" $ do
4040
prop "Removing leaf nodes keeps Proposals consistent" $
41-
\(ps :: Proposals ConwayEra) -> do
41+
\(ps :: Proposals era) -> do
4242
let gais = Set.fromList $ toList $ SSeq.takeLast 4 $ proposalsIds ps
4343
ps' = fst $ proposalsRemoveWithDescendants gais ps
4444
proposalsSize ps' `shouldBe` proposalsSize ps - Set.size gais
4545
prop "Removing root nodes keeps Proposals consistent" $
46-
\(ps :: Proposals ConwayEra) -> do
46+
\(ps :: Proposals era) -> do
4747
let gais = Set.fromList $ toList $ SSeq.take 4 $ proposalsIds ps
4848
ps' = fst $ proposalsRemoveWithDescendants gais ps
4949
proposalsSize ps' `shouldSatisfy` (<= proposalsSize ps)
5050
prop "Removing non-member nodes throws an AssertionFailure" $
51-
\(ProposalsNewActions ps actions :: ProposalsNewActions ConwayEra) ->
51+
\(ProposalsNewActions ps actions :: ProposalsNewActions era) ->
5252
(evaluate . force) (proposalsRemoveWithDescendants (Set.fromList $ gasId <$> actions) ps)
5353
`shouldThrow` \AssertionFailed {} -> True
5454
describe "Enactment" $ do
5555
prop "Adding votes preserves consistency" $
56-
\( ProposalsForEnactment {pfeProposals, pfeToEnact} :: ProposalsForEnactment ConwayEra
56+
\( ProposalsForEnactment {pfeProposals, pfeToEnact} :: ProposalsForEnactment era
5757
, voter :: Voter
5858
, vote :: Vote
5959
) -> do
@@ -62,20 +62,20 @@ spec = do
6262
_ -> True
6363
prop "Enacting exhaustive lineages reduces Proposals to their roots" $
6464
\( ProposalsForEnactment {pfeProposals, pfeToEnact, pfeToRemove, pfeToRetain} ::
65-
ProposalsForEnactment ConwayEra
65+
ProposalsForEnactment era
6666
) -> do
6767
let (ps', enacted, removedDueToEnactment, expiredRemoved) = proposalsApplyEnactment pfeToEnact Set.empty pfeProposals
6868
expiredRemoved `shouldSatisfy` Map.null
6969
enacted `shouldBe` fromElems gasId pfeToEnact
7070
Map.keysSet removedDueToEnactment `shouldBe` pfeToRemove
7171
proposalsSize ps' `shouldBe` Set.size pfeToRetain
7272
prop "Enacting non-member nodes throws an AssertionFailure" $
73-
\(ProposalsNewActions ps actions :: ProposalsNewActions ConwayEra) ->
73+
\(ProposalsNewActions ps actions :: ProposalsNewActions era) ->
7474
(evaluate . force) (proposalsApplyEnactment (fromList actions) Set.empty ps)
7575
`shouldThrow` \AssertionFailed {} -> True
7676
prop "Expiring compliments of exhaustive lineages keeps proposals consistent" $
7777
\( ProposalsForEnactment {pfeProposals, pfeToEnact, pfeToRemove, pfeToRetain} ::
78-
ProposalsForEnactment ConwayEra
78+
ProposalsForEnactment era
7979
) -> do
8080
let (ps', enacted, removedDueToEnactment, expiredRemoved) =
8181
proposalsApplyEnactment Seq.Empty pfeToRemove pfeProposals
@@ -88,12 +88,12 @@ spec = do
8888
proposalsSize emptyProposals `shouldBe` Set.size pfeToRetain
8989
enactedMap `shouldBe` enactMap
9090
prop "Expiring non-member nodes throws an AssertionFailure" $
91-
\(ProposalsNewActions ps actions :: ProposalsNewActions ConwayEra) ->
91+
\(ProposalsNewActions ps actions :: ProposalsNewActions era) ->
9292
(evaluate . force) (proposalsApplyEnactment Seq.Empty (Set.fromList $ gasId <$> actions) ps)
9393
`shouldThrow` \AssertionFailed {} -> True
9494
prop "Enacting and expiring conflicting proposals does not lead to removal due to enactment" $
9595
\( ProposalsForEnactment {pfeProposals, pfeToEnact, pfeToRemove, pfeToRetain} ::
96-
ProposalsForEnactment ConwayEra
96+
ProposalsForEnactment era
9797
) -> do
9898
let (ps', enacted, enactedRemoved, expiredRemoved) = proposalsApplyEnactment pfeToEnact pfeToRemove pfeProposals
9999
Map.keysSet expiredRemoved `shouldBe` pfeToRemove

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/SPORatifySpec.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Cardano.Ledger.Address (RewardAccount (..))
1414
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
1515
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
1616
import Cardano.Ledger.Compactible (Compactible (..))
17-
import Cardano.Ledger.Conway
1817
import Cardano.Ledger.Conway.Core
1918
import Cardano.Ledger.Conway.Governance (
2019
GovAction (..),
@@ -46,17 +45,18 @@ import Data.Ratio ((%))
4645
import Lens.Micro
4746
import Test.Cardano.Ledger.Common
4847
import Test.Cardano.Ledger.Conway.Arbitrary ()
48+
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)
4949
import Test.Cardano.Ledger.Core.Arbitrary ()
5050

51-
spec :: Spec
51+
spec :: forall era. ConwayEraTest era => Spec
5252
spec = do
5353
describe "SPO Ratification" $ do
54-
acceptedRatioProp @ConwayEra
55-
noStakeProp @ConwayEra
56-
allAbstainProp @ConwayEra
57-
noVotesProp @ConwayEra
58-
allYesProp @ConwayEra
59-
noConfidenceProp @ConwayEra
54+
acceptedRatioProp @era
55+
noStakeProp @era
56+
allAbstainProp @era
57+
noVotesProp @era
58+
allYesProp @era
59+
noConfidenceProp @era
6060

6161
acceptedRatioProp ::
6262
forall era.

0 commit comments

Comments
 (0)