Skip to content

Commit fab9cc2

Browse files
committed
refactor: Factor out governance initialization for tests
1 parent 27f5505 commit fab9cc2

File tree

6 files changed

+77
-114
lines changed

6 files changed

+77
-114
lines changed

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
module Cardano.Mock.Forging.Tx.Conway.Scenarios (
1111
delegateAndSendBlocks,
1212
registerDRepsAndDelegateVotes,
13+
registerCommitteeCreds,
1314
) where
1415

1516
import Cardano.Ledger.Address (Addr (..), Withdrawals (..))
@@ -120,3 +121,10 @@ registerDRepAndDelegateVotes' drepId stakeIx ledger = do
120121
delegTx <- Conway.mkDCertTx [regDelegCert] (Withdrawals mempty) Nothing
121122

122123
pure [paymentTx, regTx, delegTx]
124+
125+
registerCommitteeCreds :: Interpreter -> IO CardanoBlock
126+
registerCommitteeCreds interpreter = do
127+
let txs' = mapM (uncurry Conway.mkCommitteeAuthTx) bootstrapCommitteeCreds
128+
blockTxs <- withConwayLedgerState interpreter $ const txs'
129+
130+
forgeNextFindLeader interpreter (map TxConway blockTxs)

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module Cardano.Mock.Forging.Tx.Generic (
2626
registeredShelleyGenesisKeys,
2727
bootstrapCommitteeCreds,
2828
unregisteredDRepIds,
29+
spoVoters,
30+
committeeVoters,
31+
drepVoters,
2932
consPoolParams,
3033
getPoolStakeCreds,
3134
) where
@@ -36,6 +39,7 @@ import qualified Cardano.Crypto.Hash as Hash
3639
import Cardano.Ledger.Address
3740
import Cardano.Ledger.BaseTypes
3841
import Cardano.Ledger.Coin (Coin (..))
42+
import Cardano.Ledger.Conway.Governance (Voter (..))
3943
import qualified Cardano.Ledger.Core as Core
4044
import Cardano.Ledger.Credential
4145
import Cardano.Ledger.Crypto (ADDRHASH)
@@ -51,7 +55,7 @@ import qualified Cardano.Ledger.UMap as UMap
5155
import Cardano.Mock.Forging.Crypto
5256
import Cardano.Mock.Forging.Tx.Alonzo.ScriptsExamples
5357
import Cardano.Mock.Forging.Types
54-
import Cardano.Prelude hiding (length, (.))
58+
import Cardano.Prelude
5559
import Data.Coerce (coerce)
5660
import Data.List (nub)
5761
import Data.List.Extra ((!?))
@@ -64,6 +68,7 @@ import Ouroboros.Consensus.Cardano.Block (LedgerState)
6468
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
6569
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
6670
import qualified Ouroboros.Consensus.Shelley.Ledger.Ledger as Consensus
71+
import Prelude ((!!))
6772

