Skip to content

Commit 1dfa4ac

Browse files
committed
fix tests
1 parent d25a038 commit 1dfa4ac

File tree

9 files changed

+139
-56
lines changed

9 files changed

+139
-56
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Test.Cardano.Db.Mock.Config (
3535
configMetadataEnable,
3636
configMetadataDisable,
3737
configMetadataKeys,
38+
configPoolStats,
3839
mkFingerPrint,
3940
mkMutableDir,
4041
mkDBSyncEnv,
@@ -353,6 +354,10 @@ configMetadataKeys :: SyncNodeConfig -> SyncNodeConfig
353354
configMetadataKeys cfg = do
354355
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioMetadata = MetadataKeys $ 1 :| []}}
355356

357+
configPoolStats :: SyncNodeConfig -> SyncNodeConfig
358+
configPoolStats cfg = do
359+
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioPoolStats = PoolStatsConfig True}}
360+
356361
initCommandLineArgs :: CommandLineArgs
357362
initCommandLineArgs =
358363
CommandLineArgs

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ unitTests iom knownMigrations =
120120
, test "rollback stake address cache" Rollback.stakeAddressRollback
121121
, test "rollback change order of txs" Rollback.rollbackChangeTxOrder
122122
, test "rollback full tx" Rollback.rollbackFullTx
123-
, test "pool stat rollback no duplicates" Rollback.poolStatRollback
123+
, test "basic pool stats functionality" Rollback.poolStatBasicTest
124+
, test "pool stat rollback no duplicates" Rollback.poolStatRollbackNoDuplicates
125+
, test "pool stat rollback general" Rollback.poolStatRollbackGeneral
124126
]
125127
, testGroup
126128
"different configs"

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

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ module Test.Cardano.Db.Mock.Unit.Conway.Rollback (
1010
stakeAddressRollback,
1111
rollbackChangeTxOrder,
1212
rollbackFullTx,
13-
poolStatRollback,
13+
poolStatBasicTest,
14+
poolStatRollbackNoDuplicates,
15+
poolStatRollbackGeneral,
1416
) where
1517

18+
import qualified Cardano.Db as Db
1619
import Cardano.Ledger.Coin (Coin (..))
1720
import Cardano.Ledger.Conway.TxCert (ConwayDelegCert (..), Delegatee (..))
1821
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock, rollback)
@@ -26,11 +29,10 @@ import Ouroboros.Network.Block (blockPoint)
2629
import Test.Cardano.Db.Mock.Config
2730
import Test.Cardano.Db.Mock.Examples (mockBlock0, mockBlock1, mockBlock2)
2831
import Test.Cardano.Db.Mock.UnifiedApi
32+
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
2933
import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertTxCount, runQuery)
3034
import Test.Tasty.HUnit (Assertion (), assertBool, assertEqual)
3135
import Prelude (last)
32-
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
33-
import qualified Cardano.Db as Db
3436

3537
simpleRollback :: IOManager -> [(Text, Text)] -> Assertion
3638
simpleRollback =
@@ -295,39 +297,107 @@ rollbackFullTx =
295297
where
296298
testLabel = "conwayRollbackFullTx"
297299

298-
poolStatRollback :: IOManager -> [(Text, Text)] -> Assertion
299-
poolStatRollback =
300-
withFullConfigAndDropDB conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
301-
startDBSync dbSync
302-
303-
-- Create pools and stake to generate pool stats
304-
void $ Api.registerAllStakeCreds interpreter mockServer
305-
void $ Api.fillEpochs interpreter mockServer 2
300+
poolStatBasicTest :: IOManager -> [(Text, Text)] -> Assertion
301+
poolStatBasicTest =
302+
withCustomConfigAndDropDB args (Just configPoolStats) conwayConfigDir testLabel $
303+
\interpreter mockServer dbSync -> do
304+
startDBSync dbSync
306305

307-
-- Create rollback point
308-
blks <- Api.forgeAndSubmitBlocks interpreter mockServer 10
309-
assertBlockNoBackoff dbSync (2 + length blks)
306+
-- Test basic pool stats functionality
307+
void $ Api.registerAllStakeCreds interpreter mockServer
308+
assertBlockNoBackoff dbSync 1
310309

