From 525f6cae3b92d9d4010b23bc632e7f99c0a33aa4 Mon Sep 17 00:00:00 2001 From: Collin Brittain Date: Mon, 14 Aug 2023 13:41:18 -0500 Subject: [PATCH] Param store migration and test --- module/x/gravity/keeper/migrations.go | 6 ++++ module/x/gravity/migrations/v2/keys.go | 6 ++++ module/x/gravity/migrations/v2/store.go | 20 ++++++++++++ module/x/gravity/migrations/v2/store_test.go | 33 ++++++++++++++++++++ module/x/gravity/module.go | 10 +++--- 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 module/x/gravity/migrations/v2/keys.go create mode 100644 module/x/gravity/migrations/v2/store.go create mode 100644 module/x/gravity/migrations/v2/store_test.go diff --git a/module/x/gravity/keeper/migrations.go b/module/x/gravity/keeper/migrations.go index 38d50b0ca..c5a52cc70 100644 --- a/module/x/gravity/keeper/migrations.go +++ b/module/x/gravity/keeper/migrations.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" v1 "github.com/peggyjv/gravity-bridge/module/v3/x/gravity/migrations/v1" + v2 "github.com/peggyjv/gravity-bridge/module/v3/x/gravity/migrations/v2" ) // Migrator is a struct for handling in-place store migrations. @@ -19,3 +20,8 @@ func NewMigrator(keeper Keeper) Migrator { func (m Migrator) Migrate1to2(ctx sdk.Context) error { return v1.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) } + +// Migrate2to3 migrates from consensus version 2 to 3. +func (m Migrator) Migrate2to3(ctx sdk.Context) error { + return v2.MigrateParamStore(ctx, m.keeper.paramSpace) +} diff --git a/module/x/gravity/migrations/v2/keys.go b/module/x/gravity/migrations/v2/keys.go new file mode 100644 index 000000000..4454afd45 --- /dev/null +++ b/module/x/gravity/migrations/v2/keys.go @@ -0,0 +1,6 @@ +package v2 + +const ( + // ModuleName is the name of the module + ModuleName = "gravity" +) diff --git a/module/x/gravity/migrations/v2/store.go b/module/x/gravity/migrations/v2/store.go new file mode 100644 index 000000000..fc297eac5 --- /dev/null +++ b/module/x/gravity/migrations/v2/store.go @@ -0,0 +1,20 @@ +package v2 + +import ( + sdktypes "github.com/cosmos/cosmos-sdk/types" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/peggyjv/gravity-bridge/module/v3/x/gravity/types" +) + +func MigrateParamStore(ctx sdktypes.Context, subspace paramstypes.Subspace) error { + if subspace.HasKeyTable() { + subspace.Set(ctx, types.ParamStoreConfirmedOutgoingTxWindow, types.DefaultParams().ConfirmedOutgoingTxWindow) + subspace.Set(ctx, types.ParamStoreEventVoteWindow, types.DefaultParams().EthereumEventVoteWindow) + } else { + subspace.WithKeyTable(types.ParamKeyTable()) + subspace.Set(ctx, types.ParamStoreConfirmedOutgoingTxWindow, types.DefaultParams().ConfirmedOutgoingTxWindow) + subspace.Set(ctx, types.ParamStoreEventVoteWindow, types.DefaultParams().EthereumEventVoteWindow) + } + + return nil +} diff --git a/module/x/gravity/migrations/v2/store_test.go b/module/x/gravity/migrations/v2/store_test.go new file mode 100644 index 000000000..31315044a --- /dev/null +++ b/module/x/gravity/migrations/v2/store_test.go @@ -0,0 +1,33 @@ +package v2_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + v2 "github.com/peggyjv/gravity-bridge/module/v3/x/gravity/migrations/v2" + "github.com/peggyjv/gravity-bridge/module/v3/x/gravity/types" + "github.com/stretchr/testify/require" +) + +func TestStoreMigration(t *testing.T) { + gravityKey := storetypes.NewKVStoreKey(v2.ModuleName) + tGravityKey := storetypes.NewTransientStoreKey("transient_test") + ctx := testutil.DefaultContext(gravityKey, tGravityKey) + aminoCodec := codec.NewLegacyAmino() + paramstore := paramtypes.NewSubspace(nil, aminoCodec, gravityKey, tGravityKey, v2.ModuleName) + + // Check no params + require.False(t, paramstore.Has(ctx, types.ParamStoreConfirmedOutgoingTxWindow)) + require.False(t, paramstore.Has(ctx, types.ParamStoreEventVoteWindow)) + + // Run migrations. + err := v2.MigrateParamStore(ctx, paramstore) + require.NoError(t, err) + + // Make sure the new params are set. + require.True(t, paramstore.Has(ctx, types.ParamStoreConfirmedOutgoingTxWindow)) + require.True(t, paramstore.Has(ctx, types.ParamStoreEventVoteWindow)) +} diff --git a/module/x/gravity/module.go b/module/x/gravity/module.go index 49bea2a9c..9712e08b8 100644 --- a/module/x/gravity/module.go +++ b/module/x/gravity/module.go @@ -137,11 +137,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { panic(fmt.Sprintf("failed to migrate x/gravity from version 1 to 2: %v", err)) } - - // The 2-to-3 migration is a no-op because there are no required store migration steps - cfg.RegisterMigration(types.ModuleName, 2, func (ctx sdk.Context) error { - return nil - }) + + // The 2-to-3 migration is a no-op because there are no required store migration steps + if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { + panic(fmt.Sprintf("failed to migrate x/gravity from version 2 to 3: %v", err)) + } } // InitGenesis initializes the genesis state for this module and implements app module.