From 429a5f21a09f642c8e6dcc76390a2ee864846645 Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Sun, 26 Mar 2023 21:52:46 +0300 Subject: [PATCH] fix: handle NextSequenceReceive for unordered channels. --- modules/core/04-channel/keeper/grpc_query.go | 15 ++++++++++-- .../core/04-channel/keeper/grpc_query_test.go | 23 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/modules/core/04-channel/keeper/grpc_query.go b/modules/core/04-channel/keeper/grpc_query.go index ee11b40251e..ec019c65c62 100644 --- a/modules/core/04-channel/keeper/grpc_query.go +++ b/modules/core/04-channel/keeper/grpc_query.go @@ -480,14 +480,25 @@ func (q Keeper) NextSequenceReceive(c context.Context, req *types.QueryNextSeque } ctx := sdk.UnwrapSDKContext(c) - sequence, found := q.GetNextSequenceRecv(ctx, req.PortId, req.ChannelId) + channel, found := q.GetChannel(ctx, req.PortId, req.ChannelId) if !found { return nil, status.Error( codes.NotFound, - errorsmod.Wrapf(types.ErrSequenceReceiveNotFound, "port-id: %s, channel-id %s", req.PortId, req.ChannelId).Error(), + errorsmod.Wrapf(types.ErrChannelNotFound, "port-id: %s, channel-id %s", req.PortId, req.ChannelId).Error(), ) } + // Return the next sequence received for ordered channels and 0 for unordered channels. + var sequence uint64 + if channel.Ordering == types.ORDERED { + sequence, found = q.GetNextSequenceRecv(ctx, req.PortId, req.ChannelId) + if !found { + return nil, status.Error( + codes.NotFound, + errorsmod.Wrapf(types.ErrSequenceReceiveNotFound, "port-id: %s, channel-id %s", req.PortId, req.ChannelId).Error(), + ) + } + } selfHeight := clienttypes.GetSelfHeight(ctx) return types.NewQueryNextSequenceReceiveResponse(sequence, nil, selfHeight), nil } diff --git a/modules/core/04-channel/keeper/grpc_query_test.go b/modules/core/04-channel/keeper/grpc_query_test.go index 0082663a243..8d6b898dfc0 100644 --- a/modules/core/04-channel/keeper/grpc_query_test.go +++ b/modules/core/04-channel/keeper/grpc_query_test.go @@ -1419,12 +1419,29 @@ func (suite *KeeperTestSuite) TestQueryNextSequenceReceive() { false, }, { - "success", + "basic success on unordered channel returns zero", func() { path := ibctesting.NewPath(suite.chainA, suite.chainB) suite.coordinator.Setup(path) - expSeq = 1 - suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, expSeq) + + expSeq = 0 + req = &types.QueryNextSequenceReceiveRequest{ + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, + } + }, + true, + }, + { + "basic success on ordered channel returns the set receive sequence", + func() { + path := ibctesting.NewPath(suite.chainA, suite.chainB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + expSeq = 3 + seq := uint64(3) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq) req = &types.QueryNextSequenceReceiveRequest{ PortId: path.EndpointA.ChannelConfig.PortID,