Skip to content

Commit

Permalink
feat(client): migrate client params (#3640)
Browse files Browse the repository at this point in the history
* feat(client): migrate client params

* Address initial feedback.

* Use anonymous function as handler.

* Add tests for msg_server. whoops.

* Revert anon function as handler.

* Implicitly test GetAllowedClients in TestDefaultSetParams.

* Consistency is more important than perfection.

* Add msg.signer option back, remove cosmos.scalar for the time being.

* Remove authority comment, update GetParams as per suggestion.

* Update modules/core/02-client/keeper/keeper.go

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* Update modules/core/keeper/keeper.go

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* Check that new parameters updated match what is expected.

* Add note to migration docs.

---------

Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
DimitrisJim and damiannolan committed May 30, 2023
1 parent 25de1c4 commit d61886d
Show file tree
Hide file tree
Showing 23 changed files with 820 additions and 121 deletions.
13 changes: 13 additions & 0 deletions docs/migrations/v7-to-v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ TODO: https://github.com/cosmos/ibc-go/pull/3505 (extra parameter added to trans
)
```

- You should pass the `authority` to the IBC keeper. ([#3640](https://github.com/cosmos/ibc-go/pull/3640)) See [diff](https://github.com/cosmos/ibc-go/pull/3640/files#diff-d18972debee5e64f16e40807b2ae112ddbe609504a93ea5e1c80a5d489c3a08a).

```diff
// app.go

// IBC Keepers

