-
Notifications
You must be signed in to change notification settings - Fork 156
/
cardano-db-sync.hs
233 lines (210 loc) · 6.34 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
{-# 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 qualified Data.Text.IO as Text
import Data.Version (showVersion)
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 ) -> Text.putStrLn $
"Error: Using `--dissable-ledger` doesn't require having a --state-dir. " <> moreDetails
(Nothing, True) -> Text.putStrLn $
"Error: If not using --state-dir then make sure to have --dissable-ledger. " <> moreDetails
where
knownMigrationsPlain :: [(Text, Text)]
knownMigrationsPlain = (\x -> (hash x, filepath x)) <$> knownMigrations
moreDetails :: Text
moreDetails = "For more details view https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/configuration.md#--disable-ledger"
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
<*> 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-plutus-data-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 "only-plutus-data-fix"
<> Opt.help "Runs only the db-sync fix procedure for the wrong datum and redeemer_data bytes and exits."
)
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"
)
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