311-
-- Check initial pool stat count
312-
initialCount <- runQuery dbSync Db.queryPoolStatCount
310+
-- Create some epochs with pool stats
311+
void $ Api.forgeAndSubmitBlocks interpreter mockServer 200
312+
assertBlockNoBackoff dbSync 201
313313

314-
-- Forge more blocks to create additional pool stats
315-
void $ Api.fillEpochs interpreter mockServer 1
316-
assertBlockNoBackoff dbSync (3 + length blks)
314+
poolStatCount <- runQuery dbSync Db.queryPoolStatCount
317315

318-
-- Verify pool stats increased
319-
afterCount <- runQuery dbSync Db.queryPoolStatCount
320-
assertBool "Pool stats should have increased" (afterCount > initialCount)
321-
322-
-- Rollback to previous point
323-
atomically $ rollback mockServer (blockPoint $ last blks)
324-
assertBlockNoBackoff dbSync (3 + length blks) -- Delayed rollback
325-
326-
-- Re-sync the same blocks - should not create duplicates
327-
void $ Api.fillEpochs interpreter mockServer 1
328-
finalCount <- runQuery dbSync Db.queryPoolStatCount
329-
330-
-- Verify count matches and no constraint violations occurred
331-
assertEqual "Pool stat count should match after rollback" afterCount finalCount
316+
-- Verify pool stats are created and no duplicates exist
317+
duplicateCount <- runQuery dbSync Db.queryPoolStatDuplicates
318+
assertEqual "Should have no duplicate pool stats" 0 duplicateCount
319+
assertBool "Should have some pool stats" (poolStatCount > 0)
320+
where
321+
args = initCommandLineArgs {claFullMode = False}
322+
testLabel = "conwayPoolStatBasicTest"
323+
324+
poolStatRollbackNoDuplicates :: IOManager -> [(Text, Text)] -> Assertion
325+
poolStatRollbackNoDuplicates =
326+
withCustomConfigAndDropDB args (Just configPoolStats) conwayConfigDir testLabel $
327+
\interpreter mockServer dbSync -> do
328+
startDBSync dbSync
329+
330+
-- Simple setup: create some blocks with pool stats
331+
void $ Api.registerAllStakeCreds interpreter mockServer
332+
void $ Api.forgeAndSubmitBlocks interpreter mockServer 200 -- Fill 2 epochs
333+
assertBlockNoBackoff dbSync 201
334+
335+
-- Create rollback point
336+
rollbackBlks <- Api.forgeAndSubmitBlocks interpreter mockServer 50
337+
assertBlockNoBackoff dbSync 251
338+
339+
-- Add more blocks to create additional pool stats
340+
void $ Api.forgeAndSubmitBlocks interpreter mockServer 100 -- Fill 1 more epoch
341+
assertBlockNoBackoff dbSync 351
342+
343+
-- Rollback (following exact pattern from bigChain test)
344+
atomically $ rollback mockServer (blockPoint $ last rollbackBlks)
345+
assertBlockNoBackoff dbSync 351 -- Delayed rollback
346+
347+
-- Re-sync some blocks
348+
void $ Api.forgeAndSubmitBlocks interpreter mockServer 100
349+
assertBlockNoBackoff dbSync 351 -- Should stay same due to rollback
350+
351+
-- The main test: no duplicates after rollback + re-sync
352+
duplicateCount <- runQuery dbSync Db.queryPoolStatDuplicates
353+
assertEqual "Should have no duplicate pool stats after rollback" 0 duplicateCount
354+
where
355+
args = initCommandLineArgs {claFullMode = False}
356+
testLabel = "conwayPoolStatRollbackNoDuplicates"
357+
358+
poolStatRollbackGeneral :: IOManager -> [(Text, Text)] -> Assertion
359+
poolStatRollbackGeneral =
360+
withCustomConfigAndDropDB args (Just configPoolStats) conwayConfigDir testLabel $
361+
\interpreter mockServer dbSync -> do
362+
startDBSync dbSync
363+
364+
-- Create pools and stake to generate pool stats
365+
void $ Api.registerAllStakeCreds interpreter mockServer
366+
epochBlks1 <- Api.fillEpochs interpreter mockServer 2
367+
368+
-- Create rollback point
369+
rollbackBlks <- Api.forgeAndSubmitBlocks interpreter mockServer 10
370+
let totalBeforeRollback = length epochBlks1 + length rollbackBlks + 1
371+
assertBlockNoBackoff dbSync totalBeforeRollback
372+
373+
-- Check initial pool stat count
374+
initialCount <- runQuery dbSync Db.queryPoolStatCount
375+
376+
-- Forge more blocks to create additional pool stats
377+
epochBlks2 <- Api.fillEpochs interpreter mockServer 1
378+
let totalAfterEpoch = totalBeforeRollback + length epochBlks2
379+
assertBlockNoBackoff dbSync totalAfterEpoch
380+
381+
-- Verify pool stats increased
382+
afterCount <- runQuery dbSync Db.queryPoolStatCount
383+
assertBool "Pool stats should have increased" (afterCount > initialCount)
384+
385+
-- Rollback to previous point
386+
atomically $ rollback mockServer (blockPoint $ last rollbackBlks)
387+
assertBlockNoBackoff dbSync totalAfterEpoch -- Delayed rollback
388+
389+
-- Re-sync the same blocks - should not create duplicates
390+
epochBlks3 <- Api.fillEpochs interpreter mockServer 1
391+
let finalTotal = totalBeforeRollback + length epochBlks3 + 1
392+
assertBlockNoBackoff dbSync finalTotal
393+
finalCount <- runQuery dbSync Db.queryPoolStatCount
394+
395+
-- Verify count matches and no constraint violations occurred
396+
assertEqual "Pool stat count should match after rollback" afterCount finalCount
397+
398+
-- Also verify no duplicates
399+
duplicateCount <- runQuery dbSync Db.queryPoolStatDuplicates
400+
assertEqual "Should have no duplicate pool stats" 0 duplicateCount
332401
where
333-
testLabel = "conwayPoolStatRollback"
402+
args = initCommandLineArgs {claFullMode = False}
403+
testLabel = "conwayPoolStatRollbackGeneral"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12,16,18,21,24,30,31,32,33,40,41,42,43,47,52,60,62,70,80,84,86,92,98,100,106,109,110,111,112,127,134,138,146,149,154,166,168,178,183,188,193,194,198,200,202,220,222,223,224,225,231,239,242,247,261,282,283,288,289,301,302,303,308,313,315,316,320,331,334,344,345,363,364,368,369,375,377,381,389,394,407,418,422,425,430,437,438,439,440,447,450,453,454,456,458,461,467,492,499,507,516,524,538,541,544,546,550,567,573,576,577,579,580,586,589,595,597,603,605,609,616,618,619,623,624,634,636,643,644,659,664,665,672,678,692,705,711,712,719,726,730,739,740,743,747,749,751,754,759,762,763,765,767,773,777,786,788,789,794,801,806,807,829,830,832,849,851,853,869,871,874,875,878,882,888,893,895,896,898,899,903,906,908,911,912,913,922,930,932,938,941,944,950,960,963,966,968,972,977,985,986]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12,16,18,21,24,30,31,32,33,40,41,42,43,47,52,60,62,70,80,84,86,92,98,100,106,109,110,111,112,127,134,138,146,149,154,166,168,178,183,188,193,194,198,200,202,220,222,223,224,225,231,239,242,247,261,282,283,288,289,301,302,303,308,313,315,316,320,331,334,344,345,363,364,368,369,375,377,381,389,394,407,418,422,425,430,437,438,439,440,447,450,453,454,456,458,461,467,492,499,507,516,524,538,541,544,546,550,567,573,576,577,579,580,586,589,595,597,603,605,609,616,618,619,623,624,634,636,643,644,659,664,665,672,678,692,705,711,712,719,726,730,739,740,743,747,749,751,754,759,762,763,765,767,773,777,786,788,789,794,801,806,807,829,830,832,849,851,853,869,871,874,875,878,882,888,893,895,896,898,899,903,906,908,911,912,913,922,930,932,938,941,944,950,960,963,966,968,972,977,985,986,988,990,991,994,997,1002,1034,1035,1036,1039,1041,1051,1059,1061,1063,1068,1076,1081,1082,1091,1101,1102,1104,1106,1123,1126,1138,1141,1143,1144,1149,1162,1167,1172,1177,1181,1184,1185,1188,1190,1203,1205,1214,1215,1221,1233,1234,1245,1249,1250,1251,1261,1265,1266,1267,1272,1281,1283,1289,1294,1300,1304,1307,1308,1310,1314,1315,1319,1325,1334,1350,1353,1359,1362,1370,1371,1373,1375,1381,1399,1404,1415,1416,1419,1420,1425,1426,1435,1436,1437,1441,1444,1448,1458,1461,1462,1467,1470,1479,1486,1489,1494,1496,1497,1513,1519,1528,1529,1538,1549,1551,1553,1555,1567,1580,1583,1595,1601,1603,1613,1614,1616,1625,1637,1638,1639,1640,1643,1653,1654,1655,1658,1667,1672,1674,1683,1692,1700,1706,1709,1712,1714,1715,1717,1726,1733,1750,1756,1758,1759,1771,1778,1781,1783,1789,1797,1800,1801,1816,1823,1828,1831,1840,1847,1852,1858,1872,1873,1874,1876,1893,1894,1895,1900,1905,1912,1918,1923,1930,1940,1944,1949,1952,1953,1958,1965,1966,1968,1972,1974,1975,1994,2005]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12,16,18,21,24,30,31,32,33,40,41,42,43,47,52,60,62,70,80,84,86,92,98,100,106,109,110,111,112,127,134,138,146,149,154,166,168,178,183,188,193,194,198,200,202,220,222,223,224,225,231,239,242,247,261,282,283,288,289,301,302,303,308,313,315,316,320,331,334,344,345,363,364,368,369,375,377,381,389,394,407,418,422,425,430,437,438,439,440,447,450,453,454,456,458,461,467,492,499,507,516,524,538,541,544,546,550,567,573,576,577,579,580,586,589,595,597,603,605,609,616,618,619,623,624,634,636,643,644,659,664,665,672,678,692,705,711,712,719,726,730,739,740,743,747,749,751,754,759,762,763,765,767,773,777,786,788,789,794,801,806,807,829,830,832,849,851,853,869,871,874,875,878,882,888,893,895,896,898,899,903,906,908,911,912,913,922,930,932,938,941,944,950,960,963,966,968,972,977,985,986,988,990,991,994,997,1002,1034,1035,1036,1039,1041,1051,1059,1061,1063,1068,1076,1081,1082,1091,1101,1102,1104,1106,1123,1126,1138,1141,1143,1144,1149,1162,1167,1172,1177,1181,1184,1185,1188,1190,1203,1205,1214,1215,1221,1233,1234,1245,1249,1250,1251,1261,1265,1266,1267,1272,1281,1283,1289,1294,1300,1304,1307,1308,1310,1314,1315,1319,1325,1334,1350,1353,1359,1362,1370,1371,1373,1375,1381,1399,1404,1415,1416,1419,1420,1425,1426,1435,1436,1437,1441,1444,1448,1458,1461,1462,1467,1470,1479,1486,1489,1494,1496,1497,1513,1519,1528,1529,1538,1549,1551,1553,1555,1567,1580,1583,1595,1601,1603,1613,1614,1616,1625,1637,1638,1639,1640,1643,1653,1654,1655,1658,1667,1672,1674,1683,1692,1700,1706,1709,1712,1714,1715,1717,1726,1733,1750,1756,1758,1759,1771,1778,1781,1783,1789,1797,1800,1801,1816,1823,1828,1831,1840,1847,1852,1858,1872,1873,1874,1876,1893,1894,1895,1900,1905,1912,1918,1923,1930,1940,1944,1949,1952,1953,1958,1965,1966,1968,1972,1974,1975,1994,2005,2011,2020,2028,2037,2038,2043,2046,2050,2051,2058,2059,2062,2063,2064,2074,2077,2078,2082,2085,2094,2104,2110,2112,2121,2122,2161,2167,2169,2172,2175,2180,2184,2186,2192,2197,2205,2210,2215,2224,2225,2233,2234,2236,2239,2240,2243,2257,2266,2273,2285,2294,2299,2302,2305,2307,2310,2315]

