Skip to content

Commit

Permalink
fix!: introduce v4 provider migration code (#1762)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek authored Apr 8, 2024
1 parent e57ec77 commit 15d2d21
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
9 changes: 8 additions & 1 deletion x/ccv/provider/migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

providerkeeper "github.com/cosmos/interchain-security/v4/x/ccv/provider/keeper"
v3 "github.com/cosmos/interchain-security/v4/x/ccv/provider/migrations/v3"
v4 "github.com/cosmos/interchain-security/v4/x/ccv/provider/migrations/v4"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -29,6 +30,12 @@ func (m Migrator) Migrate1to2(ctx sdktypes.Context) error {

// Migrate2to3 migrates x/ccvprovider state from consensus version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdktypes.Context) error {
v3.MigrateParams(ctx, m.paramSpace)
return v3.MigrateQueuedPackets(ctx, m.providerKeeper)
}

// Migrate3to4 migrates x/ccvprovider state from consensus version 3 to 4.
// The migration consists of provider chain params additions.
func (m Migrator) Migrate3to4(ctx sdktypes.Context) error {
v4.MigrateParams(ctx, m.paramSpace)
return nil
}
18 changes: 0 additions & 18 deletions x/ccv/provider/migrations/v3/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/require"

testutil "github.com/cosmos/interchain-security/v4/testutil/keeper"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
)

func TestMigrate2To3(t *testing.T) {
Expand Down Expand Up @@ -116,20 +115,3 @@ func TestMigrate2To3(t *testing.T) {
require.False(t, found)
}
}

func TestMigrateParams(t *testing.T) {
inMemParams := testutil.NewInMemKeeperParams(t)
_, ctx, ctrl, _ := testutil.GetProviderKeeperAndCtx(t, inMemParams)
defer ctrl.Finish()

// initially blocks per epoch param does not exist
require.False(t, inMemParams.ParamsSubspace.Has(ctx, providertypes.KeyBlocksPerEpoch))

MigrateParams(ctx, *inMemParams.ParamsSubspace)

// after migration, blocks per epoch param should exist and be equal to default
require.True(t, inMemParams.ParamsSubspace.Has(ctx, providertypes.KeyBlocksPerEpoch))
var blocksPerEpochParam int64
inMemParams.ParamsSubspace.Get(ctx, providertypes.KeyBlocksPerEpoch, &blocksPerEpochParam)
require.Equal(t, providertypes.DefaultBlocksPerEpoch, blocksPerEpochParam)
}
11 changes: 0 additions & 11 deletions x/ccv/provider/migrations/v3/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (

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

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
providerkeeper "github.com/cosmos/interchain-security/v4/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
)

// MigrateQueuedPackets processes all queued packet data for all consumer chains that were stored
Expand All @@ -25,12 +23,3 @@ func MigrateQueuedPackets(ctx sdk.Context, k providerkeeper.Keeper) error {
}
return nil
}

func MigrateParams(ctx sdk.Context, paramsSubspace paramtypes.Subspace) {
if paramsSubspace.HasKeyTable() {
paramsSubspace.Set(ctx, providertypes.KeyBlocksPerEpoch, providertypes.DefaultBlocksPerEpoch)
} else {
paramsSubspace.WithKeyTable(providertypes.ParamKeyTable())
paramsSubspace.Set(ctx, providertypes.KeyBlocksPerEpoch, providertypes.DefaultBlocksPerEpoch)
}
}
27 changes: 27 additions & 0 deletions x/ccv/provider/migrations/v4/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v4

import (
"testing"

"github.com/stretchr/testify/require"

testutil "github.com/cosmos/interchain-security/v4/testutil/keeper"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
)

func TestMigrateParams(t *testing.T) {
inMemParams := testutil.NewInMemKeeperParams(t)
_, ctx, ctrl, _ := testutil.GetProviderKeeperAndCtx(t, inMemParams)
defer ctrl.Finish()

// initially blocks per epoch param does not exist
require.False(t, inMemParams.ParamsSubspace.Has(ctx, providertypes.KeyBlocksPerEpoch))

MigrateParams(ctx, *inMemParams.ParamsSubspace)

// after migration, blocks per epoch param should exist and be equal to default
require.True(t, inMemParams.ParamsSubspace.Has(ctx, providertypes.KeyBlocksPerEpoch))
var blocksPerEpochParam int64
inMemParams.ParamsSubspace.Get(ctx, providertypes.KeyBlocksPerEpoch, &blocksPerEpochParam)
require.Equal(t, providertypes.DefaultBlocksPerEpoch, blocksPerEpochParam)
}
18 changes: 18 additions & 0 deletions x/ccv/provider/migrations/v4/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v4

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

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
)

// MigrateParams adds missing provider chain params to the param store.
func MigrateParams(ctx sdk.Context, paramsSubspace paramtypes.Subspace) {
if paramsSubspace.HasKeyTable() {
paramsSubspace.Set(ctx, providertypes.KeyBlocksPerEpoch, providertypes.DefaultBlocksPerEpoch)
} else {
paramsSubspace.WithKeyTable(providertypes.ParamKeyTable())
paramsSubspace.Set(ctx, providertypes.KeyBlocksPerEpoch, providertypes.DefaultBlocksPerEpoch)
}
}
2 changes: 1 addition & 1 deletion x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 3 }
func (AppModule) ConsensusVersion() uint64 { return 4 }

// BeginBlock implements the AppModule interface
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
Expand Down

0 comments on commit 15d2d21

Please sign in to comment.