Skip to content

Commit 812e67f

Browse files
committed
feat(connection): migrate connection params
1 parent d61886d commit 812e67f

21 files changed

+793
-140
lines changed

docs/migrations/v7-to-v8.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ TODO: https://github.com/cosmos/ibc-go/pull/3505 (extra parameter added to trans
4747
)
4848
```
4949

50-
- 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).
50+
- You should pass the `authority` to the IBC keeper. ([#3640](https://github.com/cosmos/ibc-go/pull/3640) and [#3650](https://github.com/cosmos/ibc-go/pull/3650)) See [diff](https://github.com/cosmos/ibc-go/pull/3640/files#diff-d18972debee5e64f16e40807b2ae112ddbe609504a93ea5e1c80a5d489c3a08a).
5151

5252
```diff
5353
// app.go
5454

5555
// IBC Keepers
56-
5756
app.IBCKeeper = ibckeeper.NewKeeper(
5857
- appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
5958
+ appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),

modules/core/03-connection/genesis.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package connection
22

33
import (
4+
"fmt"
5+
46
sdk "github.com/cosmos/cosmos-sdk/types"
57

68
"github.com/cosmos/ibc-go/v7/modules/core/03-connection/keeper"
@@ -18,6 +20,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
1820
k.SetClientConnectionPaths(ctx, connPaths.ClientId, connPaths.Paths)
1921
}
2022
k.SetNextConnectionSequence(ctx, gs.NextConnectionSequence)
23+
if err := gs.Params.Validate(); err != nil {
24+
panic(fmt.Sprintf("invalid ibc connection genesis state parameters: %v", err))
25+
}
2126
k.SetParams(ctx, gs.Params)
2227

2328
k.CreateSentinelLocalhostConnection(ctx)

modules/core/03-connection/keeper/keeper.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ type Keeper struct {
2020
// implements gRPC QueryServer interface
2121
types.QueryServer
2222

23-
storeKey storetypes.StoreKey
24-
paramSpace paramtypes.Subspace
25-
cdc codec.BinaryCodec
26-
clientKeeper types.ClientKeeper
23+
storeKey storetypes.StoreKey
24+
legacySubspace paramtypes.Subspace
25+
cdc codec.BinaryCodec
26+
clientKeeper types.ClientKeeper
2727
}
2828

2929
// NewKeeper creates a new IBC connection Keeper instance
30-
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace, ck types.ClientKeeper) Keeper {
30+
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, legacySubspace paramtypes.Subspace, ck types.ClientKeeper) Keeper {
3131
// set KeyTable if it has not already been set
32-
if !paramSpace.HasKeyTable() {
33-
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
32+
if !legacySubspace.HasKeyTable() {
33+
legacySubspace = legacySubspace.WithKeyTable(types.ParamKeyTable())
3434
}
3535

3636
return Keeper{
37-
storeKey: key,
38-
cdc: cdc,
39-
paramSpace: paramSpace,
40-
clientKeeper: ck,
37+
storeKey: key,
38+
cdc: cdc,
39+
legacySubspace: legacySubspace,
40+
clientKeeper: ck,
4141
}
4242
}
4343

@@ -221,3 +221,23 @@ func (k Keeper) addConnectionToClient(ctx sdk.Context, clientID, connectionID st
221221
k.SetClientConnectionPaths(ctx, clientID, conns)
222222
return nil
223223
}
224+
225+
// GetParams returns the total set of ibc-connection parameters.
226+
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
227+
store := ctx.KVStore(k.storeKey)
228+
bz := store.Get([]byte(types.ParamsKey))
229+
if len(bz) == 0 {
230+
return types.Params{}
231+
}
232+
233+
var params types.Params
234+
k.cdc.MustUnmarshal(bz, &params)
235+
return params
236+
}
237+
238+
// SetParams sets the total set of ibc-connection parameters.
239+
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
240+
store := ctx.KVStore(k.storeKey)
241+
bz := k.cdc.MustMarshal(&params)
242+
store.Set([]byte(types.ParamsKey), bz)
243+
}

