Skip to content

Commit 137bfca

Browse files
committed
fix restart and transactions
1 parent 79563b6 commit 137bfca

File tree

32 files changed

+305
-234
lines changed

32 files changed

+305
-234
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
6666
import Cardano.DbSync.Era.Shelley.Generic.Util
6767
import qualified Cardano.Ledger.Address as Ledger
6868
import Cardano.Ledger.BaseTypes
69-
import Cardano.Ledger.Shelley.LedgerState (EraCertState)
7069
import qualified Cardano.Ledger.Core as Core
70+
import Cardano.Ledger.Shelley.LedgerState (EraCertState)
7171
import Cardano.Mock.Forging.Tx.Generic
7272
import Cardano.Mock.Forging.Types
7373
import Cardano.Prelude (MonadIO)

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Cardano.DbSync (
2525
extractSyncOptions,
2626
) where
2727

28+
import Control.Concurrent.Async
2829
import Control.Monad.Extra (whenJust)
2930
import qualified Data.Strict.Maybe as Strict
3031
import qualified Data.Text as Text
@@ -36,7 +37,6 @@ import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
3637
import Paths_cardano_db_sync (version)
3738
import System.Directory (createDirectoryIfMissing)
3839
import Prelude (id)
39-
import Control.Concurrent.Async
4040

4141
import Cardano.BM.Trace (Trace, logError, logInfo, logWarning)
4242
import qualified Cardano.Crypto as Crypto
@@ -240,14 +240,18 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runDelayedMigrationFnc syncN
240240

241241
-- communication channel between datalayer thread and chainsync-client thread
242242
threadChannels <- liftIO newThreadChannels
243-
liftIO $ race_
244-
(runDbThread syncEnv metricsSetters threadChannels) -- Main App thread
245-
(mapConcurrently_ id [ -- Non-critical threads
246-
runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams),
247-
runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile,
248-
runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile,
249-
runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)
250-
])
243+
liftIO $
244+
race_
245+
(runDbThread syncEnv metricsSetters threadChannels) -- Main App thread
246+
( mapConcurrently_
247+
id
248+
[ -- Non-critical threads
249+
runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)
250+
, runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile
251+
, runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile
252+
, runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)
253+
]
254+
)
251255
)
252256
where
253257
useShelleyInit :: SyncNodeConfig -> Bool

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Cardano.Ledger.Core (Value)
2727
import Cardano.Ledger.Mary.Value
2828
import Cardano.Ledger.Shelley.LedgerState
2929
import Cardano.Ledger.TxIn
30-
import Cardano.Prelude (textShow, MonadError (..))
30+
import Cardano.Prelude (MonadError (..), textShow)
3131
import Control.Concurrent.Class.MonadSTM.Strict (atomically, readTVarIO, writeTVar)
3232
import Control.Monad.Extra
3333
import Control.Monad.IO.Class (MonadIO, liftIO)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ queryPoolKeyWithCache cache cacheUA hsh =
242242
liftIO $ missPools (cStats ci)
243243
mPhId <- DB.queryPoolHashId (Generic.unKeyHashRaw hsh)
244244
case mPhId of
245-
Nothing -> throwError $ DB.DbError (DB.mkDbCallStack "queryPoolKeyWithCache") "ActiveCache queryPoolHashId" Nothing
245+
Nothing -> pure $ Left $ DB.DbError (DB.mkDbCallStack "queryPoolKeyWithCache") "ActiveCache queryPoolHashId" Nothing
246246
Just phId -> do
247247
-- missed so we can't evict even with 'EvictAndReturn'
248248
when (shouldCache cacheUA) $
@@ -412,7 +412,7 @@ queryMAWithCache cache policyId asset =
412412
queryTxIdWithCacheEither ::
413413
MonadIO m =>
414414
CacheStatus ->
415-
Ledger.TxId -> -- Use the original input type
415+
Ledger.TxId -> -- Use the original input type
416416
DB.DbAction m (Either DB.DbError DB.TxId)
417417
queryTxIdWithCacheEither cache txIdLedger = do
418418
case cache of
@@ -441,7 +441,7 @@ queryTxIdWithCacheEither cache txIdLedger = do
441441
-- Return lookup failure.
442442
Left err -> pure $ Left err
443443
where
444-
txHash = Generic.unTxHash txIdLedger -- Convert to ByteString for DB query
444+
txHash = Generic.unTxHash txIdLedger -- Convert to ByteString for DB query
445445
qTxHash = do
446446
result <- DB.queryTxId txHash
447447
case result of
@@ -514,8 +514,13 @@ queryTxIdWithCache cache txIdLedger = do
514514
result <- DB.queryTxId txHash
515515
case result of
516516
Just txId -> pure $ Right txId
517-
Nothing -> pure $ Left $ DB.DbError (DB.mkDbCallStack "queryTxIdWithCacheEither")
518-
("TxId not found for hash: " <> textShow txHash) Nothing
517+
Nothing ->
518+
pure $
519+
Left $
520+
DB.DbError
521+
(DB.mkDbCallStack "queryTxIdWithCacheEither")
522+
("TxId not found for hash: " <> textShow txHash)
523+
Nothing
519524

