Skip to content

Commit a1056ec

Browse files
committed
Run CDDL tests in cardano-test
1 parent f162f1c commit a1056ec

File tree

10 files changed

+245
-14
lines changed

10 files changed

+245
-14
lines changed

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

Lines changed: 14 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,
@@ -435,6 +438,7 @@ test-suite cardano-test
435438
main-is: Main.hs
436439
other-modules:
437440
Test.Consensus.Cardano.DiffusionPipelining
441+
Test.Consensus.Cardano.GenCDDLs
438442
Test.Consensus.Cardano.Golden
439443
Test.Consensus.Cardano.MiniProtocol.LocalTxSubmission.ByteStringTxParser
440444
Test.Consensus.Cardano.MiniProtocol.LocalTxSubmission.Server
@@ -448,27 +452,34 @@ test-suite cardano-test
448452
Test.ThreadNet.MaryAlonzo
449453
Test.ThreadNet.ShelleyAllegra
450454

455+
other-modules: Paths_ouroboros_consensus_cardano
451456
build-depends:
452457
QuickCheck,
453458
base,
454459
base16-bytestring,
455460
bytestring,
461+
cardano-ledger-allegra:testlib,
456462
cardano-ledger-alonzo,
463+
cardano-ledger-alonzo:testlib,
457464
cardano-ledger-alonzo-test,
458465
cardano-ledger-api,
466+
cardano-ledger-babbage:testlib,
459467
cardano-ledger-babbage-test,
460468
cardano-ledger-binary:testlib,
461469
cardano-ledger-byron,
462470
cardano-ledger-conway:testlib,
463471
cardano-ledger-core:{cardano-ledger-core, testlib},
472+
cardano-ledger-mary:testlib,
464473
cardano-ledger-shelley,
474+
cardano-ledger-shelley:testlib,
465475
cardano-ledger-shelley-test,
466476
cardano-protocol-tpraos,
467477
cardano-slotting,
468478
cborg,
469479
constraints,
470480
containers,
471481
contra-tracer,
482+
directory,
472483
filepath,
473484
microlens,
474485
ouroboros-consensus:{ouroboros-consensus, unstable-consensus-testlib, unstable-mempool-test-utils},
@@ -478,12 +489,14 @@ test-suite cardano-test
478489
ouroboros-network-api,
479490
ouroboros-network-protocols:{ouroboros-network-protocols, testlib},
480491
pretty-simple,
492+
process-extras,
481493
sop-core,
482494
sop-extras,
483495
strict-sop-core,
484496
tasty,
485497
tasty-hunit,
486498
tasty-quickcheck,
499+
temporary,
487500
typed-protocols ^>=0.3,
488501
unstable-byron-testlib,
489502
unstable-cardano-testlib,

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

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 =>

0 commit comments

Comments
 (0)