cardano-db/src/Cardano/Db/Operations/Delete.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ deleteUsingEpochNo epochN = do
127127
[ onlyDelete "Epoch" [EpochNo ==. epochN]
128128
, onlyDelete "DrepDistr" [DrepDistrEpochNo >. epochN]
129129
, onlyDelete "RewardRest" [RewardRestSpendableEpoch >. epochN]
130-
, onlyDelete "PoolStat" [PoolStatEpochNo >. epochN]
130+
, onlyDelete "PoolStat" [PoolStatEpochNo >=. epochN]
131131
]
132132
nullLogs <- do
133133
a <- setNullEnacted epochN

cardano-db/src/Cardano/Db/Operations/Query.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ module Cardano.Db.Operations.Query (
7070
queryReservedTickers,
7171
queryDelistedPools,
7272
queryPoolStatCount,
73+
queryPoolStatByEpoch,
74+
queryPoolStatDuplicates,
7375
queryOffChainPoolFetchError,
7476
existsDelistedPool,
7577
-- queries used in tools
@@ -110,6 +112,7 @@ import Cardano.Db.Schema.BaseSchema
110112
import Cardano.Db.Types
111113
import Cardano.Ledger.BaseTypes (CertIx (..), TxIx (..))
112114
import Cardano.Ledger.Credential (Ptr (..), SlotNo32 (..))
115+
import Cardano.Prelude (Int64)
113116
import Cardano.Slotting.Slot (SlotNo (..))
114117
import Control.Monad.Extra (join, whenJust)
115118
import Control.Monad.IO.Class (MonadIO)
@@ -125,6 +128,7 @@ import Database.Esqueleto.Experimental (
125128
Entity (..),
126129
PersistEntity,
127130
PersistField,
131+
Single (..),
128132
SqlBackend,
129133
Value (Value, unValue),
130134
asc,
@@ -145,6 +149,7 @@ import Database.Esqueleto.Experimental (
145149
on,
146150
orderBy,
147151
persistIdField,
152+
rawSql,
148153
select,
149154
selectOne,
150155
sum_,
@@ -930,6 +935,24 @@ queryPoolStatCount = do
930935
pure countRows
931936
pure $ maybe 0 unValue (listToMaybe res)
932937

938+
queryPoolStatByEpoch :: MonadIO m => Word64 -> ReaderT SqlBackend m Word64
939+
queryPoolStatByEpoch eNo = do
940+
res <- select $ do
941+
poolStat <- from $ table @PoolStat
942+
where_ (poolStat ^. PoolStatEpochNo ==. val (fromIntegral eNo))
943+
pure countRows
944+
pure $ maybe 0 unValue (listToMaybe res)
945+
946+
queryPoolStatDuplicates :: MonadIO m => ReaderT SqlBackend m Word64
947+
queryPoolStatDuplicates = do
948+
res <-
949+
rawSql
950+
"SELECT COUNT(*) FROM (SELECT pool_hash_id, epoch_no, COUNT(*) as cnt FROM pool_stat GROUP BY pool_hash_id, epoch_no HAVING COUNT(*) > 1) AS duplicates"
951+
[]
952+
case res of
953+
[Single c] -> pure $ fromIntegral (c :: Int64)
954+
_otherwise -> pure 0
955+
933956
-- Returns also the metadata hash
934957
queryOffChainPoolFetchError :: MonadIO m => ByteString -> Maybe UTCTime -> ReaderT SqlBackend m [(OffChainPoolFetchError, ByteString)]
935958
queryOffChainPoolFetchError hash Nothing = do

schema/migration-2-0045-20250708.sql

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)