Skip to content

Commit

Permalink
feat: v1_4 upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JoowonYun committed Jan 8, 2024
1 parent 3152a2b commit b976d9e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/xpladev/xpla/docs"

evmupgrade "github.com/xpladev/xpla/app/upgrades/evm"
"github.com/xpladev/xpla/app/upgrades/v1_4"
"github.com/xpladev/xpla/app/upgrades/volunteer"
xplareward "github.com/xpladev/xpla/app/upgrades/xpla_reward"
)
Expand Down Expand Up @@ -422,6 +423,12 @@ func (app *XplaApp) setUpgradeHandlers() {
volunteer.CreateUpgradeHandler(app.mm, app.configurator, &app.AppKeepers),
)

// v1_4 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v1_4.UpgradeName,
v1_4.CreateUpgradeHandler(app.mm, app.configurator, &app.AppKeepers.StakingKeeper),
)

// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
// This will read that value, and execute the preparations for the upgrade.
Expand Down Expand Up @@ -449,6 +456,9 @@ func (app *XplaApp) setUpgradeHandlers() {
storeUpgrades = &storetypes.StoreUpgrades{
Added: volunteer.AddModules,
}
case v1_4.UpgradeName:
storeUpgrades = &storetypes.StoreUpgrades{}

}

if storeUpgrades != nil {
Expand Down
5 changes: 5 additions & 0 deletions app/upgrades/v1_4/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v1_4

const (
UpgradeName = "v1_4"
)
41 changes: 41 additions & 0 deletions app/upgrades/v1_4/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v1_4

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

stakingkeeper "github.com/xpladev/xpla/x/staking/keeper"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
stakingKeeper *stakingkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {

validators := stakingKeeper.GetAllValidators(ctx)
for _, validator := range validators {
tolerance, err := validator.SharesFromTokens(sdk.OneInt())
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "validator must have valid share")
}

delegations := stakingKeeper.GetValidatorDelegations(ctx, validator.GetOperator())
for _, delegation := range delegations {
if delegation.Shares.GTE(tolerance) {
continue
}

_, err := stakingKeeper.Unbond(ctx, delegation.GetDelegatorAddr(), validator.GetOperator(), delegation.GetShares())
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "dust delegation must be unbond")
}
}
}

return mm.RunMigrations(ctx, configurator, fromVM)
}
}

0 comments on commit b976d9e

Please sign in to comment.