Skip to content

Commit 8fe0c61

Browse files
committed
Make use of cuddle for validation
1 parent 30f9ec0 commit 8fe0c61

File tree

3 files changed

+119
-52
lines changed

3 files changed

+119
-52
lines changed

cabal.project

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ index-state:
1616
-- Bump this if you need newer packages from Hackage
1717
, hackage.haskell.org 2025-05-15T07:59:00Z
1818
-- Bump this if you need newer packages from CHaP
19-
, cardano-haskell-packages 2025-05-15T08:36:14Z
19+
, cardano-haskell-packages 2025-05-15T10:30:42Z
2020

2121
packages:
2222
ouroboros-consensus
@@ -68,7 +68,7 @@ if impl (ghc >= 9.12)
6868
source-repository-package
6969
type: git
7070
location: https://github.com/IntersectMBO/cardano-ledger
71-
tag: d48965e0dc1a324f432f1ee01bc0cf2d60a8a702
71+
tag: 3028f02583b22cd1d1039a42acfad94626bfa39c
7272
--sha256: sha256-UFIQ1qinge1TMtqU2e0Nyjdpj42yj+lLKrpnXeqo1mI=
7373
subdir:
7474
eras/allegra/impl
@@ -96,6 +96,11 @@ source-repository-package
9696
eras/byron/ledger/impl
9797
eras/byron/crypto
9898

99+
source-repository-package
100+
type: git
101+
location: https://github.com/input-output-hk/cuddle
102+
tag: 847e9c4fbacc261a2fb154d8d28e50227eaa9085
103+
99104
allow-newer:
100105
ouroboros-consensus:cardano-ledger-core,
101106
ouroboros-consensus-cardano:cardano-ledger-byron,

ouroboros-consensus-cardano/cddl/pull-cddls.hs

