Skip to content

Commit

Permalink
chore: add channel consensus state rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Dec 4, 2024
1 parent 3864793 commit 6dd062b
Show file tree
Hide file tree
Showing 7 changed files with 1,029 additions and 168 deletions.
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
@@ -1,17 +1,43 @@
package cli

import (
"context"
"encoding/binary"

errorsmod "cosmossdk.io/errors"

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

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

func queryChannelConsensusStateABCI(clientCtx client.Context, channelID string, height clienttypes.Height) (*types.QueryChannelConsensusStateResponse, error) {

Check failure on line 18 in modules/core/04-channel/v2/client/cli/abci.go

View workflow job for this annotation

GitHub Actions / lint

func `queryChannelConsensusStateABCI` is unused (unused)

Check failure on line 18 in modules/core/04-channel/v2/client/cli/abci.go

View workflow job for this annotation

GitHub Actions / lint

func `queryChannelConsensusStateABCI` is unused (unused)
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryChannelConsensusStateRequest{
ChannelId: channelID,
RevisionNumber: height.RevisionNumber,
RevisionHeight: height.RevisionHeight,
}

res, err := queryClient.ChannelConsensusState(context.Background(), req)
if err != nil {
return nil, err
}

consensusStateRes, err := clientutils.QueryConsensusStateABCI(clientCtx, res.ClientId, height)
if err != nil {
return nil, err
}

res = types.NewQueryChannelConsensusStateResponse(res.ClientId, consensusStateRes.ConsensusState, consensusStateRes.Proof, consensusStateRes.ProofHeight)

return res, nil
}

func queryNextSequenceSendABCI(clientCtx client.Context, channelID string) (*types.QueryNextSequenceSendResponse, error) {
key := hostv2.NextSequenceSendKey(channelID)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
Expand Down
32 changes: 32 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,38 @@ func (q *queryServer) Channel(ctx context.Context, req *types.QueryChannelReques
return types.NewQueryChannelResponse(channel), nil
}

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

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

channel, found := q.GetChannel(ctx, req.ChannelId)
if !found {
return nil, status.Error(codes.NotFound, errorsmod.Wrapf(types.ErrChannelNotFound, "channel-id: %s", req.ChannelId).Error())
}

consHeight := clienttypes.NewHeight(req.RevisionNumber, req.RevisionHeight)
consensusState, found := q.ClientKeeper.GetClientConsensusState(ctx, channel.ClientId, consHeight)
if !found {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "client-id: %s", channel.ClientId).Error(),
)
}

anyConsensusState, err := clienttypes.PackConsensusState(consensusState)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return types.NewQueryChannelConsensusStateResponse(channel.ClientId, anyConsensusState, nil, clienttypes.GetSelfHeight(ctx)), 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
2 changes: 2 additions & 0 deletions modules/core/04-channel/v2/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type ClientKeeper interface {
// GetClientTimestampAtHeight returns the timestamp for a given height on the client
// given its client ID and height
GetClientTimestampAtHeight(ctx context.Context, clientID string, height exported.Height) (uint64, error)
// GetClientConsensusState gets the stored consensus state from a client at a given height.
GetClientConsensusState(ctx context.Context, clientID string, height exported.Height) (exported.ConsensusState, bool)
}
16 changes: 15 additions & 1 deletion modules/core/04-channel/v2/types/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

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

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 {
Expand All @@ -16,6 +20,16 @@ func NewQueryChannelResponse(channel Channel) *QueryChannelResponse {
}
}

// NewQueryChannelConsensusStateResponse creates and returns a new ChannelConsensusState query response.
func NewQueryChannelConsensusStateResponse(clientID string, anyConsensusState *codectypes.Any, proof []byte, height clienttypes.Height) *QueryChannelConsensusStateResponse {
return &QueryChannelConsensusStateResponse{
ConsensusState: anyConsensusState,
ClientId: clientID,
Proof: proof,
ProofHeight: height,
}
}

// NewQueryNextSequenceSendRequest creates a new next sequence send query.
func NewQueryNextSequenceSendRequest(channelID string) *QueryNextSequenceSendRequest {
return &QueryNextSequenceSendRequest{
Expand Down
Loading

0 comments on commit 6dd062b

Please sign in to comment.