Skip to content

Commit

Permalink
test(cardano-chain-gen): Add a Conway deleg vote test
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed May 13, 2024
1 parent 0b90886 commit bea5738
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
16 changes: 16 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,
queryDelegVoteDrepIds,
queryGovActionProposalCount,
) where

Expand Down Expand Up @@ -84,6 +85,21 @@ queryDRepRegistrationHashes = do

pure $ mapMaybe unValue res

queryDelegVoteDrepIds :: MonadIO io => ReaderT SqlBackend io [ByteString]
queryDelegVoteDrepIds = do
res <- select $ do
(_ :& drep :& _) <-
from
$ table @Db.DelegationVote
`innerJoin` table @Db.DrepHash
`on` (\(vote :& drep) -> vote ^. Db.DelegationVoteDrepHashId ==. drep ^. Db.DrepHashId)
`innerJoin` table @Db.StakeAddress
`on` (\(vote :& _ :& addr) -> vote ^. Db.DelegationVoteAddrId ==. addr ^. Db.StakeAddressId)

pure (drep ^. Db.DrepHashRaw)

pure $ mapMaybe unValue res

queryGovActionProposalCount :: MonadIO io => ReaderT SqlBackend io Word
queryGovActionProposalCount = do
res <- selectOne $ do
Expand Down
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 "delegate vote to drep" Governance.delegVote
, test "new committee member" Governance.newCommittee
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

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

import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
import Cardano.Ledger.Address (Withdrawals (..))
import Cardano.Ledger.Conway.TxCert
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.DRep (DRep (..))
import Cardano.Ledger.Keys (KeyHash (..))
import Cardano.Mock.ChainSync.Server (IOManager)
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
Expand Down Expand Up @@ -42,6 +46,45 @@ drepRegistration =
where
testLabel = "conwayDRepRegistration"

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

-- Register a DRep
let drepIdHash = "0d94e174732ef9aae73f395ab44507bfa983d65023c11a951f0c32e4"
drepId = KeyHashObj (KeyHash drepIdHash)

void $
Api.withConwayFindLeaderAndSubmitTx interpreter server $ \_ ->
Conway.mkRegDRepTx drepId

-- Register stake credential
void $ Api.registerAllStakeCreds interpreter server

-- Delegate to the DRep above
let stakeCredHash = KeyHash "addfa484e8095ff53f45b25cf337923cf79abe6ec192fdf288d621f9"
stakeCred = KeyHashObj stakeCredHash

void $
Api.withConwayFindLeaderAndSubmitTx interpreter server $ \_ ->
let cert = Conway.mkDelegTxCert delegatee stakeCred
delegatee = DelegVote (DRepCredential drepId)
in Conway.mkDCertTx [cert] (Withdrawals mempty) Nothing

-- Wait for it to sync
assertBlockNoBackoff dbSync 3
-- Should have a matching delegation vote
assertEqQuery
dbSync
-- TODO[sgillespie]: Would be better to verify the @stakeCredHash@, but the raw hash
-- in the DB has as extra leading character
Query.queryDelegVoteDrepIds
[unCredentialHash drepId]
"Unexpected Delegation Vote"
where
testLabel = "conwayDelegVote"

newCommittee :: IOManager -> [(Text, Text)] -> Assertion
newCommittee =
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[5,11,15]

0 comments on commit bea5738

Please sign in to comment.