Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add v2 packet commitment query #7494

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions modules/core/04-channel/v2/client/cli/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
errorsmod "cosmossdk.io/errors"

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

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
ibcclient "github.com/cosmos/ibc-go/v9/modules/core/client"
)

func queryPacketCommitmentABCI(clientCtx client.Context, channelID string, sequence uint64) (*types.QueryPacketCommitmentResponse, error) {
key := host.PacketCommitmentKey(channelID, sequence)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}

// check if packet commitment exists
if len(value) == 0 {
return nil, errorsmod.Wrapf(types.ErrPacketCommitmentNotFound, "channelID (%s), sequence (%d)", channelID, sequence)
}

return types.NewQueryPacketCommitmentResponse(value, proofBz, proofHeight), nil
}
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
54 changes: 52 additions & 2 deletions modules/core/04-channel/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"

"github.com/spf13/cobra"

Expand All @@ -26,20 +27,69 @@ func getCmdQueryChannel() *cobra.Command {
if err != nil {
return err
}

channelID := args[0]

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Channel(cmd.Context(), types.NewQueryChannelRequest(channelID))
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func getCmdQueryPacketCommitment() *cobra.Command {
cmd := &cobra.Command{
Use: "packet-commitment [channel-id] [sequence]",
Short: "Query a channel/v2 packet commitment",
Long: "Query a channel/v2 packet commitment by channel-id and sequence",
Example: fmt.Sprintf(
"%s query %s %s packet-commitment [channel-id] [sequence]", version.AppName, exported.ModuleName, types.SubModuleName,
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

channelID := args[0]
seq, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

req := &types.QueryChannelRequest{ChannelId: channelID}
prove, err := cmd.Flags().GetBool(flags.FlagProve)
if err != nil {
return err
}

if prove {
res, err := queryPacketCommitmentABCI(clientCtx, channelID, seq)
if err != nil {
return err
}

res, err := queryClient.Channel(cmd.Context(), req)
return clientCtx.PrintProto(res)
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.PacketCommitment(cmd.Context(), types.NewQueryPacketCommitmentRequest(channelID, seq))
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results")
flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
40 changes: 29 additions & 11 deletions modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

errorsmod "cosmossdk.io/errors"

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 @@ -38,12 +37,8 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return nil, status.Error(codes.InvalidArgument, err.Error())
}

res := types.QueryChannelResponse{}

sdkCtx := sdk.UnwrapSDKContext(ctx)

creator, foundCreator := q.GetCreator(sdkCtx, req.ChannelId)
channel, foundChannel := q.GetChannel(sdkCtx, req.ChannelId)
creator, foundCreator := q.GetCreator(ctx, req.ChannelId)
channel, foundChannel := q.GetChannel(ctx, req.ChannelId)

if !foundCreator && !foundChannel {
return nil, status.Error(
Expand All @@ -52,8 +47,31 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
)
}

res.Channel = channel
res.Creator = creator
return types.NewQueryChannelResponse(creator, channel), 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 &res, nil
return types.NewQueryPacketCommitmentResponse(commitment, nil, 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) {
bz := k.ChannelStore(ctx, channelID).Get([]byte(types.CreatorKey))
Expand Down
13 changes: 7 additions & 6 deletions modules/core/04-channel/v2/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

var (
ErrInvalidChannel = errorsmod.Register(SubModuleName, 2, "invalid channel")
ErrChannelNotFound = errorsmod.Register(SubModuleName, 3, "channel not found")
ErrInvalidPacket = errorsmod.Register(SubModuleName, 4, "invalid packet")
ErrInvalidPayload = errorsmod.Register(SubModuleName, 5, "invalid payload")
ErrSequenceSendNotFound = errorsmod.Register(SubModuleName, 6, "sequence send not found")
ErrInvalidAcknowledgement = errorsmod.Register(SubModuleName, 8, "invalid acknowledgement")
ErrInvalidChannel = errorsmod.Register(SubModuleName, 2, "invalid channel")
ErrChannelNotFound = errorsmod.Register(SubModuleName, 3, "channel not found")
ErrInvalidPacket = errorsmod.Register(SubModuleName, 4, "invalid packet")
ErrInvalidPayload = errorsmod.Register(SubModuleName, 5, "invalid payload")
ErrSequenceSendNotFound = errorsmod.Register(SubModuleName, 6, "sequence send not found")
ErrInvalidAcknowledgement = errorsmod.Register(SubModuleName, 7, "invalid acknowledgement")
ErrPacketCommitmentNotFound = errorsmod.Register(SubModuleName, 8, "packet commitment not found")
)
35 changes: 35 additions & 0 deletions modules/core/04-channel/v2/types/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package types

import clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"

// NewQueryChannelRequest creates and returns a new channel query request.
func NewQueryChannelRequest(channelID string) *QueryChannelRequest {
return &QueryChannelRequest{
ChannelId: channelID,
}
}

// NewQueryChannelResponse creates and returns a new channel query response.
func NewQueryChannelResponse(creator string, channel Channel) *QueryChannelResponse {
return &QueryChannelResponse{
Creator: creator,
Channel: channel,
}
}

// NewQueryPacketCommitmentRequest creates and returns a new packet commitment query request.
func NewQueryPacketCommitmentRequest(channelID string, sequence uint64) *QueryPacketCommitmentRequest {
return &QueryPacketCommitmentRequest{
ChannelId: channelID,
Sequence: sequence,
}
}

// NewQueryPacketCommitmentResponse creates and returns a new packet commitment query response.
func NewQueryPacketCommitmentResponse(commitmentHash []byte, proof []byte, proofHeight clienttypes.Height) *QueryPacketCommitmentResponse {
return &QueryPacketCommitmentResponse{
Commitment: commitmentHash,
Proof: proof,
ProofHeight: proofHeight,
}
}
Loading
Loading