Skip to content

Commit b01a7cf

Browse files
committed
test(cardano-chain-gen): Add a Conway DRep distribution test
1 parent 80bbd6a commit b01a7cf

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ test-suite cardano-chain-gen
158158
Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.ForceIndex
159159
Test.Cardano.Db.Mock.Unit.Conway.Config.Parse
160160
Test.Cardano.Db.Mock.Unit.Conway.Config.MigrateConsumedPruneTxOut
161+
Test.Cardano.Db.Mock.Unit.Conway.Governance
161162
Test.Cardano.Db.Mock.Unit.Conway.InlineAndReference
162163
Test.Cardano.Db.Mock.Unit.Conway.Other
163164
Test.Cardano.Db.Mock.Unit.Conway.Plutus

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE NumericUnderscores #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
56
{-# LANGUAGE TypeApplications #-}
@@ -29,12 +30,14 @@ module Cardano.Mock.Forging.Tx.Conway (
2930
mkScriptDCertTx,
3031
mkMultiAssetsScriptTx,
3132
mkDepositTxPools,
33+
mkRegisterDRepTx,
3234
mkDummyRegisterTx,
3335
mkDummyTxBody,
3436
mkTxDelegCert,
3537
mkRegTxCert,
3638
mkUnRegTxCert,
3739
mkDelegTxCert,
40+
mkRegDelegTxCert,
3841
Babbage.mkParamUpdateTx,
3942
mkFullTx,
4043
mkScriptMint',
@@ -436,6 +439,14 @@ mkDepositTxPools inputIndex deposit state' = do
436439
(allPoolStakeCert' state')
437440
(Withdrawals mempty)
438441

442+
mkRegisterDRepTx ::
443+
Credential 'DRepRole StandardCrypto ->
444+
Either ForgingError (AlonzoTx StandardConway)
445+
mkRegisterDRepTx cred = mkDCertTx [cert] (Withdrawals mempty) Nothing
446+
where
447+
cert = ConwayTxCertGov (ConwayRegDRep cred deposit SNothing)
448+
deposit = Coin 500_000_000
449+
439450
mkDummyRegisterTx :: Int -> Int -> Either ForgingError (AlonzoTx StandardConway)
440451
mkDummyRegisterTx n m = mkDCertTx consDelegCert (Withdrawals mempty) Nothing
441452
where
@@ -459,6 +470,14 @@ mkUnRegTxCert ::
459470
ConwayTxCert StandardConway
460471
mkUnRegTxCert coin' = mkTxDelegCert $ \cred -> ConwayUnRegCert cred coin'
461472

473+
mkRegDelegTxCert ::
474+
Coin ->
475+
Delegatee StandardCrypto ->
476+
StakeCredential StandardCrypto ->
477+
ConwayTxCert StandardConway
478+
mkRegDelegTxCert deposit delegatee =
479+
mkTxDelegCert $ \cred -> ConwayRegDelegCert cred delegatee deposit
480+
462481
mkDelegTxCert ::
463482
Delegatee StandardCrypto ->
464483
StakeCredential StandardCrypto ->

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ module Cardano.Mock.Query (
66
queryNullTxDepositExists,
77
queryMultiAssetCount,
88
queryTxMetadataCount,
9+
queryDRepDistrAmount,
910
) where
1011

1112
import qualified Cardano.Db as Db
12-
import Cardano.Prelude hiding (from)
13+
import Cardano.Prelude hiding (from, on)
1314
import Database.Esqueleto.Experimental
1415
import Prelude ()
1516

@@ -68,3 +69,23 @@ queryTxMetadataCount = do
6869
pure countRows
6970

7071
pure $ maybe 0 unValue res
72+
73+
queryDRepDistrAmount ::
74+
MonadIO io =>
75+
ByteString ->
76+
Word64 ->
77+
ReaderT SqlBackend io Word64
78+
queryDRepDistrAmount drepHash epochNo = do
79+
res <- selectOne $ do
80+
(distr :& hash) <-
81+
from
82+
$ table @Db.DrepDistr
83+
`innerJoin` table @Db.DrepHash
84+
`on` (\(distr :& hash) -> (hash ^. Db.DrepHashId) ==. (distr ^. Db.DrepDistrHashId))
85+
86+
where_ $ hash ^. Db.DrepHashRaw ==. just (val drepHash)
87+
where_ $ distr ^. Db.DrepDistrEpochNo ==. val epochNo
88+
89+
pure (distr ^. Db.DrepDistrAmount)
90+
91+
pure $ maybe 0 unValue res

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qualified Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.EpochDisabled a
77
import qualified Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.ForceIndex as ForceIndex
88
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
99
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.Parse as Config
10+
import qualified Test.Cardano.Db.Mock.Unit.Conway.Governance as Governance
1011
import qualified Test.Cardano.Db.Mock.Unit.Conway.InlineAndReference as InlineRef
1112
import qualified Test.Cardano.Db.Mock.Unit.Conway.Other as Other
1213
import qualified Test.Cardano.Db.Mock.Unit.Conway.Plutus as Plutus
@@ -214,6 +215,10 @@ unitTests iom knownMigrations =
214215
, test "fork from Babbage to Conway and rollback" Other.rollbackFork
215216
, test "fork with protocol change proposal" Other.forkParam
216217
]
218+
, testGroup
219+
"Governance"
220+
[ test "drep distribution" Governance.drepDistr
221+
]
217222
]
218223
where
219224
test :: String -> (IOManager -> [(Text, Text)] -> Assertion) -> TestTree
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE TypeApplications #-}
4+
5+
module Test.Cardano.Db.Mock.Unit.Conway.Governance (
6+
drepDistr,
7+
) where
8+
9+
import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
10+
import Cardano.Ledger.Address (Withdrawals (..))
11+
import Cardano.Ledger.Conway.TxCert (Delegatee (..))
12+
import Cardano.Ledger.Credential (Credential (..))
13+
import Cardano.Ledger.DRep (DRep (..))
14+
import Cardano.Ledger.Keys (KeyHash (..))
15+
import Cardano.Mock.ChainSync.Server (IOManager)
16+
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
17+
import Cardano.Mock.Forging.Tx.Generic (resolveStakeCreds)
18+
import Cardano.Mock.Forging.Types
19+
import qualified Cardano.Mock.Query as Query
20+
import Cardano.Prelude
21+
import Test.Cardano.Db.Mock.Config
22+
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
23+
import Test.Cardano.Db.Mock.Validate
24+
import Test.Tasty.HUnit (Assertion)
25+
import Prelude ()
26+
27+
drepDistr :: IOManager -> [(Text, Text)] -> Assertion
28+
drepDistr =
29+
withFullConfigAndDropDB conwayConfigDir testLabel $ \interpreter server dbSync -> do
30+
startDBSync dbSync
31+
32+
-- Add stake
33+
void (Api.registerAllStakeCreds interpreter server)
34+
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+
41+
-- Register a DRep
42+
let drepHash = "0d94e174732ef9aae73f395ab44507bfa983d65023c11a951f0c32e4"
43+
drepId = KeyHashObj (KeyHash drepHash)
44+
45+
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
57+
58+
-- DRep distribution is calculated at end of the current epoch
59+
epoch1 <- Api.fillUntilNextEpoch interpreter server
60+
61+
-- Wait for it to sync
62+
assertBlockNoBackoff dbSync (length epoch1 + 4)
63+
64+
-- Should now have a DRep distribution
65+
assertEqQuery
66+
dbSync
67+
(Query.queryDRepDistrAmount (unCredentialHash drepId) 1)
68+
10_000
69+
"Unexpected drep distribution amount"
70+
71+
pure ()
72+
where
73+
testLabel = "conwayDrepDistr"
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]

0 commit comments

Comments
 (0)