Skip to content

Commit a07cc88

Browse files
committed
2045 - fix rollback tests
1 parent b8748fb commit a07cc88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+113
-132
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Property/Property.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Cardano.Mock.Forging.Interpreter
2121
import Cardano.Mock.Forging.Tx.Babbage
2222
import Cardano.Mock.Forging.Types
2323
import Control.Concurrent.Class.MonadSTM.Strict (MonadSTM (atomically))
24+
import Control.Monad (void)
2425
import Data.Foldable
2526
import Data.Maybe (isJust)
2627
import Data.Text (Text)
@@ -129,7 +130,8 @@ transition m cmd resp = case (cmd, resp) of
129130
, dbSyncMaxBlockNo = dbSyncMaxBlockNo'
130131
}
131132
(RollBack blkNo, _) ->
132-
m {serverChain = rollbackChain blkNo (serverChain m)}
133+
-- rollbackTo now forges an empty block after rollback
134+
m {serverChain = rollbackChain blkNo (serverChain m) ++ [0]}
133135
(StopDBSync, _)
134136
| dbSynsIsOn m ->
135137
m {dbSynsIsOn = False}
@@ -178,12 +180,12 @@ postcondition _ _ resp = case resp of
178180
semantics :: Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> Command Concrete -> IO (Response Concrete)
179181
semantics interpreter mockServer dbSync cmd = case cmd of
180182
RollForward n -> NewBlockAdded . reference . Opaque <$> createBlock n interpreter mockServer
181-
RollBack Nothing -> Unit <$> rollbackTo interpreter mockServer GenesisPoint
183+
RollBack Nothing -> Unit <$> void (rollbackTo interpreter mockServer GenesisPoint)
182184
RollBack (Just blkNo) -> do
183185
chain <- atomically $ readChain mockServer
184186
case findFirstPointByBlockNo chain blkNo of
185187
Nothing -> pure $ Error $ "Failed to find point for " <> show blkNo
186-
Just pnt -> Unit <$> rollbackTo interpreter mockServer pnt
188+
Just pnt -> Unit <$> void (rollbackTo interpreter mockServer pnt)
187189
StopDBSync -> Unit <$> stopDBSync dbSync
188190
StartDBSync -> Unit <$> startDBSync dbSync
189191
RestartNode -> Unit <$> restartServer mockServer

cardano-chain-gen/test/Test/Cardano/Db/Mock/UnifiedApi.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,15 @@ fillEpochPercentage interpreter mockServer percentage = do
200200
let blocksToCreate = div (percentage * blocksPerEpoch) 100
201201
replicateM blocksToCreate $ forgeNextFindLeaderAndSubmit interpreter mockServer []
202202

203-
rollbackTo :: Interpreter -> ServerHandle IO CardanoBlock -> CardanoPoint -> IO ()
203+
rollbackTo :: Interpreter -> ServerHandle IO CardanoBlock -> CardanoPoint -> IO [CardanoBlock]
204204
rollbackTo interpreter mockServer point = do
205205
rollbackInterpreter interpreter point
206206
atomically $ rollback mockServer point
207+
-- Forge an empty block to establish a new thread after rollback.
208+
-- This ensures DBSync recognizes the rollback and deletes data.
209+
blk <- forgeNextFindLeader interpreter []
210+
atomically $ addBlock mockServer blk
211+
pure [blk]
207212

208213
registerAllStakeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock
209214
registerAllStakeCreds interpreter mockServer = do

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Plutus.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ multipleScriptsRollback =
241241
assertBlockNoBackoff dbSync 2
242242
assertAlonzoCounts dbSync (1, 2, 1, 1, 3, 2, 0, 0)
243243

244-
rollbackTo interpreter mockServer genesisPoint
244+
void $ rollbackTo interpreter mockServer genesisPoint
245245
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
246246

247247
void $ forgeNextAndSubmit interpreter mockServer $ MockBlock [TxBabbage tx0] (NodeId 1)

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Reward.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Cardano.Ledger.Shelley.TxBody (
1717
Withdrawals (..),
1818
)
1919
import Cardano.Ledger.Shelley.TxCert
20-
import Cardano.Mock.ChainSync.Server (IOManager, addBlock, rollback)
20+
import Cardano.Mock.ChainSync.Server (IOManager, addBlock)
2121
import Cardano.Mock.Forging.Interpreter (withShelleyLedgerState)
2222
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
2323
import Cardano.Mock.Forging.Tx.Babbage.Scenarios (delegateAndSendBlocks)
@@ -314,7 +314,7 @@ _mirRewardRollback =
314314
assertBlockNoBackoff dbSync (fromIntegral $ 4 + length (a <> b <> c <> d))
315315
assertRewardCounts dbSync st True Nothing [(StakeIndexNew 1, (0, 0, 0, 1, 0))]
316316

