forked from larry0x/abstract-account
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request larry0x#1 from burnt-labs/edjroz/enhancement/add-m…
…igration Add migration
- Loading branch information
Showing
6 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶ms); err != nil { | ||
return nil, types.ErrParsingParams.Wrap(err.Error()) | ||
} | ||
|
||
return ¶ms, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters