Skip to content

Commit

Permalink
Define new property for validating CDDL conformance of ledger events.
Browse files Browse the repository at this point in the history
  Also include a first (near empty) draft of a CDDL specification.
  • Loading branch information
KtorZ committed Sep 26, 2023
1 parent 14e7108 commit 0a134b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions cardano-node/cardano-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ test-suite cardano-node-test
, cardano-ledger-core
, cardano-node
, cardano-slotting >= 0.1
, cddl
, directory
, filepath
, hedgehog
Expand Down
16 changes: 16 additions & 0 deletions cardano-node/ledger_events.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rule = ledger_event

ledger_event =
[ (0, new_epoch)
// (1, body_event)
// (2, tick)
]

new_epoch =
#

body_event =
#

tick_event =
#
17 changes: 16 additions & 1 deletion cardano-node/test/Test/Cardano/Node/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
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 Hedgehog (Property, discover, footnote)
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)

specification :: Text
specification =

Check warning on line 18 in cardano-node/test/Test/Cardano/Node/LedgerEvent.hs

View workflow job for this annotation

GitHub Actions / build

Warning in specification in module Test.Cardano.Node.LedgerEvent: Missing NOINLINE pragma ▫︎ Found: "specification = unsafePerformIO $ TIO.readFile \"ledger_event.cddl\"" ▫︎ Perhaps: "{-# NOINLINE specification #-}\nspecification = unsafePerformIO $ TIO.readFile \"ledger_event.cddl\""
unsafePerformIO $ TIO.readFile "ledger_event.cddl"

prop_roundtrip_LedgerEvent_CBOR :: Property
prop_roundtrip_LedgerEvent_CBOR =
Expand All @@ -19,6 +26,14 @@ prop_roundtrip_LedgerEvent_CBOR =
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 ()

genEvent :: Hedgehog.Gen AnchoredEvent
genEvent =
AnchoredEvent
Expand Down

0 comments on commit 0a134b2

Please sign in to comment.