Skip to content

Commit 58badd8

Browse files
committed
test: Add more governance tests
* Hard Fork * Parameter Change * Info Action
1 parent b8007fe commit 58badd8

File tree

12 files changed

+330
-37
lines changed

12 files changed

+330
-37
lines changed

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ module Cardano.Mock.Forging.Tx.Conway (
5050
mkRegDelegTxCert,
5151
mkAddCommitteeTx,
5252
mkTreasuryWithdrawalTx,
53+
mkParamChangeTx,
54+
mkHardForkInitTx,
55+
mkInfoTx,
5356
mkGovActionProposalTx,
5457
mkGovVoteTx,
58+
mkGovVoteYesTx,
5559
Babbage.mkParamUpdateTx,
5660
mkFullTx,
5761
mkScriptMint',
@@ -107,6 +111,7 @@ import qualified Data.OSet.Strict as OSet
107111
import Data.Sequence.Strict (StrictSeq ())
108112
import qualified Data.Sequence.Strict as StrictSeq
109113
import qualified Data.Set as Set
114+
import Lens.Micro
110115
import Ouroboros.Consensus.Cardano.Block (EraCrypto, LedgerState ())
111116
import Ouroboros.Consensus.Shelley.Eras (StandardConway (), StandardCrypto ())
112117
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
@@ -563,6 +568,24 @@ mkTreasuryWithdrawalTx rewardAccount amount = mkGovActionProposalTx govAction
563568
withdrawals = Map.singleton rewardAccount amount
564569
hashProtection = SNothing
565570

571+
mkParamChangeTx :: AlonzoTx StandardConway
572+
mkParamChangeTx = mkGovActionProposalTx govAction
573+
where
574+
govAction = Governance.ParameterChange SNothing paramUpdate hasProtection
575+
paramUpdate = Core.emptyPParamsUpdate & Core.ppuMaxTxSizeL .~ SJust 32_000
576+
hasProtection = SNothing
577+
578+
mkHardForkInitTx :: AlonzoTx StandardConway
579+
mkHardForkInitTx = mkGovActionProposalTx govAction
580+
where
581+
govAction = Governance.HardForkInitiation SNothing protoVersion
582+
protoVersion = ProtVer (natVersion @11) 0
583+
584+
mkInfoTx :: AlonzoTx StandardConway
585+
mkInfoTx = mkGovActionProposalTx govAction
586+
where
587+
govAction = Governance.InfoAction
588+
566589
mkGovActionProposalTx ::
567590
Governance.GovAction StandardConway ->
568591
AlonzoTx StandardConway
@@ -585,18 +608,25 @@ mkGovActionProposalTx govAction = mkSimpleTx True txBody
585608
, Governance.anchorDataHash = hashAnchorData (Governance.AnchorData mempty)
586609
}
587610

