Skip to content

Commit 4c402d4

Browse files
committed
db-sync: Fix a race condition maintaining ledger state
When blocks and rollback notifications arrive via the chainsync protocol, they are put in a queue and then a separate thread reads from the queue and operates on the DB. When ledger-state management was added, code to apply a block to a ledger state was added at the read end of the queue, but the rollback handling was incorrectly added at the write end of the queue. The solution to the problem is trivial, move the ledger state rollback handling from the write end of the queue to the read end. Closes: https://github.com/input-output-hk/cardano-db-sync/issue/398
1 parent 8f7a5cc commit 4c402d4

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ dbSyncProtocols trce env plugin queryVar ledgerVar _version codecs _connectionId
237237
(cChainSyncCodec codecs)
238238
channel
239239
(chainSyncClientPeerPipelined
240-
$ chainSyncClient trce env queryVar ledgerVar metrics latestPoints currentTip actionQueue)
240+
$ chainSyncClient trce env queryVar metrics latestPoints currentTip actionQueue)
241241
)
242242
atomically $ writeDbActionQueue actionQueue DbFinish
243243
cancel server
@@ -317,9 +317,9 @@ getCurrentTipBlockNo = do
317317
chainSyncClient
318318
:: Trace IO Text -> DbSyncEnv
319319
-> StateQueryTMVar CardanoBlock (Interpreter (CardanoEras StandardCrypto))
320-
-> LedgerStateVar -> Metrics -> [Point CardanoBlock] -> WithOrigin BlockNo -> DbActionQueue
320+
-> Metrics -> [Point CardanoBlock] -> WithOrigin BlockNo -> DbActionQueue
321321
-> ChainSyncClientPipelined CardanoBlock (Tip CardanoBlock) IO ()
322-
chainSyncClient trce env queryVar ledgerVar metrics latestPoints currentTip actionQueue =
322+
chainSyncClient trce env queryVar metrics latestPoints currentTip actionQueue =
323323
ChainSyncClientPipelined $ pure $
324324
-- Notify the core node about the our latest points at which we are
325325
-- synchronised. This client is not persistent and thus it just
@@ -373,7 +373,6 @@ chainSyncClient trce env queryVar ledgerVar metrics latestPoints currentTip acti
373373
-- but will only be incorrect for a short time span.
374374
let slot = toRollbackSlot point
375375
atomically $ writeDbActionQueue actionQueue (mkDbRollback slot)
376-
loadLedgerState (envLedgerStateDir env) ledgerVar slot
377376
newTip <- getCurrentTipBlockNo
378377
pure $ finish newTip tip
379378
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ runActions trce env plugin ledgerState actions = do
9393
case spanDbApply xs of
9494
([], DbFinish:_) -> do
9595
pure Done
96-
([], DbRollBackToPoint pt:ys) -> do
97-
runRollbacks trce plugin pt
96+
([], DbRollBackToPoint sn:ys) -> do
97+
runRollbacks trce plugin sn
98+
liftIO $ loadLedgerState (envLedgerStateDir env) ledgerState sn
9899
dbAction Continue ys
99100
(ys, zs) -> do
100101
insertBlockList trce env ledgerState plugin ys

0 commit comments

Comments
 (0)