Skip to content

Commit 269f26b

Browse files
committed
2086 - fix manual rollbacks
1 parent 7c634df commit 269f26b

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

  • cardano-db/src/Cardano/Db/Statement

cardano-db/src/Cardano/Db/Statement/Base.hs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -857,17 +857,41 @@ deleteBlocksSlotNo ::
857857
Bool ->
858858
DbM Bool
859859
deleteBlocksSlotNo trce txOutVariantType (SlotNo slotNo) isConsumedTxOut = do
860-
blockEpochE <- queryNearestBlockSlotNo slotNo
861-
case blockEpochE of
862-
Nothing -> pure False
863-
(Just (blockId, epochN)) -> do
864-
-- Delete the block and return whether it was successful
865-
deleteCount <- deleteBlocksBlockId trce txOutVariantType blockId epochN isConsumedTxOut
866-
if deleteCount > 0
867-
then pure True
868-
else do
869-
liftIO $ logWarning trce $ "deleteBlocksSlotNo: No blocks found for slot: " <> Text.pack (show slotNo)
860+
mBlock <- queryNearestBlockSlotNo slotNo
861+
case mBlock of
862+
Nothing -> do
863+
liftIO $ logWarning trce $ "deleteBlocksSlotNo: No block found at or after slot: " <> textShow slotNo
864+
pure False
865+
Just (blockId, blockNo) -> do
866+
-- Get the correct epoch number (same approach as rollbackFromBlockNo)
867+
mBlockAndEpoch <- queryBlockNoAndEpoch blockNo
868+
case mBlockAndEpoch of
869+
Nothing -> do
870+
liftIO $ logWarning trce $ "deleteBlocksSlotNo: Block not found for block_no: " <> textShow blockNo
870871
pure False
872+
Just (_, epochNo) -> do
873+
nBlocks <- queryBlockCountAfterBlockNo blockNo True
874+
liftIO $
875+
logInfo trce $
876+
"Rollback: Deleting "
877+
<> textShow nBlocks
878+
<> " blocks with block_no >= "
879+
<> textShow blockNo
880+
<> " (slot >= "
881+
<> textShow slotNo
882+
<> ", epoch "
883+
<> textShow epochNo
884+
<> ")"
885+
886+
deleteCount <- deleteBlocksBlockId trce txOutVariantType blockId epochNo isConsumedTxOut
887+
888+
if deleteCount > 0
889+
then do
890+
liftIO $ logInfo trce $ "Rollback: Successfully deleted " <> textShow deleteCount <> " blocks"
891+
pure True
892+
else do
893+
liftIO $ logWarning trce $ "deleteBlocksSlotNo: No blocks were deleted for slot: " <> textShow slotNo
894+
pure False
871895

872896
--------------------------------------------------------------------------------
873897
deleteBlocksSlotNoNoTrace :: TxOutVariantType -> SlotNo -> DbM Bool

0 commit comments

Comments
 (0)