Skip to content

Commit 80cc82b

Browse files
committed
convert DB.DbAction to DB.DbM
1 parent 7738702 commit 80cc82b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2477
-3141
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ test-suite cardano-chain-gen
192192
, transformers-except
193193
, tree-diff
194194
, tasty-hunit
195-
, monad-logger
196195
, ouroboros-consensus
197196
, ouroboros-consensus-cardano
198197
, ouroboros-network-api

cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ import Control.Concurrent.STM.TMVar (
7474
import Control.Exception (SomeException, bracket)
7575
import Control.Monad (void)
7676
import Control.Monad.Extra (eitherM)
77-
import Control.Monad.Logger (NoLoggingT)
7877
import Control.Monad.Trans.Except.Extra (runExceptT)
7978
import Control.Tracer (nullTracer)
8079
import Data.Text (Text)
@@ -230,9 +229,9 @@ withDBSyncEnv mkEnv = bracket mkEnv stopDBSyncIfRunning
230229
getDBSyncPGPass :: DBSyncEnv -> DB.PGPassSource
231230
getDBSyncPGPass = enpPGPassSource . dbSyncParams
232231

233-
queryDBSync :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> IO a
232+
queryDBSync :: DBSyncEnv -> DB.DbM a -> IO a
234233
queryDBSync env = do
235-
DB.runWithConnectionNoLogging (getDBSyncPGPass env)
234+
DB.runDbMTransactionNoLogging (getDBSyncPGPass env)
236235

237236
getPoolLayer :: DBSyncEnv -> IO PoolDataLayer
238237
getPoolLayer env = do

cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ module Test.Cardano.Db.Mock.Validate (
4646
import Control.Concurrent
4747
import Control.Exception
4848
import Control.Monad (forM_)
49-
import Control.Monad.Logger (NoLoggingT)
5049
import Data.Bifunctor (first)
5150
import Data.ByteString (ByteString)
5251
import Data.Either (isRight)
@@ -69,7 +68,6 @@ import qualified Cardano.Ledger.Core as Core
6968
import Cardano.Ledger.Shelley.LedgerState (EraCertState)
7069
import Cardano.Mock.Forging.Tx.Generic
7170
import Cardano.Mock.Forging.Types
72-
import Cardano.Prelude (MonadIO)
7371
import Cardano.SMASH.Server.PoolDataLayer
7472
import Cardano.SMASH.Server.Types
7573
import Test.Cardano.Db.Mock.Config
@@ -129,16 +127,16 @@ assertUnspentTx dbSyncEnv = do
129127
defaultDelays :: [Int]
130128
defaultDelays = [1, 2, 4, 8, 16, 32, 64, 128, 256]
131129

132-
assertEqQuery :: (Eq a, Show a) => DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> a -> String -> IO ()
130+
assertEqQuery :: (Eq a, Show a) => DBSyncEnv -> DB.DbM a -> a -> String -> IO ()
133131
assertEqQuery env query a msg = do
134132
assertEqBackoff env query a defaultDelays msg
135133

136-
assertEqBackoff :: (Eq a, Show a) => DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> a -> [Int] -> String -> IO ()
134+
assertEqBackoff :: (Eq a, Show a) => DBSyncEnv -> DB.DbM a -> a -> [Int] -> String -> IO ()
137135
assertEqBackoff env query a delays msg = do
138136
checkStillRuns env
139137
assertBackoff env query delays (== a) (\a' -> msg <> ": got " <> show a' <> " expected " <> show a)
140138

141-
assertBackoff :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> [Int] -> (a -> Bool) -> (a -> String) -> IO ()
139+
assertBackoff :: DBSyncEnv -> DB.DbM a -> [Int] -> (a -> Bool) -> (a -> String) -> IO ()
142140
assertBackoff env query delays check errMsg = go delays
143141
where
144142
go ds = do
@@ -150,7 +148,7 @@ assertBackoff env query delays check errMsg = go delays
150148
threadDelay $ dl * 100_000
151149
go rest
152150

153-
assertQuery :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> (a -> Bool) -> (a -> String) -> IO (Maybe String)
151+
assertQuery :: DBSyncEnv -> DB.DbM a -> (a -> Bool) -> (a -> String) -> IO (Maybe String)
154152
assertQuery env query check errMsg = do
155153
ma <- try @DB.DbError $ queryDBSync env query
156154
case ma of
@@ -161,7 +159,7 @@ assertQuery env query check errMsg = do
161159
Right a | not (check a) -> pure $ Just $ errMsg a
162160
_ -> pure Nothing
163161

164-
runQuery :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> IO a
162+
runQuery :: DBSyncEnv -> DB.DbM a -> IO a
165163
runQuery env query = do
166164
ma <- try @DB.DbError $ queryDBSync env query
167165
case ma of
@@ -371,7 +369,7 @@ assertPoolCounters :: DBSyncEnv -> (Word64, Word64, Word64, Word64, Word64, Word
371369
assertPoolCounters env expected =
372370
assertEqBackoff env poolCountersQuery expected defaultDelays "Unexpected Pool counts"
373371

374-
poolCountersQuery :: MonadIO m => DB.DbAction m (Word64, Word64, Word64, Word64, Word64, Word64)
372+
poolCountersQuery :: DB.DbM (Word64, Word64, Word64, Word64, Word64, Word64)
375373
poolCountersQuery = do
376374
poolHash <- DB.queryPoolHashCount
377375
poolMetadataRef <- DB.queryPoolMetadataRefCount

cardano-db-sync/app/test-http-get-json-metadata.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import Cardano.DbSync.Types (
1616
OffChainUrlType (..),
1717
)
1818
import Control.Monad (foldM)
19-
import Control.Monad.IO.Class (MonadIO)
2019
import Control.Monad.Trans.Except.Extra (runExceptT)
2120
import Data.ByteString.Char8 (ByteString)
2221
import qualified Data.List as List
@@ -32,7 +31,7 @@ import Network.HTTP.Client.TLS (tlsManagerSettings)
3231
main :: IO ()
3332
main = do
3433
manager <- Http.newManager tlsManagerSettings
35-
xs <- DB.runDbNoLoggingEnv queryTestOffChainData
34+
xs <- DB.runDbMTransactionNoLogging DB.PGPassDefaultEnv queryTestOffChainData
3635
putStrLn $ "testOffChainPoolDataFetch: " ++ show (length xs) ++ " tests to run."
3736
tfs <- foldM (testOne manager) emptyTestFailure xs
3837
reportTestFailures tfs
@@ -74,7 +73,7 @@ data TestFailure = TestFailure
7473
, tfOtherError :: !Word
7574
}
7675

77-
queryTestOffChainData :: MonadIO m => DB.DbAction m [TestOffChain]
76+
queryTestOffChainData :: DB.DbM [TestOffChain]
7877
queryTestOffChainData = do
7978
res <- DB.queryTestOffChainData
8079
pure . organise $ map convert res

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ library
187187
, lifted-base
188188
, memory
189189
, microlens
190-
, monad-control
190+
-- , monad-control
191191
, network-mux
192192
, ouroboros-consensus
193193
, ouroboros-consensus-cardano
@@ -215,6 +215,7 @@ library
215215
, transformers
216216
, transformers-except
217217
, typed-protocols
218+
, unliftio-core
218219
, vector
219220
, wide-word
220221
, yaml

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
208208
pool <- liftIO $ DB.createHasqlConnectionPool [dbConnSetting] 4 -- 4 connections for reasonable parallelism
209209
let dbEnv =
210210
if isLogingEnabled
211-
then DB.createDbEnv dbConn pool (Just trce)
212-
else DB.createDbEnv dbConn pool Nothing
211+
then DB.createDbEnv dbConn (Just pool) (Just trce)
212+
else DB.createDbEnv dbConn (Just pool) Nothing
213213
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
214214
isJsonbInSchema <- liftDbError $ DB.queryJsonbInSchemaExists dbConn
215215
logProtocolMagicId trce $ genesisProtocolMagicId genCfg

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ runConsumedTxOutMigrationsMaybe syncEnv = do
143143
let pcm = getPruneConsume syncEnv
144144
txOutVariantType = getTxOutVariantType syncEnv
145145
logInfo (getTrace syncEnv) $ "runConsumedTxOutMigrationsMaybe: " <> textShow pcm
146-
DB.runDbIohkNoLogging (envDbEnv syncEnv) $
146+
DB.runDbTransactionIohkNoLogging (envDbEnv syncEnv) $
147147
DB.runConsumedTxOutMigrations
148148
(getTrace syncEnv)
149149
maxBulkSize
@@ -153,11 +153,11 @@ runConsumedTxOutMigrationsMaybe syncEnv = do
153153

154154
runAddJsonbToSchema :: SyncEnv -> IO ()
155155
runAddJsonbToSchema syncEnv =
156-
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.enableJsonbInSchema
156+
void $ DB.runDbTransactionIohkNoLogging (envDbEnv syncEnv) DB.enableJsonbInSchema
157157

158158
runRemoveJsonbFromSchema :: SyncEnv -> IO ()
159159
runRemoveJsonbFromSchema syncEnv =
160-
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.disableJsonbInSchema
160+
void $ DB.runDbTransactionIohkNoLogging (envDbEnv syncEnv) DB.disableJsonbInSchema
161161

162162
getSafeBlockNoDiff :: SyncEnv -> Word64
163163
getSafeBlockNoDiff syncEnv = 2 * getSecurityParam syncEnv
@@ -243,7 +243,7 @@ getInsertOptions :: SyncEnv -> InsertOptions
243243
getInsertOptions = soptInsertOptions . envOptions
244244

245245
getSlotHash :: DB.DbEnv -> SlotNo -> IO [(SlotNo, ByteString)]
246-
getSlotHash backend = DB.runDbIohkNoLogging backend . DB.querySlotHash
246+
getSlotHash backend = DB.runDbTransactionIohkNoLogging backend . DB.querySlotHash
247247

248248
hasLedgerState :: SyncEnv -> Bool
249249
hasLedgerState syncEnv =
@@ -254,7 +254,7 @@ hasLedgerState syncEnv =
254254
getDbLatestBlockInfo :: DB.DbEnv -> IO (Maybe TipInfo)
255255
getDbLatestBlockInfo dbEnv = do
256256
runMaybeT $ do
257-
block <- MaybeT $ DB.runDbIohkNoLogging dbEnv DB.queryLatestBlock
257+
block <- MaybeT $ DB.runDbTransactionIohkNoLogging dbEnv DB.queryLatestBlock
258258
-- The EpochNo, SlotNo and BlockNo can only be zero for the Byron
259259
-- era, but we need to make the types match, hence `fromMaybe`.
260260
pure $
@@ -309,7 +309,7 @@ mkSyncEnv ::
309309
RunMigration ->
310310
IO SyncEnv
311311
mkSyncEnv trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc = do
312-
dbCNamesVar <- newTVarIO =<< DB.runDbActionIO dbEnv DB.queryRewardAndEpochStakeConstraints
312+
dbCNamesVar <- newTVarIO =<< DB.runDbTransactionIohkNoLogging dbEnv DB.queryRewardAndEpochStakeConstraints
313313
cache <-
314314
if soptCache syncOptions
315315
then
@@ -434,7 +434,7 @@ getLatestPoints env = do
434434
verifySnapshotPoint env snapshotPoints
435435
NoLedger _ -> do
436436
-- Brings the 5 latest.
437-
lastPoints <- DB.runDbIohkNoLogging (envDbEnv env) DB.queryLatestPoints
437+
lastPoints <- DB.runDbTransactionIohkNoLogging (envDbEnv env) DB.queryLatestPoints
438438
pure $ mapMaybe convert lastPoints
439439
where
440440
convert (Nothing, _) = Nothing
@@ -489,7 +489,7 @@ getBootstrapInProgress ::
489489
DB.DbEnv ->
490490
IO Bool
491491
getBootstrapInProgress trce bootstrapFlag dbEnv = do
492-
DB.runDbIohkNoLogging dbEnv $ do
492+
DB.runDbTransactionIohkNoLogging dbEnv $ do
493493
ems <- DB.queryAllExtraMigrations
494494
let btsState = DB.bootstrapState ems
495495
case (bootstrapFlag, btsState) of

cardano-db-sync/src/Cardano/DbSync/Api/Ledger.hs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import Cardano.Ledger.Core (Value)
1616
import Cardano.Ledger.Mary.Value
1717
import Cardano.Ledger.Shelley.LedgerState
1818
import Cardano.Ledger.TxIn
19-
import Cardano.Prelude (textShow, throwIO)
19+
import Cardano.Prelude (ExceptT, lift, textShow, throwIO)
2020
import Control.Concurrent.Class.MonadSTM.Strict (atomically, readTVarIO, writeTVar)
2121
import Control.Monad.Extra
22-
import Control.Monad.IO.Class (MonadIO, liftIO)
22+
import Control.Monad.IO.Class (liftIO)
2323
import Data.List.Extra
2424
import Data.Map (Map)
2525
import qualified Data.Map.Strict as Map
@@ -39,34 +39,33 @@ import Cardano.DbSync.Era.Shelley.Generic.Tx.Types (DBPlutusScript)
3939
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
4040
import Cardano.DbSync.Era.Universal.Insert.Grouped
4141
import Cardano.DbSync.Era.Universal.Insert.Tx (insertTxOut)
42+
import Cardano.DbSync.Error (SyncNodeError)
4243
import Cardano.DbSync.Ledger.State
4344
import Cardano.DbSync.Types
4445
import Cardano.DbSync.Util (maxBulkSize)
4546

4647
bootStrapMaybe ::
47-
MonadIO m =>
4848
SyncEnv ->
49-
DB.DbAction m ()
49+
ExceptT SyncNodeError DB.DbM ()
5050
bootStrapMaybe syncEnv = do
5151
bts <- liftIO $ readTVarIO (envBootstrap syncEnv)
5252
when bts $ migrateBootstrapUTxO syncEnv
5353

5454
migrateBootstrapUTxO ::
55-
MonadIO m =>
5655
SyncEnv ->
57-
DB.DbAction m ()
56+
ExceptT SyncNodeError DB.DbM ()
5857
migrateBootstrapUTxO syncEnv = do
5958
case envLedgerEnv syncEnv of
6059
HasLedger lenv -> do
6160
liftIO $ logInfo trce "Starting UTxO bootstrap migration"
6261
cls <- liftIO $ readCurrentStateUnsafe lenv
63-
count <- DB.deleteTxOut (getTxOutVariantType syncEnv)
62+
count <- lift $ DB.deleteTxOut (getTxOutVariantType syncEnv)
6463
when (count > 0) $
6564
liftIO $
6665
logWarning trce $
6766
"Found and deleted " <> textShow count <> " tx_out."
6867
storeUTxOFromLedger syncEnv cls
69-
DB.insertExtraMigration DB.BootstrapFinished
68+
lift $ DB.insertExtraMigration DB.BootstrapFinished
7069
liftIO $ logInfo trce "UTxO bootstrap migration done"
7170
liftIO $ atomically $ writeTVar (envBootstrap syncEnv) False
7271
NoLedger _ ->
@@ -75,10 +74,9 @@ migrateBootstrapUTxO syncEnv = do
7574
trce = getTrace syncEnv
7675

7776
storeUTxOFromLedger ::
78-
MonadIO m =>
7977
SyncEnv ->
8078
ExtLedgerState CardanoBlock ->
81-
DB.DbAction m ()
79+
ExceptT SyncNodeError DB.DbM ()
8280
storeUTxOFromLedger env st = case ledgerState st of
8381
LedgerStateBabbage bts -> storeUTxO env (getUTxO bts)
8482
LedgerStateConway stc -> storeUTxO env (getUTxO stc)
@@ -93,13 +91,12 @@ storeUTxO ::
9391
, Script era ~ AlonzoScript era
9492
, TxOut era ~ BabbageTxOut era
9593
, BabbageEraTxOut era
96-
, MonadIO m
9794
, DBPlutusScript era
9895
, NativeScript era ~ Timelock era
9996
) =>
10097
SyncEnv ->
10198
Map TxIn (BabbageTxOut era) ->
102-
DB.DbAction m ()
99+
ExceptT SyncNodeError DB.DbM ()
103100
storeUTxO env mp = do
104101
liftIO $
105102
logInfo trce $
@@ -123,18 +120,17 @@ storePage ::
123120
, DBPlutusScript era
124121
, BabbageEraTxOut era
125122
, NativeScript era ~ Timelock era
126-
, MonadIO m
127123
) =>
128124
SyncEnv ->
129125
Float ->
130126
(Int, [(TxIn, BabbageTxOut era)]) ->
131-
DB.DbAction m ()
127+
ExceptT SyncNodeError DB.DbM ()
132128
storePage syncEnv percQuantum (n, ls) = do
133129
when (n `mod` 10 == 0) $ liftIO $ logInfo trce $ "Bootstrap in progress " <> prc <> "%"
134130
txOuts <- mapM (prepareTxOut syncEnv) ls
135-
txOutIds <- DB.insertBulkTxOut False $ etoTxOut . fst <$> txOuts
131+
txOutIds <- lift $ DB.insertBulkTxOut False $ etoTxOut . fst <$> txOuts
136132
let maTxOuts = concatMap (mkmaTxOuts txOutVariantType) $ zip txOutIds (snd <$> txOuts)
137-
void $ DB.insertBulkMaTxOut maTxOuts
133+
void . lift $ DB.insertBulkMaTxOut maTxOuts
138134
where
139135
txOutVariantType = getTxOutVariantType syncEnv
140136
trce = getTrace syncEnv
@@ -145,13 +141,12 @@ prepareTxOut ::
145141
, Script era ~ AlonzoScript era
146142
, TxOut era ~ BabbageTxOut era
147143
, BabbageEraTxOut era
148-
, MonadIO m
149144
, DBPlutusScript era
150145
, NativeScript era ~ Timelock era
151146
) =>
152147
SyncEnv ->
153148
(TxIn, BabbageTxOut era) ->
154-
DB.DbAction m (ExtendedTxOut, [MissingMaTxOut])
149+
ExceptT SyncNodeError DB.DbM (ExtendedTxOut, [MissingMaTxOut])
155150
prepareTxOut syncEnv (TxIn txIntxId (TxIx index), txOut) = do
156151
let txHashByteString = Generic.safeHashToByteString $ unTxId txIntxId
157152
let genTxOut = fromTxOut (fromIntegral index) txOut

0 commit comments

Comments
 (0)