github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/03-connection/keeper/grpc_query.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/prefix"
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/query"
    10  	clienttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types"
    11  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/03-connection/types"
    12  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    13  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/common"
    14  	"google.golang.org/grpc/codes"
    15  	"google.golang.org/grpc/status"
    16  )
    17  
    18  var _ types.QueryServer = Keeper{}
    19  
    20  // Connection implements the Query/Connection gRPC method
    21  func (q Keeper) Connection(c context.Context, req *types.QueryConnectionRequest) (*types.QueryConnectionResponse, error) {
    22  	if req == nil {
    23  		return nil, status.Error(codes.InvalidArgument, "empty request")
    24  	}
    25  
    26  	if err := host.ConnectionIdentifierValidator(req.ConnectionId); err != nil {
    27  		return nil, status.Error(codes.InvalidArgument, err.Error())
    28  	}
    29  
    30  	ctx := sdk.UnwrapSDKContext(c)
    31  	connection, found := q.GetConnection(ctx, req.ConnectionId)
    32  	if !found {
    33  		return nil, status.Error(
    34  			codes.NotFound,
    35  			sdkerrors.Wrap(types.ErrConnectionNotFound, req.ConnectionId).Error(),
    36  		)
    37  	}
    38  
    39  	return &types.QueryConnectionResponse{
    40  		Connection:  &connection,
    41  		ProofHeight: clienttypes.GetSelfHeight(ctx),
    42  	}, nil
    43  }
    44  
    45  // Connections implements the Query/Connections gRPC method
    46  func (q Keeper) Connections(c context.Context, req *types.QueryConnectionsRequest) (*types.QueryConnectionsResponse, error) {
    47  	if req == nil {
    48  		return nil, status.Error(codes.InvalidArgument, "empty request")
    49  	}
    50  
    51  	ctx := sdk.UnwrapSDKContext(c)
    52  
    53  	connections := []*types.IdentifiedConnection{}
    54  	store := prefix.NewStore(ctx.KVStore(q.storeKey), []byte(host.KeyConnectionPrefix))
    55  
    56  	pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
    57  		var result types.ConnectionEnd
    58  
    59  		if retr, err := common.UnmarshalConnection(q.cdc, value); err != nil {
    60  			return err
    61  		} else {
    62  			result = *retr
    63  		}
    64  
    65  		connectionID, err := host.ParseConnectionPath(string(key))
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		identifiedConnection := types.NewIdentifiedConnection(connectionID, result)
    71  		connections = append(connections, &identifiedConnection)
    72  		return nil
    73  	})
    74  
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return &types.QueryConnectionsResponse{
    80  		Connections: connections,
    81  		Pagination:  pageRes,
    82  		Height:      clienttypes.GetSelfHeight(ctx),
    83  	}, nil
    84  }
    85  
    86  // ClientConnections implements the Query/ClientConnections gRPC method
    87  func (q Keeper) ClientConnections(c context.Context, req *types.QueryClientConnectionsRequest) (*types.QueryClientConnectionsResponse, error) {
    88  	if req == nil {
    89  		return nil, status.Error(codes.InvalidArgument, "empty request")
    90  	}
    91  
    92  	if err := host.ClientIdentifierValidator(req.ClientId); err != nil {
    93  		return nil, status.Error(codes.InvalidArgument, err.Error())
    94  	}
    95  
    96  	ctx := sdk.UnwrapSDKContext(c)
    97  	clientConnectionPaths, found := q.GetClientConnectionPaths(ctx, req.ClientId)
    98  	if !found {
    99  		return nil, status.Error(
   100  			codes.NotFound,
   101  			sdkerrors.Wrap(types.ErrClientConnectionPathsNotFound, req.ClientId).Error(),
   102  		)
   103  	}
   104  
   105  	return &types.QueryClientConnectionsResponse{
   106  		ConnectionPaths: clientConnectionPaths,
   107  		ProofHeight:     clienttypes.GetSelfHeight(ctx),
   108  	}, nil
   109  }
   110  
   111  // ConnectionClientState implements the Query/ConnectionClientState gRPC method
   112  func (q Keeper) ConnectionClientState(c context.Context, req *types.QueryConnectionClientStateRequest) (*types.QueryConnectionClientStateResponse, error) {
   113  	if req == nil {
   114  		return nil, status.Error(codes.InvalidArgument, "empty request")
   115  	}
   116  
   117  	if err := host.ConnectionIdentifierValidator(req.ConnectionId); err != nil {
   118  		return nil, status.Error(codes.InvalidArgument, err.Error())
   119  	}
   120  
   121  	ctx := sdk.UnwrapSDKContext(c)
   122  
   123  	connection, found := q.GetConnection(ctx, req.ConnectionId)
   124  	if !found {
   125  		return nil, status.Error(
   126  			codes.NotFound,
   127  			sdkerrors.Wrapf(types.ErrConnectionNotFound, "connection-id: %s", req.ConnectionId).Error(),
   128  		)
   129  	}
   130  
   131  	clientState, found := q.clientKeeper.GetClientState(ctx, connection.ClientId)
   132  	if !found {
   133  		return nil, status.Error(
   134  			codes.NotFound,
   135  			sdkerrors.Wrapf(clienttypes.ErrClientNotFound, "client-id: %s", connection.ClientId).Error(),
   136  		)
   137  	}
   138  
   139  	identifiedClientState := clienttypes.NewIdentifiedClientState(connection.ClientId, clientState)
   140  
   141  	height := clienttypes.GetSelfHeight(ctx)
   142  	return types.NewQueryConnectionClientStateResponse(identifiedClientState, nil, height), nil
   143  
   144  }
   145  
   146  // ConnectionConsensusState implements the Query/ConnectionConsensusState gRPC method
   147  func (q Keeper) ConnectionConsensusState(c context.Context, req *types.QueryConnectionConsensusStateRequest) (*types.QueryConnectionConsensusStateResponse, error) {
   148  	if req == nil {
   149  		return nil, status.Error(codes.InvalidArgument, "empty request")
   150  	}
   151  
   152  	if err := host.ConnectionIdentifierValidator(req.ConnectionId); err != nil {
   153  		return nil, status.Error(codes.InvalidArgument, err.Error())
   154  	}
   155  
   156  	ctx := sdk.UnwrapSDKContext(c)
   157  
   158  	connection, found := q.GetConnection(ctx, req.ConnectionId)
   159  	if !found {
   160  		return nil, status.Error(
   161  			codes.NotFound,
   162  			sdkerrors.Wrapf(types.ErrConnectionNotFound, "connection-id: %s", req.ConnectionId).Error(),
   163  		)
   164  	}
   165  
   166  	height := clienttypes.NewHeight(req.RevisionNumber, req.RevisionHeight)
   167  	consensusState, found := q.clientKeeper.GetClientConsensusState(ctx, connection.ClientId, height)
   168  	if !found {
   169  		return nil, status.Error(
   170  			codes.NotFound,
   171  			sdkerrors.Wrapf(clienttypes.ErrConsensusStateNotFound, "client-id: %s", connection.ClientId).Error(),
   172  		)
   173  	}
   174  
   175  	anyConsensusState, err := clienttypes.PackConsensusState(consensusState)
   176  	if err != nil {
   177  		return nil, status.Error(codes.Internal, err.Error())
   178  	}
   179  
   180  	proofHeight := clienttypes.GetSelfHeight(ctx)
   181  	return types.NewQueryConnectionConsensusStateResponse(connection.ClientId, anyConsensusState, height, nil, proofHeight), nil
   182  }