Skip to content

Commit

Permalink
migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Nov 26, 2024
1 parent 86fc22c commit f1e7c6a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
13 changes: 13 additions & 0 deletions x/ccv/provider/migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
providerkeeper "github.com/cosmos/interchain-security/v6/x/ccv/provider/keeper"
v7 "github.com/cosmos/interchain-security/v6/x/ccv/provider/migrations/v7"
v8 "github.com/cosmos/interchain-security/v6/x/ccv/provider/migrations/v8"
v9 "github.com/cosmos/interchain-security/v6/x/ccv/provider/migrations/v9"
)

// Migrator is a struct for handling in-place store migrations.
Expand Down Expand Up @@ -96,3 +97,15 @@ func (m Migrator) Migrate7to8(ctx sdktypes.Context) error {

return nil
}

// Migrate8to9 migrates x/ccvprovider state from consensus version 8 to 9.
// The migration consists of the following actions:
// - insert infraction parameters for each consumer
func (m Migrator) Migrate8to9(ctx sdktypes.Context) error {
store := ctx.KVStore(m.storeKey)
if err := v9.MigrateConsumerInfractionParams(ctx, store, m.providerKeeper); err != nil {
return err
}

return nil
}
39 changes: 39 additions & 0 deletions x/ccv/provider/migrations/v9/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package v9

import (
"time"

"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

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

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

func MigrateConsumerInfractionParams(ctx sdk.Context, store storetypes.KVStore, pk providerkeeper.Keeper) error {
infractionParameters := defaultInfractionParams()

activeConsumerIds := pk.GetAllActiveConsumerIds(ctx)
for _, consumerId := range activeConsumerIds {
if err := pk.SetInfractionParameters(ctx, consumerId, infractionParameters); err != nil {
return err
}
}

return nil
}

func defaultInfractionParams() providertypes.InfractionParameters {
return providertypes.InfractionParameters{
DoubleSign: &providertypes.SlashJailParameters{
JailDuration: time.Duration(1<<63 - 1), // the largest value a time.Duration can hold 9223372036854775807 (approximately 292 years)
SlashFraction: math.LegacyNewDecWithPrec(5, 2), // 0.05
},
Downtime: &providertypes.SlashJailParameters{
JailDuration: 600 * time.Second,
SlashFraction: math.LegacyNewDec(0), // no slashing for downtime on the consumer
},
}
}
35 changes: 35 additions & 0 deletions x/ccv/provider/migrations/v9/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v9

import (
"testing"

"github.com/stretchr/testify/require"

testutil "github.com/cosmos/interchain-security/v6/testutil/keeper"
)

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

store := ctx.KVStore(inMemParams.StoreKey)

activeConsumerIds := k.GetAllActiveConsumerIds(ctx)

for _, consumerId := range activeConsumerIds {
_, err := k.GetInfractionParameters(ctx, consumerId)
require.Error(t, err)
}

err := MigrateConsumerInfractionParams(ctx, store, k)
require.NoError(t, err)

defaultInfractionParams := defaultInfractionParams()
for _, consumerId := range activeConsumerIds {
infractionParams, err := k.GetInfractionParameters(ctx, consumerId)
require.NoError(t, err)
require.Equal(t, defaultInfractionParams, infractionParams)
}
}
6 changes: 5 additions & 1 deletion x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err := cfg.RegisterMigration(providertypes.ModuleName, 7, migrator.Migrate7to8); err != nil {
panic(fmt.Sprintf("failed to register migrator for %s: %s -- from 7 -> 8", providertypes.ModuleName, err))
}
if err := cfg.RegisterMigration(providertypes.ModuleName, 8, migrator.Migrate8to9); err != nil {
panic(fmt.Sprintf("failed to register migrator for %s: %s -- from 8 -> 9", providertypes.ModuleName, err))
}

}

// InitGenesis performs genesis initialization for the provider module. It returns validator updates
Expand All @@ -168,7 +172,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 8 }
func (AppModule) ConsensusVersion() uint64 { return 9 }

// BeginBlock implements the AppModule interface
func (am AppModule) BeginBlock(ctx context.Context) error {
Expand Down

0 comments on commit f1e7c6a

Please sign in to comment.