520525
tryUpdateCacheTx ::
521526
MonadIO m =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ textShowStats (ActiveCache ic) = do
152152
, textLruSection "Multi Assets" mAssets (multiAssetsHits stats) (multiAssetsQueries stats)
153153
, textPrevBlockSection stats
154154
, textFifoSection "TxId" txIds (txIdsHits stats) (txIdsQueries stats)
155+
, "\n-----------------------------------------------------------------"
155156
]
156157
where
157158
textCacheSection title cacheLru cacheStable hits queries =

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Ouroboros.Consensus.Cardano.Block (HardForkBlock (..))
1919
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
2020
import Ouroboros.Network.Block (blockHash, blockNo, getHeaderFields, headerFieldBlockNo, unBlockNo)
2121

22-
import Cardano.BM.Trace (logInfo, Trace)
22+
import Cardano.BM.Trace (Trace, logInfo)
2323
import qualified Cardano.Db as DB
2424
import Cardano.DbSync.Api
2525
import Cardano.DbSync.Api.Ledger
@@ -76,11 +76,12 @@ applyAndInsertBlockMaybe syncEnv tracer cblk = do
7676
-- equal, insert the block and restore consistency between ledger and db.
7777
case eiBlockInDbAlreadyId of
7878
Left _ -> do
79-
liftIO . logInfo tracer $ mconcat
80-
[ "Received block which is not in the db with "
81-
, textShow (getHeaderFields cblk)
82-
, ". Time to restore consistency."
83-
]
79+
liftIO . logInfo tracer $
80+
mconcat
81+
[ "Received block which is not in the db with "
82+
, textShow (getHeaderFields cblk)
83+
, ". Time to restore consistency."
84+
]
8485
lift $ rollbackFromBlockNo syncEnv (blockNo cblk)
8586
lift $ insertBlock syncEnv cblk applyRes True tookSnapshot
8687
liftIO $ setConsistentLevel syncEnv Consistent
@@ -198,7 +199,7 @@ insertBlock syncEnv cblk applyRes firstAfterRollback tookSnapshot = do
198199
commitOrIndexes withinTwoMin withinHalfHour = do
199200
commited <-
200201
if withinTwoMin || tookSnapshot
201-
then do pure True
202+
then pure True
202203
else pure False
203204
when withinHalfHour $ do
204205
bootStrapMaybe syncEnv

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ insertByronTx syncEnv blkId tx blockIndex = do
279279
where
280280
iopts = getInsertOptions syncEnv
281281

282-
283282
insertByronTx' ::
284283
(MonadIO m) =>
285284
SyncEnv ->
@@ -289,7 +288,11 @@ insertByronTx' ::
289288
DB.DbAction m Word64
290289
insertByronTx' syncEnv blkId tx blockIndex = do
291290
-- Resolve all transaction inputs - any failure will throw via MonadError
292-
resolvedInputs <- mapM (resolveTxInputsByron txOutVariantType) (toList $ Byron.txInputs (Byron.taTx tx))
291+
resolvedResults <- mapM (resolveTxInputsByron txOutVariantType) (toList $ Byron.txInputs (Byron.taTx tx))
292+
293+
resolvedInputs <- case sequence resolvedResults of
294+
Right inputs -> pure inputs
295+
Left dbErr -> throwError dbErr
293296

294297
-- Calculate transaction fee
295298
valFee <- case calculateTxFee (Byron.taTx tx) resolvedInputs of
@@ -435,14 +438,17 @@ insertTxIn _tracer txInTxId (Byron.TxInUtxo _txHash inIndex, txOutTxId, _, _) =
435438

436439
-------------------------------------------------------------------------------
437440

