Skip to content

Commit 11eff7c

Browse files
committed
remove panics
1 parent 4e0872e commit 11eff7c

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

app/upgrades/v15/upgrades.go

+48-30
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ func CreateUpgradeHandler(
4545
return vm, err
4646
}
4747

48-
UpgradeMinCommissionRate(ctx, *keepers.StakingKeeper)
49-
UpgradeSigningInfos(ctx, keepers.SlashingKeeper)
50-
ClawbackVestingFunds(ctx, sdk.MustAccAddressFromBech32("cosmos145hytrc49m0hn6fphp8d5h4xspwkawcuzmx498"), keepers)
48+
if err := UpgradeMinCommissionRate(ctx, *keepers.StakingKeeper); err != nil {
49+
return nil, fmt.Errorf("failed migrating min commission rates: %s", err)
50+
}
51+
if err := UpgradeSigningInfos(ctx, keepers.SlashingKeeper); err != nil {
52+
return nil, fmt.Errorf("failed migrating signing infos: %s", err)
53+
}
54+
if err := ClawbackVestingFunds(
55+
ctx,
56+
sdk.MustAccAddressFromBech32("cosmos145hytrc49m0hn6fphp8d5h4xspwkawcuzmx498"),
57+
keepers); err != nil {
58+
return nil, fmt.Errorf("failed migrating vesting funds: %s", err)
59+
}
5160

