Skip to content

Commit 35823f6

Browse files
committed
test(cardano-chain-gen): Add a "new constitution" Conway test
1 parent 5eb3119 commit 35823f6

File tree

6 files changed

+137
-5
lines changed

6 files changed

+137
-5
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ module Cardano.Mock.Forging.Tx.Conway (
3333
mkMultiAssetsScriptTx,
3434
mkDepositTxPools,
3535
mkRegisterDRepTx,
36+
mkCommitteeAuthTx,
37+
mkNewConstitutionTx,
3638
mkDummyRegisterTx,
3739
mkDummyTxBody,
3840
mkTxDelegCert,
@@ -452,6 +454,14 @@ mkRegisterDRepTx cred = mkDCertTx [cert] (Withdrawals mempty) Nothing
452454
cert = ConwayTxCertGov (ConwayRegDRep cred deposit SNothing)
453455
deposit = Coin 500_000_000
454456

457+
mkCommitteeAuthTx ::
458+
Credential 'ColdCommitteeRole StandardCrypto ->
459+
Credential 'HotCommitteeRole StandardCrypto ->
460+
Either ForgingError (AlonzoTx StandardConway)
461+
mkCommitteeAuthTx cold hot = mkDCertTx [cert] (Withdrawals mempty) Nothing
462+
where
463+
cert = ConwayTxCertGov (ConwayAuthCommitteeHotKey cold hot)
464+
455465
mkDummyRegisterTx :: Int -> Int -> Either ForgingError (AlonzoTx StandardConway)
456466
mkDummyRegisterTx n m = mkDCertTx consDelegCert (Withdrawals mempty) Nothing
457467
where
@@ -504,6 +514,14 @@ mkAddCommitteeTx cred = mkGovActionProposalTx govAction
504514
newMembers = Map.singleton cred (EpochNo 20)
505515
threshold = fromJust $ boundRational (1 % 1)
506516

517+
mkNewConstitutionTx ::
518+
Anchor StandardCrypto ->
519+
AlonzoTx StandardConway
520+
mkNewConstitutionTx anchor = mkGovActionProposalTx govAction
521+
where
522+
govAction = Governance.NewConstitution SNothing constitution
523+
constitution = Governance.Constitution anchor SNothing
524+
507525
mkGovActionProposalTx ::
508526
Governance.GovAction StandardConway ->
509527
AlonzoTx StandardConway

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,28 @@ registeredShelleyGenesisKeys =
264264
, KeyHash "471cc34983f6a2fd7b4018e3147532185d69a448d6570d46019e58e6"
265265
]
266266

