From 0a134b2487f055501252b03aa3af818f795391d2 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Tue, 26 Sep 2023 15:49:08 +0200 Subject: [PATCH] Define new property for validating CDDL conformance of ledger events. Also include a first (near empty) draft of a CDDL specification. --- cardano-node/cardano-node.cabal | 1 + cardano-node/ledger_events.cddl | 16 ++++++++++++++++ .../test/Test/Cardano/Node/LedgerEvent.hs | 17 ++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 cardano-node/ledger_events.cddl diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index 385ce090494..4945c04de83 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -253,6 +253,7 @@ test-suite cardano-node-test , cardano-ledger-core , cardano-node , cardano-slotting >= 0.1 + , cddl , directory , filepath , hedgehog diff --git a/cardano-node/ledger_events.cddl b/cardano-node/ledger_events.cddl new file mode 100644 index 00000000000..ae4eecedb42 --- /dev/null +++ b/cardano-node/ledger_events.cddl @@ -0,0 +1,16 @@ +rule = ledger_event + +ledger_event = + [ (0, new_epoch) + // (1, body_event) + // (2, tick) + ] + +new_epoch = + # + +body_event = + # + +tick_event = + # diff --git a/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs b/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs index 80d12ceb189..b59e35d85fd 100644 --- a/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs +++ b/cardano-node/test/Test/Cardano/Node/LedgerEvent.hs @@ -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 = + unsafePerformIO $ TIO.readFile "ledger_event.cddl" prop_roundtrip_LedgerEvent_CBOR :: Property prop_roundtrip_LedgerEvent_CBOR = @@ -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