Skip to content

Commit

Permalink
provider migration boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed May 23, 2023
1 parent 9521ecb commit 9589144
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
6 changes: 4 additions & 2 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import (

const (
AppName = "interchain-security-p"
upgradeName = "v07-Theta"
upgradeName = "ics-v1-to-v2"
AccountAddressPrefix = "cosmos"
)

Expand Down Expand Up @@ -415,7 +415,7 @@ func New(
authtypes.FeeCollectorName,
)

providerModule := ibcprovider.NewAppModule(&app.ProviderKeeper)
providerModule := ibcprovider.NewAppModule(&app.ProviderKeeper, app.GetSubspace(providertypes.ModuleName))

// register the proposal types
govRouter := govtypes.NewRouter()
Expand Down Expand Up @@ -616,6 +616,8 @@ func New(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

// Note this upgrade handler is just an example and may not be exactly what you need to implement.
// See https://docs.cosmos.network/v0.45/building-modules/upgrade.html
app.UpgradeKeeper.SetUpgradeHandler(
upgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) {
Expand Down
20 changes: 12 additions & 8 deletions x/ccv/provider/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-pcf-coinit1
// Spec Tag: [CCV-PCF-COINIT.1]
func TestOnChanOpenInit(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))
t, keeperParams)
defer ctrl.Finish()
providerModule := provider.NewAppModule(&providerKeeper)
providerModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)

// OnChanOpenInit must error for provider even with correct arguments
_, err := providerModule.OnChanOpenInit(
Expand Down Expand Up @@ -112,9 +113,10 @@ func TestOnChanOpenTry(t *testing.T) {
for _, tc := range testCases {

// Setup
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))
providerModule := provider.NewAppModule(&providerKeeper)
t, keeperParams)
providerModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)

providerKeeper.SetPort(ctx, ccv.ProviderPortID)
providerKeeper.SetConsumerClientId(ctx, "consumerChainID", "clientIDToConsumer")
Expand Down Expand Up @@ -181,10 +183,11 @@ func TestOnChanOpenTry(t *testing.T) {
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-pcf-coack1
// Spec tag: [CCV-PCF-COACK.1]
func TestOnChanOpenAck(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))
t, keeperParams)
defer ctrl.Finish()
providerModule := provider.NewAppModule(&providerKeeper)
providerModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)

// OnChanOpenAck must error for provider even with correct arguments
err := providerModule.OnChanOpenAck(
Expand Down Expand Up @@ -296,16 +299,17 @@ func TestOnChanOpenConfirm(t *testing.T) {

for _, tc := range testCases {

keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))
t, keeperParams)

gomock.InOrder(tc.mockExpectations(ctx, mocks)...)

if tc.setDuplicateChannel {
providerKeeper.SetChainToChannel(ctx, "consumerChainID", "existingChannelID")
}

providerModule := provider.NewAppModule(&providerKeeper)
providerModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)

err := providerModule.OnChanOpenConfirm(ctx, "providerPortID", "channelID")

Expand Down
4 changes: 4 additions & 0 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,7 @@ func (k Keeper) GetSlashLog(
bz := store.Get(types.SlashLogKey(providerAddr))
return bz != nil
}

func (k Keeper) BondDenom(ctx sdk.Context) string {
return k.stakingKeeper.BondDenom(ctx)
}
7 changes: 2 additions & 5 deletions x/ccv/provider/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
ibctmtypes "github.com/cosmos/ibc-go/v4/modules/light-clients/07-tendermint/types"
v2providertypes "github.com/cosmos/interchain-security/v2/x/ccv/provider/types"
v2ccvtypes "github.com/cosmos/interchain-security/v2/x/ccv/types"
v1providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
v1ccvtypes "github.com/cosmos/interchain-security/x/ccv/types"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
ccvProviderKeeper Keeper
stakingKeeper v2ccvtypes.StakingKeeper
ccvProviderParamSpace paramtypes.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(ccvProviderKeeper Keeper, stakingKeeper v2ccvtypes.StakingKeeper,
ccvProviderParamSpace paramtypes.Subspace,
func NewMigrator(ccvProviderKeeper Keeper, ccvProviderParamSpace paramtypes.Subspace,
) Migrator {
return Migrator{ccvProviderKeeper: ccvProviderKeeper, ccvProviderParamSpace: ccvProviderParamSpace}
}
Expand All @@ -33,7 +30,7 @@ func (m Migrator) Migratev1Tov2(ctx sdk.Context) error {
// See https://github.com/cosmos/interchain-security/blob/7861804cb311507ec6aebebbfad60ea42eb8ed4b/x/ccv/provider/keeper/params.go#L84
// The v1.1.0-multiden version of ICS hardcodes this param as 10 of bond type: k.stakingKeeper.BondDenom(ctx).
// Here we use the same starting value, but the param can now be changed through governance.
sdk.NewCoin(m.stakingKeeper.BondDenom(ctx), sdk.NewInt(10000000)),
sdk.NewCoin(m.ccvProviderKeeper.BondDenom(ctx), sdk.NewInt(10000000)),
)

return nil
Expand Down
14 changes: 11 additions & 3 deletions x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
porttypes "github.com/cosmos/ibc-go/v4/modules/core/05-port/types"
"github.com/cosmos/interchain-security/v2/x/ccv/provider/client/cli"
"github.com/cosmos/interchain-security/v2/x/ccv/provider/keeper"
Expand Down Expand Up @@ -88,13 +89,15 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
// AppModule represents the AppModule for this module
type AppModule struct {
AppModuleBasic
keeper *keeper.Keeper
keeper *keeper.Keeper
paramSpace paramtypes.Subspace
}

// NewAppModule creates a new provider module
func NewAppModule(k *keeper.Keeper) AppModule {
func NewAppModule(k *keeper.Keeper, paramSpace paramtypes.Subspace) AppModule {
return AppModule{
keeper: k,
keeper: k,
paramSpace: paramSpace,
}
}

Expand Down Expand Up @@ -122,6 +125,11 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
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)
if err := cfg.RegisterMigration(providertypes.ModuleName, 1, m.Migratev1Tov2); err != nil {
panic(fmt.Sprintf("failed to register migrator: %s", err))
}
}

// InitGenesis performs genesis initialization for the provider module. It returns no validator updates.
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestInitGenesis(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, keeperParams)

appModule := provider.NewAppModule(&providerKeeper)
appModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)
genState := types.NewGenesisState(
providerKeeper.GetValidatorSetUpdateId(ctx),
nil,
Expand Down

0 comments on commit 9589144

Please sign in to comment.