1
+ {-# LANGUAGE ViewPatterns #-}
2
+ {-# LANGUAGE BangPatterns #-}
1
3
{-# LANGUAGE RecordWildCards #-}
2
4
{-# LANGUAGE CPP #-}
3
5
-- |
4
6
5
7
module Main (main ) where
6
8
7
9
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
10
13
import Data.Maybe (isNothing )
11
14
import qualified Data.List as L
12
15
import Paths_ouroboros_consensus_cardano
13
16
import qualified System.Directory as D
14
17
import qualified System.Environment as E
15
- import System.Exit ( exitFailure )
18
+ import System.Exit
16
19
import qualified System.FilePath as F
17
20
import qualified System.Process.ByteString.Lazy as P
18
21
import qualified Test.Cardano.Chain.Binary.Cddl as Byron
@@ -22,16 +25,43 @@ import qualified Test.Cardano.Ledger.Babbage.Binary.Cddl as Babbage
22
25
import qualified Test.Cardano.Ledger.Conway.Binary.Cddl as Conway
23
26
import qualified Test.Cardano.Ledger.Mary.Binary.Cddl as Mary
24
27
import qualified Test.Cardano.Ledger.Shelley.Binary.Cddl as Shelley
28
+ -- import System.IO
29
+ import Test.Tasty
30
+ import Test.Tasty.HUnit
25
31
26
32
main :: IO ()
27
33
main = do
28
34
probeTools
29
- setupEnv
30
- -- For now I just print this.
31
- print =<< getCDDLs
35
+ setupCDDLCEnv
32
36
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
35
65
byron <- map takePath <$> Byron. readByronCddlFileNames
36
66
shelley <- map takePath <$> Shelley. readShelleyCddlFileNames
37
67
allegra <- map takePath <$> Allegra. readAllegraCddlFileNames
@@ -58,7 +88,7 @@ setupEnv = do
58
88
59
89
E. setEnv " CDDL_INCLUDE_PATH" (include_path <> " :" )
60
90
61
- newtype CDDLSpec = CDDLSpec BL . ByteString deriving Show
91
+ newtype CDDLSpec = CDDLSpec { cddlSpec :: BS . ByteString } deriving Show
62
92
63
93
data CDDLs = CDDLs {
64
94
diskBlockCDDL :: CDDLSpec
@@ -83,42 +113,71 @@ data CDDLs = CDDLs {
83
113
, ntcTxMonitorSlotNoCDDL :: CDDLSpec
84
114
} deriving Show
85
115
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
116
175
117
176
cddlc :: FilePath -> IO CDDLSpec
118
177
cddlc dataFile = do
119
178
putStrLn $ " Generating: " <> dataFile
120
179
path <- getDataFileName dataFile
121
- (_, cddl, err) <-
180
+ (_, BSL. toStrict -> cddl, BSL. toStrict -> err) <-
122
181
#ifdef POSIX
123
182
P. readProcessWithExitCode " cddlc" [" -u" , " -2" , " -t" , " cddl" , path] mempty
124
183
#else
@@ -129,7 +188,7 @@ cddlc dataFile = do
129
188
prefix <- E. getEnv " MSYSTEM_PREFIX"
130
189
P. readProcessWithExitCode " ruby" [prefix F. </> " bin/cddlc" , " -u" , " -2" , " -t" , " cddl" , path] mempty
131
190
#endif
132
- Monad. unless (BL .null err) $ red $ BL8. toString err
191
+ Monad. unless (BS .null err) $ red $ BS8. unpack err
133
192
return $ CDDLSpec cddl
134
193
where
135
194
red s = putStrLn $ " \ESC [31m" <> s <> " \ESC [0m"
@@ -177,7 +236,7 @@ probeTools = do
177
236
rubyExe <- D. findExecutable " ruby"
178
237
if (isNothing rubyExe)
179
238
then do
180
- putStrLn " not found!\n Please install ruby and the `cddl` and `cddlc` gems "
239
+ putStrLn " not found!\n Please install ruby"
181
240
exitFailure
182
241
else
183
242
putStrLn " found"
0 commit comments