Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update migration handling for provider consensus v2->v3 #1536

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,21 @@ import (
"fmt"

sdktypes "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

providertypes "github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
providerKeeper Keeper
paramSpace paramtypes.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(providerKeeper Keeper, paramSpace paramtypes.Subspace) Migrator {
return Migrator{providerKeeper: providerKeeper, paramSpace: paramSpace}
}

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

func (k Keeper) MigrateQueuedPackets(ctx sdktypes.Context) error {
for _, consumer := range k.GetAllConsumerChains(ctx) {
slashData, vscmData := k.GetAllThrottledPacketData(ctx, consumer.ChainId)
if len(slashData) > 0 {
k.Logger(ctx).Error(fmt.Sprintf("slash data being dropped: %v", slashData))
}
for _, data := range vscmData {
k.HandleVSCMaturedPacket(ctx, consumer.ChainId, data)
}
k.DeleteThrottledPacketDataForConsumer(ctx, consumer.ChainId)
}
return nil
}

// Pending packet data type enum, used to encode the type of packet data stored at each entry in the mutual queue.
// Note this type is copy/pasted from throttle v1 code.
const (
slashPacketData byte = iota
vscMaturedPacketData
)

// GetAllThrottledPacketData returns all throttled packet data for a given consumer chain, only used for 2 -> 3 migration.
// Note this method is adapted from throttle v1 code.
func (k Keeper) GetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID string) (
// Deprecated: LegacyGetAllThrottledPacketData is deprecated for ICS >= v4.0.0.
// LegacyGetAllThrottledPacketData returns all throttled packet data that was queued on the provider for a given consumer chain.
func (k Keeper) LegacyGetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID string) (
MSalopek marked this conversation as resolved.
Show resolved Hide resolved
slashData []ccvtypes.SlashPacketData, vscMaturedData []ccvtypes.VSCMaturedPacketData,
) {
slashData = []ccvtypes.SlashPacketData{}
Expand Down Expand Up @@ -86,8 +55,9 @@ func (k Keeper) GetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID
return slashData, vscMaturedData
}

// Note this method is copy/pasted from throttle v1 code.
func (k Keeper) DeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consumerChainID string) {
// Deprecated: LegacyDeleteThrottledPacketDataForConsumer is deprecated for ICS >= v4.0.0.
// LegacyDeleteThrottledPacketDataForConsumer removes all throttled packet data that was queued on the provider for a given consumer chain.
func (k Keeper) LegacyDeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consumerChainID string) {
store := ctx.KVStore(k.storeKey)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID)
iterator := sdktypes.KVStorePrefixIterator(store, iteratorPrefix)
Expand All @@ -106,8 +76,10 @@ func (k Keeper) DeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consu
store.Delete(providertypes.ThrottledPacketDataSizeKey(consumerChainID))
}

