Skip to content

Commit

Permalink
Merge pull request #10 from decentrio/sdk-47-upgrade
Browse files Browse the repository at this point in the history
Fix store to save new commission
  • Loading branch information
GNaD13 authored May 15, 2024
2 parents 0d7dc42 + 873277d commit 2ea4c8b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
multistaking "github.com/realiotech/realio-network/app/upgrades/multi-staking"
Expand Down Expand Up @@ -34,6 +35,7 @@ func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
app.IBCKeeper.ClientKeeper,
app.ParamsKeeper,
app.StakingKeeper,
app.GetSubspace(stakingtypes.ModuleName),
app.appCodec,
),
)
Expand Down
33 changes: 11 additions & 22 deletions app/upgrades/v4/commission.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
package v4

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func fixMinCommisionRate(ctx sdk.Context, staking *stakingkeeper.Keeper) {
func fixMinCommisionRate(ctx sdk.Context, staking *stakingkeeper.Keeper, stakingLegacySubspace paramstypes.Subspace) {
// Upgrade every validators min-commission rate
validators := staking.GetAllValidators(ctx)
minComm := sdk.MustNewDecFromStr(NewMinCommisionRate)
params := staking.GetParams(ctx)
params.MinCommissionRate = minComm

err := staking.SetParams(ctx, params)
if err != nil {
panic(err)
if stakingLegacySubspace.HasKeyTable() {
stakingLegacySubspace.Set(ctx, stakingtypes.KeyMinCommissionRate, minComm)
} else {
stakingLegacySubspace.WithKeyTable(stakingtypes.ParamKeyTable())
stakingLegacySubspace.Set(ctx, stakingtypes.KeyMinCommissionRate, minComm)
}

for _, v := range validators {
//nolint
if v.Commission.Rate.LT(minComm) {
comm, err := updateValidatorCommission(ctx, staking, v, minComm)
if err != nil {
panic(err)
}

v.Commission = comm
v.Commission = updateValidatorCommission(ctx, v, minComm)

// call the before-modification hook since we're about to update the commission
staking.Hooks().BeforeValidatorModified(ctx, v.GetOperator())
Expand All @@ -37,22 +30,18 @@ func fixMinCommisionRate(ctx sdk.Context, staking *stakingkeeper.Keeper) {
}
}

func updateValidatorCommission(ctx sdk.Context, staking *stakingkeeper.Keeper,
func updateValidatorCommission(ctx sdk.Context,
validator stakingtypes.Validator, newRate sdk.Dec,
) (stakingtypes.Commission, error) {
) stakingtypes.Commission {
commission := validator.Commission
blockTime := ctx.BlockHeader().Time

if newRate.LT(staking.MinCommissionRate(ctx)) {
return commission, fmt.Errorf("cannot set validator commission to less than minimum rate of %s", staking.MinCommissionRate(ctx))
}

commission.Rate = newRate
if commission.MaxRate.LT(newRate) {
commission.MaxRate = newRate
}

commission.UpdateTime = blockTime

return commission, nil
return commission
}
Empty file modified app/upgrades/v4/param_subspace.go
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"
Expand All @@ -18,11 +19,13 @@ func CreateUpgradeHandler(
clientKeeper ibctmmigrations.ClientKeeper,
pk paramskeeper.Keeper,
sk *stakingkeeper.Keeper,
stakingLegacySubspace paramstypes.Subspace,
cdc codec.BinaryCodec,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade for multi staking...")

fixMinCommisionRate(ctx, sk, stakingLegacySubspace)
migrateParamSubspace(ctx, ck, pk)
// fixMinCommisionRate(ctx, sk)

Expand Down

0 comments on commit 2ea4c8b

Please sign in to comment.