Skip to content

Commit dc8b8cc

Browse files
committed
test(cardano-chain-gen): Add a treasury withdrawal test
1 parent 35823f6 commit dc8b8cc

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Cardano.Mock.Forging.Tx.Conway (
1919
consTxCertPool,
2020
mkPaymentTx,
2121
mkPaymentTx',
22+
mkDonationTx,
2223
mkLockByScriptTx,
2324
mkUnlockScriptTx,
2425
mkUnlockScriptTxBabbage,
@@ -43,6 +44,7 @@ module Cardano.Mock.Forging.Tx.Conway (
4344
mkDelegTxCert,
4445
mkRegDelegTxCert,
4546
mkAddCommitteeTx,
47+
mkTreasuryWithdrawalTx,
4648
mkGovActionProposalTx,
4749
mkGovVoteTx,
4850
Babbage.mkParamUpdateTx,
@@ -215,6 +217,11 @@ mkPaymentTx' inputIndex outputIndices fees state' = do
215217
addr <- resolveAddress outIx state'
216218
pure (BabbageTxOut addr val NoDatum SNothing)
217219

220+
mkDonationTx :: Coin -> AlonzoTx StandardConway
221+
mkDonationTx amount = mkSimpleTx True txBody
222+
where
223+
txBody = mkDummyTxBody {ctbTreasuryDonation = amount}
224+
218225
mkLockByScriptTx ::
219226
ConwayUTxOIndex ->
220227
[Babbage.TxOutScriptType] ->
@@ -522,6 +529,16 @@ mkNewConstitutionTx anchor = mkGovActionProposalTx govAction
522529
govAction = Governance.NewConstitution SNothing constitution
523530
constitution = Governance.Constitution anchor SNothing
524531

532+
mkTreasuryWithdrawalTx ::
533+
RewardAccount StandardCrypto ->
534+
Coin ->
535+
AlonzoTx StandardConway
536+
mkTreasuryWithdrawalTx rewardAccount amount = mkGovActionProposalTx govAction
537+
where
538+
govAction = Governance.TreasuryWithdrawals withdrawals hashProtection
539+
withdrawals = Map.singleton rewardAccount amount
540+
hashProtection = SNothing
541+
525542
mkGovActionProposalTx ::
526543
Governance.GovAction StandardConway ->
527544
AlonzoTx StandardConway

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Cardano.Mock.Query (
1010
queryDRepDistrAmount,
1111
queryGovActionCounts,
1212
queryConstitutionAnchor,
13+
queryRewardRests,
1314
) where
1415

1516
import qualified Cardano.Db as Db
@@ -139,3 +140,13 @@ queryConstitutionAnchor epochNo = do
139140
pure (anchor ^. Db.VotingAnchorUrl, anchor ^. Db.VotingAnchorDataHash)
140141

141142
pure $ bimap (Db.unVoteUrl . unValue) unValue <$> res
143+
144+
queryRewardRests ::
145+
MonadIO io =>
146+
ReaderT SqlBackend io [(Db.RewardSource, Word64)]
147+
queryRewardRests = do
148+
res <- select $ do
149+
reward <- from $ table @Db.RewardRest
150+
pure (reward ^. Db.RewardRestType, reward ^. Db.RewardRestAmount)
151+
152+
pure $ map (bimap unValue (Db.unDbLovelace . unValue)) res

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ unitTests iom knownMigrations =
220220
[ test "drep distribution" Governance.drepDistr
221221
, test "new committee member" Governance.newCommittee
222222
, test "update constitution" Governance.updateConstitution
223+
, test "treasury withdrawal" Governance.treasuryWithdrawal
223224
]
224225
]
225226
where

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ module Test.Cardano.Db.Mock.Unit.Conway.Governance (
77
drepDistr,
88
newCommittee,
99
updateConstitution,
10+
treasuryWithdrawal,
1011
) where
1112

13+
import qualified Cardano.Db as Db
1214
import Cardano.DbSync.Era.Shelley.Generic.Util (unCredentialHash)
13-
import Cardano.Ledger.BaseTypes (AnchorData (..), hashAnchorData, textToUrl)
15+
import Cardano.Ledger.Address (RewardAccount (..))
16+
import Cardano.Ledger.BaseTypes (AnchorData (..), Network (..), hashAnchorData, textToUrl)
17+
import Cardano.Ledger.Coin (Coin (..))
1418
import Cardano.Ledger.Conway.Governance (GovActionId (..), GovActionIx (..), Voter (..))
1519
import qualified Cardano.Ledger.Conway.Governance as Governance
1620
import Cardano.Ledger.Core (txIdTx)
@@ -175,3 +179,71 @@ updateConstitution =
175179
"Unexpected constution voting anchor"
176180
where
177181
testLabel = "conwayUpdateConstitution"
182+
183+
treasuryWithdrawal :: IOManager -> [(Text, Text)] -> Assertion
184+
treasuryWithdrawal =
185+
withFullConfig conwayConfigDir testLabel $ \interpreter server dbSync -> do
186+
startDBSync dbSync
187+
188+
-- Add stake
189+
void (Api.registerAllStakeCreds interpreter server)
190+
191+
-- Register a DRep and delegate votes to it
192+
void (Api.registerDRepsAndDelegateVotes interpreter server)
193+
194+
-- DRep distribution is calculated at end of the current epoch
195+
epoch0 <- Api.fillUntilNextEpoch interpreter server
196+
197+
-- Register committee hot credentials
198+
-- TODO[sgillespie]: Let's get this in UnifiedApi or something
199+
void $
200+
Api.withConwayFindLeaderAndSubmit interpreter server $ \_ ->
201+
mapM (uncurry Conway.mkCommitteeAuthTx) Forging.bootstrapCommitteeCreds
202+
203+
-- Make sure we have treasury to spend
204+
void $
205+
Api.withConwayFindLeaderAndSubmitTx interpreter server $ \_ ->
206+
Right $ Conway.mkDonationTx (Coin 50_000)
207+
208+
-- Create and vote for a governance proposal
209+
void $
210+
Api.withConwayFindLeaderAndSubmit interpreter server $ \ledger -> do
211+
rewardAccount <-
212+
RewardAccount Testnet <$> Forging.resolveStakeCreds (StakeIndex 0) ledger
213+
214+
let
215+
proposalTx =
216+
Conway.mkTreasuryWithdrawalTx
217+
rewardAccount
218+
(Coin 10_000)
219+
220+
addVoteTx =
221+
Conway.mkGovVoteTx
222+
govActionId
223+
( DRepVoter (Prelude.head Forging.unregisteredDRepIds)
224+
: map (CommitteeVoter . snd) Forging.bootstrapCommitteeCreds
225+
)
226+
227+
govActionId =
228+
GovActionId
229+
{ gaidTxId = txIdTx proposalTx
230+
, gaidGovActionIx = GovActionIx 0
231+
}
232+
233+
pure [proposalTx, addVoteTx]
234+
235+
-- It takes 2 epochs to enact a proposal--ratification will happen on the next
236+
-- epoch and enacted on the following.
237+
epoch1 <- Api.fillEpochs interpreter server 2
238+
239+
-- Wait for it to sync
240+
assertBlockNoBackoff dbSync (length (epoch0 <> epoch1) + 5)
241+
242+
-- Should now have a treasury reward
243+
assertEqQuery
244+
dbSync
245+
Query.queryRewardRests
246+
[(Db.RwdTreasury, 10_000)]
247+
"Unexpected constution voting anchor"
248+
where
249+
testLabel = "conwayTreasuryWithdrawal"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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,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,1500]

0 commit comments

Comments
 (0)