Skip to content

Commit b8d12a5

Browse files
committed
test: Add a new committee member rollback test
1 parent 662acc9 commit b8d12a5

File tree

3 files changed

+95
-30
lines changed

3 files changed

+95
-30
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ unitTests iom knownMigrations =
246246
, test "parameter change" Governance.parameterChange
247247
, test "hard fork" Governance.hardFork
248248
, test "info action" Governance.infoAction
249+
, test "rollback new committee member" Governance.rollbackNewCommittee
249250
]
250251
]
251252
where

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

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Test.Cardano.Db.Mock.Unit.Conway.Governance (
1616
parameterChange,
1717
hardFork,
1818
infoAction,
19+
rollbackNewCommittee,
1920
) where
2021

2122
import qualified Cardano.Db as Db
@@ -29,8 +30,8 @@ import Cardano.Ledger.Core (txIdTx)
2930
import Cardano.Ledger.Credential (Credential (..))
3031
import Cardano.Ledger.Keys (KeyHash (..))
3132
import Cardano.Ledger.SafeHash (SafeToHash (..))
32-
import Cardano.Mock.ChainSync.Server (IOManager)
33-
import Cardano.Mock.Forging.Interpreter (getCurrentEpoch)
33+
import Cardano.Mock.ChainSync.Server (IOManager, ServerHandle)
34+
import Cardano.Mock.Forging.Interpreter (Interpreter, getCurrentEpoch)
3435
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
3536
import qualified Cardano.Mock.Forging.Tx.Generic as Forging
3637
import Cardano.Mock.Forging.Types
@@ -39,11 +40,13 @@ import Cardano.Prelude
3940
import Cardano.Slotting.Slot (EpochNo (..))
4041
import qualified Data.Map as Map
4142
import Data.Maybe (fromJust)
43+
import Data.Maybe.Strict (StrictMaybe (..))
4244
import qualified Ouroboros.Consensus.Shelley.Eras as Consensus
45+
import Ouroboros.Network.Block (blockPoint)
4346
import Test.Cardano.Db.Mock.Config
4447
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
4548
import Test.Cardano.Db.Mock.Validate
46-
import Test.Tasty.HUnit (Assertion)
49+
import Test.Tasty.HUnit (Assertion, assertFailure)
4750
import qualified Prelude
4851

4952
drepDistr :: IOManager -> [(Text, Text)] -> Assertion
@@ -80,48 +83,108 @@ newCommittee =
8083

8184
-- Add stake
8285
void (Api.registerAllStakeCreds interpreter server)
83-
8486
-- Register a DRep and delegate votes to it
8587
void (Api.registerDRepsAndDelegateVotes interpreter server)
8688

87-
-- Create and vote for gov action
88-
let committeeHash = "e0a714319812c3f773ba04ec5d6b3ffcd5aad85006805b047b082541"
89-
committeeCred = KeyHashObj (KeyHash committeeHash)
89+
-- Propose, ratify, and enact a new committee member
90+
epochs <- enactNewCommittee interpreter server
9091

91-
void $
92-
Api.withConwayFindLeaderAndSubmit interpreter server $ \ledger -> do
93-
let
94-
-- Create gov action tx
95-
addCcTx = Conway.mkAddCommitteeTx committeeCred
96-
-- Create votes for all stake pools. We start in the Conway bootstrap phase, so
97-
-- DRep votes are not yet required.
98-
addVoteTx =
99-
Conway.mkGovVoteYesTx
100-
govActionId
101-
(Forging.drepVoters ++ Forging.resolveStakePoolVoters ledger)
102-
govActionId =
103-
GovActionId
104-
{ gaidTxId = txIdTx addCcTx
105-
, gaidGovActionIx = GovActionIx 0
106-
}
92+
-- Wait for it to sync
93+
assertBlockNoBackoff dbSync (length epochs + 2)
94+
-- Should now have a committee member
95+
assertEqQuery
96+
dbSync
97+
Query.queryGovActionCounts
98+
(1, 1, 0, 0)
99+
"Unexpected governance action counts"
100+
where
101+
testLabel = "conwayNewCommittee"
107102