modules/core/03-connection/keeper/keeper_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,53 @@ func (suite *KeeperTestSuite) TestLocalhostConnectionEndCreation() {
176176
expectedCounterParty := types.NewCounterparty(exported.LocalhostClientID, exported.LocalhostConnectionID, commitmenttypes.NewMerklePrefix(connectionKeeper.GetCommitmentPrefix().Bytes()))
177177
suite.Require().Equal(expectedCounterParty, connectionEnd.Counterparty)
178178
}
179+
180+
// TestDefaultSetParams tests the default params set are what is expected
181+
func (suite *KeeperTestSuite) TestDefaultSetParams() {
182+
expParams := types.DefaultParams()
183+
184+
params := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetParams(suite.chainA.GetContext())
185+
suite.Require().Equal(expParams, params)
186+
}
187+
188+
// TestParams tests that param setting and retrieval works properly
189+
func (suite *KeeperTestSuite) TestParams() {
190+
testCases := []struct {
191+
name string
192+
input types.Params
193+
expPass bool
194+
}{
195+
{"success: set default params", types.DefaultParams(), true},
196+
{"success: valid value for MaxExpectedTimePerBlock", types.NewParams(10), true},
197+
{"failure: invalid value for MaxExpectedTimePerBlock", types.NewParams(0), false},
198+
}
199+
200+
for _, tc := range testCases {
201+
tc := tc
202+
203+
suite.Run(tc.name, func() {
204+
suite.SetupTest() // reset
205+
ctx := suite.chainA.GetContext()
206+
err := tc.input.Validate()
207+
suite.chainA.GetSimApp().IBCKeeper.ConnectionKeeper.SetParams(ctx, tc.input)
208+
if tc.expPass {
209+
suite.Require().NoError(err)
210+
expected := tc.input
211+
p := suite.chainA.GetSimApp().IBCKeeper.ConnectionKeeper.GetParams(ctx)
212+
suite.Require().Equal(expected, p)
213+
} else {
214+
suite.Require().Error(err)
215+
}
216+
})
217+
}
218+
}
219+
220+
// TestUnsetParams tests that trying to get params that are not set panics.
221+
func (suite *KeeperTestSuite) TestUnsetParams() {
222+
suite.SetupTest()
223+
ctx := suite.chainA.GetContext()
224+
store := ctx.KVStore(suite.chainA.GetSimApp().GetKey(exported.StoreKey))
225+
store.Delete([]byte(types.ParamsKey))
226+
227+
suite.Require().Equal(suite.chainA.GetSimApp().IBCKeeper.ConnectionKeeper.GetParams(ctx), types.Params{})
228+
}

modules/core/03-connection/keeper/migrations.go

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55

66
connectionv7 "github.com/cosmos/ibc-go/v7/modules/core/03-connection/migrations/v7"
7+
"github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
78
)
89

910
// Migrator is a struct for handling in-place store migrations.
@@ -22,3 +23,17 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
2223
connectionv7.MigrateLocalhostConnection(ctx, m.keeper)
2324
return nil
2425
}
26+
27+
// MigrateParams migrates from consensus version 4 to 5.
28+
// This migration takes the parameters that are currently stored and managed by x/params
29+
// and stores them directly in the ibc module's state.
30+
func (m Migrator) MigrateParams(ctx sdk.Context) error {
31+
var params types.Params
32+
m.keeper.legacySubspace.GetParamSet(ctx, &params)
33+
34+
if err := params.Validate(); err != nil {
35+
return err
36+
}
37+
m.keeper.SetParams(ctx, params)
38+
return nil
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package keeper_test
2+
3+
import (
4+
"github.com/cosmos/ibc-go/v7/modules/core/03-connection/keeper"
5+
"github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
6+
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
7+
)
8+
9+
// TestMigrateParams tests that the params for the connection are properly migrated
10+
func (suite *KeeperTestSuite) TestMigrateParams() {
11+
testCases := []struct {
12+
name string
13+
malleate func()
14+
expectedParams types.Params
15+
}{
16+
{
17+
"success: default params",
18+
func() {
19+
params := types.DefaultParams()
20+
subspace := suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName)
21+
subspace.SetParamSet(suite.chainA.GetContext(), &params) // set params
22+
},
23+
types.DefaultParams(),
24+
},
25+
}
26+
27+
for _, tc := range testCases {
28+
tc := tc
29+
suite.Run(tc.name, func() {
30+
suite.SetupTest() // reset
31+
32+
tc.malleate()
33+
34+
ctx := suite.chainA.GetContext()
35+
migrator := keeper.NewMigrator(suite.chainA.GetSimApp().IBCKeeper.ConnectionKeeper)
36+
err := migrator.MigrateParams(ctx)
37+
suite.Require().NoError(err)
38+
39+
params := suite.chainA.GetSimApp().IBCKeeper.ConnectionKeeper.GetParams(ctx)
40+
suite.Require().Equal(tc.expectedParams, params)
41+
})
42+
}
43+
}

modules/core/03-connection/keeper/params.go

-24
This file was deleted.

modules/core/03-connection/keeper/params_test.go

-17
This file was deleted.

