-
Notifications
You must be signed in to change notification settings - Fork 156
/
cardano-db-sync.hs
322 lines (291 loc) · 8.38 KB
/
cardano-db-sync.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
import Cardano.Db (MigrationDir (..), PGPassSource (PGPassDefaultEnv, PGPassEnv), gitRev)
import Cardano.DbSync (runDbSyncNode)
import Cardano.DbSync.Config
import Cardano.DbSync.Metrics (withMetricSetters)
import Cardano.Prelude
import Cardano.Slotting.Slot (SlotNo (..))
import Data.String (String)
import qualified Data.Text as Text
import Data.Version (showVersion)
import GHC.Base (error)
import MigrationValidations (KnownMigration (..), knownMigrations)
import Options.Applicative (Parser, ParserInfo)
import qualified Options.Applicative as Opt
import Paths_cardano_db_sync (version)
import System.Info (arch, compilerName, compilerVersion, os)
main :: IO ()
main = do
cmd <- Opt.execParser opts
case cmd of
CmdVersion -> runVersionCommand
CmdRun params -> do
let maybeLedgerStateDir = enpMaybeLedgerStateDir params
case (maybeLedgerStateDir, enpShouldUseLedger params) of
(Just _, True) -> run params
(Nothing, False) -> run params
(Just _, False) -> run params
(Nothing, True) -> error stateDirErrorMsg
where
knownMigrationsPlain :: [(Text, Text)]
knownMigrationsPlain = (\x -> (hash x, filepath x)) <$> knownMigrations
stateDirErrorMsg :: [Char]
stateDirErrorMsg =
"Error: If not using --state-dir then make sure to have --disable-ledger. "
<> "For more details view https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/syncing-and-rollbacks.md#ledger-state"
run :: SyncNodeParams -> IO ()
run prms = do
prometheusPort <- dncPrometheusPort <$> readSyncNodeConfig (enpConfigFile prms)
withMetricSetters prometheusPort $ \metricsSetters ->
runDbSyncNode metricsSetters knownMigrationsPlain prms
-- -------------------------------------------------------------------------------------------------
opts :: ParserInfo SyncCommand
opts =
Opt.info
(pCommandLine <**> Opt.helper)
( Opt.fullDesc
<> Opt.progDesc "Cardano PostgreSQL sync node."
)
pCommandLine :: Parser SyncCommand
pCommandLine =
asum
[ pVersionCommand
, CmdRun <$> pRunDbSyncNode
]
pRunDbSyncNode :: Parser SyncNodeParams
pRunDbSyncNode =
SyncNodeParams
<$> pConfigFile
<*> pSocketPath
<*> optional pLedgerStateDir
<*> pMigrationDir
<*> pPGPassSource
<*> pExtended
<*> pHasCache
<*> pUseLedger
<*> pSkipFix
<*> pOnlyFix
<*> pForceIndexes
<*> pHasMultiAssets
<*> pHasMetadata
<*> pHasPlutusExtra
<*> pHasOfflineData
<*> pTurboMode
<*> pFullMode
<*> pMigrateConsumed
<*> pPruneTxOut
<*> pure 500
<*> pure 10000
<*> optional pSlotNo
pConfigFile :: Parser ConfigFile
pConfigFile =
ConfigFile
<$> Opt.strOption
( Opt.long "config"
<> Opt.help "Path to the db-sync node config file"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)
pLedgerStateDir :: Parser LedgerStateDir
pLedgerStateDir =
LedgerStateDir
<$> Opt.strOption
( Opt.long "state-dir"
<> Opt.help "The directory for persisting ledger state."
<> Opt.completer (Opt.bashCompleter "directory")
<> Opt.metavar "FILEPATH"
)
pMigrationDir :: Parser MigrationDir
pMigrationDir =
MigrationDir
<$> Opt.strOption
( Opt.long "schema-dir"
<> Opt.help "The directory containing the migrations."
<> Opt.completer (Opt.bashCompleter "directory")
<> Opt.metavar "FILEPATH"
)
pPGPassSource :: Parser PGPassSource
pPGPassSource =
Opt.option
(PGPassEnv <$> Opt.str)
( Opt.long "pg-pass-env"
<> Opt.help "Alternative env variable to use, defaults to PGPASSFILE env variable."
<> Opt.value PGPassDefaultEnv
<> Opt.metavar "ENV"
)
pExtended :: Parser Bool
pExtended =
Opt.flag
True
False
( Opt.long "disable-epoch"
<> Opt.help "Makes epoch table remain empty"
)
pSkipFix :: Parser Bool
pSkipFix =
Opt.flag
False
True
( Opt.long "skip-fix"
<> Opt.help "Disables the db-sync fix procedure for the wrong datum and redeemer_data bytes."
)
pForceIndexes :: Parser Bool
pForceIndexes =
Opt.flag
False
True
( Opt.long "force-indexes"
<> Opt.help "Forces the Index creation at the start of db-sync. Normally they're created later."
)
pOnlyFix :: Parser Bool
pOnlyFix =
Opt.flag
False
True
( Opt.long "fix-only"
<> Opt.help
"Runs only the db-sync fix procedure for the wrong datum, redeemer_data and plutus script bytes and exits. \
\This doesn't run any migrations. This can also be ran on previous schema, ie 13.0 13.1 to fix the issues without \
\bumping the schema version minor number."
)
pHasCache :: Parser Bool
pHasCache =
Opt.flag
True
False
( Opt.long "disable-cache"
<> Opt.help "Disables the db-sync caches. Reduces memory usage but it takes longer to sync."
)
pUseLedger :: Parser Bool
pUseLedger =
Opt.flag
True
False
( Opt.long "disable-ledger"
<> Opt.help "Disables the leger state. Drastically reduces memory usage and it syncs faster, but some data are missing."
)
pSocketPath :: Parser SocketPath
pSocketPath =
SocketPath
<$> Opt.strOption
( Opt.long "socket-path"
<> Opt.help "Path to a cardano-node socket"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)
pSlotNo :: Parser SlotNo
pSlotNo =
SlotNo
<$> Opt.option
Opt.auto
( Opt.long "rollback-to-slot"
<> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)."
<> Opt.metavar "WORD"
)
pHasMultiAssets :: Parser Bool
pHasMultiAssets =
Opt.flag
True
False
( Opt.long "disable-multiassets"
<> Opt.help "Disables the multi assets tables and entries."
)
pHasMetadata :: Parser Bool
pHasMetadata =
Opt.flag
True
False
( Opt.long "disable-metadata"
<> Opt.help "Disables the tx_metadata table."
)
pHasPlutusExtra :: Parser Bool
pHasPlutusExtra =
Opt.flag
True
False
( Opt.long "disable-plutus-extra"
<> Opt.help "Disables most tables and entries related to plutus and scripts."
)
pHasOfflineData :: Parser Bool
pHasOfflineData =
Opt.flag
True
False
( Opt.long "disable-offline-data"
<> Opt.help "Disables fetching pool offline metadata."
)
pTurboMode :: Parser Bool
pTurboMode =
Opt.flag
False
True
( Opt.long "turbo"
<> Opt.help "Enables turbo mode, which make db-sync running much faster with limited functionality."
)
pFullMode :: Parser Bool
pFullMode =
Opt.flag
False
True
( Opt.long "full"
<> Opt.help "Makes db-sync run with all possible functionalities."
)
pMigrateConsumed :: Parser Bool
pMigrateConsumed =
Opt.flag
False
True
( Opt.long "consumed-tx-out"
<> Opt.help "Runs the tx_out migration, which adds a new field.If this is set once,\
\ then it must be always be set on following executions of db-sync, unless prune-tx-out\
\ is used instead."
)
pPruneTxOut :: Parser Bool
pPruneTxOut =
Opt.flag
False
True
( Opt.long "prune-tx-out"
<> Opt.help "Prunes the consumed tx_out periodically. This assumes \
\ consumed-tx-out is also set, even if it's not. If this is set once,\
\ then it must be always be set on following executions of db-sync."
)
pVersionCommand :: Parser SyncCommand
pVersionCommand =
asum
[ Opt.subparser
( mconcat
[command' "version" "Show the program version" (pure CmdVersion)]
)
, Opt.flag'
CmdVersion
( Opt.long "version"
<> Opt.help "Show the program version"
<> Opt.hidden
)
]
command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a
command' c descr p =
Opt.command c $
Opt.info (p <**> Opt.helper) $
mconcat [Opt.progDesc descr]
runVersionCommand :: IO ()
runVersionCommand = do
liftIO . putTextLn $
mconcat
[ "cardano-db-sync "
, renderVersion version
, " - "
, Text.pack os
, "-"
, Text.pack arch
, " - "
, Text.pack compilerName
, "-"
, renderVersion compilerVersion
, "\ngit revision "
, gitRev
]
where
renderVersion = Text.pack . showVersion