From 21be234b576d87fae530b421a7a3ef9e499064d5 Mon Sep 17 00:00:00 2001 From: Dzung Do Date: Mon, 6 May 2024 14:45:47 +0700 Subject: [PATCH] write upgrade for new minimum commission rate --- app/upgrades.go | 10 ++++++++ app/upgrades/v4/constants.go | 7 ++++++ app/upgrades/v4/upgrades.go | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 app/upgrades/v4/constants.go create mode 100644 app/upgrades/v4/upgrades.go diff --git a/app/upgrades.go b/app/upgrades.go index 577b7b68..2ff087b4 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -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" ) @@ -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)) diff --git a/app/upgrades/v4/constants.go b/app/upgrades/v4/constants.go new file mode 100644 index 00000000..5a2bc657 --- /dev/null +++ b/app/upgrades/v4/constants.go @@ -0,0 +1,7 @@ +package v4 + +const ( + // UpgradeName defines the on-chain upgrade name. + UpgradeName = "v4-Commission" + NewMinCommisionRate = "0.05" +) \ No newline at end of file diff --git a/app/upgrades/v4/upgrades.go b/app/upgrades/v4/upgrades.go new file mode 100644 index 00000000..914d3b87 --- /dev/null +++ b/app/upgrades/v4/upgrades.go @@ -0,0 +1,44 @@ +package v4 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" +) + +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 + 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) + } + } +}