Skip to content

Commit 0242576

Browse files
committed
stake slicing with updated tests
1 parent 13838d6 commit 0242576

File tree

7 files changed

+254
-302
lines changed

7 files changed

+254
-302
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Test.Cardano.Db.Mock.Config (
3636
withDBSyncEnv,
3737
withFullConfig,
3838
withFullConfigAndDropDB,
39+
withFullConfigLogsDropDB,
3940
withFullConfigAndLogs,
4041
withCustomConfigAndLogsAndDropDB,
4142
withCustomConfig,
@@ -354,6 +355,26 @@ withFullConfigAndDropDB =
354355
initCommandLineArgs
355356
Nothing
356357

358+
withFullConfigLogsDropDB ::
359+
-- | config filepath
360+
FilePath ->
361+
-- | test label
362+
FilePath ->
363+
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
364+
IOManager ->
365+
[(Text, Text)] ->
366+
IO a
367+
withFullConfigLogsDropDB =
368+
withFullConfig'
369+
( WithConfigArgs
370+
{ hasFingerprint = True
371+
, shouldLog = True
372+
, shouldDropDB = True
373+
}
374+
)
375+
initCommandLineArgs
376+
Nothing
377+
357378
withFullConfigAndLogs ::
358379
-- | config filepath
359380
FilePath ->

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

+90-87
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
13
module Test.Cardano.Db.Mock.Unit.Alonzo.Stake (
24
-- stake addresses
35
registrationTx,
@@ -24,7 +26,7 @@ import qualified Cardano.Mock.Forging.Tx.Alonzo as Alonzo
2426
import Cardano.Mock.Forging.Tx.Alonzo.Scenarios (delegateAndSendBlocks)
2527
import Cardano.Mock.Forging.Types (StakeIndex (..), UTxOIndex (..))
2628
import Control.Concurrent.Class.MonadSTM.Strict (MonadSTM (atomically))
27-
import Control.Monad (forM_, replicateM_, void)
29+
import Control.Monad (forM_, void)
2830
import Data.Text (Text)
2931
import Ouroboros.Network.Block (blockSlot)
3032
import Test.Cardano.Db.Mock.Config (alonzoConfigDir, startDBSync, withFullConfig, withFullConfigAndDropDB)
@@ -33,7 +35,6 @@ import Test.Cardano.Db.Mock.UnifiedApi (
3335
fillUntilNextEpoch,
3436
forgeAndSubmitBlocks,
3537
forgeNextFindLeaderAndSubmit,
36-
forgeNextSkipSlotsFindLeaderAndSubmit,
3738
getAlonzoLedgerState,
3839
withAlonzoFindLeaderAndSubmit,
3940
withAlonzoFindLeaderAndSubmitTx,
@@ -215,126 +216,128 @@ stakeDistGenesis :: IOManager -> [(Text, Text)] -> Assertion
215216
stakeDistGenesis =
216217
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
217218
startDBSync dbSync
218-
a <- fillUntilNextEpoch interpreter mockServer
219-
assertBlockNoBackoff dbSync (fromIntegral $ length a)
220-
-- There are 5 delegations in genesis
221-
assertEpochStake dbSync 5
219+
blks <- fillUntilNextEpoch interpreter mockServer
220+
assertBlockNoBackoff dbSync (fromIntegral $ length blks)
221+
-- There are 10 delegations in genesis
222+
assertEpochStakeEpoch dbSync 1 5
223+
assertEpochStakeEpoch dbSync 2 5
222224
where
223225
testLabel = "stakeDistGenesis-alonzo"
224226

225227
delegations2000 :: IOManager -> [(Text, Text)] -> Assertion
226228
delegations2000 =
227-
withFullConfig alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
229+
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
228230
startDBSync dbSync
229-
a <- delegateAndSendBlocks 1995 interpreter
230-
forM_ a $ atomically . addBlock mockServer
231-
b <- fillUntilNextEpoch interpreter mockServer
232-
c <- forgeAndSubmitBlocks interpreter mockServer 10
233-
234-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c)
235-
-- There are exactly 2000 entries on the second epoch, 5 from genesis and 1995 manually added
231+
blks <- delegateAndSendBlocks 1995 interpreter
232+
forM_ blks (atomically . addBlock mockServer)
233+
-- Fill the rest of the epoch
234+
epoch <- fillUntilNextEpoch interpreter mockServer
235+
-- Wait for them to sync
236+
assertBlockNoBackoff dbSync (length blks + length epoch)
237+
assertEpochStakeEpoch dbSync 1 5
238+
-- Add some more blocks
239+
blks' <- forgeAndSubmitBlocks interpreter mockServer 10
240+
-- Wait for it to sync
241+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks')
236242
assertEpochStakeEpoch dbSync 2 2000
237-
243+
-- Forge another block
238244
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
239-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c + 1)
245+
-- Wait for it to sync
246+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks' + 1)
247+
-- There are still 2000 entries
240248
assertEpochStakeEpoch dbSync 2 2000
241249
where
242250
testLabel = "delegations2000-alonzo"
243251

244252
delegations2001 :: IOManager -> [(Text, Text)] -> Assertion
245253
delegations2001 =
246-
withFullConfig alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
254+
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
247255
startDBSync dbSync
248-
a <- delegateAndSendBlocks 1996 interpreter
249-
forM_ a $ atomically . addBlock mockServer
250-
b <- fillUntilNextEpoch interpreter mockServer
251-
c <- forgeAndSubmitBlocks interpreter mockServer 9
252-
253-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c)
254-
assertEpochStakeEpoch dbSync 2 0
256+
-- We want exactly 2001 delegations, 5 from genesis and 1996 manually added
257+
blks <- delegateAndSendBlocks 1996 interpreter
258+
forM_ blks (atomically . addBlock mockServer)
259+
-- Fill the rest of the epoch
260+
epoch <- fillUntilNextEpoch interpreter mockServer
261+
-- Add some more blocks
262+
blks' <- forgeAndSubmitBlocks interpreter mockServer 9
263+
-- Wait for it to sync
264+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks')
265+
assertEpochStakeEpoch dbSync 1 5
266+
-- The next 2000 entries is inserted on the next block
255267
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
256-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c + 1)
257-
assertEpochStakeEpoch dbSync 2 2000
258-
-- The remaining entry is inserted on the next block.
268+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks' + 1)
269+
assertEpochStakeEpoch dbSync 2 2001
270+
-- The remaining entry is inserted on the next block
259271
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
260-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c + 2)
272+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks' + 2)
261273
assertEpochStakeEpoch dbSync 2 2001
262274
where
263275
testLabel = "delegations2001-alonzo"
264276

