Skip to content

Commit

Permalink
chore: add channels rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Dec 3, 2024
1 parent ce8dcf2 commit 4b52bb9
Show file tree
Hide file tree
Showing 6 changed files with 835 additions and 134 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(),
getCmdQueryChannels(),
getCmdQueryNextSequenceSend(),
getCmdQueryPacketCommitment(),
getCmdQueryPacketCommitments(),
Expand Down
39 changes: 39 additions & 0 deletions modules/core/04-channel/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,45 @@ func getCmdQueryChannel() *cobra.Command {
return cmd
}

// getCmdQueryChannels defines the command to query all the v2 channels that this chain maintains.
func getCmdQueryChannels() *cobra.Command {
cmd := &cobra.Command{
Use: "channels",
Short: "Query all channels",
Long: "Query all channels from a chain",
Example: fmt.Sprintf("%s query %s %s channels", version.AppName, exported.ModuleName, types.SubModuleName),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

req := &types.QueryChannelsRequest{
Pagination: pageReq,
}

res, err := queryClient.Channels(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "channels")

return cmd
}

// getCmdQueryNextSequenceSend defines the command to query a next send sequence for a given channel
func getCmdQueryNextSequenceSend() *cobra.Command {
cmd := &cobra.Command{
Expand Down
36 changes: 36 additions & 0 deletions modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,42 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return types.NewQueryChannelResponse(channel), nil
}

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

var channels []*types.IdentifiedChannel
store := prefix.NewStore(runtime.KVStoreAdapter(q.storeService.OpenKVStore(ctx)), []byte(types.ChannelKey))

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
var channel types.Channel
if err := q.cdc.Unmarshal(value, &channel); err != nil {
return err
}

_, channelID, err := host.ParseChannelPath(string(key))
if err != nil {
return err
}

identifiedChannel := types.NewIdentifiedChannel(channelID, channel)
channels = append(channels, &identifiedChannel)
return nil
})
if err != nil {
return nil, err
}

selfHeight := clienttypes.GetSelfHeight(ctx)
return &types.QueryChannelsResponse{
Channels: channels,
Pagination: pageRes,
Height: selfHeight,
}, nil
}

// NextSequenceSend implements the Query/NextSequenceSend gRPC method
func (q *queryServer) NextSequenceSend(ctx context.Context, req *types.QueryNextSequenceSendRequest) (*types.QueryNextSequenceSendResponse, error) {
if req == nil {
Expand Down
Loading

0 comments on commit 4b52bb9

Please sign in to comment.