Skip to content

Commit

Permalink
Merge pull request #158 from Fairblock/add-keyshare-migration
Browse files Browse the repository at this point in the history
Add keyshare migration
  • Loading branch information
p0p3yee authored Jul 29, 2024
2 parents 56f059a + 7777438 commit 6322d7b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
21 changes: 21 additions & 0 deletions x/keyshare/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper

import (
v2 "github.com/Fairblock/fairyring/x/keyshare/migrations/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc)
}
27 changes: 27 additions & 0 deletions x/keyshare/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v2

import (
"cosmossdk.io/core/store"
"github.com/Fairblock/fairyring/x/keyshare/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// MigrateStore migrates the x/keyshare module state from the consensus version 1 to version 2.
func MigrateStore(ctx sdk.Context, storeService store.KVStoreService, cdc codec.BinaryCodec) error {
currParams := types.NewParams(
463000,
[]string{"fairy1r6q07ne3deq64ezcjwkedcfe6669f0ewpwnxy9"},
types.DefaultMinimumBonded,
types.DefaultSlashFractionNoKeyShare,
types.DefaultSlashFractionWrongKeyShare,
types.DefaultMaxIdledBlock)

bz, err := cdc.Marshal(&currParams)
if err != nil {
return err
}

store := storeService.OpenKVStore(ctx)
return store.Set(types.ParamsKey, bz)
}
9 changes: 8 additions & 1 deletion x/keyshare/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ var (
_ porttypes.IBCModule = IBCModule{}
)

// ConsensusVersion defines the current x/keyshare module consensus version.
const ConsensusVersion = 2

// ----------------------------------------------------------------------------
// AppModuleBasic
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -132,6 +135,10 @@ func NewAppModule(
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err))
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -155,7 +162,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// ConsensusVersion is a sequence number for state-breaking change of the module.
// It should be incremented on each consensus-breaking change introduced by the module.
// To avoid wrong/empty versions, the initial version should be set to 1.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block.
// The begin block implementation is optional.
Expand Down
5 changes: 4 additions & 1 deletion x/pep/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var (
_ porttypes.IBCModule = IBCModule{}
)

// ConsensusVersion defines the current x/pep module consensus version.
const ConsensusVersion = 1

// ----------------------------------------------------------------------------
// AppModuleBasic
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -174,7 +177,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// ConsensusVersion is a sequence number for state-breaking change of the module.
// It should be incremented on each consensus-breaking change introduced by the module.
// To avoid wrong/empty versions, the initial version should be set to 1.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block.
// The begin block implementation is optional.
Expand Down

0 comments on commit 6322d7b

Please sign in to comment.