Skip to content

Commit 79563b6

Browse files
committed
fix timestamp conversion and rebase errors
1 parent a17b6ae commit 79563b6

40 files changed

+1365
-961
lines changed

cabal.project

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ repository cardano-haskell-packages
1010
d4a35cd3121aa00d18544bb0ac01c3e1691d618f462c46129271bccf39f7e8ee
1111

1212
index-state:
13-
<<<<<<< HEAD
1413
, hackage.haskell.org 2025-05-23T06:30:40Z
1514
, cardano-haskell-packages 2025-05-16T20:03:45Z
16-
=======
17-
, hackage.haskell.org 2025-02-05T12:01:20Z
18-
, cardano-haskell-packages 2025-02-04T11:56:25Z
19-
>>>>>>> 38627e31 (make a start to stetting up hasql with db pools)
2015

2116
packages:
2217
cardano-db

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import qualified Data.Text as Text
3131
import Data.Version (showVersion)
3232
import qualified Hasql.Connection as HsqlC
3333
import qualified Hasql.Connection.Setting as HsqlSet
34-
import qualified Ouroboros.Consensus.HardFork.Simple as HardFork
34+
import Ouroboros.Consensus.Cardano (CardanoHardForkTrigger (..))
3535
import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
3636
import Paths_cardano_db_sync (version)
3737
import System.Directory (createDirectoryIfMissing)
3838
import Prelude (id)
39+
import Control.Concurrent.Async
3940

4041
import Cardano.BM.Trace (Trace, logError, logInfo, logWarning)
4142
import qualified Cardano.Crypto as Crypto
@@ -58,7 +59,6 @@ import Cardano.DbSync.Tracing.ToObjectOrphans ()
5859
import Cardano.DbSync.Types
5960
import Cardano.Prelude hiding (Nat, (%))
6061
import Cardano.Slotting.Slot (EpochNo (..))
61-
import Control.Concurrent.Async
6262

6363
runDbSyncNode :: MetricSetters -> [(Text, Text)] -> SyncNodeParams -> SyncNodeConfig -> IO ()
6464
runDbSyncNode metricsSetters knownMigrations params syncNodeConfigFromFile =
@@ -240,15 +240,14 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runDelayedMigrationFnc syncN
240240

241241
-- communication channel between datalayer thread and chainsync-client thread
242242
threadChannels <- liftIO newThreadChannels
243-
liftIO $
244-
mapConcurrently_
245-
id
246-
[ runDbThread syncEnv metricsSetters threadChannels
247-
, runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)
248-
, runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile
249-
, runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile
250-
, runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)
251-
]
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+
])
252251
)
253252
where
254253
useShelleyInit :: SyncNodeConfig -> Bool

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

Lines changed: 5 additions & 2 deletions
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)
30+
import Cardano.Prelude (textShow, MonadError (..))
3131
import Control.Concurrent.Class.MonadSTM.Strict (atomically, readTVarIO, writeTVar)
3232
import Control.Monad.Extra
3333
import Control.Monad.IO.Class (MonadIO, liftIO)
@@ -156,7 +156,10 @@ prepareTxOut ::
156156
prepareTxOut syncEnv (TxIn txIntxId (TxIx index), txOut) = do
157157
let txHashByteString = Generic.safeHashToByteString $ unTxId txIntxId
158158
let genTxOut = fromTxOut (fromIntegral index) txOut
159-
txId <- queryTxIdWithCache cache txIntxId
159+
eTxId <- queryTxIdWithCache cache txIntxId
160+
txId <- case eTxId of
161+
Left err -> throwError err
162+
Right tid -> pure tid
160163
insertTxOut trce cache iopts (txId, txHashByteString) genTxOut
161164
where
162165
trce = getTrace syncEnv

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

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,46 @@ queryMAWithCache cache policyId asset =
407407
let !assetNameBs = Generic.unAssetName asset
408408
maybe (Left (policyBs, assetNameBs)) Right <$> DB.queryMultiAssetId policyBs assetNameBs
409409

