Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DO NOT MERGE: stkOSMO migration #671

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import (
pstakeappparams "github.com/persistenceOne/pstake-native/v2/app/params"
"github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc"
liquidstakeibckeeper "github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/keeper"
"github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/migrations/stkosmo"
liquidstakeibctypes "github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/types"
"github.com/persistenceOne/pstake-native/v2/x/lscosmos"
lscosmostypes "github.com/persistenceOne/pstake-native/v2/x/lscosmos/types"
Expand Down Expand Up @@ -997,4 +998,23 @@ func (app *PstakeApp) RegisterUpgradeHandler() {
// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

app.UpgradeKeeper.SetUpgradeHandler(
StkOSMOUpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("running 2.5.0-stkosmo upgrade handler")

err = stkosmo.MigrateStore(
ctx,
sdk.NewKVStoreKey(liquidstakeibctypes.StoreKey),
app.appCodec,
app.BankKeeper,
)
if err != nil {
return nil, err
}

return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)
}
5 changes: 3 additions & 2 deletions app/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

const (
appName = "pStake"
UpgradeName = "v2.3.0"
appName = "pStake"
UpgradeName = "v2.3.0"
StkOSMOUpgradeName = "v2.5.0-stkosmo"
)
143 changes: 143 additions & 0 deletions x/liquidstakeibc/migrations/stkosmo/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package stkosmo

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/types"
)

const (
OsmosisTestnetChainID = "osmo-test-5"
OsmosisTestnetMintDenom = "stk/uosmo"
)

// MigrateStore performs in-place store migrations from v2.3.0 to remove stkOSMO from test-core-2.
// The migration includes:
//
// - Burn all minted stkOSMO.
// - Remove all the related store objects.
// - Migrate stkOSMO host chain to remove it from the store.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, k types.BankKeeper) error {

// Burn all the coins present in the module account.
if err := k.BurnCoins(
ctx,
types.ModuleName,
sdk.NewCoins(k.GetBalance(ctx, authtypes.NewModuleAddress(types.ModuleName), OsmosisTestnetMintDenom)),
); err != nil {
return err
}

// Burn all coins on other addresses.
k.IterateAllBalances(
ctx,
func(address sdk.AccAddress, coin sdk.Coin) (stop bool) {

if coin.Denom == OsmosisTestnetMintDenom {
// Send the whole address balance to the module account.
if err := k.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, sdk.NewCoins(coin)); err != nil {
kruspy marked this conversation as resolved.
Show resolved Hide resolved
return false
}

// Burn the coins.
if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(coin)); err != nil {
return false
}
}

return false
},
)

// Remove all the object stores related to the chain.
deleteDeposits(ctx, storeKey, cdc)
deleteLSMDeposits(ctx, storeKey, cdc)
deleteUnbondings(ctx, storeKey, cdc)
deleteUserUnbondings(ctx, storeKey, cdc)
deleteValidatorUnbondings(ctx, storeKey, cdc)

// Remove the host chain from the store.
store := prefix.NewStore(ctx.KVStore(storeKey), types.HostChainKey)
store.Delete([]byte(OsmosisTestnetChainID))
kruspy marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func deleteDeposits(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := prefix.NewStore(ctx.KVStore(storeKey), types.DepositKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
deposit := types.Deposit{}
cdc.MustUnmarshal(iterator.Value(), &deposit)

if deposit.ChainId == OsmosisTestnetChainID {
store.Delete(types.GetDepositStoreKey(deposit.ChainId, deposit.Epoch))
}
}
}

func deleteLSMDeposits(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := prefix.NewStore(ctx.KVStore(storeKey), types.LSMDepositKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
deposit := types.LSMDeposit{}
cdc.MustUnmarshal(iterator.Value(), &deposit)

if deposit.ChainId == OsmosisTestnetChainID {
store.Delete(types.GetLSMDepositStoreKey(deposit.ChainId, deposit.DelegatorAddress, deposit.Denom))
}
}
}

func deleteUnbondings(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := prefix.NewStore(ctx.KVStore(storeKey), types.UnbondingKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
ub := types.Unbonding{}
cdc.MustUnmarshal(iterator.Value(), &ub)

if ub.ChainId == OsmosisTestnetChainID {
store.Delete(types.GetUnbondingStoreKey(ub.ChainId, ub.EpochNumber))
}
}
}

func deleteUserUnbondings(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := prefix.NewStore(ctx.KVStore(storeKey), types.UserUnbondingKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
ub := types.UserUnbonding{}
cdc.MustUnmarshal(iterator.Value(), &ub)

if ub.ChainId == OsmosisTestnetChainID {
store.Delete(types.GetUserUnbondingStoreKey(ub.ChainId, ub.Address, ub.EpochNumber))
}
}
}

func deleteValidatorUnbondings(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := prefix.NewStore(ctx.KVStore(storeKey), types.ValidatorUnbondingKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
ub := types.ValidatorUnbonding{}
cdc.MustUnmarshal(iterator.Value(), &ub)

if ub.ChainId == OsmosisTestnetChainID {
store.Delete(types.GetValidatorUnbondingStoreKey(ub.ChainId, ub.ValidatorAddress, ub.EpochNumber))
}
}
}
1 change: 1 addition & 0 deletions x/liquidstakeibc/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BankKeeper interface {
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
IterateAllBalances(ctx sdk.Context, cb func(address sdk.AccAddress, coin sdk.Coin) (stop bool))
}

type ScopedKeeper interface {
Expand Down
Loading