Skip to content

Commit

Permalink
fix: migrate BaseAccount -> EthAccount (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoowonYun committed Jan 24, 2024
1 parent 074c99b commit f81c91f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (app *XplaApp) setUpgradeHandlers() {
// v1_4 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v1_4.UpgradeName,
v1_4.CreateUpgradeHandler(app.mm, app.configurator, &app.AppKeepers.StakingKeeper),
v1_4.CreateUpgradeHandler(app.mm, app.configurator, app.AppKeepers.AccountKeeper, &app.AppKeepers.StakingKeeper),
)

// When a planned update height is reached, the old binary will panic
Expand Down
65 changes: 49 additions & 16 deletions app/upgrades/v1_4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,71 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/ethereum/go-ethereum/common"
etherminttypes "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"

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

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
authKeeper authkeeper.AccountKeeper,
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")
}
migrateBaseAccountToEthAccount(ctx, authKeeper)

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

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

_, err := stakingKeeper.Unbond(ctx, delegation.GetDelegatorAddr(), validator.GetOperator(), delegation.GetShares())
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "dust delegation must be unbond")
}
func migrateBaseAccountToEthAccount(ctx sdk.Context, authKeeper authkeeper.AccountKeeper) {
authKeeper.IterateAccounts(ctx, func(acc authtypes.AccountI) (stop bool) {
switch acc := acc.(type) {
case *authtypes.BaseAccount:
ethAcc := &etherminttypes.EthAccount{
BaseAccount: acc,
CodeHash: common.BytesToHash(evmtypes.EmptyCodeHash).String(),
}

authKeeper.SetAccount(ctx, ethAcc)
}

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

func migrateDustDelegation(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper) error {
validators := stakingKeeper.GetAllValidators(ctx)
for _, validator := range validators {
tolerance, err := validator.SharesFromTokens(sdk.OneInt())
if err != nil {
return 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 sdkerrors.Wrapf(sdkerrors.ErrLogic, "dust delegation must be unbond")
}
}
}

return nil
}

0 comments on commit f81c91f

Please sign in to comment.