410+
-- CORRECT VERSION - match the original cache behavior exactly:
411+
410412
queryTxIdWithCacheEither ::
411413
MonadIO m =>
412414
CacheStatus ->
413-
Ledger.TxId StandardCrypto ->
415+
Ledger.TxId -> -- Use the original input type
414416
DB.DbAction m (Either DB.DbError DB.TxId)
415417
queryTxIdWithCacheEither cache txIdLedger = do
416-
catchError
417-
(Right <$> queryTxIdWithCache cache txIdLedger)
418-
(pure . Left)
418+
case cache of
419+
-- Direct database query if no cache.
420+
NoCache -> qTxHash
421+
ActiveCache ci ->
422+
withCacheOptimisationCheck ci qTxHash $ do
423+
-- Read current cache state.
424+
cacheTx <- liftIO $ readTVarIO (cTxIds ci)
425+
426+
case FIFO.lookup txIdLedger cacheTx of
427+
-- Cache hit, return the transaction ID.
428+
Just txId -> do
429+
liftIO $ hitTxIds (cStats ci)
430+
pure $ Right txId
431+
-- Cache miss.
432+
Nothing -> do
433+
eTxId <- qTxHash
434+
liftIO $ missTxIds (cStats ci)
435+
case eTxId of
436+
Right txId -> do
437+
-- Update cache.
438+
liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO.insert txIdLedger txId
439+
-- Return ID after updating cache.
440+
pure $ Right txId
441+
-- Return lookup failure.
442+
Left err -> pure $ Left err
443+
where
444+
txHash = Generic.unTxHash txIdLedger -- Convert to ByteString for DB query
445+
qTxHash = do
446+
result <- DB.queryTxId txHash
447+
case result of
448+
Just txId -> pure $ Right txId
449+
Nothing -> pure $ Left $ DB.DbError (DB.mkDbCallStack "queryTxIdWithCacheEither") "TxId not found" Nothing
419450

420451
queryPrevBlockWithCache ::
421452
MonadIO m =>
@@ -450,7 +481,7 @@ queryTxIdWithCache ::
450481
MonadIO m =>
451482
CacheStatus ->
452483
Ledger.TxId ->
453-
DB.DbAction m DB.TxId
484+
DB.DbAction m (Either DB.DbError DB.TxId)
454485
queryTxIdWithCache cache txIdLedger = do
455486
case cache of
456487
-- Direct database query if no cache.
@@ -459,22 +490,32 @@ queryTxIdWithCache cache txIdLedger = do
459490
withCacheOptimisationCheck ci qTxHash $ do
460491
-- Read current cache state.
461492
cacheTx <- liftIO $ readTVarIO (cTxIds ci)
493+
462494
case FIFO.lookup txIdLedger cacheTx of
463495
-- Cache hit, return the transaction ID.
464496
Just txId -> do
465497
liftIO $ hitTxIds (cStats ci)
466-
pure txId
498+
pure $ Right txId
467499
-- Cache miss.
468500
Nothing -> do
469-
txId <- qTxHash
501+
eTxId <- qTxHash
470502
liftIO $ missTxIds (cStats ci)
471-
-- Update cache.
472-
liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO.insert txIdLedger txId
473-
-- Return ID after updating cache.
474-
pure txId
503+
case eTxId of
504+
Right txId -> do
505+
-- Update cache ONLY on successful lookup.
506+
liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO.insert txIdLedger txId
507+
-- Return ID after updating cache.
508+
pure $ Right txId
509+
-- Return lookup failure - DON'T update cache.
510+
Left err -> pure $ Left err
475511
where
476512
txHash = Generic.unTxHash txIdLedger
477-
qTxHash = DB.queryTxId txHash
513+
qTxHash = do
514+
result <- DB.queryTxId txHash
515+
case result of
516+
Just txId -> pure $ Right txId
517+
Nothing -> pure $ Left $ DB.DbError (DB.mkDbCallStack "queryTxIdWithCacheEither")
518+
("TxId not found for hash: " <> textShow txHash) Nothing
478519

