-
Notifications
You must be signed in to change notification settings - Fork 156
/
Config.hs
312 lines (284 loc) · 9.92 KB
/
Config.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Test.Cardano.Db.Mock.Config (
Config (..),
DBSyncEnv (..),
emptyMetricsSetters,
fingerprintRoot,
getDBSyncPGPass,
getPoolLayer,
mkConfig,
mkConfigDir,
mkFingerPrint,
mkMutableDir,
mkDBSyncEnv,
mkShelleyCredentials,
mkSyncNodeParams,
pollDBSync,
prepareFingerprintFile,
queryDBSync,
recreateDir,
rootTestDir,
setupTestsDir,
stopDBSync,
stopDBSyncIfRunning,
startDBSync,
withDBSyncEnv,
withFullConfig,
withFullConfig',
) where
import Cardano.Api (NetworkId (..), NetworkMagic (..))
import Cardano.CLI.Shelley.Commands (GenesisCmd (..))
import qualified Cardano.CLI.Shelley.Commands as CLI
import qualified Cardano.CLI.Shelley.Run.Genesis as CLI
import qualified Cardano.Db as Db
import Cardano.DbSync
import Cardano.DbSync.Config
import Cardano.DbSync.Config.Cardano
import Cardano.DbSync.Error
import Cardano.DbSync.Types (CardanoBlock, MetricSetters (..))
import Cardano.Mock.ChainSync.Server
import Cardano.Mock.Forging.Interpreter
import Cardano.Node.Protocol.Shelley (readLeaderCredentials)
import Cardano.Node.Types (ProtocolFilepaths (..))
import Cardano.Prelude (ReaderT, panic, stderr)
import Cardano.SMASH.Server.PoolDataLayer
import Control.Concurrent.Async (Async, async, cancel, poll)
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TMVar (
TMVar,
newEmptyTMVarIO,
takeTMVar,
tryPutTMVar,
tryReadTMVar,
)
import Control.Exception (SomeException, bracket)
import Control.Monad (void)
import Control.Monad.Extra (eitherM)
import Control.Monad.Logger (NoLoggingT, runNoLoggingT)
import Control.Monad.Trans.Except.Exit (orDie)
import Control.Monad.Trans.Except.Extra (newExceptT, runExceptT)
import Control.Tracer (nullTracer)
import Data.Text (Text)
import qualified Data.Text as Text
import Database.Persist.Postgresql (createPostgresqlPool)
import Database.Persist.Sql (SqlBackend)
import Ouroboros.Consensus.Config (TopLevelConfig)
import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
import Ouroboros.Consensus.Shelley.Node (ShelleyLeaderCredentials)
import System.Directory (createDirectoryIfMissing, removePathForcibly)
import System.FilePath.Posix ((</>))
import System.IO.Silently (hSilence)
data Config = Config
{ topLevelConfig :: TopLevelConfig CardanoBlock
, protocolInfo :: Consensus.ProtocolInfo IO CardanoBlock
, protocolInfoForging :: Consensus.ProtocolInfo IO CardanoBlock
, syncNodeParams :: SyncNodeParams
}
data DBSyncEnv = DBSyncEnv
{ dbSyncParams :: SyncNodeParams
, dbSyncForkDB :: IO (Async ())
, dbSyncThreadVar :: TMVar (Async ())
}
rootTestDir :: FilePath
rootTestDir = "test/testfiles"
mkMutableDir :: FilePath -> FilePath
mkMutableDir testLabel = rootTestDir </> "temp" </> testLabel
mkConfigDir :: FilePath -> FilePath
mkConfigDir config = rootTestDir </> config
fingerprintRoot :: FilePath
fingerprintRoot = rootTestDir </> "fingerprint"
mkFingerPrint :: FilePath -> FilePath
mkFingerPrint testLabel = fingerprintRoot </> testLabel
mkDBSyncEnv :: SyncNodeParams -> IO () -> IO DBSyncEnv
mkDBSyncEnv params runDBSync = do
runningVar <- newEmptyTMVarIO
pure $
DBSyncEnv
{ dbSyncParams = params
, dbSyncForkDB = async runDBSync
, dbSyncThreadVar = runningVar
}
stopDBSync :: DBSyncEnv -> IO ()
stopDBSync env = do
thr <- atomically $ tryReadTMVar (dbSyncThreadVar env)
case thr of
Nothing -> error "Could not cancel db-sync when it's not running"
Just a -> do
cancel a
-- make it empty
void . atomically $ takeTMVar (dbSyncThreadVar env)
pure ()
stopDBSyncIfRunning :: DBSyncEnv -> IO ()
stopDBSyncIfRunning env = do
thr <- atomically $ tryReadTMVar (dbSyncThreadVar env)
case thr of
Nothing -> pure ()
Just a -> do
cancel a
-- make it empty
void . atomically $ takeTMVar (dbSyncThreadVar env)
startDBSync :: DBSyncEnv -> IO ()
startDBSync env = do
thr <- atomically $ tryReadTMVar (dbSyncThreadVar env)
case thr of
Just _a -> error "db-sync already running"
Nothing -> do
a <- dbSyncForkDB env
void . atomically $ tryPutTMVar (dbSyncThreadVar env) a
pollDBSync :: DBSyncEnv -> IO (Maybe (Either SomeException ()))
pollDBSync env = do
thr <- atomically $ tryReadTMVar (dbSyncThreadVar env)
case thr of
Nothing -> error "Could not poll db-sync when it's not running"
Just a -> poll a
withDBSyncEnv :: IO DBSyncEnv -> (DBSyncEnv -> IO a) -> IO a
withDBSyncEnv mkEnv = bracket mkEnv stopDBSyncIfRunning
getDBSyncPGPass :: DBSyncEnv -> Db.PGPassSource
getDBSyncPGPass = enpPGPassSource . dbSyncParams
queryDBSync :: DBSyncEnv -> ReaderT SqlBackend (NoLoggingT IO) a -> IO a
queryDBSync env = Db.runWithConnectionNoLogging (getDBSyncPGPass env)
getPoolLayer :: DBSyncEnv -> IO PoolDataLayer
getPoolLayer env = do
pgconfig <- orDie Db.renderPGPassError $ newExceptT $ Db.readPGPass (enpPGPassSource $ dbSyncParams env)
pool <- runNoLoggingT $ createPostgresqlPool (Db.toConnectionString pgconfig) 1 -- Pool size of 1 for tests
pure $
postgresqlPoolDataLayer
nullTracer
pool
setupTestsDir :: FilePath -> IO ()
setupTestsDir dir = do
eitherM (panic . textShow) pure $
runExceptT $
CLI.runGenesisCmd $
GenesisCreateStaked
(CLI.GenesisDir dir)
3
3
3
3
Nothing
(Just 3000000)
3000000
(Testnet $ NetworkMagic 42)
1
3
0
Nothing
mkConfig :: FilePath -> FilePath -> IO Config
mkConfig staticDir mutableDir = do
config <- readSyncNodeConfig $ ConfigFile (staticDir </> "test-db-sync-config.json")
genCfg <- either (error . Text.unpack . renderSyncNodeError) id <$> runExceptT (readCardanoGenesisConfig config)
let pInfoDbSync = mkProtocolInfoCardano genCfg []
creds <- mkShelleyCredentials $ staticDir </> "pools" </> "bulk1.creds"
let pInfoForger = mkProtocolInfoCardano genCfg creds
syncPars <- mkSyncNodeParams staticDir mutableDir
pure $ Config (Consensus.pInfoConfig pInfoDbSync) pInfoDbSync pInfoForger syncPars
mkShelleyCredentials :: FilePath -> IO [ShelleyLeaderCredentials StandardCrypto]
mkShelleyCredentials bulkFile = do
eitherM (panic . textShow) pure $ runExceptT $ readLeaderCredentials (Just protFiles)
where
protFiles =
ProtocolFilepaths
{ byronCertFile = Nothing
, byronKeyFile = Nothing
, shelleyKESFile = Nothing
, shelleyVRFFile = Nothing
, shelleyCertFile = Nothing
, shelleyBulkCredsFile = Just bulkFile
}
-- | staticDir can be shared by tests running in parallel. mutableDir not.
mkSyncNodeParams :: FilePath -> FilePath -> IO SyncNodeParams
mkSyncNodeParams staticDir mutableDir = do
pgconfig <- orDie Db.renderPGPassError $ newExceptT Db.readPGPassDefault
pure $
SyncNodeParams
{ enpConfigFile = ConfigFile $ staticDir </> "test-db-sync-config.json"
, enpSocketPath = SocketPath $ mutableDir </> ".socket"
, enpMaybeLedgerStateDir = Just $ LedgerStateDir $ mutableDir </> "ledger-states"
, enpMigrationDir = MigrationDir "../schema"
, enpPGPassSource = Db.PGPassCached pgconfig
, enpExtended = True
, enpHasCache = True
, enpShouldUseLedger = True
, enpSkipFix = True
, enpOnlyFix = False
, enpForceIndexes = False
, enpHasMultiAssets = True
, enpHasMetadata = True
, enpHasPlutusExtra = True
, enpHasOfflineData = True
, enpTurboMode = False
, enpFullMode = True
, enpMigrateConsumed = False
, enpPruneTxOut = False
, enpSnEveryFollowing = 35
, enpSnEveryLagging = 35
, enpMaybeRollback = Nothing
}
emptyMetricsSetters :: MetricSetters
emptyMetricsSetters =
MetricSetters
{ metricsSetNodeBlockHeight = \_ -> pure ()
, metricsSetDbQueueLength = \_ -> pure ()
, metricsSetDbBlockHeight = \_ -> pure ()
, metricsSetDbSlotHeight = \_ -> pure ()
}
withFullConfig ::
FilePath ->
FilePath ->
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
IOManager ->
[(Text, Text)] ->
IO a
withFullConfig = withFullConfig' True
withFullConfig' ::
Bool ->
FilePath ->
FilePath ->
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
IOManager ->
[(Text, Text)] ->
IO a
withFullConfig' hasFingerprint config testLabel action iom migr = do
recreateDir mutableDir
cfg <- mkConfig configDir mutableDir
fingerFile <- if hasFingerprint then Just <$> prepareFingerprintFile testLabel else pure Nothing
let dbsyncParams = syncNodeParams cfg
-- Set to True to disable logging, False to enable it.
trce <-
if True
then pure nullTracer
else configureLogging dbsyncParams "db-sync-node"
let dbsyncRun = runDbSync emptyMetricsSetters migr iom trce dbsyncParams True
let initSt = Consensus.pInfoInitLedger $ protocolInfo cfg
withInterpreter (protocolInfoForging cfg) nullTracer fingerFile $ \interpreter -> do
-- TODO: get 42 from config
withServerHandle @CardanoBlock
iom
(topLevelConfig cfg)
initSt
(NetworkMagic 42)
(unSocketPath (enpSocketPath $ syncNodeParams cfg))
$ \mockServer ->
-- we dont fork dbsync here. Just prepare it as an action
withDBSyncEnv (mkDBSyncEnv dbsyncParams dbsyncRun) $ \dbSync -> do
void . hSilence [stderr] $ Db.recreateDB (getDBSyncPGPass dbSync)
action interpreter mockServer dbSync
where
configDir = mkConfigDir config
mutableDir = mkMutableDir testLabel
prepareFingerprintFile :: FilePath -> IO FilePath
prepareFingerprintFile testLabel = do
createDirectoryIfMissing True fingerprintRoot
pure fingerprintFile
where
fingerprintFile = mkFingerPrint testLabel
recreateDir :: FilePath -> IO ()
recreateDir path = do
removePathForcibly path
createDirectoryIfMissing True path
textShow :: Show a => a -> Text
textShow = Text.pack . show