Skip to content

Commit

Permalink
test(cardano-chain-gen): Add a basic committee update test
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed May 9, 2024
1 parent ee66685 commit 8d0e8f5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
43 changes: 39 additions & 4 deletions cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Cardano.Mock.Forging.Tx.Conway (
mkDummyRegisterTx,
mkDummyTxBody,
mkRegDRepTx,
mkNewCommitteeTx,
mkTxDelegCert,
mkRegTxCert,
mkUnRegTxCert,
Expand All @@ -53,10 +54,10 @@ import Cardano.Ledger.Alonzo.Scripts
import Cardano.Ledger.Alonzo.Tx (IsValid (..))
import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData (..), mkAlonzoTxAuxData)
import Cardano.Ledger.Babbage.TxOut (BabbageEraTxOut, BabbageTxOut (..))
import Cardano.Ledger.BaseTypes (EpochNo (..), Network (..))
import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Binary (Sized (..))
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Conway.Governance (VotingProcedures (..))
import qualified Cardano.Ledger.Conway.Governance as Governance
import Cardano.Ledger.Conway.Scripts
import Cardano.Ledger.Conway.Tx (AlonzoTx (..))
import Cardano.Ledger.Conway.TxBody (ConwayTxBody (..))
Expand Down Expand Up @@ -87,6 +88,7 @@ import Cardano.Prelude
import Data.List (nub)
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
import Data.Maybe (fromJust)
import Data.Maybe.Strict (StrictMaybe (..), maybeToStrictMaybe)
import qualified Data.OSet.Strict as OSet
import Data.Sequence.Strict (StrictSeq ())
Expand Down Expand Up @@ -130,7 +132,7 @@ consTxBody ins cols ref outs colOut fees minted certs withdrawals =
, ctbScriptIntegrityHash = SNothing
, ctbAdHash = SNothing
, ctbTxNetworkId = SJust Testnet
, ctbVotingProcedures = VotingProcedures mempty
, ctbVotingProcedures = Governance.VotingProcedures mempty
, ctbProposalProcedures = mempty
, ctbCurrentTreasuryValue = SNothing
, ctbTreasuryDonation = Coin 0
Expand Down Expand Up @@ -457,6 +459,39 @@ mkRegDRepTx credential = mkDCertTx [mkCert] (Withdrawals mempty) Nothing
mkCert = ConwayTxCertGov (ConwayRegDRep credential deposit SNothing)
deposit = Coin 500_000_000

mkNewCommitteeTx :: Either ForgingError (AlonzoTx StandardConway)
mkNewCommitteeTx =
Right (mkSimpleTx True txBody)
where
txBody =
mkDummyTxBody
{ ctbProposalProcedures = OSet.singleton proposal
}

proposal =
Governance.ProposalProcedure
{ Governance.pProcDeposit = Coin 50_000_000_000
, Governance.pProcReturnAddr =
RewardAccount Testnet (Prelude.head unregisteredStakeCredentials)
, Governance.pProcGovAction = govAction
, Governance.pProcAnchor = anchor
}

govAction =
Governance.UpdateCommittee
SNothing
mempty
(Map.singleton credential (EpochNo 5))
(fromJust $ boundRational (1 % 1))

credential = KeyHashObj $ KeyHash $ mkDummyHash Proxy 1

anchor =
Anchor
{ anchorUrl = fromJust (textToUrl 64 "cc")
, anchorDataHash = hashAnchorData (AnchorData mempty)
}

mkRegTxCert ::
StrictMaybe Coin ->
StakeCredential StandardCrypto ->
Expand Down Expand Up @@ -545,7 +580,7 @@ mkFullTx n m state' = do
, ctbScriptIntegrityHash = SNothing
, ctbAdHash = SNothing
, ctbTxNetworkId = SJust Testnet
, ctbVotingProcedures = VotingProcedures mempty
, ctbVotingProcedures = Governance.VotingProcedures mempty
, ctbProposalProcedures = mempty
, ctbCurrentTreasuryValue = SNothing
, ctbTreasuryDonation = Coin 0
Expand Down
9 changes: 9 additions & 0 deletions cardano-chain-gen/src/Cardano/Mock/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Cardano.Mock.Query (
queryMultiAssetCount,
queryTxMetadataCount,
queryDRepRegistrationHashes,
queryGovActionProposalCount,
) where

import qualified Cardano.Db as Db
Expand Down Expand Up @@ -82,3 +83,11 @@ queryDRepRegistrationHashes = do
pure (hash ^. Db.DrepHashRaw)

pure $ mapMaybe unValue res

queryGovActionProposalCount :: MonadIO io => ReaderT SqlBackend io Word
queryGovActionProposalCount = do
res <- selectOne $ do
_ <- from (table @Db.GovActionProposal)
pure countRows

pure $ maybe 0 unValue res
1 change: 1 addition & 0 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ unitTests iom knownMigrations =
, testGroup
"Governance"
[ test "drep registration" Governance.drepRegistration
, test "new committee member" Governance.newCommittee
]
]
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

module Test.Cardano.Db.Mock.Unit.Conway.Governance (
drepRegistration,
newCommittee,
) where

import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.Keys (KeyHash (..))
import Cardano.Mock.ChainSync.Server (IOManager)
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
import Cardano.Mock.Query (queryDRepRegistrationHashes)
import qualified Cardano.Mock.Query as Query
import Cardano.Prelude
import Test.Cardano.Db.Mock.Config
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
Expand All @@ -35,8 +36,32 @@ drepRegistration =
-- Should have a matching DRep registration
assertEqQuery
dbSync
queryDRepRegistrationHashes
Query.queryDRepRegistrationHashes
[unCredentialHash credential]
"Unexpected DRep registrations"
where
testLabel = "conwayDRepRegistration"

newCommittee :: IOManager -> [(Text, Text)] -> Assertion
newCommittee =
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
startDBSync dbSync

-- Propose a new constitutional committee member
void $
Api.withConwayFindLeaderAndSubmitTx interpreter server $
const Conway.mkNewCommitteeTx

-- Wait for it to sync
epoch <- Api.fillUntilNextEpoch interpreter server
assertBlockNoBackoff dbSync (1 + length epoch)

-- Should have a governance action proposal
-- TODO[sgillespie]: Should also ratify/enact
assertEqQuery
dbSync
Query.queryGovActionProposalCount
1
"Unexpected gov action proposal count"
where
testLabel = "conwayNewCcMember"
Original file line number Diff line number Diff line change
@@ -0,0 +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]

0 comments on commit 8d0e8f5

Please sign in to comment.