Skip to content

Commit

Permalink
Added migration + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bermuell committed Nov 14, 2023
1 parent 849d913 commit 7e436b7
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 13 deletions.
111 changes: 111 additions & 0 deletions x/ccv/consumer/keeper/legacy_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package keeper

import (
"time"

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

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

// Legacy: used for migration only!
// GetConsumerParamsLegacy returns the params for the consumer ccv module from x/param subspace
// which will be depreacted soon
func (k Keeper) GetConsumerParamsLegacy(ctx sdk.Context, paramSpace paramtypes.Subspace) ccvtypes.Params {
return ccvtypes.NewParams(
getEnabled(ctx, paramSpace),
getBlocksPerDistributionTransmission(ctx, paramSpace),
getDistributionTransmissionChannel(ctx, paramSpace),
getProviderFeePoolAddrStr(ctx, paramSpace),
getCCVTimeoutPeriod(ctx, paramSpace),
getTransferTimeoutPeriod(ctx, paramSpace),
getConsumerRedistributionFrac(ctx, paramSpace),
getHistoricalEntries(ctx, paramSpace),
getUnbondingPeriod(ctx, paramSpace),
getSoftOptOutThreshold(ctx, paramSpace),
getRewardDenoms(ctx, paramSpace),
getProviderRewardDenoms(ctx, paramSpace),
)
}

// getEnabled returns the enabled flag for the consumer module
func getEnabled(ctx sdk.Context, paramStore paramtypes.Subspace) bool {
var enabled bool
paramStore.Get(ctx, ccvtypes.KeyEnabled, &enabled)
return enabled
}

func getBlocksPerDistributionTransmission(ctx sdk.Context, paramStore paramtypes.Subspace) int64 {
var bpdt int64
paramStore.Get(ctx, ccvtypes.KeyBlocksPerDistributionTransmission, &bpdt)
return bpdt
}

func getDistributionTransmissionChannel(ctx sdk.Context, paramStore paramtypes.Subspace) string {
var s string
paramStore.Get(ctx, ccvtypes.KeyDistributionTransmissionChannel, &s)
return s
}

func getProviderFeePoolAddrStr(ctx sdk.Context, paramStore paramtypes.Subspace) string {
var s string
paramStore.Get(ctx, ccvtypes.KeyProviderFeePoolAddrStr, &s)
return s
}

// getCCVTimeoutPeriod returns the timeout period for sent ccv related ibc packets
func getCCVTimeoutPeriod(ctx sdk.Context, paramStore paramtypes.Subspace) time.Duration {
var p time.Duration
paramStore.Get(ctx, ccvtypes.KeyCCVTimeoutPeriod, &p)
return p
}

// getTransferTimeoutPeriod returns the timeout period for sent transfer related ibc packets
func getTransferTimeoutPeriod(ctx sdk.Context, paramStore paramtypes.Subspace) time.Duration {
var p time.Duration
paramStore.Get(ctx, ccvtypes.KeyTransferTimeoutPeriod, &p)
return p
}

// getConsumerRedistributionFrac returns the fraction of tokens allocated to the consumer redistribution
// address during distribution events. The fraction is a string representing a
// decimal number. For example "0.75" would represent 75%.
func getConsumerRedistributionFrac(ctx sdk.Context, paramStore paramtypes.Subspace) string {
var str string
paramStore.Get(ctx, ccvtypes.KeyConsumerRedistributionFrac, &str)
return str
}

// getHistoricalEntries returns the number of historical info entries to persist in store
func getHistoricalEntries(ctx sdk.Context, paramStore paramtypes.Subspace) int64 {
var n int64
paramStore.Get(ctx, ccvtypes.KeyHistoricalEntries, &n)
return n
}

func getUnbondingPeriod(ctx sdk.Context, paramStore paramtypes.Subspace) time.Duration {
var period time.Duration
paramStore.Get(ctx, ccvtypes.KeyConsumerUnbondingPeriod, &period)
return period
}

// getSoftOptOutThreshold returns the percentage of validators at the bottom of the set
// that can opt out of running the consumer chain
func getSoftOptOutThreshold(ctx sdk.Context, paramStore paramtypes.Subspace) string {
var str string
paramStore.Get(ctx, ccvtypes.KeySoftOptOutThreshold, &str)
return str
}

func getRewardDenoms(ctx sdk.Context, paramStore paramtypes.Subspace) []string {
var denoms []string
paramStore.Get(ctx, ccvtypes.KeyRewardDenoms, &denoms)
return denoms
}

func getProviderRewardDenoms(ctx sdk.Context, paramStore paramtypes.Subspace) []string {
var denoms []string
paramStore.Get(ctx, ccvtypes.KeyProviderRewardDenoms, &denoms)
return denoms
}
13 changes: 13 additions & 0 deletions x/ccv/consumer/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ func NewMigrator(ccvConsumerKeeper Keeper, ccvConsumerParamSpace paramtypes.Subs
return Migrator{ccvConsumerKeeper: ccvConsumerKeeper, ccvConsumerParamSpace: ccvConsumerParamSpace}
}

// MigrateParams migrates the consumers module's parameters from the x/params subspace to the
// consumer modules store.
func (m Migrator) MigrateParams(ctx sdk.Context) error {
params := m.ccvConsumerKeeper.GetConsumerParamsLegacy(ctx, m.ccvConsumerParamSpace)
err := params.Validate()
if err != nil {
return err
}
m.ccvConsumerKeeper.SetParams(ctx, params)
m.ccvConsumerKeeper.Logger(ctx).Info("successfully migrated provider parameters")
return nil
}

// MigrateConsumerPacketData migrates consumer packet data according to
// https://github.com/cosmos/interchain-security/pull/1037
//
Expand Down
34 changes: 34 additions & 0 deletions x/ccv/consumer/keeper/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package keeper_test
import (
"testing"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/require"

testutil "github.com/cosmos/interchain-security/v3/testutil/keeper"

"github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper"

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

Expand Down Expand Up @@ -61,3 +65,33 @@ func TestMigrateConsumerPacketData(t *testing.T) {
require.Equal(t, uint64(88), obtainedPackets[1].GetVscMaturedPacketData().ValsetUpdateId)
require.Equal(t, uint64(99), obtainedPackets[2].GetVscMaturedPacketData().ValsetUpdateId)
}

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

testCases := []struct {
name string
legacyParams func() paramtypes.Subspace
expetedParams ccvtypes.Params
}{
{
"default params",
func() paramtypes.Subspace {
subspace := params.ParamsSubspace
defaultParams := ccvtypes.DefaultParams()
subspace.SetParamSet(ctx, &defaultParams)
return *subspace
},
ccvtypes.DefaultParams(),
},
}
for _, tc := range testCases {
migrator := keeper.NewMigrator(consumerKeeper, tc.legacyParams())
err := migrator.MigrateParams(ctx)
require.NoError(t, err)
params := consumerKeeper.GetConsumerParams(ctx)
require.Equal(t, tc.expetedParams, params)
}
}
6 changes: 6 additions & 0 deletions x/ccv/consumer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
consumertypes.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(am.keeper, am.paramSpace)
err := cfg.RegisterMigration(am.Name(), 1, migrator.MigrateParams)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the consumer module. It returns
Expand Down
95 changes: 95 additions & 0 deletions x/ccv/provider/keeper/legacy_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package keeper

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"

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

// getTemplateClient returns the template client for provider proposals
func getTemplateClient(ctx sdk.Context, paramSpace paramtypes.Subspace) *ibctmtypes.ClientState {
var cs ibctmtypes.ClientState
paramSpace.Get(ctx, types.KeyTemplateClient, &cs)
return &cs
}

// getTrustingPeriodFraction returns a TrustingPeriodFraction
// used to compute the provider IBC client's TrustingPeriod as UnbondingPeriod / TrustingPeriodFraction
func getTrustingPeriodFraction(ctx sdk.Context, paramSpace paramtypes.Subspace) string {
var f string
paramSpace.Get(ctx, types.KeyTrustingPeriodFraction, &f)
return f
}

// getCCVTimeoutPeriod returns the timeout period for sent ibc packets
func getCCVTimeoutPeriod(ctx sdk.Context, paramSpace paramtypes.Subspace) time.Duration {
var p time.Duration
paramSpace.Get(ctx, ccvtypes.KeyCCVTimeoutPeriod, &p)
return p
}

// getInitTimeoutPeriod returns the init timeout period
func getInitTimeoutPeriod(ctx sdk.Context, paramSpace paramtypes.Subspace) time.Duration {
var p time.Duration
paramSpace.Get(ctx, types.KeyInitTimeoutPeriod, &p)
return p
}

// getVscTimeoutPeriod returns the vsc timeout period
func getVscTimeoutPeriod(ctx sdk.Context, paramSpace paramtypes.Subspace) time.Duration {
var p time.Duration
paramSpace.Get(ctx, types.KeyVscTimeoutPeriod, &p)
return p
}

// getSlashMeterReplenishPeriod returns the period in which:
// Once the slash meter becomes not-full, the slash meter is replenished after this period.
func getSlashMeterReplenishPeriod(ctx sdk.Context, paramSpace paramtypes.Subspace) time.Duration {
var p time.Duration
paramSpace.Get(ctx, types.KeySlashMeterReplenishPeriod, &p)
return p
}

// getSlashMeterReplenishFraction returns the string fraction of total voting power that is replenished
// to the slash meter every replenish period. This param also serves as a maximum fraction of total
// voting power that the slash meter can hold.
func getSlashMeterReplenishFraction(ctx sdk.Context, paramSpace paramtypes.Subspace) string {
var f string
paramSpace.Get(ctx, types.KeySlashMeterReplenishFraction, &f)
return f
}

// getMaxThrottledPackets returns the maximum amount of throttled slash or vsc matured packets
// that can be queued for a single consumer before the provider chain halts.
func getMaxThrottledPackets(ctx sdk.Context, paramSpace paramtypes.Subspace) int64 {
var p int64
paramSpace.Get(ctx, types.KeyMaxThrottledPackets, &p)
return p
}

func getConsumerRewardDenomRegistrationFee(ctx sdk.Context, paramSpace paramtypes.Subspace) sdk.Coin {
var c sdk.Coin
paramSpace.Get(ctx, types.KeyConsumerRewardDenomRegistrationFee, &c)
return c
}

// Legacy: Only for migration purposes. GetParamsLegacy returns the paramset for the provider
// module from a given param subspace
func GetParamsLegacy(ctx sdk.Context, paramspace paramtypes.Subspace) types.Params {
return types.NewParams(
getTemplateClient(ctx, paramspace),
getTrustingPeriodFraction(ctx, paramspace),
getCCVTimeoutPeriod(ctx, paramspace),
getInitTimeoutPeriod(ctx, paramspace),
getVscTimeoutPeriod(ctx, paramspace),
getSlashMeterReplenishPeriod(ctx, paramspace),
getSlashMeterReplenishFraction(ctx, paramspace),
getMaxThrottledPackets(ctx, paramspace),
getConsumerRewardDenomRegistrationFee(ctx, paramspace),
)
}
19 changes: 11 additions & 8 deletions x/ccv/provider/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
)

