From bfc8aaaafcef04f85496b98f2c0beec8acf8cf62 Mon Sep 17 00:00:00 2001 From: Eduardo Diaz Date: Thu, 20 Jul 2023 23:07:50 -0400 Subject: [PATCH 1/4] add initial migrator implementation --- x/abstractaccount/keeper/keeper.go | 5 ++ x/abstractaccount/keeper/migration.go | 25 ++++++++++ x/abstractaccount/migrations/v2/migration.go | 48 ++++++++++++++++++++ x/abstractaccount/module.go | 6 +++ 4 files changed, 84 insertions(+) create mode 100644 x/abstractaccount/keeper/migration.go create mode 100644 x/abstractaccount/migrations/v2/migration.go diff --git a/x/abstractaccount/keeper/keeper.go b/x/abstractaccount/keeper/keeper.go index 4d436a2..7c7d5c1 100644 --- a/x/abstractaccount/keeper/keeper.go +++ b/x/abstractaccount/keeper/keeper.go @@ -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) +} diff --git a/x/abstractaccount/keeper/migration.go b/x/abstractaccount/keeper/migration.go new file mode 100644 index 0000000..0a157f8 --- /dev/null +++ b/x/abstractaccount/keeper/migration.go @@ -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} +} + +// Migrate1to2 migrates from version 1 to 2. +func (m Migrator) Migrate1to2(ctx sdk.Context, key storetypes.StoreKey) error { + return v2.MigrateStore(ctx, keys) +} diff --git a/x/abstractaccount/migrations/v2/migration.go b/x/abstractaccount/migrations/v2/migration.go new file mode 100644 index 0000000..419eebb --- /dev/null +++ b/x/abstractaccount/migrations/v2/migration.go @@ -0,0 +1,48 @@ +package v2 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/larry0x/abstract-account/x/abstractaccount/types" +) + +// MigrateStore performs in-place params migrations of +// BypassMinFeeMsgTypes and MaxTotalBypassMinFeeMsgGasUsage +// from app.toml to globalfee params. +func MigrateStore(ctx sdk.Context, store sdk.KVStore, cdc codec.BinaryCodec) error { + store := ctx.KVStore(key) + + params := types.DefaultParams() + params, err := getParams(ctx, store, cdc) + if err != nil { + return err + } + + return setParams(ctx, store, cdc, ¶ms) +} + +func getParams(ctx sdk.Context, store sdk.KVStore, cdc u.BinaryCodec) (*types.Params, error) { + bz := store.Get(types.KeyParams) + if bz == nil { + return nil, sdkerrors.ErrNotFound.Wrap("x/abstractaccount module params") + } + + 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 +} diff --git a/x/abstractaccount/module.go b/x/abstractaccount/module.go index bc79c8b..c10b1ee 100644 --- a/x/abstractaccount/module.go +++ b/x/abstractaccount/module.go @@ -81,6 +81,12 @@ 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.k.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 { From 74587ace8b6677268e25f91ed675e0a065c9743b Mon Sep 17 00:00:00 2001 From: Eduardo Diaz Date: Thu, 20 Jul 2023 23:50:54 -0400 Subject: [PATCH 2/4] add test migration --- x/abstractaccount/migrations/v2/migration.go | 12 ++-- .../migrations/v2/v2_test/migration_test.go | 62 +++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 x/abstractaccount/migrations/v2/v2_test/migration_test.go diff --git a/x/abstractaccount/migrations/v2/migration.go b/x/abstractaccount/migrations/v2/migration.go index 419eebb..0da8259 100644 --- a/x/abstractaccount/migrations/v2/migration.go +++ b/x/abstractaccount/migrations/v2/migration.go @@ -5,27 +5,29 @@ import ( "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, store sdk.KVStore, cdc codec.BinaryCodec) error { +func MigrateStore(ctx sdk.Context, key storetypes.StoreKey, cdc codec.BinaryCodec) error { store := ctx.KVStore(key) - params := types.DefaultParams() params, err := getParams(ctx, store, cdc) if err != nil { return err } - return setParams(ctx, store, cdc, ¶ms) + return setParams(ctx, store, cdc, params) } -func getParams(ctx sdk.Context, store sdk.KVStore, cdc u.BinaryCodec) (*types.Params, error) { +func getParams(ctx sdk.Context, store sdk.KVStore, cdc codec.BinaryCodec) (*types.Params, error) { bz := store.Get(types.KeyParams) if bz == nil { - return nil, sdkerrors.ErrNotFound.Wrap("x/abstractaccount module params") + params := types.DefaultParams() + return params, nil } var params types.Params diff --git a/x/abstractaccount/migrations/v2/v2_test/migration_test.go b/x/abstractaccount/migrations/v2/v2_test/migration_test.go new file mode 100644 index 0000000..bcce211 --- /dev/null +++ b/x/abstractaccount/migrations/v2/v2_test/migration_test.go @@ -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) + +} From 68904221c088f3bef6ed6bf2693fab86f28921e1 Mon Sep 17 00:00:00 2001 From: Eduardo Diaz Date: Fri, 21 Jul 2023 00:04:54 -0400 Subject: [PATCH 3/4] fix mm module migration --- go.mod | 2 ++ x/abstractaccount/keeper/migration.go | 6 +++--- x/abstractaccount/module.go | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index c459349..9bcf531 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/x/abstractaccount/keeper/migration.go b/x/abstractaccount/keeper/migration.go index 0a157f8..ad2707d 100644 --- a/x/abstractaccount/keeper/migration.go +++ b/x/abstractaccount/keeper/migration.go @@ -16,10 +16,10 @@ type Migrator struct { // NewMigrator returns a new Migrator. func NewMigrator(key storetypes.StoreKey, cdc codec.BinaryCodec) Migrator { - return Migrator{key: key} + return Migrator{key: key, cdc: cdc} } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context, key storetypes.StoreKey) error { - return v2.MigrateStore(ctx, keys) +func (m Migrator) Migrate1to2(ctx sdk.Context) error { + return v2.MigrateStore(ctx, m.key, m.cdc) } diff --git a/x/abstractaccount/module.go b/x/abstractaccount/module.go index c10b1ee..940a7d1 100644 --- a/x/abstractaccount/module.go +++ b/x/abstractaccount/module.go @@ -82,7 +82,7 @@ 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.k.Migrator() + 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)) } From ef9e3f63d0cd6039d4a251ce719090cea5d688f1 Mon Sep 17 00:00:00 2001 From: Eduardo Diaz Date: Fri, 21 Jul 2023 00:25:01 -0400 Subject: [PATCH 4/4] upgrade consensus version --- x/abstractaccount/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/abstractaccount/module.go b/x/abstractaccount/module.go index 940a7d1..2632ff5 100644 --- a/x/abstractaccount/module.go +++ b/x/abstractaccount/module.go @@ -90,7 +90,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { } func (AppModule) ConsensusVersion() uint64 { - return 1 + return 2 } func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {