Skip to content

Commit b90efd6

Browse files
committed
Run CDDL tests in cardano-test
1 parent 2e9702f commit b90efd6

File tree

10 files changed

+242
-14
lines changed

10 files changed

+242
-14
lines changed

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ extra-doc-files:
2323
CHANGELOG.md
2424
README.md
2525

26+
data-files:
27+
cddl/**/*.cddl
28+
2629
source-repository head
2730
type: git
2831
location: https://github.com/IntersectMBO/ouroboros-consensus
@@ -316,7 +319,7 @@ library unstable-shelley-testlib
316319
cardano-ledger-shelley:{cardano-ledger-shelley, testlib},
317320
cardano-ledger-shelley-ma-test,
318321
cardano-ledger-shelley-test,
319-
cardano-protocol-tpraos:{cardano-protocol-tpraos, testlib},
322+
cardano-protocol-tpraos:{cardano-protocol-tpraos},
320323
cardano-slotting,
321324
cardano-strict-containers,
322325
containers,
@@ -436,6 +439,7 @@ test-suite cardano-test
436439
other-modules:
437440
Test.Consensus.Cardano.DiffusionPipelining
438441
Test.Consensus.Cardano.Golden
442+
Test.Consensus.Cardano.GenCDDLs
439443
Test.Consensus.Cardano.MiniProtocol.LocalTxSubmission.ByteStringTxParser
440444
Test.Consensus.Cardano.MiniProtocol.LocalTxSubmission.Server
441445
Test.Consensus.Cardano.Serialisation
@@ -448,7 +452,9 @@ test-suite cardano-test
448452
Test.ThreadNet.MaryAlonzo
449453
Test.ThreadNet.ShelleyAllegra
450454

455+
other-modules: Paths_ouroboros_consensus_cardano
451456
build-depends:
457+
temporary,
452458
QuickCheck,
453459
base,
454460
base16-bytestring,
@@ -488,6 +494,19 @@ test-suite cardano-test
488494
unstable-byron-testlib,
489495
unstable-cardano-testlib,
490496
unstable-shelley-testlib,
497+
bytestring,
498+
cardano-ledger-allegra:testlib,
499+
cardano-ledger-alonzo:testlib,
500+
cardano-ledger-babbage:testlib,
501+
cardano-ledger-byron,
502+
cardano-ledger-conway:testlib,
503+
cardano-ledger-mary:testlib,
504+
cardano-ledger-shelley:testlib,
505+
directory,
506+
filepath,
507+
process-extras,
508+
tasty-hunit,
509+
tasty,
491510

492511
library unstable-cardano-tools
493512
import: common-lib

ouroboros-consensus-cardano/test/byron-test/Test/Consensus/Byron/Golden.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Test.Util.Paths
1414
import Test.Util.Serialisation.Golden
1515

1616
tests :: TestTree
17-
tests = goldenTest_all codecConfig ($(getGoldenDir) </> "byron") examples
17+
tests = goldenTest_all codecConfig ($(getGoldenDir) </> "byron") Nothing examples
1818

1919
instance ToGoldenDirectory ByronNodeToNodeVersion
2020

ouroboros-consensus-cardano/test/byron-test/Test/Consensus/Byron/Serialisation.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tests :: TestTree
3737
tests =
3838
testGroup
3939
"Byron"
40-
[ roundtrip_all testCodecCfg dictNestedHdr
40+
[ roundtrip_all testCodecCfg dictNestedHdr Nothing
4141
, testProperty "BinaryBlockInfo sanity check" prop_byronBinaryBlockInfo
4242
, testGroup
4343
"Integrity"

ouroboros-consensus-cardano/test/cardano-test/Main.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Main (main) where
22

33
import System.IO (BufferMode (LineBuffering), hSetBuffering, stdout)
44
import qualified Test.Consensus.Cardano.DiffusionPipelining
5+
import Test.Consensus.Cardano.GenCDDLs
56
import qualified Test.Consensus.Cardano.Golden
67
import qualified Test.Consensus.Cardano.MiniProtocol.LocalTxSubmission.Server
78
import qualified Test.Consensus.Cardano.Serialisation (tests)
@@ -29,8 +30,12 @@ tests =
2930
testGroup
3031
"cardano"
3132
[ Test.Consensus.Cardano.DiffusionPipelining.tests
32-
, Test.Consensus.Cardano.Golden.tests
33-
, Test.Consensus.Cardano.Serialisation.tests
33+
, withCDDLs $
34+
testGroup
35+
"Serialisation"
36+
[ Test.Consensus.Cardano.Golden.tests
37+
, Test.Consensus.Cardano.Serialisation.tests
38+
]
3439
, Test.Consensus.Cardano.SupportedNetworkProtocolVersion.tests
3540
, Test.Consensus.Cardano.SupportsSanityCheck.tests
3641
, Test.ThreadNet.AllegraMary.tests
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{-# LANGUAGE ViewPatterns #-}
2+
{-# LANGUAGE BangPatterns #-}
3+
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE CPP #-}
5+
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
6+
-- |
7+
8+
module Test.Consensus.Cardano.GenCDDLs (withCDDLs) where
9+
10+
import qualified Control.Monad as Monad
11+
import qualified Data.ByteString as BS
12+
import qualified Data.ByteString.Lazy as BSL
13+
import qualified Data.ByteString.Char8 as BS8
14+
import Data.Maybe (isNothing)
15+
import qualified Data.List as L
16+
import Paths_ouroboros_consensus_cardano
17+
import qualified System.Directory as D
18+
import qualified System.Environment as E
19+
import System.Exit
20+
import qualified System.FilePath as F
21+
import qualified System.Process.ByteString.Lazy as P
22+
23+
-- TODO: this is waiting to update to a newer ledger
24+
--import qualified Test.Cardano.Chain.Binary.Cddl as Byron
25+
import qualified Test.Cardano.Ledger.Allegra.Binary.Cddl as Allegra
26+
import qualified Test.Cardano.Ledger.Alonzo.Binary.Cddl as Alonzo
27+
import qualified Test.Cardano.Ledger.Babbage.Binary.Cddl as Babbage
28+
import qualified Test.Cardano.Ledger.Conway.Binary.Cddl as Conway
29+
import qualified Test.Cardano.Ledger.Mary.Binary.Cddl as Mary
30+
import qualified Test.Cardano.Ledger.Shelley.Binary.Cddl as Shelley
31+
import System.IO
32+
import Test.Tasty
33+
import System.IO.Temp
34+
35+
newtype CDDLSpec = CDDLSpec { cddlSpec :: BS.ByteString } deriving Show
36+
37+
withCDDLs :: TestTree -> TestTree
38+
withCDDLs f = withResource
39+
(do
40+
probeTools
41+
setupCDDLCEnv
42+
BS.writeFile "ntnblock.cddl" . cddlSpec
43+
=<< (cddlc "cddl/node-to-node/blockfetch/block.cddl" >>= fixupBlockCDDL)
44+
BS.writeFile "ntnheader.cddl" . cddlSpec
45+
=<< cddlc "cddl/node-to-node/chainsync/header.cddl"
46+
)
47+
(\() -> do
48+
D.removeFile "ntnblock.cddl"
49+
D.removeFile "ntnheader.cddl"
50+
)
51+
(\_ -> f)
52+
53+
54+
fixupBlockCDDL :: CDDLSpec -> IO CDDLSpec
55+
fixupBlockCDDL spec =
56+
withTempFile "." "block-temp.cddl" $ \fp h -> do
57+
hClose h
58+
BS.writeFile fp . cddlSpec $ spec
59+
-- This is wrong, both the metadata_hash of a pool and a transaction body
60+
-- point to this type, but only the latter must be 32B.
61+
sed fp ["-i", "s/\\(metadata_hash = \\)/\\1 bytes ;/g"]
62+
-- For plutus, the type is actually `bytes`, but the distinct construct is
63+
-- for forcing generation of different values.
64+
sed fp ["-i", "s/\\(conway\\.distinct_VBytes = \\)/\\1 bytes ;\\//g"]
65+
-- These 3 below are hardcoded for generation. See cardano-ledger#5054
66+
sed fp ["-i", "s/\\([yaoye]\\.address = \\)/\\1 bytes ;/g"]
67+
sed fp ["-i", "s/\\(reward_account = \\)/\\1 bytes ;/g"]
68+
sed fp ["-i", "-z", "s/unit_interval = #6\\.30(\\[\\n\\s*1,\\n\\s*2,\\n\\])/unit_interval = #6.30([uint, uint])/g"]
69+
CDDLSpec <$> BS.readFile fp
70+
71+
setupCDDLCEnv :: IO ()
72+
setupCDDLCEnv = do
73+
-- This is not how it should be because we can't update the Ledger
74+
-- to a newer one. On `master` there is a function
75+
-- `Byron.readByronCddlFileNames` which we would want to use.
76+
--
77+
-- Note also that cabal run will run in the root of the project and
78+
-- cabal test will run in `ouroboros-consensus-cardano`. This path
79+
-- is for the latter.
80+
byron <- pure ["../../cardano-ledger/eras/byron/cddl-spec/"]
81+
shelley <- map takePath <$> Shelley.readShelleyCddlFileNames
82+
allegra <- map takePath <$> Allegra.readAllegraCddlFileNames
83+
mary <- map takePath <$> Mary.readMaryCddlFileNames
84+
alonzo <- map takePath <$> Alonzo.readAlonzoCddlFileNames
85+
babbage <- map takePath <$> Babbage.readBabbageCddlFileNames
86+
conway <- map takePath <$> Conway.readConwayCddlFileNames
87+
88+
localDataDir <- takePath <$> getDataDir
89+
let local_paths = map (localDataDir F.</>) [
90+
"cddl"
91+
, "cddl/disk"
92+
, "cddl/disk/snapshot"
93+
, "cddl/node-to-client/localstatequery/byron"
94+
, "cddl/node-to-client/localstatequery/consensus"
95+
, "cddl/node-to-client/localstatequery/shelley"
96+
, "cddl/node-to-client/txmonitor"
97+
]
98+
99+
include_path =
100+
mconcat
101+
$ L.intersperse ":"
102+
$ map (mconcat . L.intersperse ":") [byron, shelley, allegra, mary, alonzo, babbage, conway] <> local_paths
103+
104+
writeFile "env" ("CDDL_INCLUDE_PATH=" <> include_path <> ":")
105+
E.setEnv "CDDL_INCLUDE_PATH" (include_path <> ":")
106+
107+
sed :: FilePath -> [String] -> IO ()
108+
sed fp args =
109+
Monad.void $ P.readProcessWithExitCode "sed" (args ++ [fp]) mempty
110+
111+
cddlc :: FilePath -> IO CDDLSpec
112+
cddlc dataFile = do
113+
putStrLn $ "Generating: " <> dataFile
114+
path <- getDataFileName dataFile
115+
(_, BSL.toStrict -> cddl, BSL.toStrict -> err) <-
116+
#ifdef POSIX
117+
P.readProcessWithExitCode "cddlc" ["-u", "-2", "-t", "cddl", path] mempty
118+
#else
119+
-- we cannot call @cddlc@ directly because it is not an executable in
120+
-- Haskell eyes, but we can call @ruby@ and pass the @cddlc@ script path as
121+
-- an argument
122+
do
123+
prefix <- E.getEnv "MSYSTEM_PREFIX"
124+
P.readProcessWithExitCode "ruby" [prefix F.</> "bin/cddlc", "-u", "-2", "-t", "cddl", path] mempty
125+
#endif
126+
Monad.unless (BS.null err) $ red $ BS8.unpack err
127+
return $ CDDLSpec cddl
128+
where
129+
red s = putStrLn $ "\ESC[31m" <> s <> "\ESC[0m"
130+
131+
takePath :: FilePath -> FilePath
132+
takePath x =
133+
#ifdef POSIX
134+
F.takeDirectory x
135+
#else
136+
-- @cddlc@ is not capable of using backlashes
137+
--
138+
-- @cddlc@ mixes @C:@ with the separator in @CDDL_INCLUDE_PATH@, and it
139+
-- doesn't understand @;@ as a separator. It works if we remove @C:@ and we
140+
-- are running in the same drive as the cddl files.
141+
let f = [ if c /= '\\' then c else '/' | c <- F.takeDirectory x ]
142+
in if "C:" `L.isPrefixOf` f
143+
then drop 2 f
144+
else f
145+
#endif
146+
147+
probeTools :: IO ()
148+
probeTools = do
149+
putStrLn "Probing tools:"
150+
#ifdef POSIX
151+
posixProbeTool "cddlc" "install the `cddlc` ruby gem"
152+
where
153+
posixProbeTool :: String -> Sring -> IO ()
154+
posixProbeTool tool suggestion = do
155+
putStr $ "- " <> tool <> " "
156+
exe <- D.findExecutable tool
157+
if isNothing exe
158+
then do
159+
putStrLn "not found!"
160+
putStrLn $ "Please " <> suggestion
161+
exitFailure
162+
else
163+
putStrLn "found"
164+
#else
165+
-- On Windows, the cddl and cddlc files are POSIX scripts and therefore not
166+
-- recognized as executables by @findExecutable@, so we need to do some dirty
167+
-- tricks here. We check that ruby executable exists and then that there are
168+
-- cddl and cddlc files in the binary folder of the MSYS2 installation.
169+
putStr "- ruby "
170+
rubyExe <- D.findExecutable "ruby"
171+
if (isNothing rubyExe)
172+
then do
173+
putStrLn "not found!\nPlease install ruby"
174+
exitFailure
175+
else
176+
putStrLn "found"
177+
178+
putStr "- cddlc "
179+
cddlcExe <- D.doesFileExist . (F.</> "bin/cddlc") =<< E.getEnv "MSYSTEM_PREFIX"
180+
if cddlcExe
181+
then putStrLn "found"
182+
else do
183+
putStrLn "not found!\nPlease install the `cddlc` ruby gem"
184+
exitFailure
185+
pure ()
186+
#endif

ouroboros-consensus-cardano/test/cardano-test/Test/Consensus/Cardano/Golden.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE FlexibleContexts #-}
22
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE TemplateHaskell #-}
45
{-# LANGUAGE TypeFamilies #-}
56
{-# OPTIONS_GHC -Wno-orphans #-}
@@ -16,10 +17,16 @@ import System.FilePath ((</>))
1617
import Test.Consensus.Cardano.Examples
1718
import Test.Tasty
1819
import Test.Util.Paths
20+
import Test.Util.Serialisation.CDDL
1921
import Test.Util.Serialisation.Golden
2022

2123
tests :: TestTree
22-
tests = goldenTest_all codecConfig ($(getGoldenDir) </> "cardano") examples
24+
tests =
25+
goldenTest_all
26+
codecConfig
27+
($(getGoldenDir) </> "cardano")
28+
(Just $ CDDLsForNodeToNode ("ntnblock.cddl", "serialisedCardanoBlock") ("ntnheader.cddl", "header"))
29+
examples
2330

2431
instance
2532
CardanoHardForkConstraints c =>

ouroboros-consensus-cardano/test/cardano-test/Test/Consensus/Cardano/Serialisation.hs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE GADTs #-}
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE NamedFieldPuns #-}
4+
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
56

67
module Test.Consensus.Cardano.Serialisation (tests) where
@@ -21,7 +22,6 @@ import Ouroboros.Network.Block (Serialised (..))
2122
import Test.Consensus.Byron.Generators (epochSlots)
2223
import qualified Test.Consensus.Cardano.Examples as Cardano.Examples
2324
import Test.Consensus.Cardano.Generators ()
24-
import Test.Consensus.Cardano.MockCrypto (MockCryptoCompatByron)
2525
import Test.Tasty
2626
import Test.Tasty.QuickCheck (Property, testProperty, (===))
2727
import Test.Util.Orphans.Arbitrary ()
@@ -33,15 +33,26 @@ tests =
3333
"Cardano"
3434
[ testGroup "Examples roundtrip" $
3535
examplesRoundtrip Cardano.Examples.codecConfig Cardano.Examples.examples
36-
, roundtrip_all_skipping result testCodecCfg dictNestedHdr
36+
, roundtrip_all_skipping
37+
result
38+
testCodecCfg
39+
dictNestedHdr
40+
-- We would want to use this instead, but the generated blocks
41+
-- do not quite validate yet or sometimes they are not
42+
-- entirely coherent, so for now this is commented out.
43+
--
44+
-- ( Just $
45+
-- CDDLsForNodeToNode ("ntnmockblock.cddl", "serialisedCardanoBlock") ("ntnheader.cddl", "header")
46+
-- )
47+
Nothing
3748
, testProperty "BinaryBlockInfo sanity check" prop_CardanoBinaryBlockInfo
3849
]
3950
where
4051
-- See https://github.com/IntersectMBO/cardano-ledger/issues/3800
4152
result "roundtrip Result" = DoNotCheckCBORValidity
4253
result _ = CheckCBORValidity
4354

44-
testCodecCfg :: CardanoCodecConfig MockCryptoCompatByron
55+
testCodecCfg :: CardanoCodecConfig StandardCrypto
4556
testCodecCfg =
4657
CardanoCodecConfig
4758
(ByronCodecConfig epochSlots)
@@ -54,7 +65,7 @@ testCodecCfg =
5465

5566
dictNestedHdr ::
5667
forall a.
57-
NestedCtxt_ (CardanoBlock MockCryptoCompatByron) Header a ->
68+
NestedCtxt_ (CardanoBlock StandardCrypto) Header a ->
5869
Dict (Eq a, Show a)
5970
dictNestedHdr = \case
6071
NCZ (CtxtByronBoundary{}) -> Dict
@@ -70,7 +81,7 @@ dictNestedHdr = \case
7081
BinaryBlockInfo
7182
-------------------------------------------------------------------------------}
7283

73-
prop_CardanoBinaryBlockInfo :: CardanoBlock MockCryptoCompatByron -> Property
84+
prop_CardanoBinaryBlockInfo :: CardanoBlock StandardCrypto -> Property
7485
prop_CardanoBinaryBlockInfo blk =
7586
encodedNestedHeader === extractedHeader
7687
where

ouroboros-consensus-cardano/test/shelley-test/Test/Consensus/Shelley/Golden.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Test.Util.Paths
1717
import Test.Util.Serialisation.Golden
1818

1919
tests :: TestTree
20-
tests = goldenTest_all codecConfig ($(getGoldenDir) </> "shelley") examplesShelley
20+
tests = goldenTest_all codecConfig ($(getGoldenDir) </> "shelley") Nothing examplesShelley
2121

2222
instance ToGoldenDirectory ShelleyNodeToNodeVersion
2323

ouroboros-consensus-cardano/test/shelley-test/Test/Consensus/Shelley/Serialisation.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tests :: TestTree
3131
tests =
3232
testGroup
3333
"Shelley"
34-
[ roundtrip_all testCodecCfg dictNestedHdr
34+
[ roundtrip_all testCodecCfg dictNestedHdr Nothing
3535
, -- Test for real crypto too
3636
testProperty "hashSize real crypto" $ prop_hashSize pReal
3737
, testProperty "ConvertRawHash real crypto" $ roundtrip_ConvertRawHash pReal

ouroboros-consensus-diffusion/test/mock-test/Test/ThreadNet/BFT.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ instance Arbitrary TestSetup where
5555
tests :: TestTree
5656
tests =
5757
testGroup "BFT" $
58-
[ roundtrip_all SimpleCodecConfig dictNestedHdr
58+
[ roundtrip_all SimpleCodecConfig dictNestedHdr Nothing
5959
, testProperty "simple convergence" $ \setup ->
6060
prop_simple_bft_convergence setup
6161
]

0 commit comments

Comments
 (0)