Skip to content

Commit

Permalink
write upgrade for new minimum commission rate
Browse files Browse the repository at this point in the history
  • Loading branch information
neitdung committed May 6, 2024
1 parent 7fd9755 commit 21be234
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
10 changes: 10 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"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
multistaking "github.com/realiotech/realio-network/app/upgrades/multi-staking"
v4 "github.com/realiotech/realio-network/app/upgrades/v4"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
Expand All @@ -24,6 +25,15 @@ func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
),
)

app.UpgradeKeeper.SetUpgradeHandler(
v4.UpgradeName,
v4.CreateUpgradeHandler(
app.mm,
app.configurator,
app.StakingKeeper,
),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("failed to read upgrade info from disk: %w", err))
Expand Down
7 changes: 7 additions & 0 deletions app/upgrades/v4/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v4

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v4-Commission"

Check failure on line 5 in app/upgrades/v4/constants.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
NewMinCommisionRate = "0.05"
)

Check failure on line 7 in app/upgrades/v4/constants.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
44 changes: 44 additions & 0 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v4

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

Check failure on line 5 in app/upgrades/v4/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

Check failure on line 7 in app/upgrades/v4/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
sk stakingkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade for multi staking...")
fixMinCommisionRate(ctx, sk)
return mm.RunMigrations(ctx, configurator, vm)
}
}

func fixMinCommisionRate(ctx sdk.Context, staking stakingkeeper.Keeper) {
// Upgrade every validators min-commission rate
validators := staking.GetAllValidators(ctx)
newComm := sdk.MustNewDecFromStr(NewMinCommisionRate)
params := staking.GetParams(ctx)
params.MinCommissionRate = newComm
staking.SetParams(ctx, params)
for _, v := range validators {
// nolint

Check failure on line 30 in app/upgrades/v4/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

directive `// nolint` should be written without leading space as `//nolint` (nolintlint)
if v.Commission.Rate.LT(newComm) {
comm, err := staking.UpdateValidatorCommission(ctx, v, newComm)
if err != nil {
panic(err)
}

v.Commission = comm

// call the before-modification hook since we're about to update the commission
staking.BeforeValidatorModified(ctx, v.GetOperator())
staking.SetValidator(ctx, v)
}
}
}

0 comments on commit 21be234

Please sign in to comment.