317-
rollbackTo interpreter mockServer (blockPoint $ last c)
317+
void $ rollbackTo interpreter mockServer (blockPoint $ last c)
318318
void $ withBabbageFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
319319
Babbage.mkDummyRegisterTx 1 1
320320
d' <- fillUntilNextEpoch interpreter mockServer
@@ -491,13 +491,13 @@ rollbackBoundary =
491491
blks' <- fillUntilNextEpoch interpreter mockServer
492492

493493
assertRewardCount dbSync 3
494-
atomically $ rollback mockServer (blockPoint $ last blks)
495-
assertBlockNoBackoff dbSync (2 + length a + length blks + length blks')
494+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint $ last blks)
495+
assertBlockNoBackoff dbSync (length (a <> blks <> blks' <> rbBlocks) + 1)
496496
forM_ blks' $ atomically . addBlock mockServer
497-
assertBlockNoBackoff dbSync (2 + length a + length blks + length blks')
497+
assertBlockNoBackoff dbSync (length (a <> blks <> rbBlocks <> blks') + 1)
498498
assertRewardCount dbSync 3
499499
blks'' <- fillUntilNextEpoch interpreter mockServer
500-
assertBlockNoBackoff dbSync (2 + length a + length blks + length blks' + length blks'')
500+
assertBlockNoBackoff dbSync (length (a <> blks <> rbBlocks <> blks'') + 2)
501501
where
502502
testLabel = "rollbackBoundary"
503503

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/MigrateConsumedPruneTxOut.hs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ performPruneWithSimpleRollback useTxOutAddress =
126126
assertUnspentTx dbSync
127127

128128
-- Rollback
129-
rollbackTo interpreter mockServer (blockPoint blk1)
129+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint blk1)
130130
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 0 "Unexpected TxOutConsumedByTxId count after rollback"
131-
assertBlockNoBackoff dbSync (fullBlockSize blks)
131+
assertBlockNoBackoff dbSync (2 + length rbBlocks)
132132
where
133133
cmdLineArgs = initCommandLineArgs
134134
testLabel = "conwayConfigPruneSimpleRollback"
@@ -160,7 +160,7 @@ performPruneWithFullTxRollback useTxOutAddress =
160160
assertEqQuery dbSync (DB.queryTxOutCount txOutVariantType) 14 "new epoch didn't prune tx_out column that are null"
161161

162162
-- Rollback
163-
rollbackTo interpreter mockServer $ blockPoint blk0
163+
rbBlocks <- rollbackTo interpreter mockServer $ blockPoint blk0
164164
-- Add more transactions
165165
void $ withConwayFindLeaderAndSubmit interpreter mockServer $ \st -> do
166166
tx0 <- Conway.mkFullTx 0 100 st
@@ -169,7 +169,7 @@ performPruneWithFullTxRollback useTxOutAddress =
169169
pure [tx1, tx2, tx0]
170170

171171
-- Verify tx_out was pruned again
172-
assertBlockNoBackoff dbSync 2
172+
assertBlockNoBackoff dbSync (2 + length rbBlocks)
173173
assertTxCount dbSync 14
174174
assertEqQuery dbSync (DB.queryTxOutCount txOutVariantType) 16 "new epoch didn't prune tx_out column that are null"
175175
assertUnspentTx dbSync
@@ -244,18 +244,16 @@ performPruneAndRollBackOneBlock useTxOutAddress =
244244
assertBlockNoBackoff dbSync 101
245245
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 2 "Unexpected TxOutConsumedByTxId count before rollback"
246246

247-
rollbackTo interpreter mockServer (blockPoint blk100)
247+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint blk100)
248248

249-
-- Add an empty block
250-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
251-
-- Verify the transactions were removed in the rollback
252-
assertBlockNoBackoff dbSync 101
249+
-- Verify the transactions were removed in the rollback (100 is max block number at blk100)
250+
assertBlockNoBackoff dbSync (100 + length rbBlocks)
253251
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 1 "Unexpected TxOutConsumedByTxId count after rollback"
254252

255253
-- Trigger a prune
256254
void $ forgeAndSubmitBlocks interpreter mockServer 102
257255
-- Verify everything was pruned
258-
assertBlockNoBackoff dbSync 203
256+
assertBlockNoBackoff dbSync (100 + length rbBlocks + 102)
259257
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 0 "Unexpected TxOutConsumedByTxId count after rollback"
260258
where
261259
cmdLineArgs = initCommandLineArgs
@@ -290,18 +288,16 @@ performNoPruneAndRollBack useTxOutAddress =
290288
assertBlockNoBackoff dbSync 101
291289
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 2 "Unexpected TxOutConsumedByTxId count before rollback"
292290

293-
rollbackTo interpreter mockServer (blockPoint blk100)
291+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint blk100)
294292

295-
-- Add an empty block
296-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
297-
-- Verify transactions were removed
298-
assertBlockNoBackoff dbSync 101
293+
-- Verify transactions were removed (100 is max block number at blk100)
294+
assertBlockNoBackoff dbSync (100 + length rbBlocks)
299295
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 1 "Unexpected TxOutConsumedByTxId count after rollback"
300296

301297
-- Add some more blocks
302298
void $ forgeAndSubmitBlocks interpreter mockServer 102
303299
-- Verify nothing has been pruned
304-
assertBlockNoBackoff dbSync 203
300+
assertBlockNoBackoff dbSync (100 + length rbBlocks + 102)
305301
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 1 "Unexpected TxOutConsumedByTxId count after rollback"
306302
where
307303
cmdLineArgs = initCommandLineArgs
@@ -338,12 +334,11 @@ performPruneSameBlock useTxOutAddress =
338334
assertBlockNoBackoff dbSync 100
339335
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 0 "Unexpected TxOutConsumedByTxId after prune"
340336

341-
rollbackTo interpreter mockServer (blockPoint blk77)
337+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint blk77)
338+
let rbBlocksLength = length rbBlocks
342339

343-
-- Add an empty block
344-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
345-
-- Verify the transactions were pruned again
346-
assertBlockNoBackoff dbSync 78
340+
-- Verify the transactions were pruned again (77 is max block number at blk77, + rbBlocks)
341+
assertBlockNoBackoff dbSync (77 + rbBlocksLength)
347342
assertTxInCount dbSync 0
348343
assertEqQuery dbSync (DB.queryTxOutConsumedCount txOutVariantType) 0 "Unexpected TxOutConsumedByTxId after rollback"
349344
where
@@ -375,14 +370,14 @@ performNoPruneSameBlock useTxOutAddress =
375370
-- Verify the blocks exist
376371
assertBlockNoBackoff dbSync 100
377372

378-
rollbackTo interpreter mockServer (blockPoint blk97)
373+
rbBlocks <- rollbackTo interpreter mockServer (blockPoint blk97)
379374

380375
-- Verify we haven't pruned anything yet
381376
assertBlockNoBackoff dbSync 100
382377
-- Add an empty block
383378
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
384379
-- Verify everything was pruned
385-
assertBlockNoBackoff dbSync 98
380+
assertBlockNoBackoff dbSync (98 + length rbBlocks)
386381
assertEqQuery dbSync (DB.queryTxOutConsumedCount $ txOutVariantTypeFromConfig dbSync) 0 "Unexpected TxOutConsumedByTxId after rollback"
387382
where
388383
cmdLineArgs = initCommandLineArgs

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Governance.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ rollbackNewCommittee =
122122
(1, 1, 0, 0)
123123
"Unexpected governance action counts"
124124

125-
-- Rollback the last 2 blocks
126-
epoch1' <- rollbackBlocks interpreter server 2 epoch3
125+
-- Rollback the last 4 blocks
126+
epoch1' <- rollbackBlocks interpreter server 4 epoch3
127127
-- Wait for it to sync
128128
assertBlockNoBackoff dbSync (length $ epoch1 <> epoch1')
129129
-- Should not have a new committee member
@@ -277,13 +277,13 @@ rollbackBlocks interpreter server n blocks = do
277277
[] -> assertFailure $ "Expected at least " <> show n <> " blocks"
278278

279279
-- Rollback to the previous epoch
280-
Api.rollbackTo interpreter server rollbackPoint
280+
rbBlocks <- Api.rollbackTo interpreter server rollbackPoint
281281
-- Create a fork
282282
newBlock <-
283283
Api.withConwayFindLeaderAndSubmitTx interpreter server $
284284
Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
285285

286-
pure $ reverse (newBlock : blocks')
286+
pure $ reverse ([newBlock] <> rbBlocks <> blocks')
287287

288288
updateConstitution :: IOManager -> [(Text, Text)] -> Assertion
289289
updateConstitution =
@@ -507,8 +507,8 @@ rollbackHardFork =
507507
(Just 11)
508508
"Unexpected governance action counts"
509509

510-
-- Rollback the last 2 blocks
511-
epoch2 <- rollbackBlocks interpreter server 2 epoch3
510+
-- Rollback the last 4 blocks
511+
epoch2 <- rollbackBlocks interpreter server 4 epoch3
512512
-- Wait for it to sync
513513
assertBlockNoBackoff dbSync (length $ epoch1 <> epoch2)
514514
-- Should not have a new committee member

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/InlineAndReference.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,8 @@ spendCollateralOutputRollback =
324324
-- Create and spend collateral
325325
mkSpendCollOutput interpreter mockServer dbSync 0
326326

327-
-- Rollback past action above
328-
Api.rollbackTo interpreter mockServer (blockPoint blk)
329-
-- Forge and submit another block
330-
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
327+
-- Rollback past action above (forges a block to establish new thread)
328+
void $ Api.rollbackTo interpreter mockServer (blockPoint blk)
331329
-- Create and spend collateral
332330
mkSpendCollOutput interpreter mockServer dbSync 1
333331
where

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Other.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Cardano.Ledger.Conway.TxCert (ConwayTxCert (..))
3030
import Cardano.Ledger.Core (PoolCert (..))
3131
import Cardano.Ledger.Credential (StakeCredential)
3232
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
33-
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock, rollback)
33+
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock)
3434
import Cardano.Mock.Forging.Interpreter (forgeNext, getCurrentEpoch)
3535
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
3636
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
@@ -430,14 +430,14 @@ rollbackFork =
430430
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500 0
431431

432432
-- Wait for it to sync
433-
assertBlockNoBackoff dbSync $ 2 + length (epoch0 <> epoch1 <> epoch1')
433+
assertBlockNoBackoff dbSync $ 1 + length (epoch0 <> epoch1 <> epoch1' <> [blk])
434434
-- Rollback
435-
atomically $ rollback mockServer (blockPoint $ last epoch1)
435+
rollbackBlocks <- Api.rollbackTo interpreter mockServer (blockPoint $ last epoch1)
436436
-- Replay remaining blocks
437437
forM_ (epoch1' <> [blk]) (atomically . addBlock mockServer)
438438

439439
-- Verify block count
440-
assertBlockNoBackoff dbSync $ 2 + length (epoch0 <> epoch1 <> epoch1')
440+
assertBlockNoBackoff dbSync $ length (epoch0 <> epoch1 <> rollbackBlocks <> epoch1' <> [blk])
441441
where
442442
configDir = "config-conway-hf-epoch1"
443443
testLabel = "conwayRollbackFork"

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Plutus.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,7 @@ multipleScriptsRollback =
376376
assertAlonzoCounts dbSync (1, 2, 1, 1, 3, 2, 0, 0)
377377

378378
-- Roll back to genesis
379-
Api.rollbackTo interpreter mockServer genesisPoint
380-
-- Forge another block
381-
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
379+
void $ Api.rollbackTo interpreter mockServer genesisPoint
382380

383381
-- Submit the txs again
384382
void $
@@ -388,7 +386,7 @@ multipleScriptsRollback =
388386
Api.forgeNextAndSubmit interpreter mockServer $
389387
MockBlock [TxConway tx1] (NodeId 1)
390388

391-
-- Verify tx counts
389+
-- Verify tx counts (rollbackTo forges 1 block, then we forge 2 more)
392390
assertBlockNoBackoff dbSync 3
393391
assertAlonzoCounts dbSync (1, 2, 1, 1, 3, 2, 0, 0)
394392
where

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Reward.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Test.Cardano.Db.Mock.Unit.Conway.Reward (
88
) where
99

1010
import Cardano.Ledger.Keys (KeyHash (..))
11-
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock, rollback)
11+
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock)
1212
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
1313
import Cardano.Mock.Forging.Types (PoolIndex (..), StakeIndex (..), UTxOIndex (..))
1414
import Cardano.Prelude
@@ -104,17 +104,17 @@ rollbackBoundary =
104104
assertRewardCount dbSync 3
105105

106106
-- Rollback
107-
atomically $ rollback mockServer (blockPoint $ last blks)
107+
rbBlocks <- Api.rollbackTo interpreter mockServer (blockPoint $ last blks)
108108
-- Rollback effects are delayed
109-
assertBlockNoBackoff dbSync (2 + length (epochs <> blks <> epochs'))
109+
assertBlockNoBackoff dbSync (1 + length (epochs <> blks <> rbBlocks <> epochs'))
110110

111111
-- Add the blocks again
112112
forM_ epochs' $ atomically . addBlock mockServer
113113
-- Should have the same amount of rewards
114-
assertBlockNoBackoff dbSync (2 + length (epochs <> blks <> epochs'))
114+
assertBlockNoBackoff dbSync (1 + length (epochs <> blks <> rbBlocks <> epochs'))
115115
assertRewardCount dbSync 3
116116
-- Add some more blocks and verify
117117
epochs'' <- Api.fillUntilNextEpoch interpreter mockServer
118-
assertBlockNoBackoff dbSync (2 + length (epochs <> blks <> epochs' <> epochs''))
118+
assertBlockNoBackoff dbSync (2 + length (epochs <> blks <> rbBlocks <> epochs''))
119119
where
120120
testLabel = "conwayRollbackBoundary"

0 commit comments

Comments
 (0)