267-
bootstrapCommitteeCreds :: [Credential 'ColdCommitteeRole StandardCrypto]
267+
bootstrapCommitteeCreds ::
268+
[ ( Credential 'ColdCommitteeRole StandardCrypto
269+
, Credential 'HotCommitteeRole StandardCrypto
270+
)
271+
]
268272
bootstrapCommitteeCreds =
269-
[ KeyHashObj $ KeyHash "2c698e41831684b16477fb50082b0c0e396d436504e39037d5366582"
270-
, KeyHashObj $ KeyHash "8fc13431159fdda66347a38c55105d50d77d67abc1c368b876d52ad1"
271-
, KeyHashObj $ KeyHash "921e1ccb4812c4280510c9ccab81c561f3d413e7d744d48d61215d1f"
272-
, KeyHashObj $ KeyHash "d5d09d9380cf9dcde1f3c6cd88b08ca9e00a3d550022ca7ee4026342"
273+
[
274+
( ScriptHashObj $ ScriptHash "2c698e41831684b16477fb50082b0c0e396d436504e39037d5366582"
275+
, KeyHashObj $ KeyHash "583a4c8d5f9b3769f98135a2cc041a3118586bd5c74ca72e808af73b"
276+
)
277+
,
278+
( ScriptHashObj $ ScriptHash "8fc13431159fdda66347a38c55105d50d77d67abc1c368b876d52ad1"
279+
, KeyHashObj $ KeyHash "39c898a713b67e7e0ed2345753db246f0d812669445488943a9f851e"
280+
)
281+
,
282+
( ScriptHashObj $ ScriptHash "921e1ccb4812c4280510c9ccab81c561f3d413e7d744d48d61215d1f"
283+
, KeyHashObj $ KeyHash "7cad8428ef51c1eb1f916be74e43ad49fd022482484a770d43b003ea"
284+
)
285+
,
286+
( ScriptHashObj $ ScriptHash "d5d09d9380cf9dcde1f3c6cd88b08ca9e00a3d550022ca7ee4026342"
287+
, KeyHashObj $ KeyHash "ca283340187f1b87e871d903c0178ddfbf4aa896a398787ceba20f98"
288+
)
273289
]
274290

275291
unregisteredDRepIds :: [Credential 'DRepRole StandardCrypto]

cardano-chain-gen/src/Cardano/Mock/Query.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Cardano.Mock.Query (
99
queryTxMetadataCount,
1010
queryDRepDistrAmount,
1111
queryGovActionCounts,
12+
queryConstitutionAnchor,
1213
) where
1314

1415
import qualified Cardano.Db as Db
@@ -114,3 +115,27 @@ queryGovActionCounts = do
114115
pure countRows
115116

116117
pure (maybe 0 unValue res)
118+
119+
queryConstitutionAnchor ::
120+
MonadIO io =>
121+
Word64 ->
122+
ReaderT SqlBackend io (Maybe (Text, ByteString))
123+
queryConstitutionAnchor epochNo = do
124+
res <- selectOne $ do
125+
(_ :& anchor :& epochState) <-
126+
from
127+
$ table @Db.Constitution
128+
`innerJoin` table @Db.VotingAnchor
129+
`on` ( \(constit :& anchor) ->
130+
(constit ^. Db.ConstitutionVotingAnchorId) ==. (anchor ^. Db.VotingAnchorId)
131+
)
132+
`innerJoin` table @Db.EpochState
133+
`on` ( \(constit :& _ :& epoch) ->
134+
just (constit ^. Db.ConstitutionId) ==. (epoch ^. Db.EpochStateConstitutionId)
135+
)
136+
137+
where_ (epochState ^. Db.EpochStateEpochNo ==. val epochNo)
138+
139+
pure (anchor ^. Db.VotingAnchorUrl, anchor ^. Db.VotingAnchorDataHash)
140+
141+
pure $ bimap (Db.unVoteUrl . unValue) unValue <$> res

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
@@ -219,6 +219,7 @@ unitTests iom knownMigrations =
219219
"Governance"
220220
[ test "drep distribution" Governance.drepDistr
221221
, test "new committee member" Governance.newCommittee
222+
, test "update constitution" Governance.updateConstitution
222223
]
223224
]
224225
where

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE NumericUnderscores #-}
33
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TypeApplications #-}
45

56
module Test.Cardano.Db.Mock.Unit.Conway.Governance (
67
drepDistr,
78
newCommittee,
9+
updateConstitution,
810
) where
911

1012
import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
13+
import Cardano.Ledger.BaseTypes (AnchorData (..), hashAnchorData, textToUrl)
1114
import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..), Voter (..))
15+
import qualified Cardano.Ledger.Conway.Governance as Governance
1216
import Cardano.Ledger.Core (txIdTx)
1317
import Cardano.Ledger.Credential (Credential (..))
1418
import Cardano.Ledger.Keys (KeyHash (..))
19+
import Cardano.Ledger.SafeHash (SafeToHash (..))
1520
import Cardano.Mock.ChainSync.Server (IOManager)
21+
import Cardano.Mock.Forging.Interpreter (getCurrentEpoch)
1622
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
1723
import qualified Cardano.Mock.Forging.Tx.Generic as Forging
1824
import Cardano.Mock.Forging.Types
1925
import qualified Cardano.Mock.Query as Query
2026
import Cardano.Prelude
27+
import Cardano.Slotting.Slot (EpochNo (..))
28+
import Data.Maybe (fromJust)
29+
import qualified Ouroboros.Consensus.Shelley.Eras as Consensus
2130
import Test.Cardano.Db.Mock.Config
2231
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
2332
import Test.Cardano.Db.Mock.Validate
@@ -104,3 +113,65 @@ newCommittee =
104113
"Unexpected committee hashes"
105114
where
106115
testLabel = "conwayNewCommittee"
116+
117+
updateConstitution :: IOManager -> [(Text, Text)] -> Assertion
118+
updateConstitution =
119+
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
120+
startDBSync dbSync
121+
122+
-- Add stake
123+
void (Api.registerAllStakeCreds interpreter server)
124+
125+
-- Register a DRep and delegate votes to it
126+
void (Api.registerDRepsAndDelegateVotes interpreter server)
127+
128+
-- DRep distribution is calculated at end of the current epoch
129+
epoch0 <- Api.fillUntilNextEpoch interpreter server
130+
131+
-- Register committee hot credentials
132+
void $
133+
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
134+
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
135+
136+
let newUrl = fromJust (textToUrl 64 "constitution.new")
137+
dataHash = hashAnchorData @Consensus.StandardCrypto (AnchorData "constitution content")
138+
anchor = Governance.Anchor newUrl dataHash
139+
140+
-- Create and vote for a governance proposal
141+
void $
142+
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ -> do
143+
let
144+
-- Create gov action tx
145+
proposalTx = Conway.mkNewConstitutionTx anchor
146+
147+
-- Create votes
148+
addVoteTx =
149+
Conway.mkGovVoteTx
150+
govActionId
151+
( DRepVoter (Prelude.head Forging.unregisteredDRepIds)
152+
: map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
153+
)
154+
govActionId =
155+
GovActionId
156+
{ gaidTxId = txIdTx proposalTx
157+
, gaidGovActionIx = GovActionIx 0
158+
}
159+
160+
pure [proposalTx, addVoteTx]
161+
162+
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
163+
-- epoch and enacted on the following.
164+
epoch1 <- Api.fillEpochs interpreter server 2
165+
166+
-- Wait for it to sync
167+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4)
168+
169+
-- Constitution should now be updated
170+
(EpochNo epochNo) <- getCurrentEpoch interpreter
171+
assertEqQuery
172+
dbSync
173+
(Query.queryConstitutionAnchor epochNo)
174+
(Just ("constitution.new", originalBytes dataHash))
175+
"Unexpected constution voting anchor"
176+
where
177+
testLabel = "conwayUpdateConstitution"
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,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)