From a19ed281dca4ff812bf069a48934eceb04d9010c Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 27 Sep 2023 15:36:52 +0200 Subject: [PATCH] Move Ledger events CDDL conformance tests inside 'cddl' package. This is hopefully temporary until I figure out why GHC can't compile cardano-node tests when relying on the 'cddl' package. At the moment, it panics with: ``` : dlopen([...]/dist-newstyle/build/aarch64-osx/ghc-8.10.7/cddl-1.0.0/build/libHScddl-1.0.0-inplace-ghc8.10.7.dylib, 0x0005): symbol not found in flat namespace '_CFRelease' ``` It could be similar to the issue describe here: https://github.com/yvan-sraka/cargo-cabal/pull/2; and a possible work-around could be to build a dynamic library from Rust instead of static one. --- cardano-node/cardano-node.cabal | 1 - .../test/Test/Cardano/Node/LedgerEvent.hs | 23 +++-------- cddl/cddl.cabal | 4 ++ cddl/test/Cardano/Node/LedgerEventSpec.hs | 38 +++++++++++++++++++ 4 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 cddl/test/Cardano/Node/LedgerEventSpec.hs diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index 4945c04de83..385ce090494 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -253,7 +253,6 @@ test-suite cardano-node-test , cardano-ledger-core , cardano-node , cardano-slotting >= 0.1 - , cddl , directory , filepath , hedgehog diff --git a/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs b/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs index b59e35d85fd..42b3897d9fe 100644 --- a/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs +++ b/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs @@ -3,36 +3,23 @@ module Test.Cardano.Node.LedgerEvent where import Cardano.Node.LedgerEvent -import qualified Codec.CBOR.Schema as CDDL import Data.ByteString.Short(toShort) import Data.ByteString.Lazy(fromStrict) import qualified Data.ByteString.Base16 as Hex -import qualified Data.Text.IO as TIO -import Hedgehog (Property, discover, footnote, (===)) +import Hedgehog (Property, discover, footnote) import qualified Hedgehog import qualified Hedgehog.Gen as Gen import qualified Hedgehog.Range as Range -import System.IO.Unsafe(unsafePerformIO) - -specification :: Text -specification = - unsafePerformIO $ TIO.readFile "ledger_event.cddl" prop_roundtrip_LedgerEvent_CBOR :: Property prop_roundtrip_LedgerEvent_CBOR = Hedgehog.property $ do version <- Hedgehog.forAll Gen.enumBounded event <- Hedgehog.forAll genEvent - footnote ("serialized event: " <> show (Hex.encode $ serializeEvent version event)) - Hedgehog.tripping event (serializeEvent version) (deserializeEvent . fromStrict) - -prop_cddl_validation_LedgerEvent :: Property -prop_cddl_validation_LedgerEvent = - Hedgehog.property $ do - version <- Hedgehog.forAll Gen.enumBounded - event <- Hedgehog.forAll genEvent - let bytes = serializeEvent version event - CDDL.validate specification bytes === Right () + footnote ("serialized event: " <> show (Hex.encode $ serializeAnchoredEvent version event)) + Hedgehog.tripping event + (serializeAnchoredEvent version) + (fmap snd . deserializeAnchoredEvent . fromStrict) genEvent :: Hedgehog.Gen AnchoredEvent genEvent = diff --git a/cddl/cddl.cabal b/cddl/cddl.cabal index 0e3a05ee3b1..71439defe96 100644 --- a/cddl/cddl.cabal +++ b/cddl/cddl.cabal @@ -52,7 +52,11 @@ test-suite unit Paths_cddl build-depends: , base , base16-bytestring + , bytestring , cddl + , hedgehog , hspec + , hspec-hedgehog , text + , cardano-node build-tool-depends: hspec-discover:hspec-discover diff --git a/cddl/test/Cardano/Node/LedgerEventSpec.hs b/cddl/test/Cardano/Node/LedgerEventSpec.hs new file mode 100644 index 00000000000..8cefd302c3a --- /dev/null +++ b/cddl/test/Cardano/Node/LedgerEventSpec.hs @@ -0,0 +1,38 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Cardano.Node.LedgerEventSpec where + +import Prelude + +import Cardano.Node.LedgerEvent +import qualified Codec.CBOR.Schema as CDDL +import qualified Data.ByteString.Base16 as Hex +import Data.ByteString.Lazy (fromStrict) +import Data.ByteString.Short (toShort) +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Hedgehog (Property, discover, footnote, (===)) +import qualified Hedgehog +import qualified Hedgehog.Gen as Gen +import qualified Hedgehog.Range as Range +import System.IO.Unsafe (unsafePerformIO) +import Test.Hspec (Spec, describe, specify) +import Test.Hspec.Hedgehog (hedgehog) + +specification :: Text +specification = + unsafePerformIO $ TIO.readFile "../cardano-node/ledger_events.cddl" + +spec :: Spec +spec = describe "Ledger Event CDDL" $ do + specify "All events are compliant with their cddl definitions" $ hedgehog $ do + version <- Hedgehog.forAll Gen.enumBounded + event <- Hedgehog.forAll genEvent + CDDL.validate specification (serializeAnchoredEvent version event) === Right () + +genEvent :: Hedgehog.Gen AnchoredEvent +genEvent = + AnchoredEvent + <$> (toShort <$> Gen.bytes (Range.constant 32 32)) + <*> (fromIntegral <$> Gen.word64 Range.constantBounded) + <*> (LedgerNewEpochEvent . LedgerStartAtEpoch . fromIntegral <$> Gen.word16 Range.constantBounded)