438-
resolveTxInputsByron :: (MonadIO m) => DB.TxOutVariantType -> Byron.TxIn -> DB.DbAction m (Byron.TxIn, DB.TxId, DB.TxOutIdW, DbLovelace)
441+
resolveTxInputsByron ::
442+
(MonadIO m) =>
443+
DB.TxOutVariantType ->
444+
Byron.TxIn ->
445+
DB.DbAction m (Either DB.DbError (Byron.TxIn, DB.TxId, DB.TxOutIdW, DbLovelace))
439446
resolveTxInputsByron txOutVariantType txIn@(Byron.TxInUtxo txHash index) = do
440447
result <- DB.queryTxOutIdValueEither txOutVariantType (Byron.unTxHash txHash, fromIntegral index)
441-
case result of
442-
Right res -> pure $ convert res
443-
Left dbErr -> throwError dbErr -- Use MonadError instead of Either
448+
pure $ case result of
449+
Right res -> Right $ convert res
450+
Left dbErr -> Left dbErr -- Return Either instead of throwing
444451
where
445-
convert :: (DB.TxId, DB.TxOutIdW, DbLovelace) -> (Byron.TxIn, DB.TxId, DB.TxOutIdW, DbLovelace)
446452
convert (txId, txOutId, lovelace) = (txIn, txId, txOutId, lovelace)
447453

448454
calculateTxFee :: Byron.Tx -> [(Byron.TxIn, DB.TxId, DB.TxOutIdW, DbLovelace)] -> Either SyncNodeError ValueFee

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ insertValidateShelleyGenesisDist syncEnv networkName cfg shelleyInitiation = do
152152
, DB.blockSize = 0
153153
, DB.blockTime = configStartTime cfg
154154
, DB.blockTxCount = expectedTxCount
155-
-- Genesis block does not have a protocol version, so set this to '0'.
156-
, DB.blockProtoMajor = 0
155+
, -- Genesis block does not have a protocol version, so set this to '0'.
156+
DB.blockProtoMajor = 0
157157
, DB.blockProtoMinor = 0
158158
, DB.blockVrfKey = Nothing
159159
, DB.blockOpCert = Nothing
@@ -180,8 +180,9 @@ validateGenesisDistribution syncEnv prunes networkName cfg bid expectedTxCount =
180180
metaMaybe <- DB.queryMeta
181181
meta <- case metaMaybe of
182182
Just m -> pure m
183-
Nothing -> throwError $
184-
DB.DbError dbCallStack "Meta table is empty during validation - this should not happen" Nothing
183+
Nothing ->
184+
throwError $
185+
DB.DbError dbCallStack "Meta table is empty during validation - this should not happen" Nothing
185186

186187
when (DB.metaStartTime meta /= configStartTime cfg) $
187188
throwError $

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ resolveTxInputs syncEnv hasConsumed needsValue groupedOutputs txIn = do
204204
case (resolveInMemory txIn groupedOutputs, hasConsumed, needsValue) of
205205
(Nothing, _, _) ->
206206
-- Only throw if in-memory resolution also fails
207-
throwError $ DB.DbError (DB.mkDbCallStack "resolveTxInputs")
208-
("TxOut not found for TxIn: " <> textShow txIn) Nothing
207+
throwError $
208+
DB.DbError
209+
(DB.mkDbCallStack "resolveTxInputs")
210+
("TxOut not found for TxIn: " <> textShow txIn)
211+
Nothing
209212
(Just eutxo, True, True) ->
210213
pure $ convertFoundValue (etoTxOut eutxo)
211214
(Just eutxo, _, _) ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile = do
243243
tDelay
244244
-- load the offChain vote work queue using the db
245245
_ <-
246-
runExceptT $ DB.runDbIohkLoggingEither trce dbEnv $
246+
DB.runDbIohkLoggingEither trce dbEnv $
247247
loadOffChainPoolWorkQueue trce (envOffChainPoolWorkQueue threadSyncEnv)
248248
poolq <- atomically $ flushTBQueue (envOffChainPoolWorkQueue threadSyncEnv)
249249
manager <- Http.newManager tlsManagerSettings
@@ -280,7 +280,7 @@ runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile = do
280280
tDelay
281281
-- load the offChain vote work queue using the db
282282
_ <-
283-
runExceptT $ DB.runDbIohkLoggingEither trce dbEnv $
283+
DB.runDbIohkLoggingEither trce dbEnv $
284284
loadOffChainVoteWorkQueue trce (envOffChainVoteWorkQueue threadSyncEnv)
285285
voteq <- atomically $ flushTBQueue (envOffChainVoteWorkQueue threadSyncEnv)
286286
now <- liftIO Time.getPOSIXTime

0 commit comments

Comments
 (0)