diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index a9c8836003b..8906f9190a2 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -257,7 +257,6 @@ test-suite cardano-node-test , cardano-ledger-core , cardano-node , cardano-slotting - , 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)