app.IBCKeeper = ibckeeper.NewKeeper(
- appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
+ appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
```

## IBC Apps

TODO: https://github.com/cosmos/ibc-go/pull/3303
Expand Down
3 changes: 3 additions & 0 deletions modules/core/02-client/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
// InitGenesis initializes the ibc client submodule's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
if err := gs.Params.Validate(); err != nil {
panic(fmt.Sprintf("invalid ibc client genesis state parameters: %v", err))
}
k.SetParams(ctx, gs.Params)

// Set all client metadata first. This will allow client keeper to overwrite client and consensus state keys
Expand Down
46 changes: 33 additions & 13 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ import (
// Keeper represents a type that grants read and write permissions to any client
// state information
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
stakingKeeper types.StakingKeeper
upgradeKeeper types.UpgradeKeeper
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
legacySubspace paramtypes.Subspace
stakingKeeper types.StakingKeeper
upgradeKeeper types.UpgradeKeeper
}

// NewKeeper creates a new NewKeeper instance
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace, sk types.StakingKeeper, uk types.UpgradeKeeper) Keeper {
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, legacySubspace paramtypes.Subspace, sk types.StakingKeeper, uk types.UpgradeKeeper) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
if !legacySubspace.HasKeyTable() {
legacySubspace = legacySubspace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
stakingKeeper: sk,
upgradeKeeper: uk,
storeKey: key,
cdc: cdc,
legacySubspace: legacySubspace,
stakingKeeper: sk,
upgradeKeeper: uk,
}
}

Expand Down Expand Up @@ -413,3 +413,23 @@ func (k Keeper) GetClientStatus(ctx sdk.Context, clientState exported.ClientStat
}
return clientState.Status(ctx, k.ClientStore(ctx, clientID), k.cdc)
}

// GetParams returns the total set of ibc-client parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.ParamsKey))
if len(bz) == 0 {
return types.Params{}
}

var params types.Params
k.cdc.MustUnmarshal(bz, &params)
return params
}

// SetParams sets the total set of ibc-client parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set([]byte(types.ParamsKey), bz)
}
54 changes: 54 additions & 0 deletions modules/core/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,57 @@ func (suite KeeperTestSuite) TestIterateClientStates() { //nolint:govet // this
})
}
}

// TestDefaultSetParams tests the default params set are what is expected
func (suite *KeeperTestSuite) TestDefaultSetParams() {
expParams := types.DefaultParams()

clientKeeper := suite.chainA.App.GetIBCKeeper().ClientKeeper
params := clientKeeper.GetParams(suite.chainA.GetContext())

suite.Require().Equal(expParams, params)
suite.Require().Equal(expParams.AllowedClients, clientKeeper.GetParams(suite.chainA.GetContext()).AllowedClients)
}

// TestParams tests that Param setting and retrieval works properly
func (suite *KeeperTestSuite) TestParams() {
testCases := []struct {
name string
input types.Params
expPass bool
}{
{"success: set default params", types.DefaultParams(), true},
{"success: empty allowedClients", types.NewParams(), true},
{"success: subset of allowedClients", types.NewParams(exported.Tendermint, exported.Localhost), true},
{"failure: contains a single empty string value as allowedClient", types.NewParams(exported.Localhost, ""), false},
}

for _, tc := range testCases {
tc := tc

suite.Run(tc.name, func() {
suite.SetupTest() // reset
ctx := suite.chainA.GetContext()
err := tc.input.Validate()
suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.SetParams(ctx, tc.input)
if tc.expPass {
suite.Require().NoError(err)
expected := tc.input
p := suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.GetParams(ctx)
suite.Require().Equal(expected, p)
} else {
suite.Require().Error(err)
}
})
}
}

// TestUnsetParams tests that trying to get params that are not set returns empty params.
func (suite *KeeperTestSuite) TestUnsetParams() {
suite.SetupTest()
ctx := suite.chainA.GetContext()
store := ctx.KVStore(suite.chainA.GetSimApp().GetKey(exported.StoreKey))
store.Delete([]byte(types.ParamsKey))

suite.Require().Equal(suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.GetParams(ctx), types.Params{})
}
15 changes: 15 additions & 0 deletions modules/core/02-client/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

v7 "github.com/cosmos/ibc-go/v7/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
)

// Migrator is a struct for handling in-place store migrations.
Expand Down Expand Up @@ -31,3 +32,17 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v7.MigrateLocalhostClient(ctx, m.keeper)
}

// MigrateParams migrates from consensus version 4 to 5.
// This migration takes the parameters that are currently stored and managed by x/params
// and stores them directly in the ibc module's state.
func (m Migrator) MigrateParams(ctx sdk.Context) error {
var params types.Params
m.keeper.legacySubspace.GetParamSet(ctx, &params)

if err := params.Validate(); err != nil {
return err
}
m.keeper.SetParams(ctx, params)
return nil
}
43 changes: 43 additions & 0 deletions modules/core/02-client/keeper/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper_test

import (
"github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper"
"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

// TestMigrateParams tests the migration for the client params
func (suite *KeeperTestSuite) TestMigrateParams() {
testCases := []struct {
name string
malleate func()
expectedParams types.Params
}{
{
"success: default params",
func() {
params := types.DefaultParams()
subspace := suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName)
subspace.SetParamSet(suite.chainA.GetContext(), &params)
},
types.DefaultParams(),
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset

tc.malleate()

ctx := suite.chainA.GetContext()
migrator := keeper.NewMigrator(suite.chainA.GetSimApp().IBCKeeper.ClientKeeper)
err := migrator.MigrateParams(ctx)
suite.Require().NoError(err)

params := suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.GetParams(ctx)
suite.Require().Equal(tc.expectedParams, params)
})
}
}
24 changes: 0 additions & 24 deletions modules/core/02-client/keeper/params.go

This file was deleted.

17 changes: 0 additions & 17 deletions modules/core/02-client/keeper/params_test.go

This file was deleted.

1 change: 1 addition & 0 deletions modules/core/02-client/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&MsgUpdateClient{},
&MsgUpgradeClient{},
&MsgSubmitMisbehaviour{},
&MsgUpdateClientParams{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
3 changes: 3 additions & 0 deletions modules/core/02-client/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const (
// KeyNextClientSequence is the key used to store the next client sequence in
// the keeper.
KeyNextClientSequence = "nextClientSequence"

// ParamsKey is the store key for the IBC client parameters
ParamsKey = "clientParams"
)

// FormatClientIdentifier returns the client identifier with the sequence appended.
Expand Down
26 changes: 26 additions & 0 deletions modules/core/02-client/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
_ sdk.Msg = (*MsgUpdateClient)(nil)
_ sdk.Msg = (*MsgSubmitMisbehaviour)(nil)
_ sdk.Msg = (*MsgUpgradeClient)(nil)
_ sdk.Msg = (*MsgUpdateClientParams)(nil)

_ codectypes.UnpackInterfacesMessage = (*MsgCreateClient)(nil)
_ codectypes.UnpackInterfacesMessage = (*MsgUpdateClient)(nil)
Expand Down Expand Up @@ -264,3 +265,28 @@ func (msg MsgSubmitMisbehaviour) UnpackInterfaces(unpacker codectypes.AnyUnpacke
var misbehaviour exported.ClientMessage
return unpacker.UnpackAny(msg.Misbehaviour, &misbehaviour)
}

// NewMsgUpdateClientParams creates a new instance of MsgUpdateClientParams.
func NewMsgUpdateClientParams(authority string, params Params) *MsgUpdateClientParams {
return &MsgUpdateClientParams{
Authority: authority,
Params: params,
}
}

// GetSigners returns the expected signers for a MsgUpdateClientParams message.
func (msg *MsgUpdateClientParams) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Authority)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}

// ValidateBasic performs basic checks on a MsgUpdateClientParams.
func (msg *MsgUpdateClientParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return msg.Params.Validate()
}
40 changes: 40 additions & 0 deletions modules/core/02-client/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,43 @@ func (suite *TypesTestSuite) TestMsgSubmitMisbehaviour_ValidateBasic() {
}
}
}

// TestMsgUpdateClientParams_ValidateBasic tests ValidateBasic for MsgUpdateClientParams
func (suite *TypesTestSuite) TestMsgUpdateClientParams_ValidateBasic() {
authority := suite.chainA.App.GetIBCKeeper().GetAuthority()
testCases := []struct {
name string
msg *types.MsgUpdateClientParams
expPass bool
}{
{
"success: valid authority and params",
types.NewMsgUpdateClientParams(authority, types.DefaultParams()),
true,
},
{
"success: valid authority empty params",
types.NewMsgUpdateClientParams(authority, types.Params{}),
true,
},
{
"failure: invalid authority address",
types.NewMsgUpdateClientParams("invalid", types.DefaultParams()),
false,
},
{
"failure: invalid allowed client",
types.NewMsgUpdateClientParams(authority, types.NewParams("")),
false,
},
}

for _, tc := range testCases {
err := tc.msg.ValidateBasic()
if tc.expPass {
suite.Require().NoError(err, "valid case %s failed", tc.name)
} else {
suite.Require().Error(err, "invalid case %s passed", tc.name)
}
}
}
Loading

0 comments on commit d61886d

Please sign in to comment.