Skip to content

Commit ff8b787

Browse files
authored
Merge pull request IntersectMBO#1200 from input-output-hk/erikd/check-ledger-state-file-count
Add an error message when no ledger state files are found
2 parents bf9c342 + 1a5f5db commit ff8b787

File tree

4 files changed

+38
-43
lines changed

4 files changed

+38
-43
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Cardano.DbSync.Util
5050
import Control.Monad.Class.MonadSTM.Strict (StrictTVar, TBQueue, newTBQueueIO, newTVarIO,
5151
readTVar, readTVarIO, writeTVar)
5252
import Control.Monad.Trans.Maybe (MaybeT (..))
53+
5354
import qualified Data.Strict.Maybe as Strict
5455
import Data.Time.Clock (UTCTime, getCurrentTime)
5556

@@ -160,12 +161,10 @@ getDbLatestBlockInfo backend = do
160161
}
161162

162163
getDbTipBlockNo :: SyncEnv -> IO (Point.WithOrigin BlockNo)
163-
getDbTipBlockNo env = do
164-
backend <- getBackend env
165-
maybeTip <- getDbLatestBlockInfo backend
166-
case maybeTip of
167-
Just tip -> pure $ Point.At (bBlockNo tip)
168-
Nothing -> pure Point.Origin
164+
getDbTipBlockNo env =
165+
getBackend env >>=
166+
getDbLatestBlockInfo <&>
167+
maybe Point.Origin (Point.At . bBlockNo)
169168

170169
logDbState :: SyncEnv -> IO ()
171170
logDbState env = do
@@ -245,17 +244,18 @@ mkSyncEnvFromConfig trce connSring syncOptions dir genCfg =
245244

246245
getLatestPoints :: SyncEnv -> IO [CardanoPoint]
247246
getLatestPoints env = do
248-
if hasLedgerState env then do
249-
files <- listLedgerStateFilesOrdered $ leDir (envLedger env)
250-
verifyFilePoints env files
251-
else do
252-
-- Brings the 5 latest.
253-
dbBackend <- getBackend env
254-
lastPoints <- DB.runDbIohkNoLogging dbBackend DB.queryLatestPoints
255-
pure $ mapMaybe convert' lastPoints
247+
if hasLedgerState env
248+
then do
249+
files <- listLedgerStateFilesOrdered $ leDir (envLedger env)
250+
verifyFilePoints env files
251+
else do
252+
-- Brings the 5 latest.
253+
dbBackend <- getBackend env
254+
lastPoints <- DB.runDbIohkNoLogging dbBackend DB.queryLatestPoints
255+
pure $ mapMaybe convert lastPoints
256256
where
257-
convert' (Nothing, _) = Nothing
258-
convert' (Just slot, bs) = convert (SlotNo slot) bs
257+
convert (Nothing, _) = Nothing
258+
convert (Just slot, bs) = convertToPoint (SlotNo slot) bs
259259

260260
verifyFilePoints :: SyncEnv -> [LedgerStateFile] -> IO [CardanoPoint]
261261
verifyFilePoints env files =
@@ -267,11 +267,11 @@ verifyFilePoints env files =
267267
hashes <- getSlotHash backend (lsfSlotNo lsf)
268268
let valid = find (\(_, h) -> lsfHash lsf == hashToAnnotation h) hashes
269269
case valid of
270-
Just (slot, hash) | slot == lsfSlotNo lsf -> pure $ convert slot hash
270+
Just (slot, hash) | slot == lsfSlotNo lsf -> pure $ convertToPoint slot hash
271271
_ -> pure Nothing
272272

273-
convert :: SlotNo -> ByteString -> Maybe CardanoPoint
274-
convert slot hashBlob =
273+
convertToPoint :: SlotNo -> ByteString -> Maybe CardanoPoint
274+
convertToPoint slot hashBlob =
275275
Point . Point.block slot <$> convertHashBlob hashBlob
276276
where
277277
convertHashBlob :: ByteString -> Maybe (HeaderHash CardanoBlock)

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Cardano.DbSync.Default
2727
import Cardano.DbSync.Error
2828
import Cardano.DbSync.LedgerState
2929
import Cardano.DbSync.Metrics
30+
import Cardano.DbSync.Rollback (rollbackToPoint)
3031
import Cardano.DbSync.Types
3132
import Cardano.DbSync.Util hiding (whenJust)
3233

