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: impl packet acknowledgement query #7507

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 41 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,41 @@
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
}

func queryPacketAcknowledgementABCI(clientCtx client.Context, channelID string, sequence uint64) (*types.QueryPacketAcknowledgementResponse, error) {
key := host.PacketAcknowledgementKey(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.ErrAcknowledgementNotFound, "channelID (%s), sequence (%d)", channelID, sequence)
}

return types.NewQueryPacketAcknowledgementResponse(value, proofBz, proofHeight), nil
}
2 changes: 2 additions & 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,8 @@ func GetQueryCmd() *cobra.Command {

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

return queryCmd
Expand Down
105 changes: 103 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,120 @@ 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
}

req := &types.QueryChannelRequest{ChannelId: channelID}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)

res, err := queryClient.Channel(cmd.Context(), req)
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
}

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
}

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
}

func getCmdQueryPacketAcknowledgement() *cobra.Command {
cmd := &cobra.Command{
Use: "packet-acknowledgement [channel-id] [sequence]",
Short: "Query a channel/v2 packet acknowledgement",
Long: "Query a channel/v2 packet acknowledgement by channel-id and sequence",
Example: fmt.Sprintf(
"%s query %s %s packet-acknowledgement [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
}

prove, err := cmd.Flags().GetBool(flags.FlagProve)
if err != nil {
return err
}

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

return clientCtx.PrintProto(res)
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.PacketAcknowledgement(cmd.Context(), types.NewQueryPacketAcknowledgementRequest(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
66 changes: 55 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,57 @@ 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 types.NewQueryPacketCommitmentResponse(commitment, nil, clienttypes.GetSelfHeight(ctx)), nil
}

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

if err := host.ClientIdentifierValidator(req.ChannelId); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
if err := host.ClientIdentifierValidator(req.ChannelId); err != nil {
if err := host.ChannelIdentifierValidator(req.ChannelId); err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch! I am guilty of copy/paste! Looks like we're doing the same in channel grpc query. I will fix!

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())
}

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

return &res, nil
return types.NewQueryPacketAcknowledgementResponse(acknowledgement, nil, clienttypes.GetSelfHeight(ctx)), nil
}
Loading
Loading