type Migrator struct {
keeper Keeper
keeper Keeper
legacySubspace paramtypes.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
func NewMigrator(keeper Keeper, subspace paramtypes.Subspace) Migrator {
return Migrator{
keeper: keeper,
keeper: keeper,
legacySubspace: subspace,
}
}

// MigrateParams migrates the provider module's parameters from the x/params to self store.
func (m Migrator) MigrateParams(ctx sdk.Context, paramSpace paramtypes.Subspace) error {
var params types.Params
paramSpace.GetParamSet(ctx, &params)

func (m Migrator) MigrateParams(ctx sdk.Context) error {
params := GetParamsLegacy(ctx, m.legacySubspace)
err := params.Validate()
if err != nil {
return err
}
m.keeper.SetParams(ctx, params)
m.keeper.Logger(ctx).Info("successfully migrated provider parameters")
return nil
Expand Down
41 changes: 41 additions & 0 deletions x/ccv/provider/keeper/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package keeper_test

import (
"testing"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
testutil "github.com/cosmos/interchain-security/v3/testutil/keeper"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
"github.com/stretchr/testify/require"
)

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

testCases := []struct {
name string
legacyParams func() paramtypes.Subspace
expetedParams types.Params
}{
{
"default params",
func() paramtypes.Subspace {
subspace := params.ParamsSubspace
defaultParams := types.DefaultParams()
subspace.SetParamSet(ctx, &defaultParams)
return *subspace
},
types.DefaultParams(),
},
}
for _, tc := range testCases {
migrator := keeper.NewMigrator(providerKeeper, tc.legacyParams())
err := migrator.MigrateParams(ctx)
require.NoError(t, err)
params := providerKeeper.GetParams(ctx)
require.Equal(t, tc.expetedParams, params)
}
}
7 changes: 2 additions & 5 deletions x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
providertypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
providertypes.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(*am.keeper)
err := cfg.RegisterMigration(am.Name(), 2,
func(ctx sdk.Context) error {
return migrator.MigrateParams(ctx, am.paramSpace)
})
migrator := keeper.NewMigrator(*am.keeper, am.paramSpace)
err := cfg.RegisterMigration(am.Name(), 2, migrator.MigrateParams)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 7e436b7

Please sign in to comment.