@@ -89,15 +90,15 @@ runActions env actions = do
8990
([], DbFinish:_) -> do
9091
pure Done
9192
([], DbRollBackToPoint pt resultVar : ys) -> do
92-
runRollbacksDB env pt
93+
newExceptT $ rollbackToPoint env pt
9394
points <- if hasLedgerState env
9495
then lift $ rollbackLedger env pt
9596
else pure Nothing
9697
blockNo <- lift $ getDbTipBlockNo env
9798
lift $ atomically $ putTMVar resultVar (points, blockNo)
9899
dbAction Continue ys
99100
(ys, zs) -> do
100-
insertBlockList env ys
101+
newExceptT $ insertListBlocks env ys
101102
if null zs
102103
then pure Continue
103104
else dbAction Continue zs
@@ -118,7 +119,6 @@ rollbackLedger env point = do
118119
Left lsfs ->
119120
Just <$> verifyFilePoints env lsfs
120121
where
121-
122122
checkDBWithState :: CardanoPoint -> Maybe TipInfo -> IO ()
123123
checkDBWithState pnt dbTipInfo =
124124
if compareTips pnt dbTipInfo
@@ -134,22 +134,10 @@ compareTips = go
134134
where
135135
go (Point Origin) Nothing = True
136136
go (Point (At blk)) (Just tip) =
137-
getHeaderHash (blockPointHash blk) == bHash tip
138-
&& blockPointSlot blk == bSlotNo tip
137+
getHeaderHash (blockPointHash blk) == bHash tip
138+
&& blockPointSlot blk == bSlotNo tip
139139
go _ _ = False
140140

141-
runRollbacksDB
142-
:: SyncEnv -> CardanoPoint
143-
-> ExceptT SyncNodeError IO ()
144-
runRollbacksDB env point =
145-
newExceptT $ rollbackToPoint env point
146-
147-
insertBlockList
148-
:: SyncEnv -> [CardanoBlock]
149-
-> ExceptT SyncNodeError IO ()
150-
insertBlockList env blks =
151-
newExceptT $ insertListBlocks env blks
152-
153141
-- | Split the DbAction list into a prefix containing blocks to apply and a postfix.
154142
spanDbApply :: [DbAction] -> ([CardanoBlock], [DbAction])
155143
spanDbApply lst =

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
{-# LANGUAGE OverloadedStrings #-}
66
module Cardano.DbSync.Default
77
( insertListBlocks
8-
, rollbackToPoint
98
) where
109

1110

@@ -29,7 +28,6 @@ import Cardano.DbSync.Error
2928
import Cardano.DbSync.LedgerState (ApplyResult (..), LedgerEvent (..),
3029
applyBlockAndSnapshot, defaultApplyResult)
3130
import Cardano.DbSync.LocalStateQuery
32-
import Cardano.DbSync.Rollback (rollbackToPoint)
3331
import Cardano.DbSync.Types
3432
import Cardano.DbSync.Util
3533

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import Cardano.Prelude hiding (Meta, Nat, option, (%))
3030
import Control.Tracer (Tracer)
3131

3232
import Cardano.BM.Data.Tracer (ToLogObject (..), ToObject)
33-
import Cardano.BM.Trace (Trace, appendName, logInfo)
33+
import Cardano.BM.Trace (Trace, appendName, logError, logInfo)
3434
import qualified Cardano.BM.Trace as Logging
3535

3636
import Cardano.Client.Subscription (subscribe)
3737
import qualified Cardano.Crypto as Crypto
3838

39-
import Cardano.Slotting.Slot (EpochNo (..), WithOrigin (..))
39+
import Cardano.Slotting.Slot (EpochNo (..), WithOrigin (..), withOriginToMaybe)
4040

4141
import qualified Cardano.Db as Db
4242

@@ -231,6 +231,15 @@ dbSyncProtocols trce env metricsSetters _version codecs _connectionId =
231231
-- threads
232232
latestPoints <- getLatestPoints env
233233
currentTip <- getCurrentTipBlockNo env
234+
235+
when (null latestPoints && currentTip /= Origin) $ do
236+
logError trce $ mconcat
237+
[ "No ledger state files found, but current tip is "
238+
, textShow (withOriginToMaybe currentTip)
239+
]
240+
logError trce "This will require that the database is rolled back to genesis."
241+
panicAbort "This is probably not what you want."
242+
234243
logDbState env
235244
-- communication channel between datalayer thread and chainsync-client thread
236245
actionQueue <- newDbActionQueue
@@ -293,16 +302,16 @@ chainSyncClient metricsSetters trce latestPoints currentTip actionQueue = do
293302
clientPipelinedStIdle
294303
:: WithOrigin BlockNo -> [CardanoPoint]
295304
-> ClientPipelinedStIdle 'Z CardanoBlock (Point CardanoBlock) (Tip CardanoBlock) IO ()
296-
clientPipelinedStIdle clintTip points =
305+
clientPipelinedStIdle clientTip points =
297306
-- Notify the core node about the our latest points at which we are
298307
-- synchronised. This client is not persistent and thus it just
299308
-- synchronises from the genesis block. A real implementation should send
300309
-- a list of points up to a point which is k blocks deep.
301310
SendMsgFindIntersect
302311
(if null points then [genesisPoint] else points)
303312
ClientPipelinedStIntersect
304-
{ recvMsgIntersectFound = \ _hdr tip -> pure $ go policy Zero clintTip (getTipBlockNo tip) Nothing
305-
, recvMsgIntersectNotFound = \tip -> pure $ goTip policy Zero clintTip tip Nothing
313+
{ recvMsgIntersectFound = \ _hdr tip -> pure $ go policy Zero clientTip (getTipBlockNo tip) Nothing
314+
, recvMsgIntersectNotFound = \tip -> pure $ goTip policy Zero clientTip tip Nothing
306315
}
307316

308317
policy :: MkPipelineDecision

0 commit comments

Comments
 (0)