-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathOther.hs
More file actions
505 lines (444 loc) · 17 KB
/
Copy pathOther.hs
File metadata and controls
505 lines (444 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Test.Cardano.Db.Mock.Unit.Conway.Other (
-- * Different configs
configNoPools,
configNoStakes,
-- * Pools and smash
poolReg,
nonexistentPoolQuery,
poolDeReg,
poolDeRegMany,
poolDelist,
-- * Hard fork
forkFixedEpoch,
rollbackFork,
forkParam,
) where
import qualified Cardano.Db as DB
import Cardano.DbSync.Era.Shelley.Generic.Util (unKeyHashRaw)
import Cardano.Ledger.BaseTypes (EpochNo (..))
import Cardano.Ledger.Conway.TxCert (ConwayTxCert (..))
import Cardano.Ledger.Core (PoolCert (..))
import Cardano.Ledger.Credential (StakeCredential)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock)
import Cardano.Mock.Forging.Interpreter (forgeNext, getCurrentEpoch)
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
import Cardano.Mock.Forging.Tx.Generic (resolvePool)
import Cardano.Mock.Forging.Types
import Cardano.Prelude hiding (from)
import Cardano.SMASH.Server.PoolDataLayer (PoolDataLayer (..), toDbPoolId)
import Cardano.SMASH.Server.Types (DBFail (..))
import Data.List (last)
import Ouroboros.Consensus.Shelley.Eras (ConwayEra ())
import Ouroboros.Network.Block (blockPoint)
import Test.Cardano.Db.Mock.Config
import Test.Cardano.Db.Mock.Examples (mockBlock0)
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
import Test.Cardano.Db.Mock.Validate
import Test.Tasty.HUnit (Assertion (), assertEqual, assertFailure)
import Prelude ()
configNoPools :: IOManager -> [(Text, Text)] -> Assertion
configNoPools =
withFullConfig "config-conway-no-pools" testLabel $ \_ _ dbSync -> do
startDBSync dbSync
-- Wait for it to sync
assertBlocksCount dbSync 2
assertTxCount dbSync 6
-- Restart
stopDBSync dbSync
startDBSync dbSync
-- Nothing changes, so polling assertions doesn't help here We have to pause
-- and check if anything crashed.
threadDelay 3_000_000
-- Verify it's still running
checkStillRuns dbSync
-- Verify the initial blocks again
assertBlocksCount dbSync 2
assertTxCount dbSync 6
where
testLabel = "conwayConfigNoPools"
configNoStakes :: IOManager -> [(Text, Text)] -> Assertion
configNoStakes =
withFullConfig "config-conway-no-stakes" testLabel $ \interpreter _ dbSync -> do
startDBSync dbSync
-- Wait for it to sync
assertBlocksCount dbSync 2
assertTxCount dbSync 7
-- Restart
stopDBSync dbSync
startDBSync dbSync
-- Nothing changes, so polling assertions doesn't help here We have to pause
-- and check if anything crashed.
threadDelay 3_000_000
-- Verify it's still running
checkStillRuns dbSync
-- Verify the initial blocks again
assertBlocksCount dbSync 2
assertTxCount dbSync 7
-- Try to forge a block (expected to fail)
errBlk <- try $ forgeNext interpreter mockBlock0
case errBlk of
Right _ -> assertFailure "expected to fail"
-- A pool with no stakes can't create blocks
Left WentTooFar {} -> pure ()
-- TODO add an option to disable fingerprint validation for tests like this.
Left (EmptyFingerprint _ _) -> pure ()
Left err -> assertFailure $ "Expected WentTooFar, got " <> show err
where
testLabel = "conwayConfigNoStakes"
poolReg :: IOManager -> [(Text, Text)] -> Assertion
poolReg =
withFullConfigDropDB conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Verify initial pool counts:
-- (poolHashes, poolMetadataRefs, poolUpdates, poolOwners, poolRetires, poolRelays)
initCounter <- runQuery dbSync poolCountersQuery
assertEqual "Unexpected init pool counter" (3, 0, 3, 2, 0, 0) initCounter
-- Forge a pool registration
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkDCertPoolTx
[
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
]
-- Verify pool counts
assertBlockNoBackoff dbSync 2
-- We should have added two owners
assertPoolCounters dbSync (addPoolCounters (1, 1, 1, 2, 0, 1) initCounter)
state' <- Api.getConwayLedgerState interpreter
-- Should not be retired or delisted
assertPoolLayerCounters dbSync (0, 0) [(PoolIndexNew 0, (Right False, False, True))] state'
where
testLabel = "conwayPoolReg"
-- Issue https://github.com/IntersectMBO/cardano-db-sync/issues/997
nonexistentPoolQuery :: IOManager -> [(Text, Text)] -> Assertion
nonexistentPoolQuery =
withFullConfig conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Verify pool layer counts
state' <- Api.getConwayLedgerState interpreter
-- Should not exist
assertPoolLayerCounters
dbSync
(0, 0)
[(PoolIndex 0, (Left RecordDoesNotExist, False, False))]
state'
where
testLabel = "conwayNonexistentPoolQuery"
poolDeReg :: IOManager -> [(Text, Text)] -> Assertion
poolDeReg =
withFullConfig conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Verify initial pool counts:
-- (poolHashes, poolMetadataRefs, poolUpdates, poolOwners, poolRetires, poolRelays)
initCounter <- runQuery dbSync poolCountersQuery
assertEqual "Unexpected init pool counter" (3, 0, 3, 2, 0, 0) initCounter
-- Forge a registration/deregistration
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkDCertPoolTx
[ -- Register a pool
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
, -- Retire it
([], PoolIndexNew 0, \_ poolId -> ConwayTxCertPool $ RetirePool poolId (EpochNo 1))
]
-- Wait for it to sync
assertBlockNoBackoff dbSync 2
-- Should have added two pool owners
assertPoolCounters dbSync (addPoolCounters (1, 1, 1, 2, 1, 1) initCounter)
state' <- Api.getConwayLedgerState interpreter
-- Not retired yet, because epoch hasn't changed
assertPoolLayerCounters
dbSync
(0, 0)
[(PoolIndexNew 0, (Right False, False, True))]
state'
-- Get to the next epoch
blks <- Api.fillUntilNextEpoch interpreter mockServer
-- Wait for it to sync
assertBlockNoBackoff dbSync (length blks + 2)
-- Should not have changed
assertPoolCounters dbSync (addPoolCounters (1, 1, 1, 2, 1, 1) initCounter)
-- Should now be retired
assertPoolLayerCounters
dbSync
(1, 0)
[(PoolIndexNew 0, (Right True, False, False))]
state'
where
testLabel = "conwayPoolDeReg"
poolDeRegMany :: IOManager -> [(Text, Text)] -> Assertion
poolDeRegMany =
withFullConfig conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Verify initial pool counts
-- (poolHashes, poolMetadataRefs, poolUpdates, poolOwners, poolRetires, poolRelays)
initCounter <- runQuery dbSync poolCountersQuery
assertEqual "Unexpected init pool counter" (3, 0, 3, 2, 0, 0) initCounter
-- Forge pool registrations and deregistrations
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkDCertPoolTx
[ -- Register
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
, -- Deregister
([], PoolIndexNew 0, mkPoolDereg (EpochNo 4))
, -- Register--this will be deduplicated by the ledger, so counts
-- below will not include this cert
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
, -- Register with different owner and reward address
( [StakeIndexNew 2, StakeIndexNew 1, StakeIndexNew 0]
, PoolIndexNew 0
, Conway.consTxCertPool
)
]
-- Forge another block with more reg/dereg
void $ Api.withConwayFindLeaderAndSubmit interpreter mockServer $ \state' ->
sequence
[ Conway.mkDCertPoolTx
[ -- Register
( [StakeIndexNew 2, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
]
state'
, Conway.mkDCertPoolTx
[ -- Deregister
([], PoolIndexNew 0, mkPoolDereg (EpochNo 4))
, -- Register
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
, -- Deregister
([], PoolIndexNew 0, mkPoolDereg (EpochNo 1))
]
state'
]
-- Wait for it to sync
assertBlockNoBackoff dbSync 3
-- Verify pool counts
-- (poolHashes, poolMetadataRefs, poolUpdates, poolOwners, poolRetires, poolRelays)
-- TODO fix PoolOwner and PoolRelay unique key
assertPoolCounters dbSync (addPoolCounters (1, 4, 4, 8, 3, 4) initCounter)
state' <- Api.getConwayLedgerState interpreter
-- Not retired yet, because epoch hasn't changed
assertPoolLayerCounters
dbSync
(0, 0)
[(PoolIndexNew 0, (Right False, False, True))]
state'
-- Get to the next epoch
blks <- Api.fillUntilNextEpoch interpreter mockServer
-- Wait for it to sync
assertBlockNoBackoff dbSync (length blks + 3)
-- Pool counts should not have changed
assertPoolCounters dbSync (addPoolCounters (1, 4, 4, 8, 3, 4) initCounter)
-- Only the latest certificate matters, so it should be retired in epoch 0
assertPoolLayerCounters
dbSync
(1, 0)
[(PoolIndexNew 0, (Right True, False, False))]
state'
where
testLabel = "conwayPoolDeRegMany"
poolDelist :: IOManager -> [(Text, Text)] -> Assertion
poolDelist =
withFullConfig conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Verify initial pool counts
-- (poolHashes, poolMetadataRefs, poolUpdates, poolOwners, poolRetires, poolRelays)
initCounter <- runQuery dbSync poolCountersQuery
assertEqual "Unexpected init pool counter" (3, 0, 3, 2, 0, 0) initCounter
-- Register a new pool
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkDCertPoolTx
[ -- Register
( [StakeIndexNew 0, StakeIndexNew 1, StakeIndexNew 2]
, PoolIndexNew 0
, Conway.consTxCertPool
)
]
-- Forge another block
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
-- Wait for it to sync
assertBlockNoBackoff dbSync 3
-- Should not be retired/delisted
state' <- Api.getConwayLedgerState interpreter
assertPoolLayerCounters
dbSync
(0, 0)
[(PoolIndexNew 0, (Right False, False, True))]
state'
-- Delist the pool
let poolKeyHash = resolvePool (PoolIndexNew 0) state'
poolId = toDbPoolId (unKeyHashRaw poolKeyHash)
poolLayer <- getPoolLayer dbSync
void $ dlAddDelistedPool poolLayer poolId
-- This is not async, so we don't need to do exponential backoff
-- Should now be delisted
assertPoolLayerCounters
dbSync
(0, 1)
[(PoolIndexNew 0, (Right False, True, True))]
state'
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkDCertPoolTx [([], PoolIndexNew 0, mkPoolDereg (EpochNo 1))]
void $ Api.forgeNextFindLeaderAndSubmit interpreter mockServer []
where
testLabel = "conwayPoolDelist"
mkPoolDereg ::
EpochNo ->
[StakeCredential] ->
KeyHash 'StakePool ->
ConwayTxCert ConwayEra
mkPoolDereg epochNo _ keyHash = ConwayTxCertPool (RetirePool keyHash epochNo)
forkFixedEpoch :: IOManager -> [(Text, Text)] -> Assertion
forkFixedEpoch =
withFullConfigDropDB configDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add a Babbage tx
void $
Api.withBabbageFindLeaderAndSubmitTx interpreter mockServer $
Babbage.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500
-- Initiate a hard fork
epochs0 <- Api.fillEpochs interpreter mockServer 2
-- Add a simple Conway tx
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500 0
-- Fill the rest of the epoch
epochs1 <- Api.fillUntilNextEpoch interpreter mockServer
-- Verify block count
assertBlockNoBackoff dbSync $ 2 + length (epochs0 <> epochs1)
where
configDir = "config-conway-hf-epoch1"
testLabel = "conwayForkFixedEpoch"
rollbackFork :: IOManager -> [(Text, Text)] -> Assertion
rollbackFork =
withFullConfig configDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a Babbage tx
void $
Api.withBabbageFindLeaderAndSubmitTx interpreter mockServer $
Babbage.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500
-- Fill the rest of the epoch
epoch0 <- Api.fillUntilNextEpoch interpreter mockServer
-- Create a point to rollback to
epoch1 <- Api.fillEpochPercentage interpreter mockServer 85
-- Fill the rest of the epoch
epoch1' <- Api.fillUntilNextEpoch interpreter mockServer
-- Forge a Conway tx
blk <-
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500 0
-- Wait for it to sync
assertBlockNoBackoff dbSync $ 1 + length (epoch0 <> epoch1 <> epoch1' <> [blk])
-- Rollback
rollbackBlocks <- Api.rollbackTo interpreter mockServer (blockPoint $ last epoch1)
-- Replay remaining blocks
forM_ (epoch1' <> [blk]) (atomically . addBlock mockServer)
-- Verify block count
assertBlockNoBackoff dbSync $ length (epoch0 <> epoch1 <> rollbackBlocks <> epoch1' <> [blk])
where
configDir = "config-conway-hf-epoch1"
testLabel = "conwayRollbackFork"
forkParam :: IOManager -> [(Text, Text)] -> Assertion
forkParam =
withFullConfig configDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Protocol params aren't added to the DB until the following epoch
epoch0 <- Api.fillUntilNextEpoch interpreter mockServer
-- Wait for it to sync
assertBlockNoBackoff dbSync (length epoch0)
-- Protocol major version should still match config
assertEqBackoff
dbSync
(queryCurrentMajVer interpreter)
(Just 7)
[]
"Unexpected protocol major version"
-- Propose a parameter update
void $
Api.withBabbageFindLeaderAndSubmitTx interpreter mockServer $
const Babbage.mkParamUpdateTx
-- Wait for it to sync
assertBlockNoBackoff dbSync (1 + length epoch0)
-- Query protocol param proposals
assertEqBackoff
dbSync
(queryMajVerProposal interpreter)
(Just 9)
[]
"Unexpected protocol major version proposal"
-- The fork will be applied on the first block of the next epoch
epoch1 <- Api.fillUntilNextEpoch interpreter mockServer
-- Wait for it to sync
assertBlockNoBackoff dbSync $ 1 + length (epoch0 <> epoch1)
-- Protocol major version should now be updated
assertEqBackoff
dbSync
(queryCurrentMajVer interpreter)
(Just 9)
[]
"Unexpected protocol major version"
-- Add a simple Conway tx
void $
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500 0
-- Wait for it to sync
assertBlockNoBackoff dbSync $ 2 + length (epoch0 <> epoch1)
where
testLabel = "conwayForkParam"
configDir = babbageConfigDir
queryCurrentMajVer interpreter = do
epochNo <- getEpochNo interpreter
mEpochParam <- DB.queryEpochParamWithEpochNo epochNo
pure $ DB.epochParamProtocolMajor <$> mEpochParam
queryMajVerProposal interpreter = do
epochNo <- getEpochNo interpreter
prop <- DB.queryParamProposalWithEpochNo epochNo
pure (DB.paramProposalProtocolMajor =<< prop)
getEpochNo = fmap unEpochNo . liftIO . getCurrentEpoch