479520
tryUpdateCacheTx ::
480521
MonadIO m =>

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

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
{-# LANGUAGE NoImplicitPrelude #-}
55

66
module Cardano.DbSync.Database (
7-
DbEvent (..),
8-
ThreadChannels,
9-
lengthDbEventQueue,
10-
mkDbApply,
117
runDbThread,
128
) where
139

1410
import Cardano.BM.Trace (logDebug, logError, logInfo)
1511
import Cardano.DbSync.Api
16-
import Cardano.DbSync.Api.Types (ConsistentLevel (..), LedgerEnv (..), SyncEnv (..))
12+
import Cardano.DbSync.Api.Types (ConsistentLevel (..), SyncEnv (..))
1713
import Cardano.DbSync.DbEvent
1814
import Cardano.DbSync.Default
1915
import Cardano.DbSync.Error
2016
import Cardano.DbSync.Ledger.State
21-
import Cardano.DbSync.Ledger.Types (CardanoLedgerState (..), SnapshotPoint (..))
2217
import Cardano.DbSync.Metrics
2318
import Cardano.DbSync.Rollback
2419
import Cardano.DbSync.Types
@@ -27,9 +22,6 @@ import Cardano.Prelude hiding (atomically)
2722
import Cardano.Slotting.Slot (WithOrigin (..))
2823
import Control.Concurrent.Class.MonadSTM.Strict
2924
import Control.Monad.Extra (whenJust)
30-
import Control.Monad.Trans.Except.Extra (newExceptT)
31-
import Ouroboros.Consensus.HeaderValidation hiding (TipInfo)
32-
import Ouroboros.Consensus.Ledger.Extended
3325
import Ouroboros.Network.Block (BlockNo, Point (..))
3426
import Ouroboros.Network.Point (blockPointHash, blockPointSlot)
3527

@@ -75,7 +67,7 @@ runDbThread syncEnv metricsSetters queue = do
7567

7668
-- Handle the result of running the actions
7769
case result of
78-
Left err -> logError tracer $ "Error: " <> show err
70+
Left err -> logError tracer $ show err
7971
Right Continue -> processQueue -- Continue processing
8072
Right Done -> pure () -- Stop processing
8173

@@ -114,55 +106,31 @@ runActions syncEnv actions = do
114106
([], DbFinish : _) -> do
115107
pure Done
116108
([], DbRollBackToPoint chainSyncPoint serverTip resultVar : ys) -> do
117-
deletedAllBlocks <- prepareRollback syncEnv chainSyncPoint serverTip
109+
-- Fix: prepareRollback now returns IO (Either SyncNodeError Bool), so use ExceptT
110+
deletedAllBlocks <- ExceptT $ prepareRollback syncEnv chainSyncPoint serverTip
118111
points <- lift $ rollbackLedger syncEnv chainSyncPoint
119112

120-
-- Ledger state always rollbacks at least back to the 'point' given by the Node.
121-
-- It needs to rollback even further, if 'points' is not 'Nothing'.
122-
-- The db may not rollback to the Node point.
113+
-- Keep the same logic as before for consistency levels
123114
case (deletedAllBlocks, points) of
124115
(True, Nothing) -> do
125116
liftIO $ setConsistentLevel syncEnv Consistent
126117
liftIO $ validateConsistentLevel syncEnv chainSyncPoint
127118
(False, Nothing) -> do
128119
liftIO $ setConsistentLevel syncEnv DBAheadOfLedger
129120
liftIO $ validateConsistentLevel syncEnv chainSyncPoint
130-
_anyOtherOption ->
121+
_anyOtherOption -> do
131122
-- No need to validate here
132123
liftIO $ setConsistentLevel syncEnv DBAheadOfLedger
133124
blockNo <- lift $ getDbTipBlockNo syncEnv
134125
lift $ atomically $ putTMVar resultVar (points, blockNo)
135126
dbEvent Continue ys
136127
(ys, zs) -> do
137-
newExceptT $ insertListBlocks syncEnv ys
128+
-- Fix: insertListBlocks now returns IO (Either SyncNodeError ()), so use ExceptT
129+
ExceptT $ insertListBlocks syncEnv ys
138130
if null zs
139131
then pure Continue
140132
else dbEvent Continue zs
141133

142-
rollbackLedger :: SyncEnv -> CardanoPoint -> IO (Maybe [CardanoPoint])
143-
rollbackLedger syncEnv point =
144-
case envLedgerEnv syncEnv of
145-
HasLedger hle -> do
146-
mst <- loadLedgerAtPoint hle point
147-
case mst of
148-
Right st -> do
149-
let statePoint = headerStatePoint $ headerState $ clsState st
150-
-- This is an extra validation that should always succeed.
151-
unless (point == statePoint) $
152-
logAndThrowIO (getTrace syncEnv) $
153-
SNErrDatabaseRollBackLedger $
154-
mconcat
155-
[ "Ledger "
156-
, show statePoint
157-
, " and ChainSync "
158-
, show point
159-
, " don't match."
160-
]
161-
pure Nothing
162-
Left lsfs ->
163-
Just . fmap fst <$> verifySnapshotPoint syncEnv (OnDisk <$> lsfs)
164-
NoLedger _ -> pure Nothing
165-
166134
-- | This not only checks that the ledger and ChainSync points are equal, but also that the
167135
-- 'Consistent' Level is correct based on the db tip.
168136
validateConsistentLevel :: SyncEnv -> CardanoPoint -> IO ()

0 commit comments

Comments
 (0)