-
Notifications
You must be signed in to change notification settings - Fork 717
/
upgrades_test.go
272 lines (218 loc) · 8.77 KB
/
upgrades_test.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
266
267
268
269
270
271
272
package v15_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
abci "github.com/cometbft/cometbft/abci/types"
tmrand "github.com/cometbft/cometbft/libs/rand"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gaia/v15/app/helpers"
v15 "github.com/cosmos/gaia/v15/app/upgrades/v15"
)
func TestUpgradeSigningInfos(t *testing.T) {
gaiaApp := helpers.Setup(t)
ctx := gaiaApp.NewUncachedContext(true, tmproto.Header{})
slashingKeeper := gaiaApp.SlashingKeeper
signingInfosNum := 8
emptyAddrSigningInfo := make(map[string]struct{})
// create some dummy signing infos, half of which with an empty address field
for i := 0; i < signingInfosNum; i++ {
pubKey, err := mock.NewPV().GetPubKey()
require.NoError(t, err)
consAddr := sdk.ConsAddress(pubKey.Address())
info := slashingtypes.NewValidatorSigningInfo(
consAddr,
0,
0,
time.Unix(0, 0),
false,
0,
)
if i < signingInfosNum/2 {
info.Address = ""
emptyAddrSigningInfo[consAddr.String()] = struct{}{}
}
slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, info)
require.NoError(t, err)
}
require.Equal(t, signingInfosNum/2, len(emptyAddrSigningInfo))
// check that signing info are correctly set before migration
slashingKeeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
if _, ok := emptyAddrSigningInfo[address.String()]; ok {
require.Empty(t, info.Address)
} else {
require.NotEmpty(t, info.Address)
}
return false
})
// upgrade signing infos
require.NoError(t, v15.UpgradeSigningInfos(ctx, slashingKeeper))
// check that all signing info are updated as expected after migration
slashingKeeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
require.NotEmpty(t, info.Address)
return false
})
}
func TestUpgradeMinCommissionRate(t *testing.T) {
gaiaApp := helpers.Setup(t)
ctx := gaiaApp.NewUncachedContext(true, tmproto.Header{})
// set min commission rate to 0
stakingParams := gaiaApp.StakingKeeper.GetParams(ctx)
stakingParams.MinCommissionRate = sdk.ZeroDec()
err := gaiaApp.StakingKeeper.SetParams(ctx, stakingParams)
require.NoError(t, err)
stakingKeeper := gaiaApp.StakingKeeper
valNum := len(stakingKeeper.GetAllValidators(ctx))
// create 3 new validators
for i := 0; i < 3; i++ {
pk := ed25519.GenPrivKeyFromSecret([]byte{uint8(i)}).PubKey()
val, err := stakingtypes.NewValidator(
sdk.ValAddress(pk.Address()),
pk,
stakingtypes.Description{},
)
require.NoError(t, err)
// set random commission rate
val.Commission.CommissionRates.Rate = sdk.NewDecWithPrec(tmrand.Int63n(100), 2)
stakingKeeper.SetValidator(ctx, val)
valNum++
}
validators := stakingKeeper.GetAllValidators(ctx)
require.Equal(t, valNum, len(validators))
// pre-test min commission rate is 0
require.Equal(t, stakingKeeper.GetParams(ctx).MinCommissionRate, sdk.ZeroDec(), "non-zero previous min commission rate")
// run the test and confirm the values have been updated
require.NoError(t, v15.UpgradeMinCommissionRate(ctx, *stakingKeeper))
newStakingParams := stakingKeeper.GetParams(ctx)
require.NotEqual(t, newStakingParams.MinCommissionRate, sdk.ZeroDec(), "failed to update min commission rate")
require.Equal(t, newStakingParams.MinCommissionRate, sdk.NewDecWithPrec(5, 2), "failed to update min commission rate")
for _, val := range stakingKeeper.GetAllValidators(ctx) {
require.True(t, val.Commission.CommissionRates.Rate.GTE(newStakingParams.MinCommissionRate), "failed to update update commission rate for validator %s", val.GetOperator())
}
}
func TestClawbackVestingFunds(t *testing.T) {
gaiaApp := helpers.Setup(t)
now := tmtime.Now()
endTime := now.Add(24 * time.Hour)
bankKeeper := gaiaApp.BankKeeper
accountKeeper := gaiaApp.AccountKeeper
distrKeeper := gaiaApp.DistrKeeper
stakingKeeper := gaiaApp.StakingKeeper
ctx := gaiaApp.NewUncachedContext(true, tmproto.Header{Height: 1})
ctx = ctx.WithBlockHeader(tmproto.Header{Height: ctx.BlockHeight(), Time: now})
validator := stakingKeeper.GetAllValidators(ctx)[0]
bondDenom := stakingKeeper.GetParams(ctx).BondDenom
// create continuous vesting account
origCoins := sdk.NewCoins(sdk.NewInt64Coin(bondDenom, 100))
addr := sdk.AccAddress([]byte("cosmos145hytrc49m0hn6fphp8d5h4xspwkawcuzmx498"))
vestingAccount := vesting.NewContinuousVestingAccount(
authtypes.NewBaseAccountWithAddress(addr),
origCoins,
now.Unix(),
endTime.Unix(),
)
require.True(t, vestingAccount.GetVestingCoins(now).IsEqual(origCoins))
accountKeeper.SetAccount(ctx, vestingAccount)
// check vesting account balance was set correctly
require.NoError(t, bankKeeper.ValidateBalance(ctx, addr))
require.Empty(t, bankKeeper.GetAllBalances(ctx, addr))
// send original vesting coin amount
require.NoError(t, banktestutil.FundAccount(bankKeeper, ctx, addr, origCoins))
require.True(t, origCoins.IsEqual(bankKeeper.GetAllBalances(ctx, addr)))
initBal := bankKeeper.GetAllBalances(ctx, vestingAccount.GetAddress())
require.True(t, initBal.IsEqual(origCoins))
// save validator tokens
oldValTokens := validator.Tokens
// delegate all vesting account tokens
_, err := stakingKeeper.Delegate(
ctx,
vestingAccount.GetAddress(),
origCoins.AmountOf(bondDenom),
stakingtypes.Unbonded,
validator,
true)
require.NoError(t, err)
// check that the validator's tokens and shares increased
validator = stakingKeeper.GetAllValidators(ctx)[0]
del, found := stakingKeeper.GetDelegation(ctx, addr, validator.GetOperator())
require.True(t, found)
require.True(t, validator.Tokens.Equal(oldValTokens.Add(origCoins.AmountOf(bondDenom))))
require.Equal(
t,
validator.TokensFromShares(del.Shares),
math.LegacyNewDec(origCoins.AmountOf(bondDenom).Int64()),
)
// check vesting account delegations
vestingAccount = accountKeeper.GetAccount(ctx, addr).(*vesting.ContinuousVestingAccount)
require.Equal(t, vestingAccount.GetDelegatedVesting(), origCoins)
require.Empty(t, vestingAccount.GetDelegatedFree())
// check that migration succeeds when all coins are already vested
require.NoError(t, v15.ClawbackVestingFunds(ctx.WithBlockTime(endTime), addr, &gaiaApp.AppKeepers))
// vest half of the tokens
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
currVestingCoins := vestingAccount.GetVestingCoins(ctx.BlockTime())
currVestedCoins := vestingAccount.GetVestedCoins(ctx.BlockTime())
require.True(t, currVestingCoins.IsEqual(origCoins.QuoInt(math.NewInt(2))))
require.True(t, currVestedCoins.IsEqual(origCoins.QuoInt(math.NewInt(2))))
// execute migration script
require.NoError(t, v15.ClawbackVestingFunds(ctx, addr, &gaiaApp.AppKeepers))
// check that the validator's delegation is removed and that
// their total tokens decreased
validator = stakingKeeper.GetAllValidators(ctx)[0]
_, found = stakingKeeper.GetDelegation(ctx, addr, validator.GetOperator())
require.False(t, found)
require.Equal(
t,
validator.TokensFromShares(validator.DelegatorShares),
math.LegacyNewDec(oldValTokens.Int64()),
)
// verify that all modules can end/begin blocks
gaiaApp.EndBlock(abci.RequestEndBlock{})
gaiaApp.BeginBlock(
abci.RequestBeginBlock{
Header: tmproto.Header{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight() + 1,
},
},
)
// check that the resulting account is of BaseAccount type now
account, ok := accountKeeper.GetAccount(ctx, addr).(*authtypes.BaseAccount)
require.True(t, ok)
// check that the account values are still the same
require.EqualValues(t, account, vestingAccount.BaseAccount)
// check that the account's balance still has the vested tokens
require.True(t, bankKeeper.GetAllBalances(ctx, addr).IsEqual(currVestedCoins))
// check that the community pool balance received the vesting tokens
require.True(
t,
distrKeeper.GetFeePoolCommunityCoins(ctx).
IsEqual(sdk.NewDecCoinsFromCoins(currVestingCoins...)),
)
// verify that normal operations work in banking and staking
_, err = stakingKeeper.Delegate(
ctx, addr,
sdk.NewInt(30),
stakingtypes.Unbonded,
validator,
true)
require.NoError(t, err)
newAddr := sdk.AccAddress([]byte("cosmos1qqp9myctmh8mh2y7gynlsnw4y2wz3s3089dak6"))
err = bankKeeper.SendCoins(
ctx,
addr,
newAddr,
sdk.NewCoins(sdk.NewCoin(bondDenom, sdk.NewInt(10))),
)
require.NoError(t, err)
}