Skip to content

Commit

Permalink
feat: add v2 packet commitment query
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Oct 21, 2024
1 parent cc51d29 commit a52c9ae
Show file tree
Hide file tree
Showing 8 changed files with 855 additions and 23 deletions.
1 change: 1 addition & 0 deletions modules/core/04-channel/v2/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetQueryCmd() *cobra.Command {

queryCmd.AddCommand(
getCmdQueryChannel(),
getCmdQueryPacketCommitment(),
)

return queryCmd
Expand Down
4 changes: 4 additions & 0 deletions modules/core/04-channel/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ func getCmdQueryChannel() *cobra.Command {

return cmd
}

func getCmdQueryPacketCommitment() *cobra.Command {
return nil
}
31 changes: 31 additions & 0 deletions modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)
Expand Down Expand Up @@ -57,3 +58,33 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques

return &res, nil
}

// PacketCommitment implements the Query/PacketCommitment gRPC method.
func (q *queryServer) PacketCommitment(ctx context.Context, req *types.QueryPacketCommitmentRequest) (*types.QueryPacketCommitmentResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if err := host.ClientIdentifierValidator(req.ChannelId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

if req.Sequence == 0 {
return nil, status.Error(codes.InvalidArgument, "packet sequence cannot be 0")
}

if !q.HasChannel(ctx, req.ChannelId) {
return nil, status.Error(codes.NotFound, errorsmod.Wrap(types.ErrChannelNotFound, req.ChannelId).Error())
}

commitment := q.GetPacketCommitment(ctx, req.ChannelId, req.Sequence)
if len(commitment) == 0 {
return nil, status.Error(codes.NotFound, "packet commitment hash not found")
}

return &types.QueryPacketCommitmentResponse{
Commitment: commitment,
Proof: nil,
ProofHeight: clienttypes.GetSelfHeight(ctx),
}, nil
}
104 changes: 104 additions & 0 deletions modules/core/04-channel/v2/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,107 @@ func (suite *KeeperTestSuite) TestQueryChannel() {
})
}
}

func (suite *KeeperTestSuite) TestQueryPacketCommitment() {
var (
expCommitment []byte
path *ibctesting.Path
req *types.QueryPacketCommitmentRequest
)

testCases := []struct {
msg string
malleate func()
expError error
}{
{
"success",
func() {
path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupV2()

expCommitment = []byte("commitmentHash")
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelID, 1, expCommitment)

req = &types.QueryPacketCommitmentRequest{
ChannelId: path.EndpointA.ChannelID,
Sequence: 1,
}
},
nil,
},
{
"empty request",
func() {
req = nil
},
status.Error(codes.InvalidArgument, "empty request"),
},
{
"invalid channel ID",
func() {
req = &types.QueryPacketCommitmentRequest{
ChannelId: "",
Sequence: 1,
}
},
status.Error(codes.InvalidArgument, "identifier cannot be blank: invalid identifier"),
},
{
"invalid sequence",
func() {
req = &types.QueryPacketCommitmentRequest{
ChannelId: ibctesting.FirstChannelID,
Sequence: 0,
}
},
status.Error(codes.InvalidArgument, "packet sequence cannot be 0"),
},
{
"channel not found",
func() {
req = &types.QueryPacketCommitmentRequest{
ChannelId: "channel-141",
Sequence: 1,
}
},
status.Error(codes.NotFound, fmt.Sprintf("%s: channel not found", "channel-141")),
},
{
"commitment not found",
func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupV2()

req = &types.QueryPacketCommitmentRequest{
ChannelId: path.EndpointA.ChannelID,
Sequence: 1,
}
},
status.Error(codes.NotFound, "packet commitment hash not found"),
},
}

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

suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

tc.malleate()

queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeperV2)
res, err := queryServer.PacketCommitment(suite.chainA.GetContext(), req)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expCommitment, res.Commitment)
} else {
suite.Require().ErrorIs(err, tc.expError)
suite.Require().Nil(res)
}
})
}
}
6 changes: 6 additions & 0 deletions modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (k *Keeper) GetChannel(ctx context.Context, channelID string) (types.Channe
return channel, true
}

// HasChannel returns true if a Channel exists for a given channel identifier, otherwise false.
func (k *Keeper) HasChannel(ctx context.Context, channelID string) bool {
store := k.ChannelStore(ctx, channelID)
return store.Has([]byte(types.ChannelKey))
}

// GetCreator returns the creator of the channel.
func (k *Keeper) GetCreator(ctx context.Context, channelID string) (string, bool) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
Expand Down
Loading

0 comments on commit a52c9ae

Please sign in to comment.