@@ -41,6 +41,7 @@ module Test.Cardano.Db.Mock.Config (
41
41
withCustomConfigAndDropDB ,
42
42
withCustomConfigAndLogs ,
43
43
withFullConfig' ,
44
+ replaceConfigFile ,
44
45
) where
45
46
46
47
import Cardano.Api (NetworkMagic (.. ))
@@ -81,7 +82,7 @@ import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus
81
82
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto )
82
83
import Ouroboros.Consensus.Shelley.Node (ShelleyLeaderCredentials )
83
84
import System.Directory (createDirectoryIfMissing , removePathForcibly )
84
- import System.FilePath.Posix ((</>) )
85
+ import System.FilePath.Posix (takeDirectory , (</>) )
85
86
import System.IO.Silently (hSilence )
86
87
87
88
data Config = Config
@@ -94,12 +95,13 @@ data Config = Config
94
95
95
96
data DBSyncEnv = DBSyncEnv
96
97
{ dbSyncParams :: SyncNodeParams
97
- , partialRunDbSync :: SyncNodeParams -> IO ()
98
+ , dbSyncConfig :: SyncNodeConfig
99
+ , partialRunDbSync :: SyncNodeParams -> SyncNodeConfig -> IO ()
98
100
, dbSyncThreadVar :: TMVar (Async () )
99
101
}
100
102
101
103
data CommandLineArgs = CommandLineArgs
102
- { claHasConfigFile :: Bool
104
+ { claConfigFilename :: FilePath
103
105
, claEpochDisabled :: Bool
104
106
, claHasCache :: Bool
105
107
, claHasLedger :: Bool
@@ -151,12 +153,17 @@ fingerprintRoot = rootTestDir </> "fingerprint"
151
153
mkFingerPrint :: FilePath -> FilePath
152
154
mkFingerPrint testLabel = fingerprintRoot </> testLabel
153
155
154
- mkDBSyncEnv :: SyncNodeParams -> (SyncNodeParams -> IO () ) -> IO DBSyncEnv
155
- mkDBSyncEnv params pRunDbSync = do
156
+ mkDBSyncEnv ::
157
+ SyncNodeParams ->
158
+ SyncNodeConfig ->
159
+ (SyncNodeParams -> SyncNodeConfig -> IO () ) ->
160
+ IO DBSyncEnv
161
+ mkDBSyncEnv params cfg pRunDbSync = do
156
162
runningVar <- newEmptyTMVarIO
157
163
pure $
158
164
DBSyncEnv
159
165
{ dbSyncParams = params
166
+ , dbSyncConfig = cfg
160
167
, partialRunDbSync = pRunDbSync
161
168
, dbSyncThreadVar = runningVar
162
169
}
@@ -188,7 +195,7 @@ startDBSync env = do
188
195
case thr of
189
196
Just _a -> error " db-sync already running"
190
197
Nothing -> do
191
- let appliedRunDbSync = partialRunDbSync env $ dbSyncParams env
198
+ let appliedRunDbSync = partialRunDbSync env ( dbSyncParams env) (dbSyncConfig env)
192
199
-- we async the fully applied runDbSync here ad put it into the thread
193
200
asyncApplied <- async appliedRunDbSync
194
201
void . atomically $ tryPutTMVar (dbSyncThreadVar env) asyncApplied
@@ -220,17 +227,21 @@ getPoolLayer env = do
220
227
221
228
mkConfig :: FilePath -> FilePath -> CommandLineArgs -> SyncNodeConfig -> IO Config
222
229
mkConfig staticDir mutableDir cmdLineArgs config = do
230
+ let cfgDir = mkConfigDir staticDir
223
231
genCfg <- runOrThrowIO $ runExceptT (readCardanoGenesisConfig config)
224
232
let (pInfoDbSync, _) = mkProtocolInfoCardano genCfg []
225
- creds <- mkShelleyCredentials $ staticDir </> " pools" </> " bulk1.creds"
233
+ creds <- mkShelleyCredentials $ cfgDir </> " pools" </> " bulk1.creds"
226
234
let (pInfoForger, forging) = mkProtocolInfoCardano genCfg creds
227
235
forging' <- forging
228
236
syncPars <- mkSyncNodeParams staticDir mutableDir cmdLineArgs
229
237
pure $ Config (Consensus. pInfoConfig pInfoDbSync) pInfoDbSync pInfoForger forging' syncPars
230
238
231
- mkSyncNodeConfig :: FilePath -> IO SyncNodeConfig
232
- mkSyncNodeConfig configFilePath =
233
- readSyncNodeConfig $ ConfigFile (mkConfigDir configFilePath </> " test-db-sync-config.json" )
239
+ mkSyncNodeConfig :: FilePath -> CommandLineArgs -> IO SyncNodeConfig
240
+ mkSyncNodeConfig configFilePath cmdLineArgs =
241
+ readSyncNodeConfig $ mkConfigFile configDir configFilename
242
+ where
243
+ configFilename = claConfigFilename cmdLineArgs
244
+ configDir = mkConfigDir configFilePath
234
245
235
246
mkShelleyCredentials :: FilePath -> IO [ShelleyLeaderCredentials StandardCrypto ]
236
247
mkShelleyCredentials bulkFile = do
@@ -250,9 +261,10 @@ mkShelleyCredentials bulkFile = do
250
261
mkSyncNodeParams :: FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
251
262
mkSyncNodeParams staticDir mutableDir CommandLineArgs {.. } = do
252
263
pgconfig <- runOrThrowIO Db. readPGPassDefault
264
+
253
265
pure $
254
266
SyncNodeParams
255
- { enpConfigFile = ConfigFile $ staticDir </> ( if claHasConfigFile then " test-db-sync-config.json " else " " )
267
+ { enpConfigFile = mkConfigFile staticDir claConfigFilename
256
268
, enpSocketPath = SocketPath $ mutableDir </> " .socket"
257
269
, enpMaybeLedgerStateDir = Just $ LedgerStateDir $ mutableDir </> " ledger-states"
258
270
, enpMigrationDir = MigrationDir " ../schema"
@@ -285,10 +297,14 @@ mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
285
297
, enpMaybeRollback = Nothing
286
298
}
287
299
300
+ mkConfigFile :: FilePath -> FilePath -> ConfigFile
301
+ mkConfigFile staticDir cliConfigFilename =
302
+ ConfigFile $ staticDir </> cliConfigFilename
303
+
288
304
initCommandLineArgs :: CommandLineArgs
289
305
initCommandLineArgs =
290
306
CommandLineArgs
291
- { claHasConfigFile = True
307
+ { claConfigFilename = " test-db-sync-config.json "
292
308
, claEpochDisabled = True
293
309
, claHasCache = True
294
310
, claHasLedger = True
@@ -460,17 +476,17 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
460
476
syncNodeConfig <-
461
477
case mSyncNodeConfig of
462
478
Just snc -> pure snc
463
- Nothing -> mkSyncNodeConfig configFilePath
479
+ Nothing -> mkSyncNodeConfig configFilePath cmdLineArgs
464
480
465
- cfg <- mkConfig (mkConfigDir configFilePath) mutableDir cmdLineArgs syncNodeConfig
481
+ cfg <- mkConfig configFilePath mutableDir cmdLineArgs syncNodeConfig
466
482
fingerFile <- if hasFingerprint then Just <$> prepareFingerprintFile testLabelFilePath else pure Nothing
467
483
let dbsyncParams = syncNodeParams cfg
468
484
trce <-
469
485
if shouldLog
470
486
then configureLogging syncNodeConfig " db-sync-node"
471
487
else pure nullTracer
472
488
-- runDbSync is partially applied so we can pass in syncNodeParams at call site / within tests
473
- let partialDbSyncRun params = runDbSync emptyMetricsSetters migr iom trce params syncNodeConfig True
489
+ let partialDbSyncRun params cfg' = runDbSync emptyMetricsSetters migr iom trce params cfg' True
474
490
initSt = Consensus. pInfoInitLedger $ protocolInfo cfg
475
491
476
492
withInterpreter (protocolInfoForging cfg) (protocolInfoForger cfg) nullTracer fingerFile $ \ interpreter -> do
@@ -483,7 +499,7 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
483
499
(unSocketPath (enpSocketPath $ syncNodeParams cfg))
484
500
$ \ mockServer ->
485
501
-- we dont fork dbsync here. Just prepare it as an action
486
- withDBSyncEnv (mkDBSyncEnv dbsyncParams partialDbSyncRun) $ \ dbSyncEnv -> do
502
+ withDBSyncEnv (mkDBSyncEnv dbsyncParams syncNodeConfig partialDbSyncRun) $ \ dbSyncEnv -> do
487
503
let pgPass = getDBSyncPGPass dbSyncEnv
488
504
tableNames <- Db. getAllTablleNames pgPass
489
505
-- We only want to create the table schema once for the tests so here we check
@@ -509,3 +525,13 @@ recreateDir path = do
509
525
510
526
textShow :: (Show a ) => a -> Text
511
527
textShow = Text. pack . show
528
+
529
+ replaceConfigFile :: FilePath -> DBSyncEnv -> IO DBSyncEnv
530
+ replaceConfigFile newFilename dbSync@ DBSyncEnv {.. } = do
531
+ cfg <- readSyncNodeConfig $ mkConfigFile configDir newFilename
532
+
533
+ pure $ dbSync {dbSyncParams = newParams, dbSyncConfig = cfg}
534
+ where
535
+ configDir = mkConfigDir . takeDirectory . unConfigFile . enpConfigFile $ dbSyncParams
536
+ newParams =
537
+ dbSyncParams {enpConfigFile = ConfigFile $ configDir </> newFilename}
0 commit comments