Skip to content

Commit ada716b

Browse files
committed
Adopt db-synthesizer into cardano-node repository
1 parent 1eab9bd commit ada716b

14 files changed

Lines changed: 1065 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ cardano-tracer/cardano-tracer-test
7979
.codex
8080

8181
.serena/
82+
83+
cardano-node/test/db-synthesizer/disk/chaindb

cabal.project

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,17 @@ source-repository-package
141141
kes-agent
142142
kes-agent-crypto
143143

144+
source-repository-package
145+
type: git
146+
location: https://github.com/IntersectMBO/cardano-config
147+
tag: 50994eb21ae1d99528ce468cc5fba08ba67ca1ee
148+
--sha256: sha256-h+tYgNmkT+kBQcuE4ujOmbv6WoT8uLVPE2phjtPz4LQ=
149+
144150
source-repository-package
145151
type: git
146152
location: https://github.com/IntersectMBO/ouroboros-consensus.git
147-
tag: e468a936006a890d4469d1cbfaa3cfbe6867e29c
148-
--sha256: sha256-X1Yd6TMYhhxbm8qiD3y8Ad3nY2D5wieGWf9kwoRCWxc=
153+
tag: ba6574636b3f85641cea3c93aea83822d9be5342
154+
--sha256: sha256-d5xlkoy5JVuwz+kCF/1o2o5nyK58shmpgdirSBvL7xE=
149155
subdir:
150156
.
151157

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
module DBSynthesizer.Parsers (parseCommandLine) where
2+
3+
import Cardano.Node.Types (KESSource (..), ProtocolFilepaths (..))
4+
import Cardano.Tools.DBSynthesizer.Types
5+
import Data.Word (Word64)
6+
import Options.Applicative as Opt
7+
import Ouroboros.Consensus.Block.Abstract (SlotNo (..))
8+
9+
parseCommandLine :: IO (FilePath, FilePath, ProtocolFilepaths, DBSynthesizerOptions)
10+
parseCommandLine =
11+
Opt.customExecParser p opts
12+
where
13+
p = Opt.prefs Opt.showHelpOnEmpty
14+
opts = Opt.info parserCommandLine mempty
15+
16+
parserCommandLine :: Parser (FilePath, FilePath, ProtocolFilepaths, DBSynthesizerOptions)
17+
parserCommandLine =
18+
(,,,)
19+
<$> parseNodeConfigFilePath
20+
<*> parseChainDBFilePath
21+
<*> parseProtocolFilepaths
22+
<*> parseDBSynthesizerOptions
23+
24+
-- | The forging credentials, as file paths. Byron delegation credentials are
25+
-- not wired up (the synthesizer forges Shelley-based blocks); the KES key path,
26+
-- when given, is interpreted as a key file (not a KES agent socket).
27+
parseProtocolFilepaths :: Parser ProtocolFilepaths
28+
parseProtocolFilepaths =
29+
mkFilepaths
30+
<$> optional parseKesKeyFilePath
31+
<*> optional parseVrfKeyFilePath
32+
<*> optional parseOperationalCertFilePath
33+
<*> optional parseBulkFilePath
34+
where
35+
mkFilepaths mKes mVrf mCert mBulk =
36+
ProtocolFilepaths
37+
{ byronCertFile = Nothing
38+
, byronKeyFile = Nothing
39+
, shelleyKESSource = KESKeyFilePath <$> mKes
40+
, shelleyVRFFile = mVrf
41+
, shelleyCertFile = mCert
42+
, shelleyBulkCredsFile = mBulk
43+
}
44+
45+
parseDBSynthesizerOptions :: Parser DBSynthesizerOptions
46+
parseDBSynthesizerOptions =
47+
DBSynthesizerOptions
48+
<$> parseForgeOptions
49+
<*> parseOpenMode
50+
51+
parseForgeOptions :: Parser ForgeLimit
52+
parseForgeOptions =
53+
ForgeLimitSlot <$> parseSlotLimit
54+
<|> ForgeLimitBlock <$> parseBlockLimit
55+
<|> ForgeLimitEpoch <$> parseEpochLimit
56+
57+
parseChainDBFilePath :: Parser FilePath
58+
parseChainDBFilePath =
59+
strOption
60+
( long "db"
61+
<> metavar "PATH"
62+
<> help "Path to the Chain DB"
63+
<> completer (bashCompleter "directory")
64+
)
65+
66+
parseNodeConfigFilePath :: Parser FilePath
67+
parseNodeConfigFilePath =
68+
strOption
69+
( long "config"
70+
<> metavar "FILE"
71+
<> help "Path to the node's config.json"
72+
<> completer (bashCompleter "file")
73+
)
74+
75+
parseOperationalCertFilePath :: Parser FilePath
76+
parseOperationalCertFilePath =
77+
strOption
78+
( long "shelley-operational-certificate"
79+
<> metavar "FILE"
80+
<> help "Path to the delegation certificate (in JSON TextEnvelope format)"
81+
<> completer (bashCompleter "file")
82+
)
83+
84+
parseKesKeyFilePath :: Parser FilePath
85+
parseKesKeyFilePath =
86+
strOption
87+
( long "shelley-kes-key"
88+
<> metavar "FILE"
89+
<> help "Path to the KES signing key (in JSON TextEnvelope format)"
90+
<> completer (bashCompleter "file")
91+
)
92+
93+
parseVrfKeyFilePath :: Parser FilePath
94+
parseVrfKeyFilePath =
95+
strOption
96+
( long "shelley-vrf-key"
97+
<> metavar "FILE"
98+
<> help "Path to the VRF signing key (in JSON TextEnvelope format)"
99+
<> completer (bashCompleter "file")
100+
)
101+
102+
parseBulkFilePath :: Parser FilePath
103+
parseBulkFilePath =
104+
strOption
105+
( long "bulk-credentials-file"
106+
<> metavar "FILE"
107+
<> help
108+
"Path to the bulk credentials file (a JSON file containing an array of arrays containing 3 TextEnvelope objects for the opcert, VRF Signing key, KES signing key)"
109+
<> completer (bashCompleter "file")
110+
)
111+
112+
parseSlotLimit :: Parser SlotNo
113+
parseSlotLimit =
114+
SlotNo
115+
<$> option
116+
auto
117+
( short 's'
118+
<> long "slots"
119+
<> metavar "NUMBER"
120+
<> help "Amount of slots to process"
121+
)
122+
123+
parseBlockLimit :: Parser Word64
124+
parseBlockLimit =
125+
option
126+
auto
127+
( short 'b'
128+
<> long "blocks"
129+
<> metavar "NUMBER"
130+
<> help "Amount of blocks to forge"
131+
)
132+
133+
parseEpochLimit :: Parser Word64
134+
parseEpochLimit =
135+
option
136+
auto
137+
( short 'e'
138+
<> long "epochs"
139+
<> metavar "NUMBER"
140+
<> help "Amount of epochs to process"
141+
)
142+
143+
parseForce :: Parser Bool
144+
parseForce =
145+
switch
146+
( short 'f'
147+
<> help "Force overwrite an existing Chain DB"
148+
)
149+
150+
parseAppend :: Parser Bool
151+
parseAppend =
152+
switch
153+
( short 'a'
154+
<> help "Append to an existing Chain DB"
155+
)
156+
157+
parseOpenMode :: Parser DBSynthesizerOpenMode
158+
parseOpenMode =
159+
(parseForce *> pure OpenCreateForce)
160+
<|> (parseAppend *> pure OpenAppend)
161+
<|> pure OpenCreate

