Skip to content

Commit

Permalink
Param store migration and test
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Aug 14, 2023
1 parent f1c775c commit 525f6ca
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
6 changes: 6 additions & 0 deletions module/x/gravity/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
6 changes: 6 additions & 0 deletions module/x/gravity/migrations/v2/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v2

const (
// ModuleName is the name of the module
ModuleName = "gravity"
)
20 changes: 20 additions & 0 deletions module/x/gravity/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions module/x/gravity/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 5 additions & 5 deletions module/x/gravity/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 525f6ca

Please sign in to comment.