108-
-- Create votes
109-
pure [addCcTx, addVoteTx]
103+
rollbackNewCommittee :: IOManager -> [(Text, Text)] -> Assertion
104+
rollbackNewCommittee =
105+
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
106+
startDBSync dbSync
110107

111-
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
112-
-- epoch and enacted on the following.
113-
epochs <- Api.fillEpochs interpreter server 2
108+
-- Add stake
109+
void (Api.registerAllStakeCreds interpreter server)
110+
-- Register a DRep and delegate votes to it
111+
void (Api.registerDRepsAndDelegateVotes interpreter server)
114112

113+
-- Propose, ratify, and enact a new committee member
114+
epoch2 <- enactNewCommittee interpreter server
115115
-- Wait for it to sync
116-
assertBlockNoBackoff dbSync (length epochs + 3)
116+
assertBlockNoBackoff dbSync (length epoch2 + 2)
117117
-- Should now have a committee member
118118
assertEqQuery
119119
dbSync
120120
Query.queryGovActionCounts
121121
(1, 1, 0, 0)
122122
"Unexpected governance action counts"
123+
124+
-- Rollback the last 2 blocks
125+
epoch1' <- rollbackBlocks interpreter server 2 epoch2
126+
-- Wait for it to sync
127+
assertBlockNoBackoff dbSync (length epoch1' + 4)
128+
-- Should not have a new committee member
129+
assertEqQuery
130+
dbSync
131+
Query.queryGovActionCounts
132+
(1, 0, 0, 0)
133+
"Unexpected governance action counts"
123134
where
124-
testLabel = "conwayNewCommittee"
135+
testLabel = "conwayRollbackNewCommittee"
136+
137+
enactNewCommittee :: Interpreter -> ServerHandle IO CardanoBlock -> IO [CardanoBlock]
138+
enactNewCommittee interpreter server = do
139+
-- Create and vote for gov action
140+
let committeeHash = "e0a714319812c3f773ba04ec5d6b3ffcd5aad85006805b047b082541"
141+
committeeCred = KeyHashObj (KeyHash committeeHash)
142+
143+
blk <-
144+
Api.withConwayFindLeaderAndSubmit interpreter server $ \ledger -> do
145+
let
146+
-- Create gov action tx
147+
addCcTx = Conway.mkAddCommitteeTx committeeCred
148+
-- Create votes for all stake pools. We start in the Conway bootstrap phase, so
149+
-- DRep votes are not yet required.
150+
addVoteTx =
151+
Conway.mkGovVoteYesTx
152+
govActionId
153+
(Forging.drepVoters ++ Forging.resolveStakePoolVoters ledger)
154+
govActionId =
155+
GovActionId
156+
{ gaidTxId = txIdTx addCcTx
157+
, gaidGovActionIx = GovActionIx 0
158+
}
159+
160+
-- Create votes
161+
pure [addCcTx, addVoteTx]
162+
163+
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
164+
-- epoch and enacted on the following.
165+
epochs <- Api.fillEpochs interpreter server 2
166+
pure (blk : epochs)
167+
168+
rollbackBlocks ::
169+
Interpreter ->
170+
ServerHandle IO CardanoBlock ->
171+
Int ->
172+
[CardanoBlock] ->
173+
IO [CardanoBlock]
174+
rollbackBlocks interpreter server n blocks = do
175+
(rollbackPoint, blocks') <-
176+
case drop n (reverse blocks) of
177+
(blk : blks) -> pure (blockPoint blk, blks)
178+
[] -> assertFailure "Expected at least 3 blocks"
179+
180+
-- Rollback to the previous epoch
181+
Api.rollbackTo interpreter server rollbackPoint
182+
-- Create a fork
183+
newBlock <-
184+
Api.withConwayFindLeaderAndSubmitTx interpreter server $
185+
Conway.mkSimpleDCertTx [(StakeIndexNew 1, Conway.mkRegTxCert SNothing)]
186+
187+
pure (blocks' ++ [newBlock])
125188

126189
updateConstitution :: IOManager -> [(Text, Text)] -> Assertion
127190
updateConstitution =
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,1001,997]

0 commit comments

Comments
 (0)