Skip to content

Commit e6ba129

Browse files
committed
update entity, convert runs and make a start on queries
1 parent d6b698e commit e6ba129

File tree

26 files changed

+2115
-2028
lines changed

26 files changed

+2115
-2028
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
5757
import Paths_cardano_db_sync (version)
5858
import System.Directory (createDirectoryIfMissing)
5959
import Prelude (id)
60-
import Hasql.Connection as HC
60+
import qualified Hasql.Connection as HsqlC
6161

6262
runDbSyncNode :: MetricSetters -> [(Text, Text)] -> SyncNodeParams -> SyncNodeConfig -> IO ()
6363
runDbSyncNode metricsSetters knownMigrations params syncNodeConfigFromFile =
@@ -113,8 +113,7 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
113113
then logInfo trce "All user indexes were created"
114114
else logInfo trce "New user indexes were not created. They may be created later if necessary."
115115

116-
let setting = Db.toConnectionSetting pgConfig
117-
116+
let dbConnectionSetting = Db.toConnectionSetting pgConfig
118117

119118
-- For testing and debugging.
120119
whenJust (enpMaybeRollback params) $ \slotNo ->
@@ -123,7 +122,7 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
123122
metricsSetters
124123
trce
125124
iomgr
126-
connectionString
125+
dbConnectionSetting
127126
(void . runMigration)
128127
syncNodeConfigFromFile
129128
params
@@ -150,6 +149,7 @@ runSyncNode ::
150149
MetricSetters ->
151150
Trace IO Text ->
152151
IOManager ->
152+
-- | Database connection settings
153153
Setting ->
154154
-- | run migration function
155155
RunMigration ->
@@ -168,11 +168,11 @@ runSyncNode metricsSetters trce iomgr connSetting runMigrationFnc syncNodeConfig
168168
let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
169169
-- Our main thread
170170
bracket
171-
(runOrThrowIO $ HC.acquire [connSetting])
171+
(runOrThrowIO $ HsqlC.acquire [dbConnSetting])
172172
release
173-
(\connection -> do
173+
(\dbConn -> do
174174
runOrThrowIO $ runExceptT $ do
175-
let dbEnv = Db.DbEnv connection (dncEnableDbLogging syncNodeConfigFromFile)
175+
let dbEnv = Db.DbEnv dbConn (dncEnableDbLogging syncNodeConfigFromFile)
176176
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
177177
isJsonbInSchema <- queryIsJsonbInSchema dbEnv
178178
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
@@ -119,7 +119,7 @@ getIsConsumedFixed env =
119119
where
120120
txOutTableType = getTxOutTableType env
121121
pcm = soptPruneConsumeMigration $ envOptions env
122-
backend = envBackend env
122+
backend = envDbEnv env
123123

124124
getDisableInOutState :: SyncEnv -> IO Bool
125125
getDisableInOutState syncEnv = do
@@ -172,7 +172,7 @@ runRemoveJsonbFromSchema
172172
=> SyncEnv
173173
-> DbAction e m ()
174174
runRemoveJsonbFromSchema syncEnv = do
175-
DB.runDbTx DB.Write transx
175+
DB.runDbT DB.Write transx
176176
where
177177
dbEnv = envDbEnv syncEnv
178178
transx = mkDbTransaction "runRemoveJsonbFromSchema" mkCallSite (DB.disableJsonbInSchema (dbConnection dbEnv))
@@ -285,12 +285,12 @@ getDbLatestBlockInfo backend = do
285285

286286
getDbTipBlockNo :: SyncEnv -> IO (Point.WithOrigin BlockNo)
287287
getDbTipBlockNo env = do
288-
mblk <- getDbLatestBlockInfo (envBackend env)
288+
mblk <- getDbLatestBlockInfo (envDbEnv env)
289289
pure $ maybe Point.Origin (Point.At . bBlockNo) mblk
290290

291291
logDbState :: SyncEnv -> IO ()
292292
logDbState env = do
293-
mblk <- getDbLatestBlockInfo (envBackend env)
293+
mblk <- getDbLatestBlockInfo (envDbEnv env)
294294
case mblk of
295295
Nothing -> logInfo tracer "Database is empty"
296296
Just tip -> logInfo tracer $ mconcat ["Database tip is at ", showTip tip]
@@ -309,7 +309,7 @@ logDbState env = do
309309

310310
getCurrentTipBlockNo :: SyncEnv -> IO (WithOrigin BlockNo)
311311
getCurrentTipBlockNo env = do
312-
maybeTip <- getDbLatestBlockInfo (envBackend env)
312+
maybeTip <- getDbLatestBlockInfo (envDbEnv env)
313313
case maybeTip of
314314
Just tip -> pure $ At (bBlockNo tip)
315315
Nothing -> pure Origin
@@ -512,7 +512,7 @@ getLatestPoints env = do
512512
verifySnapshotPoint env snapshotPoints
513513
NoLedger _ -> do
514514
-- Brings the 5 latest.
515-
lastPoints <- DB.runDbIohkNoLogging (envBackend env) DB.queryLatestPoints
515+
lastPoints <- DB.runDbIohkNoLogging (envDbEnv env) DB.queryLatestPoints
516516
pure $ mapMaybe convert lastPoints
517517
where
518518
convert (Nothing, _) = Nothing
@@ -524,7 +524,7 @@ verifySnapshotPoint env snapPoints =
524524
where
525525
validLedgerFileToPoint :: SnapshotPoint -> IO (Maybe (CardanoPoint, Bool))
526526
validLedgerFileToPoint (OnDisk lsf) = do
527-
hashes <- getSlotHash (envBackend env) (lsfSlotNo lsf)
527+
hashes <- getSlotHash (envDbEnv env) (lsfSlotNo lsf)
528528
let valid = find (\(_, h) -> lsfHash lsf == hashToAnnotation h) hashes
529529
case valid of
530530
Just (slot, hash) | slot == lsfSlotNo lsf -> pure $ convertToDiskPoint slot hash
@@ -533,7 +533,7 @@ verifySnapshotPoint env snapPoints =
533533
case pnt of
534534
GenesisPoint -> pure Nothing
535535
BlockPoint slotNo hsh -> do
536-
hashes <- getSlotHash (envBackend env) slotNo
536+
hashes <- getSlotHash (envDbEnv env) slotNo
537537
let valid = find (\(_, dbHash) -> getHeaderHash hsh == dbHash) hashes
538538
case valid of
539539
Just (dbSlotNo, _) | slotNo == dbSlotNo -> pure $ Just (pnt, True)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ insertListBlocks ::
5151
[CardanoBlock] ->
5252
IO (Either SyncNodeError ())
5353
insertListBlocks synEnv blocks = do
54-
DB.runDbIohkLogging (envBackend synEnv) tracer
54+
DB.runDbIohkLogging (envDbEnv synEnv) tracer
5555
. runExceptT
5656
$ traverse_ (applyAndInsertBlockMaybe synEnv tracer) blocks
5757
where

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/GovAction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ insertCommitteeHash cred = do
335335
insertDrep :: (MonadBaseControl IO m, MonadIO m) => DRep StandardCrypto -> ReaderT SqlBackend m DB.DrepHashId
336336
insertDrep = \case
337337
DRepCredential cred -> insertCredDrepHash cred
338-
DRepAlwaysAbstain -> DB.insertAlwaysAbstainDrep
338+
DRepAlwaysAbstain -> DB.insertDrepHashAlwaysAbstain
339339
DRepAlwaysNoConfidence -> DB.insertAlwaysNoConfidence
340340

341341
insertCredDrepHash :: (MonadBaseControl IO m, MonadIO m) => Ledger.Credential 'DRepRole StandardCrypto -> ReaderT SqlBackend m DB.DrepHashId

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ data SyncInvariant
4242

4343
data SyncNodeError
4444
= SNErrDefault !Text
45-
| SNErrDbTransaction !DB.DbError
45+
| SNErrDatabase !DB.DbError
4646
| SNErrInvariant !Text !SyncInvariant
4747
| SNEErrBlockMismatch !Word64 !ByteString !ByteString
4848
| SNErrIgnoreShelleyInitiation
@@ -67,7 +67,7 @@ instance Show SyncNodeError where
6767
show =
6868
\case
6969
SNErrDefault t -> "Error SNErrDefault: " <> show t
70-
SNErrDbTransaction err -> "Error SNErrDbTransaction: " <> show err
70+
SNErrDatabase err -> "Error SNErrDatabase: " <> show err
7171
SNErrInvariant loc i -> "Error SNErrInvariant: " <> Show.show loc <> ": " <> show (renderSyncInvariant i)
7272
SNEErrBlockMismatch blkNo hashDb hashBlk ->
7373
mconcat

cardano-db/src/Cardano/Db/Error.hs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,80 @@ module Cardano.Db.Error (
77
AsDbError (..),
88
CallSite (..),
99
DbError (..),
10-
LookupFail (..),
1110
runOrThrowIODb,
11+
runOrThrowIO,
1212
logAndThrowIO,
13+
base16encode
1314
) where
1415

1516
import Cardano.BM.Trace (Trace, logError)
1617
import Cardano.Db.Schema.Ids
17-
import Cardano.Prelude (throwIO)
18+
import Cardano.Prelude (throwIO, MonadIO)
1819
import Control.Exception (Exception)
1920
import Data.ByteString.Char8 (ByteString)
2021
import Data.Text (Text)
2122
import Data.Word (Word16, Word64)
2223
import GHC.Generics (Generic)
2324
import qualified Data.ByteString.Base16 as Base16
2425
import qualified Data.Text.Encoding as Text
25-
import qualified Hasql.Session as HasqlS
26+
import qualified Hasql.Session as HsqlS
27+
2628

2729
class AsDbError e where
2830
toDbError :: DbError -> e
2931
fromDbError :: e -> Maybe DbError
3032

3133
data DbError
32-
= QueryError !Text !CallSite !HasqlS.SessionError
33-
| DecodingError !Text !CallSite !HasqlS.RowError
34-
| ConnectionError !Text !CallSite
35-
| TransactionError !Text !CallSite
34+
= DbError !CallSite !Text !HsqlS.SessionError
35+
| DbLookupError !CallSite !Text !LookupContext
3636
deriving (Show, Eq)
3737

38+
instance Exception DbError
39+
3840
data CallSite = CallSite
3941
{ csModule :: !Text
4042
, csFile :: !Text
4143
, csLine :: !Int
4244
} deriving (Show, Eq)
4345

44-
data LookupFail
45-
= DbLookupBlockHash !ByteString
46-
| DbLookupBlockId !Word64
47-
| DbLookupMessage !Text
48-
| DbLookupTxHash !ByteString
49-
| DbLookupTxOutPair !ByteString !Word16
50-
| DbLookupEpochNo !Word64
51-
| DbLookupSlotNo !Word64
52-
| DbLookupGovActionPair !TxId !Word64
53-
| DbMetaEmpty
54-
| DbMetaMultipleRows
55-
| DBMultipleGenesis
56-
| DBExtraMigration !String
57-
| DBPruneConsumed !String
58-
| DBRJsonbInSchema !String
59-
| DBTxOutVariant !String
60-
deriving (Eq, Generic)
46+
data LookupContext
47+
= BlockHashContext !ByteString
48+
| BlockIdContext !Word64
49+
| MessageContext !Text
50+
| TxHashContext !ByteString
51+
| TxOutPairContext !ByteString !Word16
52+
| EpochNoContext !Word64
53+
| SlotNoContext !Word64
54+
| GovActionPairContext !TxId !Word64
55+
| MetaEmptyContext
56+
| MetaMultipleRowsContext
57+
| MultipleGenesisContext
58+
| ExtraMigrationContext !String
59+
| PruneConsumedContext !String
60+
| RJsonbInSchemaContext !String
61+
| TxOutVariantContext !String
62+
deriving (Show, Eq, Generic)
6163

62-
instance Exception LookupFail
64+
instance Exception LookupContext
6365

64-
instance Show LookupFail where
65-
show =
66-
\case
67-
DbLookupBlockHash h -> "The block hash " <> show (base16encode h) <> " is missing from the DB."
68-
DbLookupBlockId blkid -> "block id " <> show blkid
69-
DbLookupMessage txt -> show txt
70-
DbLookupTxHash h -> "tx hash " <> show (base16encode h)
71-
DbLookupTxOutPair h i -> concat ["tx out pair (", show $ base16encode h, ", ", show i, ")"]
72-
DbLookupEpochNo e -> "epoch number " ++ show e
73-
DbLookupSlotNo s -> "slot number " ++ show s
74-
DbLookupGovActionPair txId index -> concat ["missing GovAction (", show txId, ", ", show index, ")"]
75-
DbMetaEmpty -> "Meta table is empty"
76-
DbMetaMultipleRows -> "Multiple rows in Meta table which should only contain one"
77-
DBMultipleGenesis -> "Multiple Genesis blocks found. These are blocks without an EpochNo"
78-
DBExtraMigration e -> "DBExtraMigration : " <> e
79-
DBPruneConsumed e -> "DBExtraMigration" <> e
80-
DBRJsonbInSchema e -> "DBRJsonbInSchema" <> e
81-
DBTxOutVariant e -> "DbTxOutVariant" <> e
66+
-- instance Show LookupFail where
67+
-- show =
68+
-- \case
69+
-- DbLookupBlockHash h -> "The block hash " <> show (base16encode h) <> " is missing from the DB."
70+
-- DbLookupBlockId blkid -> "block id " <> show blkid
71+
-- DbLookupMessage txt -> show txt
72+
-- DbLookupTxHash h -> "tx hash " <> show (base16encode h)
73+
-- DbLookupTxOutPair h i -> concat ["tx out pair (", show $ base16encode h, ", ", show i, ")"]
74+
-- DbLookupEpochNo e -> "epoch number " ++ show e
75+
-- DbLookupSlotNo s -> "slot number " ++ show s
76+
-- DbLookupGovActionPair txId index -> concat ["missing GovAction (", show txId, ", ", show index, ")"]
77+
-- DbMetaEmpty -> "Meta table is empty"
78+
-- DbMetaMultipleRows -> "Multiple rows in Meta table which should only contain one"
79+
-- DBMultipleGenesis -> "Multiple Genesis blocks found. These are blocks without an EpochNo"
80+
-- DBExtraMigration e -> "DBExtraMigration : " <> e
81+
-- DBPruneConsumed e -> "DBExtraMigration" <> e
82+
-- DBRJsonbInSchema e -> "DBRJsonbInSchema" <> e
83+
-- DBTxOutVariant e -> "DbTxOutVariant" <> e
8284

8385
base16encode :: ByteString -> Text
8486
base16encode = Text.decodeUtf8 . Base16.encode
@@ -90,6 +92,13 @@ runOrThrowIODb ioEither = do
9092
Left err -> throwIO err
9193
Right a -> pure a
9294

95+
runOrThrowIO :: forall e a m. (MonadIO m) => (Exception e) => m (Either e a) -> m a
96+
runOrThrowIO ioEither = do
97+
et <- ioEither
98+
case et of
99+
Left err -> throwIO err
100+
Right a -> pure a
101+
93102
logAndThrowIO :: Trace IO Text -> Text -> IO a
94103
logAndThrowIO tracer msg = do
95104
logError tracer msg

0 commit comments

Comments
 (0)