cardano-node/app/db-synthesizer.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- | This tool synthesizes a valid ChainDB, replicating cardano-node's UX.
2+
--
3+
-- Usage: db-synthesizer --config FILE --db PATH
4+
-- [--shelley-operational-certificate FILE]
5+
-- [--shelley-vrf-key FILE] [--shelley-kes-key FILE]
6+
-- [--bulk-credentials-file FILE]
7+
-- ((-s|--slots NUMBER) | (-b|--blocks NUMBER) |
8+
-- (-e|--epochs NUMBER)) [-f | -a]
9+
--
10+
-- The node configuration and forging credentials are turned into a Cardano
11+
-- 'ProtocolInfo' and block forgers using cardano-node's own protocol-instantiation
12+
-- machinery (see "Cardano.Node.Tools.DBSynthesizer"); the actual forging is done
13+
-- by @ouroboros-consensus@'s @synthesize@.
14+
module Main (main) where
15+
16+
import Cardano.Crypto.Init (cryptoInit)
17+
import Cardano.Node.Tools.DBSynthesizer (synthesizeFromConfig)
18+
import DBSynthesizer.Parsers (parseCommandLine)
19+
20+
main :: IO ()
21+
main = do
22+
cryptoInit
23+
(configFp, dbDir, protocolFiles, opts) <- parseCommandLine
24+
result <- synthesizeFromConfig configFp protocolFiles opts dbDir
25+
putStrLn $ "--> done; result: " ++ show result

cardano-node/cardano-node.cabal

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ library
113113
Cardano.Node.Tracing.Tracers.Shutdown
114114
Cardano.Node.Tracing.Tracers.HasIssuer
115115
Cardano.Node.Tracing.Tracers.Startup
116+
Cardano.Node.Tools.DBSynthesizer
116117
Cardano.Node.Types
117118

118119
other-modules: Paths_cardano_node
@@ -166,7 +167,7 @@ library
166167
, network-mux >= 0.8
167168
, nothunks
168169
, optparse-applicative
169-
, ouroboros-consensus:{ouroboros-consensus, lsm, cardano, diffusion, protocol} ^>= 3.0.1
170+
, ouroboros-consensus:{ouroboros-consensus, lsm, cardano, diffusion, protocol, unstable-cardano-tools} ^>= 3.0.1
170171
, ouroboros-network:{api, ouroboros-network, orphan-instances, framework, protocols, tracing} ^>= 1.1
171172
, cardano-diffusion:{api, cardano-diffusion, tracing, orphan-instances} ^>=1.0
172173
, prettyprinter
@@ -213,6 +214,34 @@ executable cardano-node
213214
, optparse-applicative
214215
, text
215216

217+
executable db-synthesizer
218+
import: project-config
219+
hs-source-dirs: app
220+
main-is: db-synthesizer.hs
221+
ghc-options: -threaded
222+
-rtsopts
223+
224+
other-modules: DBSynthesizer.Parsers
225+
226+
build-depends: base
227+
, cardano-crypto-class
228+
, cardano-node
229+
, optparse-applicative
230+
, ouroboros-consensus:{ouroboros-consensus, unstable-cardano-tools} ^>= 3.0.1
231+
232+
test-suite db-synthesizer-test
233+
import: project-config
234+
hs-source-dirs: test/db-synthesizer
235+
main-is: Main.hs
236+
type: exitcode-stdio-1.0
237+
238+
build-depends: base
239+
, cardano-crypto-class
240+
, cardano-node
241+
, ouroboros-consensus:{ouroboros-consensus, cardano, unstable-cardano-tools} ^>= 3.0.1
242+
, tasty
243+
, tasty-hunit
244+
216245
test-suite cardano-node-test
217246
import: project-config
218247
, maybe-unix

0 commit comments

Comments
 (0)