Skip to content

Commit

Permalink
Added Roundtrip tests to Conway, added JSON and CBOR instances to Con…
Browse files Browse the repository at this point in the history
…wayGenesis

Signed-off-by: Joosep Jääger <joosep.jaager@gmail.com>
Signed-off-by: Joosep Jääger <joosep.jaager@iohk.io>
  • Loading branch information
Soupstraw committed Sep 6, 2022
1 parent b019ba3 commit 2cc9bff
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
11 changes: 8 additions & 3 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ where

import Cardano.Ledger.Babbage.Genesis (extendPPWithGenesis)
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.Keys (GenDelegs)
import Data.Aeson (FromJSON (..), withObject, (.:))
import Cardano.Ledger.Keys (GenDelegs (..))
import Data.Aeson (FromJSON (..), withObject, (.:), ToJSON, object)
import Data.Unit.Strict (forceElemsToWHNF)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Data.Aeson.Types (ToJSON(..), KeyValue (..))

newtype ConwayGenesis crypto = ConwayGenesis (GenDelegs crypto)
deriving (Eq, Generic, NoThunks)
deriving (Eq, Generic, NoThunks, ToCBOR, FromCBOR, Show)

instance Crypto crypto => ToJSON (ConwayGenesis crypto) where
toJSON (ConwayGenesis genDelegs) = object ["genDelegs" .= toJSON genDelegs]

instance Crypto crypto => FromJSON (ConwayGenesis crypto) where
parseJSON = withObject "ConwayGenesis" $ \obj ->
Expand Down
22 changes: 21 additions & 1 deletion eras/conway/test-suite/cardano-ledger-conway-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ library

exposed-modules:
Test.Cardano.Ledger.Conway.Examples.Consensus,
Test.Cardano.Ledger.Conway.Serialisation.Generators
Test.Cardano.Ledger.Conway.Serialisation.Generators,
Test.Cardano.Ledger.Conway.Serialisation.Roundtrip
build-depends:
aeson,
bytestring,
cardano-binary,
cardano-ledger-alonzo,
Expand All @@ -62,7 +64,25 @@ library
cardano-ledger-shelley,
small-steps,
strict-containers,
tasty,
tasty-quickcheck,
text,
hs-source-dirs:
src

test-suite cardano-ledger-conway-test
import: base, project-config
type: exitcode-stdio-1.0
main-is: Tests.hs
hs-source-dirs: test
build-depends:
cardano-ledger-conway,
cardano-ledger-conway-test,
cardano-ledger-shelley-test,
tasty,
tasty-quickcheck,
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
"-with-rtsopts=-K4m -M250m"

Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.Conway.Serialisation.Generators () where

import Test.Cardano.Ledger.Babbage.Serialisation.Generators ()
import Test.QuickCheck (Arbitrary (..), Gen, generate)
import Cardano.Ledger.Conway.Genesis (ConwayGenesis(..))
import Cardano.Ledger.Crypto (Crypto)
import Test.Cardano.Ledger.Shelley.ConcreteCryptoTypes (C_Crypto)
import Data.Aeson (ToJSON(..), fromEncoding)
import Cardano.Binary (ToCBOR(..))
import Data.ByteString.Builder (toLazyByteString)

-- Currently Conway does not define any types that require serialization,
-- therefore we simply re-export ones from Babbage. This could change in the
-- future, thus is this placeholder module.
deriving instance Crypto c => Arbitrary (ConwayGenesis c)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Conway.Serialisation.Roundtrip (allprops) where

import Cardano.Ledger.Conway.Genesis (ConwayGenesis (..))
import Cardano.Ledger.Core (Era (..))
import Cardano.Ledger.Crypto (Crypto)
import Data.Data (Proxy (..), Typeable, typeRep)
import Test.Cardano.Ledger.Conway.Serialisation.Generators ()
import Test.QuickCheck (Property)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Roundtrip (property)

allprops ::
forall e.
( Era e
) =>
TestTree
allprops =
testGroup
(show $ typeRep (Proxy @e))
[ testProperty "ConwayGenesis" $ property @(ConwayGenesis (EraCrypto e))
]
21 changes: 21 additions & 0 deletions eras/conway/test-suite/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
module Main where

import Test.Tasty (TestTree, askOption, testGroup)
import Test.TestScenario (mainWithTestScenario, TestScenario (..))
import qualified Test.Cardano.Ledger.Conway.Serialisation.Roundtrip as Roundtrip
import Cardano.Ledger.Conway (Conway)

tests :: TestTree
tests = askOption @TestScenario $ \case
_ -> mainTests

mainTests :: TestTree
mainTests = testGroup
"Conway tests"
[ Roundtrip.allprops @Conway
]

main :: IO ()
main = mainWithTestScenario tests
6 changes: 6 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ cradle:
- path: "eras/conway/impl/src"
component: "lib:cardano-ledger-conway"

- path: "eras/conway/test-suite/src"
component: "lib:cardano-ledger-conway-test"

- path: "eras/conway/test-suite/test"
component: "cardano-ledger-conway-test:test:cardano-ledger-conway-test"

- path: "eras/babbage/test-suite/src"
component: "cardano-ledger-babbage-test"

Expand Down
4 changes: 4 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Keys.hs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ deriving instance
(Crypto crypto) =>
ToCBOR (GenDelegs crypto)

deriving instance
Crypto crypto =>
ToJSON (GenDelegs crypto)

newtype GKeys crypto = GKeys {unGKeys :: Set (VKey 'Genesis crypto)}
deriving (Eq, NoThunks, Generic)
deriving (Show) via Quiet (GKeys crypto)
Expand Down

0 comments on commit 2cc9bff

Please sign in to comment.