From 9239d5dc569b712d90ecc321690ac51d934380f7 Mon Sep 17 00:00:00 2001 From: Gjermund Garaba Date: Thu, 7 Nov 2024 11:12:35 +0100 Subject: [PATCH] fix: channel must exist before channel creator check (#7538) * fix: channel must exist before channel creator check * Update modules/core/04-channel/v2/keeper/msg_server.go Co-authored-by: DimitrisJim * fixed failing test --------- Co-authored-by: DimitrisJim --- modules/core/04-channel/v2/keeper/msg_server.go | 10 +++++----- modules/core/04-channel/v2/keeper/msg_server_test.go | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/msg_server.go b/modules/core/04-channel/v2/keeper/msg_server.go index c4623150ea1..6bda4404531 100644 --- a/modules/core/04-channel/v2/keeper/msg_server.go +++ b/modules/core/04-channel/v2/keeper/msg_server.go @@ -38,6 +38,11 @@ func (k *Keeper) CreateChannel(goCtx context.Context, msg *types.MsgCreateChanne func (k *Keeper) RegisterCounterparty(goCtx context.Context, msg *types.MsgRegisterCounterparty) (*types.MsgRegisterCounterpartyResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) + channel, ok := k.GetChannel(ctx, msg.ChannelId) + if !ok { + return nil, errorsmod.Wrapf(types.ErrChannelNotFound, "channel must exist for channel id %s", msg.ChannelId) + } + creator, found := k.GetCreator(ctx, msg.ChannelId) if !found { return nil, errorsmod.Wrap(ibcerrors.ErrUnauthorized, "channel creator must be set") @@ -47,11 +52,6 @@ func (k *Keeper) RegisterCounterparty(goCtx context.Context, msg *types.MsgRegis return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "channel creator (%s) must match signer (%s)", creator, msg.Signer) } - channel, ok := k.GetChannel(ctx, msg.ChannelId) - if !ok { - return nil, errorsmod.Wrapf(types.ErrInvalidChannel, "channel must exist for channel id %s", msg.ChannelId) - } - channel.CounterpartyChannelId = msg.CounterpartyChannelId k.SetChannel(ctx, msg.ChannelId, channel) // Delete client creator from state as it is not needed after this point. diff --git a/modules/core/04-channel/v2/keeper/msg_server_test.go b/modules/core/04-channel/v2/keeper/msg_server_test.go index b4b5d8c71c4..2fa079e1e1f 100644 --- a/modules/core/04-channel/v2/keeper/msg_server_test.go +++ b/modules/core/04-channel/v2/keeper/msg_server_test.go @@ -52,9 +52,10 @@ func (suite *KeeperTestSuite) TestRegisterCounterparty() { { "failure: channel must already exist", func() { + suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.DeleteCreator(suite.chainA.GetContext(), path.EndpointA.ChannelID) suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.ChannelStore(suite.chainA.GetContext(), path.EndpointA.ChannelID).Delete([]byte(channeltypesv2.ChannelKey)) }, - channeltypesv2.ErrInvalidChannel, + channeltypesv2.ErrChannelNotFound, }, }