588-
mkGovVoteTx ::
611+
mkGovVoteYesTx ::
589612
Governance.GovActionId StandardCrypto ->
590613
[Governance.Voter StandardCrypto] ->
591614
AlonzoTx StandardConway
592-
mkGovVoteTx govAction voters = mkSimpleTx True txBody
615+
mkGovVoteYesTx govAction =
616+
mkGovVoteTx govAction . Map.fromList . map (,Governance.VoteYes)
617+
618+
mkGovVoteTx ::
619+
Governance.GovActionId StandardCrypto ->
620+
Map (Governance.Voter StandardCrypto) Governance.Vote ->
621+
AlonzoTx StandardConway
622+
mkGovVoteTx govAction votes = mkSimpleTx True txBody
593623
where
594-
txBody = mkDummyTxBody {ctbVotingProcedures = Governance.VotingProcedures votes}
595-
votes = Map.fromList . map (,govActionVote) $ voters
596-
govActionVote = Map.singleton govAction vote
597-
vote =
624+
txBody = mkDummyTxBody {ctbVotingProcedures = Governance.VotingProcedures votes'}
625+
votes' = fmap mkGovActionVote votes
626+
mkGovActionVote = Map.singleton govAction . mkVote
627+
mkVote v =
598628
Governance.VotingProcedure
599-
{ Governance.vProcVote = Governance.VoteYes
629+
{ Governance.vProcVote = v
600630
, Governance.vProcAnchor = SNothing
601631
}
602632

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

Lines changed: 8 additions & 0 deletions
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+
blockTxs <- withConwayLedgerState interpreter $ \_ ->
128+
mapM (uncurry Conway.mkCommitteeAuthTx) bootstrapCommitteeCreds
129+
130+
forgeNextFindLeader interpreter (map TxConway blockTxs)

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module Cardano.Mock.Forging.Tx.Generic (
2828
unregisteredDRepIds,
2929
consPoolParams,
3030
getPoolStakeCreds,
31+
resolveStakePoolVoters,
32+
drepVoters,
33+
committeeVoters,
3134
) where
3235

3336
import Cardano.Binary (ToCBOR (..))
@@ -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 hiding (length, map, (.))
5559
import Data.Coerce (coerce)
5660
import Data.List (nub)
5761
import Data.List.Extra ((!?))
@@ -325,3 +329,19 @@ consPoolParams poolId rwCred owners =
325329
, ppRelays = StrictSeq.singleton $ SingleHostAddr SNothing SNothing SNothing
326330
, ppMetadata = SJust $ PoolMetadata (fromJust $ textToUrl 64 "best.pool") "89237365492387654983275634298756"
327331
}
332+
333+
resolveStakePoolVoters ::
334+
EraCrypto era ~ StandardCrypto =>
335+
LedgerState (ShelleyBlock proto era) ->
336+
[Voter StandardCrypto]
337+
resolveStakePoolVoters ledger =
338+
[ StakePoolVoter (resolvePool (PoolIndex 0) ledger)
339+
, StakePoolVoter (resolvePool (PoolIndex 1) ledger)
340+
, StakePoolVoter (resolvePool (PoolIndex 2) ledger)
341+
]
342+
343+
drepVoters :: [Voter StandardCrypto]
344+
drepVoters = map DRepVoter unregisteredDRepIds
345+
346+
committeeVoters :: [Voter StandardCrypto]
347+
committeeVoters = map (CommitteeVoter . snd) bootstrapCommitteeCreds

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Cardano.Mock.Query (
55
queryVersionMajorFromEpoch,
66
queryParamProposalFromEpoch,
7+
queryParamFromEpoch,
78
queryNullTxDepositExists,
89
queryMultiAssetCount,
910
queryTxMetadataCount,
@@ -12,6 +13,7 @@ module Cardano.Mock.Query (
1213
queryConstitutionAnchor,
1314
queryRewardRests,
1415
queryTreasuryDonations,
16+
queryVoteCounts,
1517
) where
1618

1719
import qualified Cardano.Db as Db
@@ -51,6 +53,17 @@ queryParamProposalFromEpoch epochNo = do
5153
pure prop
5254
pure $ entityVal <$> res
5355

56+
queryParamFromEpoch ::
57+
MonadIO io =>
58+
Word64 ->
59+
ReaderT SqlBackend io (Maybe Db.EpochParam)
60+
queryParamFromEpoch epochNo = do
61+
res <- selectOne $ do
62+
param <- from $ table @Db.EpochParam
63+
where_ $ param ^. Db.EpochParamEpochNo ==. val epochNo
64+
pure param
65+
pure (entityVal <$> res)
66+
5467
-- | Query whether there any null tx deposits?
5568
queryNullTxDepositExists :: MonadIO io => ReaderT SqlBackend io Bool
5669
queryNullTxDepositExists = do
@@ -162,3 +175,29 @@ queryTreasuryDonations = do
162175

163176
let total = join (unValue <$> res)
164177
pure $ maybe 0 Db.unDbLovelace total
178+
179+
queryVoteCounts ::
180+
MonadIO io =>
181+
ByteString ->
182+
Word16 ->
183+
ReaderT SqlBackend io (Word64, Word64, Word64)
184+
queryVoteCounts txHash idx = do
185+
yes <- countVotes Db.VoteYes
186+
no <- countVotes Db.VoteNo
187+
abstain <- countVotes Db.VoteAbstain
188+
189+
pure (yes, no, abstain)
190+
where
191+
countVotes v = do
192+
res <- selectOne $ do
193+
(vote :& tx) <-
194+
from
195+
$ table @Db.VotingProcedure
196+
`innerJoin` table @Db.Tx
197+
`on` (\(vote :& tx) -> vote ^. Db.VotingProcedureTxId ==. tx ^. Db.TxId)
198+
where_ $
199+
vote ^. Db.VotingProcedureVote ==. val v
200+
&&. tx ^. Db.TxHash ==. val txHash
201+
&&. vote ^. Db.VotingProcedureIndex ==. val idx
202+
pure countRows
203+
pure (maybe 0 unValue res)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Test.Cardano.Db.Mock.UnifiedApi (
2222
rollbackTo,
2323
registerAllStakeCreds,
2424
registerDRepsAndDelegateVotes,
25+
registerCommitteeCreds,
2526
) where
2627

2728
import Cardano.Ledger.Alonzo (AlonzoEra)
@@ -215,6 +216,12 @@ registerDRepsAndDelegateVotes interpreter mockServer = do
215216
atomically (addBlock mockServer blk)
216217
pure blk
217218

219+
registerCommitteeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock
220+
registerCommitteeCreds interpreter mockServer = do
221+
blk <- Conway.registerCommitteeCreds interpreter
222+
atomically (addBlock mockServer blk)
223+
pure blk
224+
218225
-- Expected number. This should be taken from the parameters, instead of hardcoded.
219226
blocksPerEpoch :: Int
220227
blocksPerEpoch = 100

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ unitTests iom knownMigrations =
243243
, test "new committee member" Governance.newCommittee
244244
, test "update constitution" Governance.updateConstitution
245245
, test "treasury withdrawal" Governance.treasuryWithdrawal
246+
, test "parameter change" Governance.parameterChange
247+
, test "hard fork" Governance.hardFork
248+
, test "info action" Governance.infoAction
246249
]
247250
]
248251
where

0 commit comments

Comments
 (0)