5261
ctx.Logger().Info("Upgrade v15 complete")
5362
return vm, err
@@ -58,14 +67,13 @@ func CreateUpgradeHandler(
5867
// and updates the commission rate for all validators that have a commission rate less than 5%
5968
// adhere to prop 826 which sets the minimum commission rate to 5% for all validators
6069
// https://www.mintscan.io/cosmos/proposals/826
61-
func UpgradeMinCommissionRate(ctx sdk.Context, sk stakingkeeper.Keeper) {
70+
func UpgradeMinCommissionRate(ctx sdk.Context, sk stakingkeeper.Keeper) error {
6271
ctx.Logger().Info("Migrating min commission rate...")
6372

6473
params := sk.GetParams(ctx)
6574
params.MinCommissionRate = sdk.NewDecWithPrec(5, 2)
66-
err := sk.SetParams(ctx, params)
67-
if err != nil {
68-
panic(err)
75+
if err := sk.SetParams(ctx, params); err != nil {
76+
return err
6977
}
7078

7179
for _, val := range sk.GetAllValidators(ctx) {
@@ -82,11 +90,12 @@ func UpgradeMinCommissionRate(ctx sdk.Context, sk stakingkeeper.Keeper) {
8290
}
8391

8492
ctx.Logger().Info("Finished migrating min commission rate")
93+
return nil
8594
}
8695

8796
// UpgradeSigningInfos updates the signing infos of validators for which
8897
// the consensus address is missing
89-
func UpgradeSigningInfos(ctx sdk.Context, sk slashingkeeper.Keeper) {
98+
func UpgradeSigningInfos(ctx sdk.Context, sk slashingkeeper.Keeper) error {
9099
ctx.Logger().Info("Migrating signing infos...")
91100

92101
signingInfos := []slashingtypes.ValidatorSigningInfo{}
@@ -112,11 +121,12 @@ func UpgradeSigningInfos(ctx sdk.Context, sk slashingkeeper.Keeper) {
112121
}
113122

114123
ctx.Logger().Info("Finished migrating signing infos")
124+
return nil
115125
}
116126

117127
// ClawbackVestingFunds transfers the vesting tokens from the given vesting account
118128
// to the community pool
119-
func ClawbackVestingFunds(ctx sdk.Context, address sdk.AccAddress, keepers *keepers.AppKeepers) {
129+
func ClawbackVestingFunds(ctx sdk.Context, address sdk.AccAddress, keepers *keepers.AppKeepers) error {
120130
ctx.Logger().Info("Migrating vesting funds...")
121131

122132
ak := keepers.AccountKeeper
@@ -130,45 +140,55 @@ func ClawbackVestingFunds(ctx sdk.Context, address sdk.AccAddress, keepers *keep
130140
// verify that it's a vesting account type
131141
vestAccount, ok := account.(*vesting.ContinuousVestingAccount)
132142
if !ok {
133-
panic(
134-
fmt.Errorf("%s:%s",
135-
sdkerrors.ErrInvalidType,
136-
"account with address %s isn't a vesting account",
137-
),
143+
ctx.Logger().Error(
144+
"failed migrating vesting funds: %s: %s",
145+
"provided account address isn't a vesting account: ",
146+
address.String(),
138147
)
148+
149+
return nil
150+
}
151+
152+
// returns if no coins are vesting
153+
_, vestingCoins := vestAccount.GetVestingCoins(ctx.BlockTime()).Find(sk.BondDenom(ctx))
154+
if vestingCoins.IsNil() {
155+
ctx.Logger().Info(
156+
"%s: %s",
157+
"no vesting coins to migrate",
158+
"Finished migrating vesting funds",
159+
)
160+
161+
return nil
139162
}
140163

141164
// unbond all delegations from vesting account
142-
err := forceUnbondAllDelegations(sk, bk, ctx, address)
143-
if err != nil {
144-
panic(err)
165+
if err := forceUnbondAllDelegations(sk, bk, ctx, address); err != nil {
166+
return err
145167
}
146168

147169
// transfers still vesting tokens of BondDenom to community pool
148-
_, vestingCoins := vestAccount.GetVestingCoins(ctx.BlockTime()).Find(sk.BondDenom(ctx))
149-
err = forceFundCommunityPool(
170+
if err := forceFundCommunityPool(
150171
ak,
151172
dk,
152173
bk,
153174
ctx,
154175
vestingCoins,
155176
address,
156177
keepers.GetKey(banktypes.StoreKey),
157-
)
158-
if err != nil {
159-
panic(err)
178+
); err != nil {
179+
return err
160180
}
161181

162182
// overwrite vesting account using its embedded base account
163183
ak.SetAccount(ctx, vestAccount.BaseAccount)
164184

165185
// validate account balance
166-
err = bk.ValidateBalance(ctx, address)
167-
if err != nil {
168-
panic(err)
186+
if err := bk.ValidateBalance(ctx, address); err != nil {
187+
return err
169188
}
170189

171190
ctx.Logger().Info("Finished migrating vesting funds")
191+
return nil
172192
}
173193

174194
// forceUnbondAllDelegations unbonds all the delegations from the given account address,
@@ -194,20 +214,18 @@ func forceUnbondAllDelegations(
194214
return err
195215
}
196216

217+
coins := sdk.NewCoins(sdk.NewCoin(sk.BondDenom(ctx), returnAmount))
218+
197219
// transfer the validator tokens to the not bonded pool
198220
if validator.IsBonded() {
199221
// doing stakingKeeper.bondedTokensToNotBonded
200-
coins := sdk.NewCoins(sdk.NewCoin(sk.BondDenom(ctx), returnAmount))
201222
err = bk.SendCoinsFromModuleToModule(ctx, stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, coins)
202223
if err != nil {
203224
return err
204225
}
205226
}
206227

207-
bondDenom := sk.GetParams(ctx).BondDenom
208-
amt := sdk.NewCoin(bondDenom, returnAmount)
209-
210-
err = bk.UndelegateCoinsFromModuleToAccount(ctx, stakingtypes.NotBondedPoolName, delegator, sdk.NewCoins(amt))
228+
err = bk.UndelegateCoinsFromModuleToAccount(ctx, stakingtypes.NotBondedPoolName, delegator, coins)
211229
if err != nil {
212230
return err
213231
}

app/upgrades/v15/upgrades_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestUpgradeSigningInfos(t *testing.T) {
7272
})
7373

7474
// upgrade signing infos
75-
v15.UpgradeSigningInfos(ctx, slashingKeeper)
75+
require.NoError(t, v15.UpgradeSigningInfos(ctx, slashingKeeper))
7676

7777
// check that all signing info are updated as expected after migration
7878
slashingKeeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
@@ -117,7 +117,7 @@ func TestUpgradeMinCommissionRate(t *testing.T) {
117117
require.Equal(t, stakingKeeper.GetParams(ctx).MinCommissionRate, sdk.ZeroDec(), "non-zero previous min commission rate")
118118

119119
// run the test and confirm the values have been updated
120-
v15.UpgradeMinCommissionRate(ctx, *stakingKeeper)
120+
require.NoError(t, v15.UpgradeMinCommissionRate(ctx, *stakingKeeper))
121121

122122
newStakingParams := stakingKeeper.GetParams(ctx)
123123
require.NotEqual(t, newStakingParams.MinCommissionRate, sdk.ZeroDec(), "failed to update min commission rate")
@@ -200,6 +200,9 @@ func TestClawbackVestingFunds(t *testing.T) {
200200
require.Equal(t, vestingAccount.GetDelegatedVesting(), origCoins)
201201
require.Empty(t, vestingAccount.GetDelegatedFree())
202202

203+
// check that migration succeeds when all coins are alreay vested
204+
require.NoError(t, v15.ClawbackVestingFunds(ctx.WithBlockTime(endTime), addr, &gaiaApp.AppKeepers))
205+
203206
// vest half of the tokens
204207
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
205208

@@ -210,7 +213,7 @@ func TestClawbackVestingFunds(t *testing.T) {
210213
require.True(t, currVestedCoins.IsEqual(origCoins.QuoInt(math.NewInt(2))))
211214

212215
// execute migration script
213-
v15.ClawbackVestingFunds(ctx, addr, &gaiaApp.AppKeepers)
216+
require.NoError(t, v15.ClawbackVestingFunds(ctx, addr, &gaiaApp.AppKeepers))
214217

215218
// check that the validator's delegation is removed and that
216219
// their total tokens decreased

0 commit comments

Comments
 (0)