Skip to content

Commit

Permalink
Move Ledger events CDDL conformance tests inside 'cddl' package.
Browse files Browse the repository at this point in the history
  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:

  ```
  <command line>: 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:
  yvan-sraka/cargo-cabal#2; and a possible
  work-around could be to build a dynamic library from Rust instead of
  static one.
  • Loading branch information
KtorZ authored and neilmayhew committed Oct 27, 2023
1 parent 0dd222a commit e2be704
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
1 change: 0 additions & 1 deletion cardano-node/cardano-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ test-suite cardano-node-test
, cardano-ledger-core
, cardano-node
, cardano-slotting
, cddl
, directory
, filepath
, hedgehog
Expand Down
23 changes: 5 additions & 18 deletions cardano-node/test/Test/Cardano/Node/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 4 additions & 0 deletions cddl/cddl.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions cddl/test/Cardano/Node/LedgerEventSpec.hs
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e2be704

Please sign in to comment.