Skip to content

Commit

Permalink
fix: handle unordered channels in NextSequenceReceive query
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim authored Mar 29, 2023
1 parent 56a519c commit ae27fa5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
15 changes: 13 additions & 2 deletions modules/core/04-channel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 20 additions & 3 deletions modules/core/04-channel/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ae27fa5

Please sign in to comment.