From 42e198bc1f9b3abbc136173848a9d704e16b8137 Mon Sep 17 00:00:00 2001 From: Sean D Gillespie Date: Mon, 22 Jul 2024 14:04:13 -0400 Subject: [PATCH] refactor: Factor out governance initialization for tests --- .../Mock/Forging/Tx/Conway/Scenarios.hs | 8 ++ .../src/Cardano/Mock/Forging/Tx/Generic.hs | 23 ++- .../test/Test/Cardano/Db/Mock/UnifiedApi.hs | 24 ++++ .../Cardano/Db/Mock/Unit/Conway/Governance.hs | 132 +++--------------- .../testfiles/fingerprint/conwayDrepDistr | 2 +- .../testfiles/fingerprint/conwayNewCommittee | 2 +- 6 files changed, 77 insertions(+), 114 deletions(-) diff --git a/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs b/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs index 9b18ec460..b668e623f 100644 --- a/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs +++ b/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs @@ -10,6 +10,7 @@ module Cardano.Mock.Forging.Tx.Conway.Scenarios ( delegateAndSendBlocks, registerDRepsAndDelegateVotes, + registerCommitteeCreds, ) where import Cardano.Ledger.Address (Addr (..), Withdrawals (..)) @@ -120,3 +121,10 @@ registerDRepAndDelegateVotes' drepId stakeIx ledger = do delegTx <- Conway.mkDCertTx [regDelegCert] (Withdrawals mempty) Nothing pure [paymentTx, regTx, delegTx] + +registerCommitteeCreds :: Interpreter -> IO CardanoBlock +registerCommitteeCreds interpreter = do + let txs' = mapM (uncurry Conway.mkCommitteeAuthTx) bootstrapCommitteeCreds + blockTxs <- withConwayLedgerState interpreter $ const txs' + + forgeNextFindLeader interpreter (map TxConway blockTxs) diff --git a/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs b/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs index 28ea4154d..99a86aab7 100644 --- a/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs +++ b/cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs @@ -26,6 +26,9 @@ module Cardano.Mock.Forging.Tx.Generic ( registeredShelleyGenesisKeys, bootstrapCommitteeCreds, unregisteredDRepIds, + spoVoters, + committeeVoters, + drepVoters, consPoolParams, getPoolStakeCreds, ) where @@ -36,6 +39,7 @@ import qualified Cardano.Crypto.Hash as Hash import Cardano.Ledger.Address import Cardano.Ledger.BaseTypes import Cardano.Ledger.Coin (Coin (..)) +import Cardano.Ledger.Conway.Governance (Voter (..)) import qualified Cardano.Ledger.Core as Core import Cardano.Ledger.Credential import Cardano.Ledger.Crypto (ADDRHASH) @@ -51,7 +55,7 @@ import qualified Cardano.Ledger.UMap as UMap import Cardano.Mock.Forging.Crypto import Cardano.Mock.Forging.Tx.Alonzo.ScriptsExamples import Cardano.Mock.Forging.Types -import Cardano.Prelude hiding (length, (.)) +import Cardano.Prelude import Data.Coerce (coerce) import Data.List (nub) import Data.List.Extra ((!?)) @@ -64,6 +68,7 @@ import Ouroboros.Consensus.Cardano.Block (LedgerState) import Ouroboros.Consensus.Shelley.Eras (StandardCrypto) import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock) import qualified Ouroboros.Consensus.Shelley.Ledger.Ledger as Consensus +import Prelude (head, (!!)) resolveAddress :: forall era p. @@ -308,6 +313,22 @@ mkDummyScriptHash n = ScriptHash $ mkDummyHash (Proxy @(ADDRHASH StandardCrypto) mkDummyHash :: forall h a. HashAlgorithm h => Proxy h -> Int -> Hash.Hash h a mkDummyHash _ = coerce . hashWithSerialiser @h toCBOR +spoVoters :: + EraCrypto era ~ StandardCrypto => + LedgerState (ShelleyBlock proto era) -> + [Voter StandardCrypto] +spoVoters ledger = + [ StakePoolVoter (resolvePool (PoolIndex 0) ledger) + , StakePoolVoter (resolvePool (PoolIndex 1) ledger) + , StakePoolVoter (resolvePool (PoolIndex 2) ledger) + ] + +committeeVoters :: [Voter StandardCrypto] +committeeVoters = map (CommitteeVoter . snd) bootstrapCommitteeCreds + +drepVoters :: [Voter StandardCrypto] +drepVoters = [DRepVoter (Prelude.head unregisteredDRepIds)] + consPoolParams :: KeyHash 'StakePool StandardCrypto -> StakeCredential StandardCrypto -> diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/UnifiedApi.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/UnifiedApi.hs index 6b877dc4d..8be4970ac 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/UnifiedApi.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/UnifiedApi.hs @@ -20,8 +20,10 @@ module Test.Cardano.Db.Mock.UnifiedApi ( fillEpochs, fillEpochPercentage, rollbackTo, + initGovernance, registerAllStakeCreds, registerDRepsAndDelegateVotes, + registerCommitteeCreds, ) where import Cardano.Ledger.Alonzo (AlonzoEra) @@ -203,6 +205,22 @@ rollbackTo interpreter mockServer point = do rollbackInterpreter interpreter point atomically $ rollback mockServer point +initGovernance :: Interpreter -> ServerHandle IO CardanoBlock -> IO [CardanoBlock] +initGovernance interpreter mockServer = do + -- Add stake + blk0 <- registerAllStakeCreds interpreter mockServer + + -- Register a DRep and delegate votes to it + blk1 <- registerDRepsAndDelegateVotes interpreter mockServer + + -- DRep distribution is calculated a-*t end of the current epoch + epoch <- fillUntilNextEpoch interpreter mockServer + + -- Register committee hot credentials + blk2 <- registerCommitteeCreds interpreter mockServer + + pure ([blk0, blk1] ++ epoch ++ [blk2]) + registerAllStakeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock registerAllStakeCreds interpreter mockServer = do blk <- forgeWithStakeCreds interpreter @@ -215,6 +233,12 @@ registerDRepsAndDelegateVotes interpreter mockServer = do atomically (addBlock mockServer blk) pure blk +registerCommitteeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock +registerCommitteeCreds interpreter mockServer = do + blk <- Conway.registerCommitteeCreds interpreter + atomically (addBlock mockServer blk) + pure blk + -- Expected number. This should be taken from the parameters, instead of hardcoded. blocksPerEpoch :: Int blocksPerEpoch = 100 diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Governance.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Governance.hs index f43157ef3..78216d75c 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Governance.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Governance.hs @@ -23,7 +23,7 @@ import Cardano.Ledger.Address (RewardAccount (..)) import Cardano.Ledger.BaseTypes (AnchorData (..), Network (..), ProtVer (..), hashAnchorData, textToUrl) import Cardano.Ledger.Binary.Version (natVersion) import Cardano.Ledger.Coin (Coin (..)) -import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..), Voter (..)) +import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..)) import qualified Cardano.Ledger.Conway.Governance as Governance import Cardano.Ledger.Conway.PParams (ppuGovActionDepositL) import Cardano.Ledger.Core (emptyPParamsUpdate, txIdTx) @@ -33,6 +33,7 @@ import Cardano.Ledger.SafeHash (SafeToHash (..)) import Cardano.Mock.ChainSync.Server (IOManager) import Cardano.Mock.Forging.Interpreter (getCurrentEpoch) import qualified Cardano.Mock.Forging.Tx.Conway as Conway +import Cardano.Mock.Forging.Tx.Generic (committeeVoters, drepVoters, spoVoters) import qualified Cardano.Mock.Forging.Tx.Generic as Forging import Cardano.Mock.Forging.Types import qualified Cardano.Mock.Query as Query @@ -53,17 +54,10 @@ drepDistr = withFullConfigAndDropDB conwayConfigDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) - - -- DRep distribution is calculated at end of the current epoch - epoch1 <- Api.fillUntilNextEpoch interpreter server + epoch0 <- Api.initGovernance interpreter server -- Wait for it to sync - assertBlockNoBackoff dbSync (length epoch1 + 2) + assertBlockNoBackoff dbSync (length epoch0) -- Should now have a DRep distribution let drepId = Prelude.head Forging.unregisteredDRepIds @@ -80,11 +74,7 @@ newCommittee = withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register a DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) + epoch0 <- Api.initGovernance interpreter server -- Create and vote for gov action let committeeHash = "e0a714319812c3f773ba04ec5d6b3ffcd5aad85006805b047b082541" @@ -95,16 +85,8 @@ newCommittee = let -- Create gov action tx addCcTx = Conway.mkAddCommitteeTx committeeCred - -- Create votes for all stake pools. We start in the Conway bootstrap phase, so - -- DRep votes are not yet required. - addVoteTx = - Conway.mkGovVoteTx - govActionId - [ DRepVoter (Prelude.head Forging.unregisteredDRepIds) - , StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger) - ] + -- Create votes for all stake pools + addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ spoVoters ledger) govActionId = GovActionId { gaidTxId = txIdTx addCcTx @@ -116,10 +98,10 @@ newCommittee = -- It takes 2 epochs to enact a proposal--ratification will happen on the next -- epoch and enacted on the following. - epochs <- Api.fillEpochs interpreter server 2 + epoch1 <- Api.fillEpochs interpreter server 2 -- Wait for it to sync - assertBlockNoBackoff dbSync (length epochs + 3) + assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1) -- Should now have a committee member assertEqQuery dbSync @@ -134,19 +116,7 @@ updateConstitution = withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register a DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) - - -- DRep distribution is calculated at end of the current epoch - epoch0 <- Api.fillUntilNextEpoch interpreter server - - -- Register committee hot credentials - void $ - Api.withConwayFindLeaderAndSubmit interpreter server $ \_ -> - mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds + epoch0 <- Api.initGovernance interpreter server let newUrl = fromJust (textToUrl 64 "constitution.new") dataHash = hashAnchorData @Consensus.StandardCrypto (AnchorData "constitution content") @@ -160,12 +130,7 @@ updateConstitution = proposalTx = Conway.mkNewConstitutionTx anchor -- Create votes - addVoteTx = - Conway.mkGovVoteTx - govActionId - ( DRepVoter (Prelude.head Forging.unregisteredDRepIds) - : map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds - ) + addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ committeeVoters) govActionId = GovActionId { gaidTxId = txIdTx proposalTx @@ -179,7 +144,7 @@ updateConstitution = epoch1 <- Api.fillEpochs interpreter server 2 -- Wait for it to sync - assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4) + assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1) -- Constitution should now be updated (EpochNo epochNo) <- getCurrentEpoch interpreter @@ -196,20 +161,7 @@ treasuryWithdrawal = withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register a DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) - - -- DRep distribution is calculated at end of the current epoch - epoch0 <- Api.fillUntilNextEpoch interpreter server - - -- Register committee hot credentials - -- TODO[sgillespie]: Let's get this in UnifiedApi or something - void $ - Api.withConwayFindLeaderAndSubmit interpreter server $ \_ -> - mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds + epoch0 <- Api.initGovernance interpreter server -- Make sure we have treasury to spend void $ @@ -228,12 +180,7 @@ treasuryWithdrawal = rewardAccount (Coin 10_000) - addVoteTx = - Conway.mkGovVoteTx - govActionId - ( DRepVoter (Prelude.head Forging.unregisteredDRepIds) - : map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds - ) + addVoteTx = Conway.mkGovVoteTx govActionId (drepVoters ++ committeeVoters) govActionId = GovActionId @@ -248,7 +195,7 @@ treasuryWithdrawal = epoch1 <- Api.fillEpochs interpreter server 2 -- Wait for it to sync - assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 5) + assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 2) -- Should now have a treasury reward assertEqQuery @@ -264,20 +211,7 @@ paramChange = withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register a DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) - - -- DRep distribution is calculated at end of the current epoch - epoch0 <- Api.fillUntilNextEpoch interpreter server - - -- Register committee hot credentials - -- TODO[sgillespie]: Let's get this in UnifiedApi or something - void $ - Api.withConwayFindLeaderAndSubmit interpreter server $ \_ -> - mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds + epoch0 <- Api.initGovernance interpreter server -- Create and vote for a governance proposal void $ @@ -289,13 +223,7 @@ paramChange = addVoteTx = Conway.mkGovVoteTx govActionId - ( map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds - ++ [ DRepVoter (Prelude.head Forging.unregisteredDRepIds) - , StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger) - ] - ) + (committeeVoters ++ drepVoters ++ spoVoters ledger) govActionId = GovActionId @@ -310,7 +238,7 @@ paramChange = epoch1 <- Api.fillEpochs interpreter server 2 -- Wait for it to synch - assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4) + assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1) epochNo <- unEpochNo <$> getCurrentEpoch interpreter @@ -328,20 +256,7 @@ hardFork = withFullConfig configDir testLabel $ \interpreter server dbSync -> do startDBSync dbSync - -- Add stake - void (Api.registerAllStakeCreds interpreter server) - - -- Register a DRep and delegate votes to it - void (Api.registerDRepsAndDelegateVotes interpreter server) - - -- DRep distribution is calculated at end of the current epoch - epoch0 <- Api.fillUntilNextEpoch interpreter server - - -- Register committee hot credentials - -- TODO[sgillespie]: Let's get this in UnifiedApi or something - void $ - Api.withConwayFindLeaderAndSubmit interpreter server $ \_ -> - mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds + epoch0 <- Api.initGovernance interpreter server -- Create and vote for a governance proposal void $ @@ -352,12 +267,7 @@ hardFork = addVoteTx = Conway.mkGovVoteTx govActionId - ( map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds - ++ [ StakePoolVoter (Forging.resolvePool (PoolIndex 0) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 1) ledger) - , StakePoolVoter (Forging.resolvePool (PoolIndex 2) ledger) - ] - ) + (committeeVoters ++ spoVoters ledger) govActionId = GovActionId @@ -372,7 +282,7 @@ hardFork = epoch1 <- Api.fillEpochs interpreter server 2 -- Wait for it to synch - assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 4) + assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 1) epochNo <- getCurrentEpoch interpreter diff --git a/cardano-chain-gen/test/testfiles/fingerprint/conwayDrepDistr b/cardano-chain-gen/test/testfiles/fingerprint/conwayDrepDistr index 1c8cb8caa..0845d76d4 100644 --- a/cardano-chain-gen/test/testfiles/fingerprint/conwayDrepDistr +++ b/cardano-chain-gen/test/testfiles/fingerprint/conwayDrepDistr @@ -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] \ No newline at end of file +[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] \ No newline at end of file diff --git a/cardano-chain-gen/test/testfiles/fingerprint/conwayNewCommittee b/cardano-chain-gen/test/testfiles/fingerprint/conwayNewCommittee index 79da17351..ebc1151d0 100644 --- a/cardano-chain-gen/test/testfiles/fingerprint/conwayNewCommittee +++ b/cardano-chain-gen/test/testfiles/fingerprint/conwayNewCommittee @@ -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] \ No newline at end of file +[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] \ No newline at end of file