Skip to content

Commit 39b12be

Browse files
committed
add tests for pool staments rollbacks
1 parent f594996 commit 39b12be

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import Cardano.Mock.Forging.Types (PoolIndex (..), StakeIndex (..), UTxOIndex (.
3434
import Cardano.Prelude
3535
import Data.Maybe.Strict (StrictMaybe (..))
3636
import Ouroboros.Network.Block (blockPoint)
37-
import Test.Cardano.Db.Mock.Config
38-
import Test.Cardano.Db.Mock.Examples (mockBlock0, mockBlock1, mockBlock2)
37+
import Test.Cardano.Db.Mock.Config (claFullMode, configPoolStats, conwayConfigDir, initCommandLineArgs, queryDBSync, startDBSync, stopDBSync, withCustomConfigDropDB, withFullConfigDropDB)
38+
import Test.Cardano.Db.Mock.Examples
3939
import Test.Cardano.Db.Mock.UnifiedApi
4040
import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertEqQuery, assertTxCount)
4141
import Test.Tasty.HUnit (Assertion (), assertBool, assertEqual)
@@ -134,9 +134,9 @@ lazyRollback =
134134
rollbackTo interpreter mockServer (blockPoint lastBlk)
135135

136136
-- Here we create the fork
137-
void
138-
$ withConwayFindLeaderAndSubmitTx interpreter mockServer
139-
$ Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
137+
void $
138+
withConwayFindLeaderAndSubmitTx interpreter mockServer $
139+
Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
140140
-- Add some more blocks
141141
void $ forgeAndSubmitBlocks interpreter mockServer 40
142142
-- Verify the new block count
@@ -162,9 +162,9 @@ lazyRollbackRestart =
162162
startDBSync dbSync
163163

164164
-- Here we create the fork
165-
void
166-
$ withConwayFindLeaderAndSubmitTx interpreter mockServer
167-
$ Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
165+
void $
166+
withConwayFindLeaderAndSubmitTx interpreter mockServer $
167+
Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
168168
-- Add some more blocks
169169
void $ forgeAndSubmitBlocks interpreter mockServer 30
170170
-- Verify the new block count
@@ -188,18 +188,18 @@ doubleRollback =
188188
-- Rollback to second block point
189189
rollbackTo interpreter mockServer (blockPoint lastBlk2)
190190
-- Here we create a fork
191-
void
192-
$ withConwayFindLeaderAndSubmitTx interpreter mockServer
193-
$ Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
191+
void $
192+
withConwayFindLeaderAndSubmitTx interpreter mockServer $
193+
Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
194194
-- Add some more blocks
195195
void $ forgeAndSubmitBlocks interpreter mockServer 50
196196

197197
-- Rollback to first block point
198198
rollbackTo interpreter mockServer (blockPoint lastBlk1)
199199
-- Create another fork
200-
void
201-
$ withConwayFindLeaderAndSubmitTx interpreter mockServer
202-
$ Conway.mkSimpleDCertTx [(StakeIndexNew 0, Conway.mkRegTxCert $ SJust (Coin 100))]
200+
void $
201+
withConwayFindLeaderAndSubmitTx interpreter mockServer $
202+
Conway.mkSimpleDCertTx [(StakeIndexNew 0, Conway.mkRegTxCert $ SJust (Coin 100))]
203203
-- Add some more blocks
204204
void $ forgeAndSubmitBlocks interpreter mockServer 50
205205
-- Wait for it to sync
@@ -379,8 +379,8 @@ drepDistrRollback =
379379

380380
poolStatBasicTest :: IOManager -> [(Text, Text)] -> Assertion
381381
poolStatBasicTest =
382-
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel
383-
$ \interpreter mockServer dbSync -> do
382+
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel $
383+
\interpreter mockServer dbSync -> do
384384
startDBSync dbSync
385385

386386
-- Test basic pool stats functionality
@@ -391,10 +391,10 @@ poolStatBasicTest =
391391
void $ forgeAndSubmitBlocks interpreter mockServer 200
392392
assertBlockNoBackoff dbSync 201
393393

394-
poolStatCount <- queryDBSync dbSync Db.queryPoolStatCount
394+
poolStatCount <- queryDBSync dbSync DB.queryPoolStatCount
395395

396396
-- Verify pool stats are created and no duplicates exist
397-
duplicateCount <- queryDBSync dbSync Db.queryPoolStatDuplicates
397+
duplicateCount <- queryDBSync dbSync DB.queryPoolStatDuplicates
398398
assertEqual "Should have no duplicate pool stats" 0 duplicateCount
399399
assertBool "Should have some pool stats" (poolStatCount > 0)
400400
where
@@ -403,8 +403,8 @@ poolStatBasicTest =
403403

404404
poolStatRollbackNoDuplicates :: IOManager -> [(Text, Text)] -> Assertion
405405
poolStatRollbackNoDuplicates =
406-
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel
407-
$ \interpreter mockServer dbSync -> do
406+
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel $
407+
\interpreter mockServer dbSync -> do
408408
startDBSync dbSync
409409

410410
-- Simple setup: create some blocks with pool stats
@@ -429,16 +429,16 @@ poolStatRollbackNoDuplicates =
429429
assertBlockNoBackoff dbSync 351 -- Should stay same due to rollbackExpand commentComment on line R349ResolvedCode has comments. Press enter to view.
430430

431431
-- The main test: no duplicates after rollback + re-sync
432-
duplicateCount <- queryDBSync dbSync Db.queryPoolStatDuplicates
432+
duplicateCount <- queryDBSync dbSync DB.queryPoolStatDuplicates
433433
assertEqual "Should have no duplicate pool stats after rollback" 0 duplicateCount
434434
where
435435
args = initCommandLineArgs {claFullMode = False}
436436
testLabel = "conwayPoolStatRollbackNoDuplicates"
437437

438438
poolStatRollbackGeneral :: IOManager -> [(Text, Text)] -> Assertion
439439
poolStatRollbackGeneral =
440-
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel
441-
$ \interpreter mockServer dbSync -> do
440+
withCustomConfigDropDB args (Just configPoolStats) conwayConfigDir testLabel $
441+
\interpreter mockServer dbSync -> do
442442
startDBSync dbSync
443443

444444
-- Create pools and stake to generate pool stats
@@ -451,15 +451,15 @@ poolStatRollbackGeneral =
451451
assertBlockNoBackoff dbSync totalBeforeRollback
452452

453453
-- Check initial pool stat count
454-
initialCount <- queryDBSync dbSync Db.queryPoolStatCount
454+
initialCount <- queryDBSync dbSync DB.queryPoolStatCount
455455

456456
-- Forge more blocks to create additional pool stats
457457
epochBlks2 <- fillEpochs interpreter mockServer 1
458458
let totalAfterEpoch = totalBeforeRollback + length epochBlks2
459459
assertBlockNoBackoff dbSync totalAfterEpoch
460460

461461
-- Verify pool stats increased
462-
afterCount <- queryDBSync dbSync Db.queryPoolStatCount
462+
afterCount <- queryDBSync dbSync DB.queryPoolStatCount
463463
assertBool "Pool stats should have increased" (afterCount > initialCount)
464464

465465
-- Rollback to previous point
@@ -470,13 +470,13 @@ poolStatRollbackGeneral =
470470
epochBlks3 <- fillEpochs interpreter mockServer 1
471471
let finalTotal = totalBeforeRollback + length epochBlks3 + 1
472472
assertBlockNoBackoff dbSync finalTotal
473-
finalCount <- queryDBSync dbSync Db.queryPoolStatCount
473+
finalCount <- queryDBSync dbSync DB.queryPoolStatCount
474474

475475
-- Verify count matches and no constraint violations occurred
476476
assertEqual "Pool stat count should match after rollback" afterCount finalCount
477477

478478
-- Also verify no duplicates
479-
duplicateCount <- queryDBSync dbSync Db.queryPoolStatDuplicates
479+
duplicateCount <- queryDBSync dbSync DB.queryPoolStatDuplicates
480480
assertEqual "Should have no duplicate pool stats" 0 duplicateCount
481481
where
482482
args = initCommandLineArgs {claFullMode = False}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,53 @@ insertBulkPoolStat :: [SCP.PoolStat] -> DbM ()
208208
insertBulkPoolStat poolStats =
209209
runSession mkDbCallStack $ HsqlSes.statement poolStats insertBulkPoolStatStmt
210210

211+
--------------------------------------------------------------------------------
212+
queryPoolStatCountStmt :: HsqlStmt.Statement () Int64
213+
queryPoolStatCountStmt =
214+
HsqlStmt.Statement sql encoder decoder True
215+
where
216+
poolStatTable = tableName (Proxy @SCP.PoolStat)
217+
218+
sql =
219+
TextEnc.encodeUtf8 $
220+
Text.concat
221+
[ "SELECT COUNT(*)::bigint FROM "
222+
, poolStatTable
223+
]
224+
225+
encoder = mempty
226+
decoder = HsqlD.singleRow (HsqlD.column $ HsqlD.nonNullable HsqlD.int8)
227+
228+
queryPoolStatCount :: DbM Int64
229+
queryPoolStatCount =
230+
runSession mkDbCallStack $ HsqlSes.statement () queryPoolStatCountStmt
231+
232+
--------------------------------------------------------------------------------
233+
queryPoolStatDuplicatesStmt :: HsqlStmt.Statement () Int64
234+
queryPoolStatDuplicatesStmt =
235+
HsqlStmt.Statement sql encoder decoder True
236+
where
237+
poolStatTable = tableName (Proxy @SCP.PoolStat)
238+
239+
sql =
240+
TextEnc.encodeUtf8 $
241+
Text.concat
242+
[ "SELECT COUNT(*)::bigint FROM ("
243+
, " SELECT pool_hash_id, epoch_no"
244+
, " FROM "
245+
, poolStatTable
246+
, " GROUP BY pool_hash_id, epoch_no"
247+
, " HAVING COUNT(*) > 1"
248+
, ") AS duplicates"
249+
]
250+
251+
encoder = mempty
252+
decoder = HsqlD.singleRow (HsqlD.column $ HsqlD.nonNullable HsqlD.int8)
253+
254+
queryPoolStatDuplicates :: DbM Int64
255+
queryPoolStatDuplicates =
256+
runSession mkDbCallStack $ HsqlSes.statement () queryPoolStatDuplicatesStmt
257+
211258
--------------------------------------------------------------------------------
212259
-- PoolOwner
213260
--------------------------------------------------------------------------------
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ BEGIN
1414
);
1515

1616
-- Then add constraint if it doesn't exist
17-
DO $$
18-
BEGIN
19-
IF NOT EXISTS (
20-
SELECT 1 FROM pg_constraint
21-
WHERE conname = 'unique_pool_stat_epoch'
22-
) THEN
23-
ALTER TABLE "pool_stat" ADD CONSTRAINT "unique_pool_stat_epoch"
24-
UNIQUE ("pool_hash_id", "epoch_no");
25-
END IF;
26-
END $$;
17+
IF NOT EXISTS (
18+
SELECT 1 FROM pg_constraint
19+
WHERE conname = 'unique_pool_stat_epoch'
20+
) THEN
21+
ALTER TABLE "pool_stat" ADD CONSTRAINT "unique_pool_stat_epoch"
22+
UNIQUE ("pool_hash_id", "epoch_no");
23+
END IF;
2724

2825
UPDATE schema_version SET stage_two = next_version ;
2926
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;

0 commit comments

Comments
 (0)