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

chore: migrate pundix chain accounts #804

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (app *App) setupUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler(
plan.UpgradeName,
plan.CreateUpgradeHandler(
app.appCodec,
app.mm,
app.configurator,
&app.AppKeepers,
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package upgrades
import (
storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

Expand All @@ -18,7 +19,7 @@ type Upgrade struct {
UpgradeName string

// CreateUpgradeHandler defines the function that creates an upgrade handler
CreateUpgradeHandler func(*module.Manager, module.Configurator, *keepers.AppKeepers) upgradetypes.UpgradeHandler
CreateUpgradeHandler func(codec.Codec, *module.Manager, module.Configurator, *keepers.AppKeepers) upgradetypes.UpgradeHandler

// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades func() *storetypes.StoreUpgrades
Expand Down
50 changes: 48 additions & 2 deletions app/upgrades/v8/pundix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
tmjson "github.com/cometbft/cometbft/libs/json"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -36,6 +37,7 @@ import (
)

type Pundix struct {
cdc codec.Codec
accountKeeper authkeeper.AccountKeeper
erc20Keeper erc20keeper.Keeper
bankKeeper bankkeeper.Keeper
Expand All @@ -44,8 +46,9 @@ type Pundix struct {
storeKey storetypes.StoreKey
}

func NewPundix(app *keepers.AppKeepers) *Pundix {
func NewPundix(cdc codec.Codec, app *keepers.AppKeepers) *Pundix {
return &Pundix{
cdc: cdc,
accountKeeper: app.AccountKeeper,
erc20Keeper: app.Erc20Keeper,
bankKeeper: app.BankKeeper,
Expand All @@ -61,6 +64,15 @@ func (m *Pundix) Migrate(ctx sdk.Context) error {
if err != nil {
return err
}

authRaw, ok := appState[types.ModuleName]
if !ok || len(authRaw) == 0 {
return sdkerrors.ErrNotFound.Wrap("auth genesis")
}
if err = m.migrateAuth(ctx, appState[types.ModuleName]); err != nil {
return err
}

// todo migrate other data
bankGenesis, ok := appState[banktypes.ModuleName]
if !ok || len(bankGenesis) == 0 {
Expand All @@ -69,9 +81,43 @@ func (m *Pundix) Migrate(ctx sdk.Context) error {
return m.migrateBank(ctx, bankGenesis)
}

func (m *Pundix) migrateAuth(ctx sdk.Context, authRaw json.RawMessage) error {
var authGenesis types.GenesisState
if err := m.cdc.UnmarshalJSON(authRaw, &authGenesis); err != nil {
return err
}
genesisAccounts, err := types.UnpackAccounts(authGenesis.Accounts)
if err != nil {
return err
}
for _, genAcc := range genesisAccounts {
pubKey := genAcc.GetPubKey()
if pubKey == nil {
continue
}
baseAcc, ok := genAcc.(*types.BaseAccount)
if !ok {
continue
}
accAddr, err := sdk.GetFromBech32(baseAcc.Address, "px")
if err != nil {
return err
}
nulnut marked this conversation as resolved.
Show resolved Hide resolved
if m.accountKeeper.HasAccount(ctx, accAddr) {
continue
}
newAcc := m.accountKeeper.NewAccountWithAddress(ctx, accAddr)
if err = newAcc.SetPubKey(pubKey); err != nil {
return err
}
m.accountKeeper.SetAccount(ctx, newAcc)
}
return nil
}
nulnut marked this conversation as resolved.
Show resolved Hide resolved

func (m *Pundix) migrateBank(ctx sdk.Context, bankRaw json.RawMessage) error {
var bankGenesis banktypes.GenesisState
if err := tmjson.Unmarshal(bankRaw, &bankGenesis); err != nil {
if err := m.cdc.UnmarshalJSON(bankRaw, &bankGenesis); err != nil {
return err
}
if err := m.migratePUNDIX(ctx, bankGenesis.Balances, bankGenesis.Supply); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -31,7 +32,7 @@ import (
fxstakingv8 "github.com/functionx/fx-core/v8/x/staking/migrations/v8"
)

func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
func CreateUpgradeHandler(cdc codec.Codec, mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
cacheCtx, commit := sdk.UnwrapSDKContext(ctx).CacheContext()

Expand All @@ -55,7 +56,7 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator,
return fromVM, err
}

if err = NewPundix(app).Migrate(cacheCtx); err != nil {
if err = NewPundix(cdc, app).Migrate(cacheCtx); err != nil {
return fromVM, err
}

Expand Down