Skip to content

Commit

Permalink
Merge pull request larry0x#1 from burnt-labs/edjroz/enhancement/add-m…
Browse files Browse the repository at this point in the history
…igration

Add migration
  • Loading branch information
edjroz authored Sep 4, 2023
2 parents 2ea462d + ef9e3f6 commit 6899550
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,5 @@ replace (
// Downgraded to avoid bugs in following commits which caused simulations to fail
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)

replace github.com/larry0x/abstract-account => github.com/burnt-labs/abstract-account v0.0.0-20230721030750-bfc8aaaafcef
5 changes: 5 additions & 0 deletions x/abstractaccount/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ func (k Keeper) DeleteSignerAddress(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeySignerAddress)
}

// ------------------------------- Migration -------------------------------
func (k Keeper) Migrator() Migrator {
return NewMigrator(k.storeKey, k.cdc)
}
25 changes: 25 additions & 0 deletions x/abstractaccount/keeper/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/larry0x/abstract-account/x/abstractaccount/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
key storetypes.StoreKey
cdc codec.BinaryCodec
}

// NewMigrator returns a new Migrator.
func NewMigrator(key storetypes.StoreKey, cdc codec.BinaryCodec) Migrator {
return Migrator{key: key, cdc: cdc}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.key, m.cdc)
}
50 changes: 50 additions & 0 deletions x/abstractaccount/migrations/v2/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v2

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/larry0x/abstract-account/x/abstractaccount/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
)

// MigrateStore performs in-place params migrations of
// BypassMinFeeMsgTypes and MaxTotalBypassMinFeeMsgGasUsage
// from app.toml to globalfee params.
func MigrateStore(ctx sdk.Context, key storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(key)

params, err := getParams(ctx, store, cdc)
if err != nil {
return err
}

return setParams(ctx, store, cdc, params)
}

func getParams(ctx sdk.Context, store sdk.KVStore, cdc codec.BinaryCodec) (*types.Params, error) {
bz := store.Get(types.KeyParams)
if bz == nil {
params := types.DefaultParams()
return params, nil
}

var params types.Params
if err := cdc.Unmarshal(bz, &params); err != nil {
return nil, types.ErrParsingParams.Wrap(err.Error())
}

return &params, nil

}

func setParams(ctx sdk.Context, store sdk.KVStore, cdc codec.BinaryCodec, params *types.Params) error {
bz, err := cdc.Marshal(params)
if err != nil {
return types.ErrParsingParams.Wrap(err.Error())
}
store.Set(types.KeyParams, bz)

return nil
}
62 changes: 62 additions & 0 deletions x/abstractaccount/migrations/v2/v2_test/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package v2_test

import (
"testing"

cometdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
"github.com/larry0x/abstract-account/x/abstractaccount/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

v2 "github.com/larry0x/abstract-account/x/abstractaccount/migrations/v2"
)

func TestMigrateStore(t *testing.T) {
db := cometdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)

storeKey := sdk.NewKVStoreKey(paramtypes.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey("mem_key")

stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

store := ctx.KVStore(storeKey)
require.NoError(t, stateStore.LoadLatestVersion())

bz := store.Get(types.KeyParams)
require.Nil(t, bz)

// run global fee migration
err := v2.MigrateStore(ctx, storeKey, cdc)
require.NoError(t, err)

// get params
storeAfterMig := ctx.KVStore(storeKey)
newBz := storeAfterMig.Get(types.KeyParams)
require.NotNil(t, newBz)

var newParams types.Params
require.NoError(t, cdc.Unmarshal(newBz, &newParams))

expectedParams := types.DefaultParams()
require.Equal(t, expectedParams.AllowAllCodeIDs, newParams.AllowAllCodeIDs)
require.Equal(t, 0, len(newParams.AllowedCodeIDs))
require.Equal(t, expectedParams.MaxGasBefore, newParams.MaxGasBefore)
require.Equal(t, expectedParams.MaxGasAfter, newParams.MaxGasAfter)

}
8 changes: 7 additions & 1 deletion x/abstractaccount/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))

m := am.keeper.Migrator()
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/abstract-account from version 1 to 2: %v", err))
}

}

func (AppModule) ConsensusVersion() uint64 {
return 1
return 2
}

func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {
Expand Down

0 comments on commit 6899550

Please sign in to comment.