6873
resolveAddress ::
6974
forall era p.
@@ -308,6 +313,22 @@ mkDummyScriptHash n = ScriptHash $ mkDummyHash (Proxy @(ADDRHASH StandardCrypto)
308313
mkDummyHash :: forall h a. HashAlgorithm h => Proxy h -> Int -> Hash.Hash h a
309314
mkDummyHash _ = coerce . hashWithSerialiser @h toCBOR
310315

316+
spoVoters ::
317+
EraCrypto era ~ StandardCrypto =>
318+
LedgerState (ShelleyBlock proto era) ->
319+
[Voter StandardCrypto]
320+
spoVoters ledger =
321+
[ StakePoolVoter (resolvePool (PoolIndex 0) ledger)
322+
, StakePoolVoter (resolvePool (PoolIndex 1) ledger)
323+
, StakePoolVoter (resolvePool (PoolIndex 2) ledger)
324+
]
325+
326+
committeeVoters :: [Voter StandardCrypto]
327+
committeeVoters = map (CommitteeVoter . snd) bootstrapCommitteeCreds
328+
329+
drepVoters :: [Voter StandardCrypto]
330+
drepVoters = maybeToList (DRepVoter <$> head unregisteredDRepIds)
331+
311332
consPoolParams ::
312333
KeyHash 'StakePool StandardCrypto ->
313334
StakeCredential StandardCrypto ->

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

+24
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ module Test.Cardano.Db.Mock.UnifiedApi (
2020
fillEpochs,
2121
fillEpochPercentage,
2222
rollbackTo,
23+
initGovernance,
2324
registerAllStakeCreds,
2425
registerDRepsAndDelegateVotes,
26+
registerCommitteeCreds,
2527
) where
2628

2729
import Cardano.Ledger.Alonzo (AlonzoEra)
@@ -203,6 +205,22 @@ rollbackTo interpreter mockServer point = do
203205
rollbackInterpreter interpreter point
204206
atomically $ rollback mockServer point
205207

208+
initGovernance :: Interpreter -> ServerHandle IO CardanoBlock -> IO [CardanoBlock]
209+
initGovernance interpreter mockServer = do
210+
-- Add stake
211+
blk0 <- registerAllStakeCreds interpreter mockServer
212+
213+
-- Register a DRep and delegate votes to it
214+
blk1 <- registerDRepsAndDelegateVotes interpreter mockServer
215+
216+
-- DRep distribution is calculated a-*t end of the current epoch
217+
epoch <- fillUntilNextEpoch interpreter mockServer
218+
219+
-- Register committee hot credentials
220+
blk2 <- registerCommitteeCreds interpreter mockServer
221+
222+
pure ([blk0, blk1] ++ epoch ++ [blk2])
223+
206224
registerAllStakeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock
207225
registerAllStakeCreds interpreter mockServer = do
208226
blk <- forgeWithStakeCreds interpreter
@@ -215,6 +233,12 @@ registerDRepsAndDelegateVotes interpreter mockServer = do
215233
atomically (addBlock mockServer blk)
216234
pure blk
217235

236+
registerCommitteeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock
237+
registerCommitteeCreds interpreter mockServer = do
238+
blk <- Conway.registerCommitteeCreds interpreter
239+
atomically (addBlock mockServer blk)
240+
pure blk
241+
218242
-- Expected number. This should be taken from the parameters, instead of hardcoded.
219243
blocksPerEpoch :: Int
220244
blocksPerEpoch = 100

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

+21-111
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Cardano.Ledger.Address (RewardAccount (..))
2323
import Cardano.Ledger.BaseTypes (AnchorData (..), Network (..), ProtVer (..), hashAnchorData, textToUrl)
2424
import Cardano.Ledger.Binary.Version (natVersion)
2525
import Cardano.Ledger.Coin (Coin (..))
26-
import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..), Voter (..))
26+
import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..))
2727
import qualified Cardano.Ledger.Conway.Governance as Governance
2828
import Cardano.Ledger.Conway.PParams (ppuGovActionDepositL)
2929
import Cardano.Ledger.Core (emptyPParamsUpdate, txIdTx)
@@ -33,6 +33,7 @@ import Cardano.Ledger.SafeHash (SafeToHash (..))
3333
import Cardano.Mock.ChainSync.Server (IOManager)
3434
import Cardano.Mock.Forging.Interpreter (getCurrentEpoch)
3535
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
36+
import Cardano.Mock.Forging.Tx.Generic (committeeVoters, drepVoters, spoVoters)
3637
import qualified Cardano.Mock.Forging.Tx.Generic as Forging
3738
import Cardano.Mock.Forging.Types
3839
import qualified Cardano.Mock.Query as Query
@@ -53,17 +54,10 @@ drepDistr =
5354
withFullConfigAndDropDB conwayConfigDir testLabel $ \interpreter server dbSync -> do
5455
startDBSync dbSync
5556

56-
-- Add stake
57-
void (Api.registerAllStakeCreds interpreter server)
58-
59-
-- Register DRep and delegate votes to it
60-
void (Api.registerDRepsAndDelegateVotes interpreter server)
61-
62-
-- DRep distribution is calculated at end of the current epoch
63-
epoch1 <- Api.fillUntilNextEpoch interpreter server
57+
epoch0 <- Api.initGovernance interpreter server
6458

6559
-- Wait for it to sync
66-
assertBlockNoBackoff dbSync (length epoch1 + 2)
60+
assertBlockNoBackoff dbSync (length epoch0)
6761

6862
-- Should now have a DRep distribution
6963
let drepId = Prelude.head Forging.unregisteredDRepIds
@@ -80,11 +74,7 @@ newCommittee =
8074
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
8175
startDBSync dbSync
8276

83-
-- Add stake
84-
void (Api.registerAllStakeCreds interpreter server)
85-
86-
-- Register a DRep and delegate votes to it
87-
void (Api.registerDRepsAndDelegateVotes interpreter server)
77+
epoch0 <- Api.initGovernance interpreter server
8878

8979
-- Create and vote for gov action
9080
let committeeHash = "e0a714319812c3f773ba04ec5d6b3ffcd5aad85006805b047b082541"
@@ -95,16 +85,8 @@ newCommittee =
9585
let
9686
-- Create gov action tx
9787
addCcTx = Conway.mkAddCommitteeTx committeeCred
98-
-- Create votes for all stake pools. We start in the Conway bootstrap phase, so
99-
-- DRep votes are not yet required.
100-
addVoteTx =
101-
Conway.mkGovVoteTx
102-
govActionId
103-
[ DRepVoter (Prelude.head Forging.unregisteredDRepIds)
104-
, StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger)
105-
, StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger)
106-
, StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger)
107-
]
88+
-- Create votes for all stake pools
89+
addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ spoVoters ledger)
10890
govActionId =
10991
GovActionId
11092
{ gaidTxId = txIdTx addCcTx
@@ -116,10 +98,10 @@ newCommittee =
11698

11799
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
118100
-- epoch and enacted on the following.
119-
epochs <- Api.fillEpochs interpreter server 2
101+
epoch1 <- Api.fillEpochs interpreter server 2
120102

121103
-- Wait for it to sync
122-
assertBlockNoBackoff dbSync (length epochs + 3)
104+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1)
123105
-- Should now have a committee member
124106
assertEqQuery
125107
dbSync
@@ -134,19 +116,7 @@ updateConstitution =
134116
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
135117
startDBSync dbSync
136118

137-
-- Add stake
138-
void (Api.registerAllStakeCreds interpreter server)
139-
140-
-- Register a DRep and delegate votes to it
141-
void (Api.registerDRepsAndDelegateVotes interpreter server)
142-
143-
-- DRep distribution is calculated at end of the current epoch
144-
epoch0 <- Api.fillUntilNextEpoch interpreter server
145-
146-
-- Register committee hot credentials
147-
void $
148-
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
149-
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
119+
epoch0 <- Api.initGovernance interpreter server
150120

151121
let newUrl = fromJust (textToUrl 64 "constitution.new")
152122
dataHash = hashAnchorData @Consensus.StandardCrypto (AnchorData "constitution content")
@@ -160,12 +130,7 @@ updateConstitution =
160130
proposalTx = Conway.mkNewConstitutionTx anchor
161131

162132
-- Create votes
163-
addVoteTx =
164-
Conway.mkGovVoteTx
165-
govActionId
166-
( DRepVoter (Prelude.head Forging.unregisteredDRepIds)
167-
: map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
168-
)
133+
addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ committeeVoters)
169134
govActionId =
170135
GovActionId
171136
{ gaidTxId = txIdTx proposalTx
@@ -179,7 +144,7 @@ updateConstitution =
179144
epoch1 <- Api.fillEpochs interpreter server 2
180145

181146
-- Wait for it to sync
182-
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4)
147+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1)
183148

184149
-- Constitution should now be updated
185150
(EpochNo epochNo) <- getCurrentEpoch interpreter
@@ -196,20 +161,7 @@ treasuryWithdrawal =
196161
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
197162
startDBSync dbSync
198163

199-
-- Add stake
200-
void (Api.registerAllStakeCreds interpreter server)
201-
202-
-- Register a DRep and delegate votes to it
203-
void (Api.registerDRepsAndDelegateVotes interpreter server)
204-
205-
-- DRep distribution is calculated at end of the current epoch
206-
epoch0 <- Api.fillUntilNextEpoch interpreter server
207-
208-
-- Register committee hot credentials
209-
-- TODO[sgillespie]: Let's get this in UnifiedApi or something
210-
void $
211-
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
212-
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
164+
epoch0 <- Api.initGovernance interpreter server
213165

214166
-- Make sure we have treasury to spend
215167
void $
@@ -228,12 +180,7 @@ treasuryWithdrawal =
228180
rewardAccount
229181
(Coin 10_000)
230182

231-
addVoteTx =
232-
Conway.mkGovVoteTx
233-
govActionId
234-
( DRepVoter (Prelude.head Forging.unregisteredDRepIds)
235-
: map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
236-
)
183+
addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ committeeVoters)
237184

238185
govActionId =
239186
GovActionId
@@ -248,7 +195,7 @@ treasuryWithdrawal =
248195
epoch1 <- Api.fillEpochs interpreter server 2
249196

250197
-- Wait for it to sync
251-
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 5)
198+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 2)
252199

253200
-- Should now have a treasury reward
254201
assertEqQuery
@@ -264,20 +211,7 @@ paramChange =
264211
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
265212
startDBSync dbSync
266213

267-
-- Add stake
268-
void (Api.registerAllStakeCreds interpreter server)
269-
270-
-- Register a DRep and delegate votes to it
271-
void (Api.registerDRepsAndDelegateVotes interpreter server)
272-
273-
-- DRep distribution is calculated at end of the current epoch
274-
epoch0 <- Api.fillUntilNextEpoch interpreter server
275-
276-
-- Register committee hot credentials
277-
-- TODO[sgillespie]: Let's get this in UnifiedApi or something
278-
void $
279-
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
280-
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
214+
epoch0 <- Api.initGovernance interpreter server
281215

282216
-- Create and vote for a governance proposal
283217
void $
@@ -289,13 +223,7 @@ paramChange =
289223
addVoteTx =
290224
Conway.mkGovVoteTx
291225
govActionId
292-
( map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
293-
++ [ DRepVoter (Prelude.head Forging.unregisteredDRepIds)
294-
, StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger)
295-
, StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger)
296-
, StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger)
297-
]
298-
)
226+
(committeeVoters ++ drepVoters ++ spoVoters ledger)
299227

300228
govActionId =
301229
GovActionId
@@ -310,7 +238,7 @@ paramChange =
310238
epoch1 <- Api.fillEpochs interpreter server 2
311239

312240
-- Wait for it to synch
313-
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4)
241+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1)
314242

315243
epochNo <- unEpochNo <$> getCurrentEpoch interpreter
316244

@@ -328,20 +256,7 @@ hardFork =
328256
withFullConfig configDir testLabel $ \interpreter server dbSync -> do
329257
startDBSync dbSync
330258

331-
-- Add stake
332-
void (Api.registerAllStakeCreds interpreter server)
333-
334-
-- Register a DRep and delegate votes to it
335-
void (Api.registerDRepsAndDelegateVotes interpreter server)
336-
337-
-- DRep distribution is calculated at end of the current epoch
338-
epoch0 <- Api.fillUntilNextEpoch interpreter server
339-
340-
-- Register committee hot credentials
341-
-- TODO[sgillespie]: Let's get this in UnifiedApi or something
342-
void $
343-
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
344-
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
259+
epoch0 <- Api.initGovernance interpreter server
345260

346261
-- Create and vote for a governance proposal
347262
void $
@@ -352,12 +267,7 @@ hardFork =
352267
addVoteTx =
353268
Conway.mkGovVoteTx
354269
govActionId
355-
( map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
356-
++ [ StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger)
357-
, StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger)
358-
, StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger)
359-
]
360-
)
270+
(committeeVoters ++ spoVoters ledger)
361271

362272
govActionId =
363273
GovActionId
@@ -372,7 +282,7 @@ hardFork =
372282
epoch1 <- Api.fillEpochs interpreter server 2
373283

374284
-- Wait for it to synch
375-
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4)
285+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1)
376286

377287
epochNo <- getCurrentEpoch interpreter
378288

Original file line numberDiff line numberDiff line change
@@ -1 +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]
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]
Original file line numberDiff line numberDiff line change
@@ -1 +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,1001]
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,1001,1005,1008,1014,1019,1020,1021,1026,1027,1031,1032,1033,1036,1037,1049,1050,1053,1057,1062,1067,1068,1070,1074,1083,1102,1104,1107,1111,1115,1117,1118,1120,1125,1127,1137,1149,1151,1155,1161,1164,1167,1174,1187,1200,1201,1206,1213,1218,1221,1237,1242,1248,1258,1263,1266,1272,1277,1286,1299,1300,1304,1309,1313,1317,1336,1338,1343,1356,1363,1366,1376,1377,1379,1390,1397,1401,1408,1409,1410,1418,1423,1424,1429,1432,1435,1438,1439,1442,1444,1449,1453,1454,1461,1462,1470,1473,1474,1481,1484,1486,1504]

0 commit comments

Comments
 (0)