Skip to content

Commit 01bc85a

Browse files
authored
Merge pull request #2089 from IntersectMBO/2086-rollback-db-tools
2086 - fix manual rollbacks
2 parents 7c634df + 1200ae2 commit 01bc85a

4 files changed

Lines changed: 54 additions & 19 deletions

File tree

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

doc/Readme.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ This directory contains various documentation files for setting up, configuring,
3232

3333
14. [Syncing and Rollbacks](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/syncing-and-rollbacks.md) - Details on the syncing procedure and handling rollbacks, explaining how the node syncs with the blockchain and manages rollbacks in case of errors or inconsistencies.
3434

35-
15. [Community Tools](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/community-tools.md) - Information on various community tools like Koios and Blockfrost, providing an overview of these tools, their features, and how they can be used to interact with Cardano DB Sync.
35+
15. [Manual Rollbacks](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/manual-rollbacks.md) - Guide to performing manual rollbacks using cardano-db-sync or cardano-db-tool, including use cases, step-by-step instructions, and best practices for rolling back the database to fix incorrect data or recover from issues.
3636

37-
16. [Interesting Queries](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/interesting-queries.md) - A collection of useful SQL queries for interacting with the database, including examples of queries for retrieving data, analyzing transactions, and generating reports.
37+
16. [Community Tools](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/community-tools.md) - Information on various community tools like Koios and Blockfrost, providing an overview of these tools, their features, and how they can be used to interact with Cardano DB Sync.
3838

39-
17. [Troubleshooting](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/troubleshooting.md) - Common issues and troubleshooting steps for Cardano DB Sync, providing solutions for various problems that users may encounter while running the node.
39+
17. [Interesting Queries](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/interesting-queries.md) - A collection of useful SQL queries for interacting with the database, including examples of queries for retrieving data, analyzing transactions, and generating reports.
4040

41-
18. [Release Process](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/release-process.md) - Detailed process for releasing new versions of Cardano DB Sync, covering the steps required to prepare, test, and publish a new release.
41+
18. [Troubleshooting](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/troubleshooting.md) - Common issues and troubleshooting steps for Cardano DB Sync, providing solutions for various problems that users may encounter while running the node.
4242

43-
19. [State Snapshot](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/state-snapshot.md) - Guide to creating and restoring state snapshots, explaining how to take snapshots of the database state and restore them when needed.
43+
19. [Release Process](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/release-process.md) - Detailed process for releasing new versions of Cardano DB Sync, covering the steps required to prepare, test, and publish a new release.
4444

45-
20. [Pool OffChain Data](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/pool-offchain-data.md) - Handling off-chain data for staking pools, providing details on managing off-chain data and integrating it with the Cardano DB Sync Node.
45+
20. [State Snapshot](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/state-snapshot.md) - Guide to creating and restoring state snapshots, explaining how to take snapshots of the database state and restore them when needed.
4646

47-
21. [SMASH](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/smash.md) - Information on the Stakepool Metadata Aggregation Server (SMASH), explaining the purpose of SMASH, how it works, and how to set it up.
47+
21. [Pool OffChain Data](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/pool-offchain-data.md) - Handling off-chain data for staking pools, providing details on managing off-chain data and integrating it with the Cardano DB Sync Node.
4848

49-
22. [HLint and Stylish Haskell](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/hlint-stylish-haskell.md) - Setting up `hlint` and `stylish-haskell` for code linting and formatting, providing instructions on configuring these tools to maintain code quality and consistency.
49+
22. [SMASH](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/smash.md) - Information on the Stakepool Metadata Aggregation Server (SMASH), explaining the purpose of SMASH, how it works, and how to set it up.
50+
51+
23. [HLint and Stylish Haskell](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/hlint-stylish-haskell.md) - Setting up `hlint` and `stylish-haskell` for code linting and formatting, providing instructions on configuring these tools to maintain code quality and consistency.

doc/command-line-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- `--force-indexes`: Forces the Index creation at the start of db-sync. Normally they're created later.
1212
- `--fix-only`: Runs only the db-sync fix procedure for the wrong datum, redeemer_data, and plutus script bytes and exits.
1313
- `--disable-cache`: Disables the db-sync caches. Reduces memory usage but it takes longer to sync.
14-
- `--rollback-to-slot SLOTNO`: Force a rollback to the specified slot, if the given slot doesn't exist it will use the next greater slot.
14+
- `--rollback-to-slot SLOTNO`: Force a rollback to the specified slot, if the given slot doesn't exist it will use the next greater slot. See [Manual Rollbacks](manual-rollbacks.md) for details.
1515
- `--disable-in-out`: Disables the `tx_in` and `tx_out` table.
1616

1717
- **Deprecated Options (for historical reference only):**

doc/syncing-and-rollbacks.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ all the blocks that needs deletion, we found only the oldest one and delete with
8585
every transaction after it. We use the property that fields like `tx.block_id` are non-decreasing,
8686
meaning newer entries will have bigger values. So if we simply find the oldest transaction that
8787
needs to be deleted, it's easy to delete everything with a single query.
88+
89+
### Manual Rollbacks
90+
91+
In addition to automatic rollbacks that occur during normal chain-sync operations, you can perform
92+
manual rollbacks using either `cardano-db-sync --rollback-to-slot` or `cardano-db-tool rollback`.
93+
These are useful for testing, fixing incorrect data after bug fixes, or recovering from corrupted
94+
database states.
95+
96+
For detailed information on performing manual rollbacks, see [Manual Rollbacks](manual-rollbacks.md).

0 commit comments

Comments
 (0)