// Note this method is adapted from throttle v1 code.
func (k Keeper) QueueThrottledPacketDataOnlyForTesting(
// Deprecated: LegacyQueueThrottledPacketData is deprecated for ICS >= v4.0.0.
// LegacyQueueThrottledPacketData queues throttled packet data for a given consumer chain on the provider.
// The method should not be used becase the provider does not process throttled packet data anymore.
func (k Keeper) LegacyQueueThrottledPacketData(
ctx sdktypes.Context, consumerChainID string, ibcSeqNum uint64, packetData interface{},
) error {
store := ctx.KVStore(k.storeKey)
Expand Down
33 changes: 33 additions & 0 deletions x/ccv/provider/migrations/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package migrations

import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

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

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
providerKeeper providerkeeper.Keeper
paramSpace paramtypes.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(providerKeeper providerkeeper.Keeper, paramSpace paramtypes.Subspace) Migrator {
return Migrator{providerKeeper: providerKeeper, paramSpace: paramSpace}
}

// Migrating consensus version 1 to 2 is a no-op.
// Migrating from v1 -> v2 -> v3 will require multiple state breaking changes and migrations.
// First run [email protected] in production to migrate from consensus version 1 to 2.
// Then, in order to migrate to consensus version 3, first upgrade to [email protected].
func (m Migrator) Migrate1to2(ctx sdktypes.Context) error {
return nil
}

// Migrate2to3 migrates x/ccvprovider state from consensus version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdktypes.Context) error {
return v3.MigrateQueuedPackets(ctx, m.providerKeeper)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper_test
package v3

import (
"testing"
Expand All @@ -19,41 +19,41 @@ func TestMigrate2To3(t *testing.T) {
providerKeeper.SetConsumerClientId(ctx, "chain-3", "client-3")

// Queue some data for chain-1
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-1", 66, testutil.GetNewSlashPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-1", 67, testutil.GetNewVSCMaturedPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-1", 68, testutil.GetNewSlashPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-1", 69, testutil.GetNewVSCMaturedPacketData())

// Queue some data for chain-2
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-2", 789, testutil.GetNewVSCMaturedPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-2", 790, testutil.GetNewSlashPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-2", 791, testutil.GetNewVSCMaturedPacketData())

// Queue some data for chain-3
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-3", 123, testutil.GetNewSlashPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-3", 124, testutil.GetNewVSCMaturedPacketData())
providerKeeper.QueueThrottledPacketDataOnlyForTesting(
providerKeeper.LegacyQueueThrottledPacketData( //nolint:staticcheck // SA1019: used in migration tests
ctx, "chain-3", 125, testutil.GetNewVSCMaturedPacketData())

// Confirm getter methods return expected values
slash1, vscm1 := providerKeeper.GetAllThrottledPacketData(ctx, "chain-1")
slash1, vscm1 := providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-1") //nolint:staticcheck // SA1019: used in migration tests
require.Len(t, slash1, 2)
require.Len(t, vscm1, 2)

slash2, vscm2 := providerKeeper.GetAllThrottledPacketData(ctx, "chain-2")
slash2, vscm2 := providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-2") //nolint:staticcheck // SA1019: used in migration tests
require.Len(t, slash2, 1)
require.Len(t, vscm2, 2)

slash3, vscm3 := providerKeeper.GetAllThrottledPacketData(ctx, "chain-3")
slash3, vscm3 := providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-3") //nolint:staticcheck // SA1019: used in migration tests
require.Len(t, slash3, 1)
require.Len(t, vscm3, 2)

Expand Down Expand Up @@ -87,17 +87,17 @@ func TestMigrate2To3(t *testing.T) {
}

// Run migration
err := providerKeeper.MigrateQueuedPackets(ctx)
err := MigrateQueuedPackets(ctx, providerKeeper)
require.NoError(t, err)

// Confirm throttled data is now deleted
slash1, vscm1 = providerKeeper.GetAllThrottledPacketData(ctx, "chain-1")
slash1, vscm1 = providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-1") //nolint:staticcheck // SA1019: used in migration tests
require.Empty(t, slash1)
require.Empty(t, vscm1)
slash2, vscm2 = providerKeeper.GetAllThrottledPacketData(ctx, "chain-2")
slash2, vscm2 = providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-2") //nolint:staticcheck // SA1019: used in migration tests
require.Empty(t, slash2)
require.Empty(t, vscm2)
slash3, vscm3 = providerKeeper.GetAllThrottledPacketData(ctx, "chain-3")
slash3, vscm3 = providerKeeper.LegacyGetAllThrottledPacketData(ctx, "chain-3") //nolint:staticcheck // SA1019: used in migration tests
require.Empty(t, slash3)
require.Empty(t, vscm3)

Expand Down
25 changes: 25 additions & 0 deletions x/ccv/provider/migrations/v3/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v3

import (
"fmt"

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

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

// MigrateQueuedPackets processes all queued packet data for all consumer chains that were stored
// on the provider in the v2 consensus version (jail throttling v1).
func MigrateQueuedPackets(ctx sdk.Context, k providerkeeper.Keeper) error {
for _, consumer := range k.GetAllConsumerChains(ctx) {
slashData, vscmData := k.LegacyGetAllThrottledPacketData(ctx, consumer.ChainId) //nolint:staticcheck // SA1019: function used for migration
if len(slashData) > 0 {
k.Logger(ctx).Error(fmt.Sprintf("slash data being dropped: %v", slashData))
}
for _, data := range vscmData {
k.HandleVSCMaturedPacket(ctx, consumer.ChainId, data)
}
k.LegacyDeleteThrottledPacketDataForConsumer(ctx, consumer.ChainId) //nolint:staticcheck // SA1019: function used for migration
}
return nil
}
3 changes: 2 additions & 1 deletion x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/cosmos/interchain-security/v3/x/ccv/provider/client/cli"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/migrations"
providertypes "github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
)

Expand Down Expand Up @@ -107,7 +108,7 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
func (am AppModule) RegisterServices(cfg module.Configurator) {
providertypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
providertypes.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(*am.keeper, am.paramSpace)
m := migrations.NewMigrator(*am.keeper, am.paramSpace)
if err := cfg.RegisterMigration(providertypes.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to register migrator for %s: %s", providertypes.ModuleName, err))
}
Expand Down
Loading