265277
delegations8000 :: IOManager -> [(Text, Text)] -> Assertion
266278
delegations8000 =
267-
withFullConfig alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
279+
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
268280
startDBSync dbSync
269-
a <- delegateAndSendBlocks 7995 interpreter
270-
forM_ a $ atomically . addBlock mockServer
271-
b <- fillEpochs interpreter mockServer 2
272-
c <- forgeAndSubmitBlocks interpreter mockServer 10
273-
274-
assertBlockNoBackoff dbSync (fromIntegral $ length a + length b + length c)
275-
assertEpochStakeEpoch dbSync 3 2000
276-
277-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
278-
assertEpochStakeEpoch dbSync 3 4000
279-
280-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
281-
assertEpochStakeEpoch dbSync 3 6000
282-
283-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
284-
assertEpochStakeEpoch dbSync 3 8000
285-
286-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
287-
assertEpochStakeEpoch dbSync 3 8000
281+
-- We want exactly 8000 delegations, 5 from genesis and 7995 manually added
282+
blks <- delegateAndSendBlocks 7995 interpreter
283+
forM_ blks (atomically . addBlock mockServer)
284+
-- Fill the rest of the epoch
285+
epoch <- fillEpochs interpreter mockServer 2
286+
-- Add some more blocks
287+
blks' <- forgeAndSubmitBlocks interpreter mockServer 10
288+
-- Wait for it to sync
289+
assertBlockNoBackoff dbSync (length blks + length epoch + length blks')
290+
assertEpochStakeEpoch dbSync 1 5
291+
assertEpochStakeEpoch dbSync 2 8000
288292
where
289293
testLabel = "delegations8000-alonzo"
290294

291295
delegationsMany :: IOManager -> [(Text, Text)] -> Assertion
292296
delegationsMany =
293-
withFullConfig alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
297+
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
294298
startDBSync dbSync
295-
a <- delegateAndSendBlocks 40000 interpreter
296-
forM_ a $ atomically . addBlock mockServer
297-
b <- fillEpochs interpreter mockServer 4
298-
c <- forgeAndSubmitBlocks interpreter mockServer 10
299-
300-
-- too long. We cannot use default delays
301-
assertBlockNoBackoffTimes (repeat 10) dbSync (fromIntegral $ length a + length b + length c)
302-
-- The slice size here is
303-
-- 1 + div (delegationsLen * 5) expectedBlocks = 2001
304-
-- instead of 2000, because there are many delegations
305-
assertEpochStakeEpoch dbSync 7 2001
306-
307-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
308-
assertEpochStakeEpoch dbSync 7 4002
309-
310-
void $ forgeNextFindLeaderAndSubmit interpreter mockServer []
311-
assertEpochStakeEpoch dbSync 7 6003
299+
-- Forge many delegations
300+
blks <- delegateAndSendBlocks 40_000 interpreter
301+
forM_ blks (atomically . addBlock mockServer)
302+
-- Fill some epochs
303+
epochs <- fillEpochs interpreter mockServer 4
304+
-- Add some more blocks
305+
blks' <- forgeAndSubmitBlocks interpreter mockServer 10
306+
-- We can't use default delays because this takes too long
307+
assertBlockNoBackoffTimes
308+
(repeat 10)
309+
dbSync
310+
(length blks + length epochs + length blks')
311+
assertEpochStakeEpoch dbSync 6 40_005
312+
assertEpochStakeEpoch dbSync 7 40_005
312313
where
313314
testLabel = "delegationsMany-alonzo"
314315

315316
delegationsManyNotDense :: IOManager -> [(Text, Text)] -> Assertion
316317
delegationsManyNotDense =
317-
withFullConfig alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
318+
withFullConfigAndDropDB alonzoConfigDir testLabel $ \interpreter mockServer dbSync -> do
318319
startDBSync dbSync
319-
a <- delegateAndSendBlocks 40000 interpreter
320-
forM_ a $ atomically . addBlock mockServer
321-
b <- fillEpochs interpreter mockServer 4
322-
c <- forgeAndSubmitBlocks interpreter mockServer 10
323-
324-
-- too long. We cannot use default delays
325-
assertBlockNoBackoffTimes (repeat 10) dbSync (fromIntegral $ length a + length b + length c)
326-
-- The slice size here is
327-
-- 1 + div (delegationsLen * 5) expectedBlocks = 2001
328-
-- instead of 2000, because there are many delegations
329-
assertEpochStakeEpoch dbSync 7 2001
330-
331-
-- Blocks come on average every 5 slots. If we skip 15 slots before each block,
332-
-- we are expected to get only 1/4 of the expected blocks. The adjusted slices
333-
-- should still be long enough to cover everything.
334-
replicateM_ 40 $
335-
forgeNextSkipSlotsFindLeaderAndSubmit interpreter mockServer 15 []
336-
337-
-- Even if the chain is sparse, all distributions are inserted.
338-
assertEpochStakeEpoch dbSync 7 40005
320+
-- Forge many delegations
321+
blks <- delegateAndSendBlocks 40_000 interpreter
322+
forM_ blks (atomically . addBlock mockServer)
323+
-- Fill some epochs
324+
epochs <- fillEpochs interpreter mockServer 4
325+
-- Add some more blocks
326+
blks' <- forgeAndSubmitBlocks interpreter mockServer 10
327+
-- We can't use default delays because this takes too long
328+
assertBlockNoBackoffTimes
329+
(repeat 10)
330+
dbSync
331+
(length blks + length epochs + length blks')
332+
-- check the stake distribution for each epoch
333+
assertEpochStakeEpoch dbSync 1 5
334+
assertEpochStakeEpoch dbSync 2 12_505
335+
assertEpochStakeEpoch dbSync 3 40_005
336+
assertEpochStakeEpoch dbSync 4 40_005
337+
assertEpochStakeEpoch dbSync 5 40_005
338+
assertEpochStakeEpoch dbSync 6 40_005
339+
assertEpochStakeEpoch dbSync 7 40_005
340+
-- check the sum of stake distribution for all epochs
341+
assertEpochStake dbSync 212_535
339342
where
340343
testLabel = "delegationsManyNotDense-alonzo"

0 commit comments

Comments
 (0)