modules/core/03-connection/keeper/verify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (k Keeper) VerifyNextSequenceRecv(
369369
func (k Keeper) getBlockDelay(ctx sdk.Context, connection exported.ConnectionI) uint64 {
370370
// expectedTimePerBlock should never be zero, however if it is then return a 0 blcok delay for safety
371371
// as the expectedTimePerBlock parameter was not set.
372-
expectedTimePerBlock := k.GetMaxExpectedTimePerBlock(ctx)
372+
expectedTimePerBlock := k.GetParams(ctx).MaxExpectedTimePerBlock
373373
if expectedTimePerBlock == 0 {
374374
return 0
375375
}

modules/core/03-connection/types/codec.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
3333
&MsgConnectionOpenTry{},
3434
&MsgConnectionOpenAck{},
3535
&MsgConnectionOpenConfirm{},
36+
&MsgUpdateConnectionParams{},
3637
)
3738

3839
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)

modules/core/03-connection/types/keys.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const (
2828

2929
// ConnectionPrefix is the prefix used when creating a connection identifier
3030
ConnectionPrefix = "connection-"
31+
32+
// ParamsKey is the store key for the IBC connection parameters
33+
ParamsKey = "connectionParams"
3134
)
3235

3336
// FormatConnectionIdentifier returns the connection identifier with the sequence appended.

modules/core/03-connection/types/msgs.go

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
_ sdk.Msg = (*MsgConnectionOpenConfirm)(nil)
1818
_ sdk.Msg = (*MsgConnectionOpenAck)(nil)
1919
_ sdk.Msg = (*MsgConnectionOpenTry)(nil)
20+
_ sdk.Msg = (*MsgUpdateConnectionParams)(nil)
2021

2122
_ codectypes.UnpackInterfacesMessage = (*MsgConnectionOpenTry)(nil)
2223
_ codectypes.UnpackInterfacesMessage = (*MsgConnectionOpenAck)(nil)
@@ -289,3 +290,28 @@ func (msg MsgConnectionOpenConfirm) GetSigners() []sdk.AccAddress {
289290
}
290291
return []sdk.AccAddress{accAddr}
291292
}
293+
294+
// NewMsgUpdateConnectionParams creates a new MsgUpdateConnectionParams instance
295+
func NewMsgUpdateConnectionParams(authority string, params Params) *MsgUpdateConnectionParams {
296+
return &MsgUpdateConnectionParams{
297+
Authority: authority,
298+
Params: params,
299+
}
300+
}
301+
302+
// GetSigners returns the expected signers for a MsgUpdateConnectionParams message.
303+
func (msg *MsgUpdateConnectionParams) GetSigners() []sdk.AccAddress {
304+
accAddr, err := sdk.AccAddressFromBech32(msg.Authority)
305+
if err != nil {
306+
panic(err)
307+
}
308+
return []sdk.AccAddress{accAddr}
309+
}
310+
311+
// ValidateBasic performs basic checks on a MsgUpdateConnectionParams.
312+
func (msg *MsgUpdateConnectionParams) ValidateBasic() error {
313+
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
314+
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
315+
}
316+
return msg.Params.Validate()
317+
}

modules/core/03-connection/types/msgs_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,38 @@ func (suite *MsgTestSuite) TestNewMsgConnectionOpenConfirm() {
231231
}
232232
}
233233
}
234+
235+
// TestMsgUpdateConnectionParams_ValidateBasic tests ValidateBasic for MsgUpdateConnectionParams
236+
func (suite *MsgTestSuite) TestMsgUpdateConnectionParams_ValidateBasic() {
237+
authority := suite.chainA.App.GetIBCKeeper().GetAuthority()
238+
testCases := []struct {
239+
name string
240+
msg *types.MsgUpdateConnectionParams
241+
expPass bool
242+
}{
243+
{
244+
"success: valid authority and params",
245+
types.NewMsgUpdateConnectionParams(authority, types.DefaultParams()),
246+
true,
247+
},
248+
{
249+
"failure: invalid authority address",
250+
types.NewMsgUpdateConnectionParams("invalid", types.DefaultParams()),
251+
false,
252+
},
253+
{
254+
"failure: invalid time per block",
255+
types.NewMsgUpdateConnectionParams(authority, types.NewParams(0)),
256+
false,
257+
},
258+
}
259+
260+
for _, tc := range testCases {
261+
err := tc.msg.ValidateBasic()
262+
if tc.expPass {
263+
suite.Require().NoError(err, "valid case %s failed", tc.name)
264+
} else {
265+
suite.Require().Error(err, "invalid case %s passed", tc.name)
266+
}
267+
}
268+
}

0 commit comments

Comments
 (0)