Skip to content

Commit d9074bd

Browse files
committed
test(cardano-chain-gen): Add a new committee membership test
1 parent 3d9f094 commit d9074bd

File tree

6 files changed

+156
-28
lines changed

6 files changed

+156
-28
lines changed

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
{-# LANGUAGE NumericUnderscores #-}
44
{-# LANGUAGE OverloadedStrings #-}
55
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE TupleSections #-}
67
{-# LANGUAGE TypeApplications #-}
78
{-# LANGUAGE TypeFamilies #-}
89
{-# LANGUAGE TypeOperators #-}
910

1011
module Cardano.Mock.Forging.Tx.Conway (
12+
ConwayLedgerState,
1113
Babbage.TxOutScriptType (..),
1214
Babbage.DatumType (..),
1315
Babbage.ReferenceScript (..),
@@ -38,6 +40,9 @@ module Cardano.Mock.Forging.Tx.Conway (
3840
mkUnRegTxCert,
3941
mkDelegTxCert,
4042
mkRegDelegTxCert,
43+
mkAddCommitteeTx,
44+
mkGovActionProposalTx,
45+
mkGovVoteTx,
4146
Babbage.mkParamUpdateTx,
4247
mkFullTx,
4348
mkScriptMint',
@@ -54,10 +59,10 @@ import Cardano.Ledger.Alonzo.Scripts
5459
import Cardano.Ledger.Alonzo.Tx (IsValid (..))
5560
import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData (..), mkAlonzoTxAuxData)
5661
import Cardano.Ledger.Babbage.TxOut (BabbageEraTxOut, BabbageTxOut (..))
57-
import Cardano.Ledger.BaseTypes (EpochNo (..), Network (..))
62+
import Cardano.Ledger.BaseTypes
5863
import Cardano.Ledger.Binary (Sized (..))
5964
import Cardano.Ledger.Coin (Coin (..))
60-
import Cardano.Ledger.Conway.Governance (VotingProcedures (..))
65+
import qualified Cardano.Ledger.Conway.Governance as Governance
6166
import Cardano.Ledger.Conway.Scripts
6267
import Cardano.Ledger.Conway.Tx (AlonzoTx (..))
6368
import Cardano.Ledger.Conway.TxBody (ConwayTxBody (..))
@@ -88,7 +93,7 @@ import Cardano.Prelude
8893
import Data.List (nub)
8994
import qualified Data.List.NonEmpty as NonEmpty
9095
import qualified Data.Map as Map
91-
import Data.Maybe.Strict (StrictMaybe (..), maybeToStrictMaybe)
96+
import Data.Maybe (fromJust)
9297
import qualified Data.OSet.Strict as OSet
9398
import Data.Sequence.Strict (StrictSeq ())
9499
import qualified Data.Sequence.Strict as StrictSeq
@@ -131,7 +136,7 @@ consTxBody ins cols ref outs colOut fees minted certs withdrawals =
131136
, ctbScriptIntegrityHash = SNothing
132137
, ctbAdHash = SNothing
133138
, ctbTxNetworkId = SJust Testnet
134-
, ctbVotingProcedures = VotingProcedures mempty
139+
, ctbVotingProcedures = Governance.VotingProcedures mempty
135140
, ctbProposalProcedures = mempty
136141
, ctbCurrentTreasuryValue = SNothing
137142
, ctbTreasuryDonation = Coin 0
@@ -490,6 +495,52 @@ mkTxDelegCert ::
490495
ConwayTxCert StandardConway
491496
mkTxDelegCert f = ConwayTxCertDeleg . f
492497

498+
mkAddCommitteeTx ::
499+
Credential 'ColdCommitteeRole StandardCrypto ->
500+
AlonzoTx StandardConway
501+
mkAddCommitteeTx cred = mkGovActionProposalTx govAction
502+
where
503+
govAction = Governance.UpdateCommittee SNothing mempty newMembers threshold
504+
newMembers = Map.singleton cred (EpochNo 20)
505+
threshold = fromJust $ boundRational (1 % 1)
506+
507+
mkGovActionProposalTx ::
508+
Governance.GovAction StandardConway ->
509+
AlonzoTx StandardConway
510+
mkGovActionProposalTx govAction = mkSimpleTx True txBody
511+
where
512+
txBody = mkDummyTxBody {ctbProposalProcedures = OSet.singleton proposal}
513+
514+
proposal =
515+
Governance.ProposalProcedure
516+
{ Governance.pProcDeposit = Coin 50_000_000_000
517+
, Governance.pProcReturnAddr =
518+
RewardAccount Testnet (Prelude.head unregisteredStakeCredentials)
519+
, Governance.pProcGovAction = govAction
520+
, Governance.pProcAnchor = anchor
521+
}
522+
523+
anchor =
524+
Governance.Anchor
525+
{ Governance.anchorUrl = fromJust (textToUrl 64 "best.cc")
526+
, Governance.anchorDataHash = hashAnchorData (Governance.AnchorData mempty)
527+
}
528+
529+
mkGovVoteTx ::
530+
Governance.GovActionId StandardCrypto ->
531+
[Governance.Voter StandardCrypto] ->
532+
AlonzoTx StandardConway
533+
mkGovVoteTx govAction voters = mkSimpleTx True txBody
534+
where
535+
txBody = mkDummyTxBody {ctbVotingProcedures = Governance.VotingProcedures votes}
536+
votes = Map.fromList . map (,govActionVote) $ voters
537+
govActionVote = Map.singleton govAction vote
538+
vote =
539+
Governance.VotingProcedure
540+
{ Governance.vProcVote = Governance.VoteYes
541+
, Governance.vProcAnchor = SNothing
542+
}
543+
493544
mkDummyTxBody :: ConwayTxBody StandardConway
494545
mkDummyTxBody =
495546
consTxBody
@@ -554,7 +605,7 @@ mkFullTx n m state' = do
554605
, ctbScriptIntegrityHash = SNothing
555606
, ctbAdHash = SNothing
556607
, ctbTxNetworkId = SJust Testnet
557-
, ctbVotingProcedures = VotingProcedures mempty
608+
, ctbVotingProcedures = Governance.VotingProcedures mempty
558609
, ctbProposalProcedures = mempty
559610
, ctbCurrentTreasuryValue = SNothing
560611
, ctbTreasuryDonation = Coin 0

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Cardano.Mock.Forging.Tx.Generic (
2424
unregisteredPools,
2525
registeredByronGenesisKeys,
2626
registeredShelleyGenesisKeys,
27+
bootstrapDRepIds,
2728
consPoolParams,
2829
getPoolStakeCreds,
2930
) where
@@ -262,6 +263,14 @@ registeredShelleyGenesisKeys =
262263
, KeyHash "471cc34983f6a2fd7b4018e3147532185d69a448d6570d46019e58e6"
263264
]
264265

266+
bootstrapDRepIds :: [Credential 'DRepRole StandardCrypto]
267+
bootstrapDRepIds =
268+
[ KeyHashObj $ KeyHash "2c698e41831684b16477fb50082b0c0e396d436504e39037d5366582"
269+
, KeyHashObj $ KeyHash "8fc13431159fdda66347a38c55105d50d77d67abc1c368b876d52ad1"
270+
, KeyHashObj $ KeyHash "921e1ccb4812c4280510c9ccab81c561f3d413e7d744d48d61215d1f"
271+
, KeyHashObj $ KeyHash "d5d09d9380cf9dcde1f3c6cd88b08ca9e00a3d550022ca7ee4026342"
272+
]
273+
265274
createStakeCredentials :: Int -> [StakeCredential StandardCrypto]
266275
createStakeCredentials n =
267276
fmap (KeyHashObj . KeyHash . mkDummyHash (Proxy @(ADDRHASH StandardCrypto))) [1 .. n]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Cardano.Mock.Query (
77
queryMultiAssetCount,
88
queryTxMetadataCount,
99
queryDRepDistrAmount,
10+
queryCommitteeHashes,
1011
) where
1112

1213
import qualified Cardano.Db as Db
@@ -89,3 +90,11 @@ queryDRepDistrAmount drepHash epochNo = do
8990
pure (distr ^. Db.DrepDistrAmount)
9091

9192
pure $ maybe 0 unValue res
93+
94+
queryCommitteeHashes :: MonadIO io => ReaderT SqlBackend io [ByteString]
95+
queryCommitteeHashes = do
96+
res <- select $ do
97+
cc <- from $ table @Db.CommitteeHash
98+
pure (cc ^. Db.CommitteeHashRaw)
99+
100+
pure (map 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
@@ -218,6 +218,7 @@ unitTests iom knownMigrations =
218218
, testGroup
219219
"Governance"
220220
[ test "drep distribution" Governance.drepDistr
221+
, test "new committee member" Governance.newCommittee
221222
]
222223
]
223224
where
Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE NumericUnderscores #-}
23
{-# LANGUAGE OverloadedStrings #-}
3-
{-# LANGUAGE TypeApplications #-}
44

55
module Test.Cardano.Db.Mock.Unit.Conway.Governance (
66
drepDistr,
7+
newCommittee,
78
) where
89

910
import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
1011
import Cardano.Ledger.Address (Withdrawals (..))
12+
import Cardano.Ledger.Alonzo.Tx (AlonzoTx)
13+
import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..), Voter (..))
1114
import Cardano.Ledger.Conway.TxCert (Delegatee (..))
15+
import Cardano.Ledger.Core (txIdTx)
1216
import Cardano.Ledger.Credential (Credential (..))
1317
import Cardano.Ledger.DRep (DRep (..))
14-
import Cardano.Ledger.Keys (KeyHash (..))
18+
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (..))
1519
import Cardano.Mock.ChainSync.Server (IOManager)
1620
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
17-
import Cardano.Mock.Forging.Tx.Generic (resolveStakeCreds)
21+
import qualified Cardano.Mock.Forging.Tx.Generic as Forging
1822
import Cardano.Mock.Forging.Types
1923
import qualified Cardano.Mock.Query as Query
2024
import Cardano.Prelude
25+
import Ouroboros.Consensus.Shelley.Eras (StandardConway, StandardCrypto)
2126
import Test.Cardano.Db.Mock.Config
2227
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
2328
import Test.Cardano.Db.Mock.Validate
@@ -32,42 +37,94 @@ drepDistr =
3237
-- Add stake
3338
void (Api.registerAllStakeCreds interpreter server)
3439

35-
-- Delegate funds to a stake address
36-
void $
37-
Api.withConwayFindLeaderAndSubmitTx interpreter server $ \state' ->
38-
let utxoStake = UTxOAddressNewWithStake 0 (StakeIndex 4)
39-
in Conway.mkPaymentTx (UTxOIndex 0) utxoStake 10_000 500 state'
40-
4140
-- Register a DRep
4241
let drepHash = "0d94e174732ef9aae73f395ab44507bfa983d65023c11a951f0c32e4"
4342
drepId = KeyHashObj (KeyHash drepHash)
4443

44+
-- Register DRep and delegate votes to it
4545
void $
46-
Api.withConwayFindLeaderAndSubmitTx interpreter server $
47-
const (Conway.mkRegisterDRepTx drepId)
48-
49-
-- Delegate votes to the drep above
50-
void $
51-
Api.withConwayFindLeaderAndSubmitTx interpreter server $ \state' -> do
52-
stakeCreds <- resolveStakeCreds (StakeIndex 4) state'
53-
let regDelegCert =
54-
Conway.mkDelegTxCert (DelegVote $ DRepCredential drepId) stakeCreds
55-
56-
Conway.mkDCertTx [regDelegCert] (Withdrawals mempty) Nothing
46+
Api.withConwayFindLeaderAndSubmit interpreter server $ \ledger ->
47+
registerDRepAndDelegateVotes drepId (StakeIndex 4) ledger
5748

5849
-- DRep distribution is calculated at end of the current epoch
5950
epoch1 <- Api.fillUntilNextEpoch interpreter server
6051

6152
-- Wait for it to sync
62-
assertBlockNoBackoff dbSync (length epoch1 + 4)
53+
assertBlockNoBackoff dbSync (length epoch1 + 2)
6354

6455
-- Should now have a DRep distribution
6556
assertEqQuery
6657
dbSync
6758
(Query.queryDRepDistrAmount (unCredentialHash drepId) 1)
6859
10_000
6960
"Unexpected drep distribution amount"
70-
71-
pure ()
7261
where
7362
testLabel = "conwayDrepDistr"
63+
64+
newCommittee :: IOManager -> [(Text, Text)] -> Assertion
65+
newCommittee =
66+
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
67+
startDBSync dbSync
68+
69+
-- Add stake
70+
void (Api.registerAllStakeCreds interpreter server)
71+
72+
-- Create and vote for gov action
73+
let committeeHash = "e0a714319812c3f773ba04ec5d6b3ffcd5aad85006805b047b082541"
74+
committeeCred = KeyHashObj (KeyHash committeeHash)
75+
76+
void $
77+
Api.withConwayFindLeaderAndSubmit interpreter server $ \ledger -> do
78+
let
79+
-- Create gov action tx
80+
addCcTx = Conway.mkAddCommitteeTx committeeCred
81+
-- Create votes for all stake pools. We start in the Conway bootstrap phase, so
82+
-- DRep votes are not yet required.
83+
addVoteTx =
84+
Conway.mkGovVoteTx
85+
govActionId
86+
[ StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger)
87+
, StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger)
88+
, StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger)
89+
]
90+
govActionId =
91+
GovActionId
92+
{ gaidTxId = txIdTx addCcTx
93+
, gaidGovActionIx = GovActionIx 0
94+
}
95+
96+
-- Create votes
97+
pure [addCcTx, addVoteTx]
98+
99+
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
100+
-- epoch and enacted on the following.
101+
epochs <- Api.fillEpochs interpreter server 2
102+
103+
-- Wait for it to sync
104+
assertBlockNoBackoff dbSync (length epochs + 2)
105+
-- Should now have a committee member
106+
assertEqQuery
107+
dbSync
108+
Query.queryCommitteeHashes
109+
(map unCredentialHash $ Forging.bootstrapDRepIds ++ [committeeCred])
110+
"Unexpected committee hashes"
111+
where
112+
testLabel = "conwayNewCommittee"
113+
114+
registerDRepAndDelegateVotes ::
115+
Credential 'DRepRole StandardCrypto ->
116+
StakeIndex ->
117+
Conway.ConwayLedgerState ->
118+
Either ForgingError [AlonzoTx StandardConway]
119+
registerDRepAndDelegateVotes drepId stakeIx ledger = do
120+
stakeCreds <- Forging.resolveStakeCreds stakeIx ledger
121+
122+
let utxoStake = UTxOAddressNewWithStake 0 stakeIx
123+
regDelegCert =
124+
Conway.mkDelegTxCert (DelegVote $ DRepCredential drepId) stakeCreds
125+
126+
paymentTx <- Conway.mkPaymentTx (UTxOIndex 0) utxoStake 10_000 500 ledger
127+
regTx <- Conway.mkRegisterDRepTx drepId
128+
delegTx <- Conway.mkDCertTx [regDelegCert] (Withdrawals mempty) Nothing
129+
130+
pure [paymentTx, regTx, delegTx]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[5,11,15,21,22,23,28,33,34,36,42,43,48,52,62,82,88,92,102,106,109,111,116,133,134,143,151,153,157,161,162,171,182,183,193,195,196,197,200,206,208,216,219,222,238,245,250,262,271,272,275,282,286,296,301,310,311,314,325,340,347,354,355,365,376,379,382,384,389,390,391,392,393,398,404,407,414,418,419,422,424,446,448,450,457,465,476,478,485,486,488,499,500,504,505,523,531,533,534,536,537,543,556,558,559,561,562,566,573,574,576,588,590,594,598,608,620,623,630,631,635,639,652,654,655,656,658,661,662,681,693,703,707,720,721,723,729,733,738,742,746,753,756,759,760,764,766,783,798,801,811,814,817,818,825,828,829,832,848,849,855,862,863,867,871,879,881,883,899,902,912,913,915,919,921,922,924,931,932,937,940,942,947,953,974,980,988,994,995,996,1003]

0 commit comments

Comments
 (0)