forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
democracy.go
265 lines (230 loc) · 12.9 KB
/
democracy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package integration
import (
"time"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/stretchr/testify/suite"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
icstestingutils "github.com/cosmos/interchain-security/v4/testutil/ibc_testing"
testutil "github.com/cosmos/interchain-security/v4/testutil/integration"
consumertypes "github.com/cosmos/interchain-security/v4/x/ccv/consumer/types"
)
type ConsumerDemocracyTestSuite struct {
suite.Suite
coordinator *ibctesting.Coordinator
consumerChain *ibctesting.TestChain
consumerApp testutil.DemocConsumerApp
setupCallback DemocSetupCallback
}
// NewCCVTestSuite returns a new instance of ConsumerDemocracyTestSuite,
// ready to be tested against using suite.Run().
func NewConsumerDemocracyTestSuite[T testutil.DemocConsumerApp](
democConsumerAppIniter icstestingutils.ValSetAppIniter,
) *ConsumerDemocracyTestSuite {
democSuite := new(ConsumerDemocracyTestSuite)
democSuite.setupCallback = func(s *suite.Suite) (
*ibctesting.Coordinator,
*ibctesting.TestChain,
testutil.DemocConsumerApp,
) {
s.T().Helper()
// Instantiate the test coordinator
coordinator := ibctesting.NewCoordinator(s.T(), 0)
// Add single democracy consumer to coordinator, store returned test chain and app.
democConsumer, democConsumerApp := icstestingutils.AddDemocracyConsumer[T](
coordinator, s, democConsumerAppIniter)
// Pass variables to suite.
return coordinator, democConsumer, democConsumerApp
}
return democSuite
}
// Callback for instantiating a new coordinator, consumer test chain, and consumer app
// before every test defined on the suite.
type DemocSetupCallback func(s *suite.Suite) (
coord *ibctesting.Coordinator,
consumerChain *ibctesting.TestChain,
consumerApp testutil.DemocConsumerApp,
)
// SetupTest sets up in-mem state before every test relevant to ccv with a democracy consumer
func (suite *ConsumerDemocracyTestSuite) SetupTest() {
// Instantiate new test utils using callback
suite.coordinator, suite.consumerChain,
suite.consumerApp = suite.setupCallback(&suite.Suite)
}
func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
s.consumerChain.NextBlock()
stakingKeeper := s.consumerApp.GetTestStakingKeeper()
accountKeeper := s.consumerApp.GetTestAccountKeeper()
distrKeeper := s.consumerApp.GetTestDistributionKeeper()
bankKeeper := s.consumerApp.GetTestBankKeeper()
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())
currentRepresentativesRewards := map[string]sdk.Dec{}
nextRepresentativesRewards := map[string]sdk.Dec{}
representativesTokens := map[string]math.Int{}
for _, representative := range stakingKeeper.GetAllValidators(s.consumerCtx()) {
currentRepresentativesRewards[representative.OperatorAddress] = sdk.NewDec(0)
nextRepresentativesRewards[representative.OperatorAddress] = sdk.NewDec(0)
representativesTokens[representative.OperatorAddress] = representative.GetTokens()
}
distrModuleAccount := distrKeeper.GetDistributionAccount(s.consumerCtx())
providerRedistributeAccount := accountKeeper.GetModuleAccount(s.consumerCtx(), consumertypes.ConsumerToSendToProviderName)
// balance of consumer redistribute address will always be 0 when checked between 2 NextBlock() calls
currentDistrModuleAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
currentProviderFeeAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
currentCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
for key := range currentRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
representativeReward := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr).Rewards.AmountOf(bondDenom)
currentRepresentativesRewards[key] = representativeReward
}
s.consumerChain.NextBlock()
nextDistrModuleAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
nextProviderFeeAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
nextCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
for key := range nextRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
representativeReward := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr).Rewards.AmountOf(bondDenom)
nextRepresentativesRewards[key] = representativeReward
}
distrModuleDifference := nextDistrModuleAccountBalance.Sub(currentDistrModuleAccountBalance)
providerDifference := nextProviderFeeAccountBalance.Sub(currentProviderFeeAccountBalance)
communityPoolDifference := nextCommunityPoolBalance.Sub(currentCommunityPoolBalance)
representativeDifference := map[string]sdk.Dec{}
consumerRedistributeDifference := communityPoolDifference
for key, currentReward := range currentRepresentativesRewards {
representativeDifference[key] = nextRepresentativesRewards[key].Sub(currentReward)
consumerRedistributeDifference = consumerRedistributeDifference.Add(representativeDifference[key])
}
consumerRedistributionFraction := sdk.MustNewDecFromStr(s.consumerApp.GetConsumerKeeper().GetConsumerRedistributionFrac(s.consumerCtx()))
// confirm that the total amount given to the community pool plus all representatives is equal to the total amount taken out of distribution
s.Require().Equal(distrModuleDifference, consumerRedistributeDifference)
// confirm that the percentage given to the community pool is equal to the configured community tax percentage.
s.Require().Equal(communityPoolDifference.Quo(consumerRedistributeDifference),
distrKeeper.GetCommunityTax(s.consumerCtx()))
// check that the fraction actually kept by the consumer is the correct fraction. using InEpsilon because the math code uses truncations
s.Require().InEpsilon(distrModuleDifference.Quo(
providerDifference.Add(distrModuleDifference)).MustFloat64(),
consumerRedistributionFraction.MustFloat64(), float64(0.0001))
// check that the fraction actually kept by the provider is the correct fraction. using InEpsilon because the math code uses truncations
s.Require().InEpsilon(providerDifference.Quo(
providerDifference.Add(distrModuleDifference)).MustFloat64(),
sdk.NewDec(1).Sub(consumerRedistributionFraction).MustFloat64(), float64(0.0001))
totalRepresentativePower := stakingKeeper.GetValidatorSet().TotalBondedTokens(s.consumerCtx())
// check that each representative has gotten the correct amount of rewards
for key, representativeTokens := range representativesTokens {
powerFraction := sdk.NewDecFromInt(representativeTokens).QuoTruncate(sdk.NewDecFromInt(totalRepresentativePower))
s.Require().Equal(powerFraction, representativeDifference[key].Quo(consumerRedistributeDifference.Sub(communityPoolDifference)))
}
}
func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
govKeeper := s.consumerApp.GetTestGovKeeper()
params := govKeeper.GetParams(s.consumerCtx())
stakingKeeper := s.consumerApp.GetTestStakingKeeper()
bankKeeper := s.consumerApp.GetTestBankKeeper()
accountKeeper := s.consumerApp.GetTestAccountKeeper()
mintKeeper := s.consumerApp.GetTestMintKeeper()
newAuthParamValue := uint64(128)
newMintParamValue := sdk.NewDecWithPrec(1, 1) // "0.100000000000000000"
votingAccounts := s.consumerChain.SenderAccounts
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())
depositAmount := params.MinDeposit
duration := (3 * time.Second)
params.VotingPeriod = &duration
err := govKeeper.SetParams(s.consumerCtx(), params)
s.Assert().NoError(err)
proposer := s.consumerChain.SenderAccount
s.consumerChain.NextBlock()
votersOldBalances := getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts)
// submit proposal with forbidden and allowed changes
mintParams := mintKeeper.GetParams(s.consumerCtx())
mintParams.InflationMax = newMintParamValue
msg_1 := &minttypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: mintParams,
}
authParams := accountKeeper.GetParams(s.consumerCtx())
authParams.MaxMemoCharacters = newAuthParamValue
msg_2 := &authtypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: authParams,
}
err = submitProposalWithDepositAndVote(govKeeper, s.consumerCtx(), []sdk.Msg{msg_1, msg_2}, votingAccounts, proposer.GetAddress(), depositAmount)
s.Assert().NoError(err)
// set current header time to be equal or later than voting end time in order to process proposal from active queue,
// once the proposal is added to the chain
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
s.consumerChain.NextBlock()
// at this moment, proposal is added, but not yet executed. we are saving old param values for comparison
oldAuthParamValue := accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
oldMintParamValue := mintKeeper.GetParams(s.consumerCtx()).InflationMax
s.consumerChain.NextBlock()
// at this moment, proposal is executed or deleted if forbidden
currentAuthParamValue := accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
currentMintParamValue := mintKeeper.GetParams(s.consumerCtx()).InflationMax
// check that parameters are not changed, since the proposal contained both forbidden and allowed changes
s.Assert().Equal(oldAuthParamValue, currentAuthParamValue)
s.Assert().NotEqual(newAuthParamValue, currentAuthParamValue)
s.Assert().Equal(oldMintParamValue, currentMintParamValue)
s.Assert().NotEqual(newMintParamValue, currentMintParamValue)
// deposit is refunded
s.Assert().Equal(votersOldBalances, getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts))
// submit proposal with allowed changes
err = submitProposalWithDepositAndVote(govKeeper, s.consumerCtx(), []sdk.Msg{msg_1}, votingAccounts, proposer.GetAddress(), depositAmount)
s.Assert().NoError(err)
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
s.consumerChain.NextBlock()
oldMintParamValue = mintKeeper.GetParams(s.consumerCtx()).InflationMax
s.consumerChain.NextBlock()
currentMintParamValue = mintKeeper.GetParams(s.consumerCtx()).InflationMax
// check that parameters are changed, since the proposal contained only allowed changes
s.Assert().Equal(newMintParamValue, currentMintParamValue)
s.Assert().NotEqual(oldMintParamValue, currentMintParamValue)
// deposit is refunded
s.Assert().Equal(votersOldBalances, getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts))
// submit proposal with forbidden changes
err = submitProposalWithDepositAndVote(govKeeper, s.consumerCtx(), []sdk.Msg{msg_2}, votingAccounts, proposer.GetAddress(), depositAmount)
s.Assert().NoError(err)
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
s.consumerChain.NextBlock()
oldAuthParamValue = accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
s.consumerChain.NextBlock()
currentAuthParamValue = accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
// check that parameters are not changed, since the proposal contained forbidden changes
s.Assert().Equal(oldAuthParamValue, currentAuthParamValue)
s.Assert().NotEqual(newAuthParamValue, currentAuthParamValue)
// deposit is refunded
s.Assert().Equal(votersOldBalances, getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts))
}
func submitProposalWithDepositAndVote(govKeeper testutil.TestGovKeeper, ctx sdk.Context, msgs []sdk.Msg,
accounts []ibctesting.SenderAccount, proposer sdk.AccAddress, depositAmount sdk.Coins,
) error {
proposal, err := govKeeper.SubmitProposal(ctx, msgs, "", "title", "summary", proposer)
if err != nil {
return err
}
_, err = govKeeper.AddDeposit(ctx, proposal.Id, accounts[0].SenderAccount.GetAddress(), depositAmount) // proposal becomes active
if err != nil {
return err
}
for _, account := range accounts {
err = govKeeper.AddVote(ctx, proposal.Id, account.SenderAccount.GetAddress(), govv1.NewNonSplitVoteOption(govv1.OptionYes), "")
if err != nil {
return err
}
}
return nil
}
func getAccountsBalances(ctx sdk.Context, bankKeeper testutil.TestBankKeeper, bondDenom string, accounts []ibctesting.SenderAccount) map[string]math.Int {
accountsBalances := map[string]math.Int{}
for _, acc := range accounts {
accountsBalances[string(acc.SenderAccount.GetAddress())] = bankKeeper.GetBalance(ctx, acc.SenderAccount.GetAddress(), bondDenom).Amount
}
return accountsBalances
}
func (s *ConsumerDemocracyTestSuite) consumerCtx() sdk.Context {
return s.consumerChain.GetContext()
}