Lines changed: 101 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
{-# LANGUAGE ViewPatterns #-}
2+
{-# LANGUAGE BangPatterns #-}
13
{-# LANGUAGE RecordWildCards #-}
24
{-# LANGUAGE CPP #-}
35
-- |
46

57
module Main (main) where
68

79
import qualified Control.Monad as Monad
8-
import qualified Data.ByteString.Lazy as BL
9-
import qualified Data.ByteString.Lazy.UTF8 as BL8
10+
import qualified Data.ByteString as BS
11+
import qualified Data.ByteString.Lazy as BSL
12+
import qualified Data.ByteString.Char8 as BS8
1013
import Data.Maybe (isNothing)
1114
import qualified Data.List as L
1215
import Paths_ouroboros_consensus_cardano
1316
import qualified System.Directory as D
1417
import qualified System.Environment as E
15-
import System.Exit (exitFailure)
18+
import System.Exit
1619
import qualified System.FilePath as F
1720
import qualified System.Process.ByteString.Lazy as P
1821
import qualified Test.Cardano.Chain.Binary.Cddl as Byron
@@ -22,16 +25,43 @@ import qualified Test.Cardano.Ledger.Babbage.Binary.Cddl as Babbage
2225
import qualified Test.Cardano.Ledger.Conway.Binary.Cddl as Conway
2326
import qualified Test.Cardano.Ledger.Mary.Binary.Cddl as Mary
2427
import qualified Test.Cardano.Ledger.Shelley.Binary.Cddl as Shelley
28+
--import System.IO
29+
import Test.Tasty
30+
import Test.Tasty.HUnit
2531

2632
main :: IO ()
2733
main = do
2834
probeTools
29-
setupEnv
30-
-- For now I just print this.
31-
print =<< getCDDLs
35+
setupCDDLCEnv
3236

33-
setupEnv :: IO ()
34-
setupEnv = do
37+
-- Only test the golden blocks
38+
goldenBlocks <-
39+
(map ("ouroboros-consensus-cardano/golden/cardano/disk" F.</>))
40+
. filter (L.isPrefixOf "Block_")
41+
<$> D.listDirectory "ouroboros-consensus-cardano/golden/cardano/disk/"
42+
43+
defaultMain
44+
$ testGroup "Golden"
45+
[ withResource
46+
(cddlc "cddl/disk/block.cddl"
47+
>>= fixupBlockCDDL
48+
>>= BS.writeFile "block.cddl" . cddlSpec
49+
)
50+
(\() -> D.removeFile "block.cddl")
51+
$ \_ -> testGroup "Blocks" $
52+
map (cuddleValidate "block.cddl" "cardanoBlock") goldenBlocks
53+
]
54+
55+
56+
cuddleValidate :: FilePath -> String -> FilePath -> TestTree
57+
cuddleValidate cddl rule cbor = testCase cbor $ do
58+
(e, err, _) <- P.readProcessWithExitCode "cuddle" ["validate-cbor", "-c", cbor, "-r", rule, cddl] mempty
59+
case e of
60+
ExitSuccess -> pure ()
61+
ExitFailure _ -> assertFailure $ BS8.unpack $ BSL.toStrict err
62+
63+
setupCDDLCEnv :: IO ()
64+
setupCDDLCEnv = do
3565
byron <- map takePath <$> Byron.readByronCddlFileNames
3666
shelley <- map takePath <$> Shelley.readShelleyCddlFileNames
3767
allegra <- map takePath <$> Allegra.readAllegraCddlFileNames
@@ -58,7 +88,7 @@ setupEnv = do
5888

5989
E.setEnv "CDDL_INCLUDE_PATH" (include_path <> ":")
6090

61-
newtype CDDLSpec = CDDLSpec BL.ByteString deriving Show
91+
newtype CDDLSpec = CDDLSpec { cddlSpec :: BS.ByteString } deriving Show
6292

6393
data CDDLs = CDDLs {
6494
diskBlockCDDL :: CDDLSpec
@@ -83,42 +113,71 @@ data CDDLs = CDDLs {
83113
, ntcTxMonitorSlotNoCDDL :: CDDLSpec
84114
} deriving Show
85115

86-
getCDDLs :: IO CDDLs
87-
getCDDLs = CDDLs
88-
-- Disk
89-
<$> cddlc "cddl/disk/block.cddl"
90-
<*> cddlc "cddl/disk/snapshot.cddl"
91-
92-
-- Node to node
93-
-- -- BlockFetch
94-
<*> cddlc "cddl/node-to-node/blockfetch/block.cddl"
95-
<*> cddlc "cddl/node-to-node/blockfetch/point.cddl"
96-
97-
-- -- ChainSync
98-
<*> cddlc "cddl/node-to-node/chainsync/header.cddl"
99-
<*> cddlc "cddl/node-to-node/chainsync/point.cddl"
100-
<*> cddlc "cddl/node-to-node/chainsync/tip.cddl"
101-
102-
-- -- TxSubmission2
103-
<*> cddlc "cddl/node-to-node/txsubmission2/ticketno.cddl"
104-
<*> cddlc "cddl/node-to-node/txsubmission2/tx.cddl"
105-
<*> cddlc "cddl/node-to-node/txsubmission2/txid.cddl"
106-
107-
-- Node to client
108-
-- -- LocalStateQuery
109-
<*> cddlc "cddl/node-to-client/localstatequery/query.cddl"
110-
<*> cddlc "cddl/node-to-client/localstatequery/result.cddl"
111-
112-
-- -- TxMonitor
113-
<*> cddlc "cddl/node-to-client/txmonitor/tx.cddl"
114-
<*> cddlc "cddl/node-to-client/txmonitor/txid.cddl"
115-
<*> cddlc "cddl/node-to-client/txmonitor/slotno.cddl"
116+
-- getCDDLs :: IO CDDLs
117+
-- getCDDLs = CDDLs
118+
-- -- Disk
119+
-- <$> cddlc "cddl/disk/block.cddl"
120+
-- <*> cddlc "cddl/disk/snapshot.cddl"
121+
122+
-- -- Node to node
123+
-- -- -- BlockFetch
124+
-- <*> cddlc "cddl/node-to-node/blockfetch/block.cddl"
125+
-- <*> cddlc "cddl/node-to-node/blockfetch/point.cddl"
126+
127+
-- -- -- ChainSync
128+
-- <*> cddlc "cddl/node-to-node/chainsync/header.cddl"
129+
-- <*> cddlc "cddl/node-to-node/chainsync/point.cddl"
130+
-- <*> cddlc "cddl/node-to-node/chainsync/tip.cddl"
131+
132+
-- -- -- TxSubmission2
133+
-- <*> cddlc "cddl/node-to-node/txsubmission2/ticketno.cddl"
134+
-- <*> cddlc "cddl/node-to-node/txsubmission2/tx.cddl"
135+
-- <*> cddlc "cddl/node-to-node/txsubmission2/txid.cddl"
136+
137+
-- -- Node to client
138+
-- -- -- LocalStateQuery
139+
-- <*> cddlc "cddl/node-to-client/localstatequery/query.cddl"
140+
-- <*> cddlc "cddl/node-to-client/localstatequery/result.cddl"
141+
142+
-- -- -- TxMonitor
143+
-- <*> cddlc "cddl/node-to-client/txmonitor/tx.cddl"
144+
-- <*> cddlc "cddl/node-to-client/txmonitor/txid.cddl"
145+
-- <*> cddlc "cddl/node-to-client/txmonitor/slotno.cddl"
146+
147+
fixupBlockCDDL :: CDDLSpec -> IO CDDLSpec
148+
fixupBlockCDDL spec = do
149+
let fp = "block-temp.cddl"
150+
BS.writeFile fp . cddlSpec $ spec
151+
-- This is wrong, both the metadata_hash of a pool and a transaction body
152+
-- point to this type, but only the latter must be 32B.
153+
sed fp ["-i", "s/\\(metadata_hash = \\)/\\1 bytes ;/g"]
154+
-- For plutus, the type is actually `bytes`, but the distinct construct is
155+
-- for forcing generation of different values.
156+
sed fp ["-i", "s/\\(conway\\.distinct_VBytes = \\)/\\1 bytes ;\\//g"]
157+
-- These 3 below are hardcoded for generation. See cardano-ledger#5054
158+
sed fp ["-i", "s/\\([yaoye]\\.address = \\)/\\1 bytes ;/g"]
159+
sed fp ["-i", "s/\\(reward_account = \\)/\\1 bytes ;/g"]
160+
sed fp ["-i", "-z", "s/unit_interval = #6\\.30(\\[\\n\\s*1,\\n\\s*2,\\n\\])/unit_interval = #6.30([uint, uint])/g"]
161+
r <- BS.readFile fp
162+
D.removeFile fp
163+
pure (CDDLSpec r)
164+
165+
-- -- | Some CDDLs in the ledger are wrong or misleading. This step is to
166+
-- -- sed-replace to fix them until e.g. cardano-ledger#5054.
167+
-- fixup :: CDDLs -> IO CDDLs
168+
-- fixup CDDLs {..} = do
169+
-- diskBlockCDDL' <- fixupBlockCDDL diskBlockCDDL
170+
-- pure CDDLs {diskBlockCDDL = diskBlockCDDL', ..}
171+
172+
sed :: FilePath -> [String] -> IO ()
173+
sed fp args =
174+
Monad.void $ P.readProcessWithExitCode "sed" (args ++ [fp]) mempty
116175

117176
cddlc :: FilePath -> IO CDDLSpec
118177
cddlc dataFile = do
119178
putStrLn $ "Generating: " <> dataFile
120179
path <- getDataFileName dataFile
121-
(_, cddl, err) <-
180+
(_, BSL.toStrict -> cddl, BSL.toStrict -> err) <-
122181
#ifdef POSIX
123182
P.readProcessWithExitCode "cddlc" ["-u", "-2", "-t", "cddl", path] mempty
124183
#else
@@ -129,7 +188,7 @@ cddlc dataFile = do
129188
prefix <- E.getEnv "MSYSTEM_PREFIX"
130189
P.readProcessWithExitCode "ruby" [prefix F.</> "bin/cddlc", "-u", "-2", "-t", "cddl", path] mempty
131190
#endif
132-
Monad.unless (BL.null err) $ red $ BL8.toString err
191+
Monad.unless (BS.null err) $ red $ BS8.unpack err
133192
return $ CDDLSpec cddl
134193
where
135194
red s = putStrLn $ "\ESC[31m" <> s <> "\ESC[0m"
@@ -177,7 +236,7 @@ probeTools = do
177236
rubyExe <- D.findExecutable "ruby"
178237
if (isNothing rubyExe)
179238
then do
180-
putStrLn "not found!\nPlease install ruby and the `cddl` and `cddlc` gems"
239+
putStrLn "not found!\nPlease install ruby"
181240
exitFailure
182241
else
183242
putStrLn "found"

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ library
136136
cardano-crypto,
137137
cardano-crypto-class ^>=2.2,
138138
cardano-crypto-wrapper,
139-
cardano-ledger-allegra ^>=1.7,
140-
cardano-ledger-alonzo ^>=1.13,
141-
cardano-ledger-api ^>=1.11,
142-
cardano-ledger-babbage ^>=1.11,
143-
cardano-ledger-binary ^>=1.6,
139+
cardano-ledger-allegra ^>=1.8,
140+
cardano-ledger-alonzo ^>=1.14,
141+
cardano-ledger-api ^>=1.12,
142+
cardano-ledger-babbage ^>=1.12,
143+
cardano-ledger-binary ^>=1.7,
144144
cardano-ledger-byron ^>=1.1,
145145
cardano-ledger-conway ^>=1.19,
146146
cardano-ledger-core ^>=1.17,
147-
cardano-ledger-mary ^>=1.8,
147+
cardano-ledger-mary ^>=1.9,
148148
cardano-ledger-shelley ^>=1.16,
149149
cardano-prelude,
150150
cardano-protocol-tpraos ^>=1.4,
@@ -744,11 +744,13 @@ executable gen-header
744744
autogen-modules:
745745
Paths_ouroboros_consensus_cardano
746746

747-
executable pull-cddls
747+
test-suite cddl-compliance
748748
import: common-exe
749+
type: exitcode-stdio-1.0
749750
hs-source-dirs: cddl
750751
main-is: pull-cddls.hs
751752
other-modules: Paths_ouroboros_consensus_cardano
753+
build-tool-depends: cuddle:cuddle
752754
build-depends:
753755
base,
754756
bytestring,
@@ -762,4 +764,5 @@ executable pull-cddls
762764
directory,
763765
filepath,
764766
process-extras,
765-
utf8-string,
767+
tasty-hunit,
768+
tasty,

0 commit comments

Comments
 (0)