Skip to content

Commit

Permalink
chore: remove duplicate proto files for the same proto file (#21648)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Sep 13, 2024
1 parent 9c5cea0 commit c0e84af
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 8,017 deletions.
20 changes: 6 additions & 14 deletions client/grpc/cmtservice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,23 @@ import (

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"

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

func getBlockHeight(ctx context.Context, clientCtx client.Context) (int64, error) {
status, err := GetNodeStatus(ctx, clientCtx)
func getBlockHeight(ctx context.Context, rpc CometRPC) (int64, error) {
status, err := GetNodeStatus(ctx, rpc)
if err != nil {
return 0, err
}
height := status.SyncInfo.LatestBlockHeight
return height, nil
}

func getBlock(ctx context.Context, clientCtx client.Context, height *int64) (*coretypes.ResultBlock, error) {
// get the node
node, err := clientCtx.GetNode()
if err != nil {
return nil, err
}

return node.Block(ctx, height)
func getBlock(ctx context.Context, rpc CometRPC, height *int64) (*coretypes.ResultBlock, error) {
return rpc.Block(ctx, height)
}

func GetProtoBlock(ctx context.Context, clientCtx client.Context, height *int64) (cmtproto.BlockID, *cmtproto.Block, error) {
block, err := getBlock(ctx, clientCtx, height)
func GetProtoBlock(ctx context.Context, rpc CometRPC, height *int64) (cmtproto.BlockID, *cmtproto.Block, error) {
block, err := getBlock(ctx, rpc, height)
if err != nil {
return cmtproto.BlockID{}, nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions client/grpc/cmtservice/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmtservice

import (
"context"

rpcclient "github.com/cometbft/cometbft/rpc/client"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
)

// CometRPC defines the interface of a CometBFT RPC client needed for
// queries and transaction handling.
type CometRPC interface {
rpcclient.ABCIClient

Validators(ctx context.Context, height *int64, page, perPage *int) (*coretypes.ResultValidators, error)
Status(context.Context) (*coretypes.ResultStatus, error)
Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error)
BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error)
BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error)
BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*coretypes.ResultBlockchainInfo, error)
Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error)
Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error)
TxSearch(
ctx context.Context,
query string,
prove bool,
page, perPage *int,
orderBy string,
) (*coretypes.ResultTxSearch, error)
BlockSearch(
ctx context.Context,
query string,
page, perPage *int,
orderBy string,
) (*coretypes.ResultBlockSearch, error)
}
50 changes: 28 additions & 22 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -28,28 +30,28 @@ type (
abciQueryFn = func(context.Context, *abci.QueryRequest) (*abci.QueryResponse, error)

queryServer struct {
clientCtx client.Context
interfaceRegistry codectypes.InterfaceRegistry
queryFn abciQueryFn
rpc CometRPC
queryFn abciQueryFn
consensusCodec address.Codec
}
)

// NewQueryServer creates a new CometBFT query server.
func NewQueryServer(
clientCtx client.Context,
interfaceRegistry codectypes.InterfaceRegistry,
clientCtx CometRPC,
queryFn abciQueryFn,
consensusAddressCodec address.Codec,
) ServiceServer {
return queryServer{
clientCtx: clientCtx,
interfaceRegistry: interfaceRegistry,
queryFn: queryFn,
rpc: clientCtx,
queryFn: queryFn,
consensusCodec: consensusAddressCodec,
}
}

// GetSyncing implements ServiceServer.GetSyncing
func (s queryServer) GetSyncing(ctx context.Context, _ *GetSyncingRequest) (*GetSyncingResponse, error) {
status, err := GetNodeStatus(ctx, s.clientCtx)
status, err := GetNodeStatus(ctx, s.rpc)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +63,7 @@ func (s queryServer) GetSyncing(ctx context.Context, _ *GetSyncingRequest) (*Get

// GetLatestBlock implements ServiceServer.GetLatestBlock
func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockRequest) (*GetLatestBlockResponse, error) {
status, err := getBlock(ctx, s.clientCtx, nil)
status, err := getBlock(ctx, s.rpc, nil)
if err != nil {
return nil, err
}
Expand All @@ -72,7 +74,7 @@ func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockReques
return nil, err
}

sdkBlock, err := convertBlock(protoBlock, s.clientCtx.ConsensusAddressCodec)
sdkBlock, err := convertBlock(protoBlock, s.consensusCodec)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +88,7 @@ func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockReques

// GetBlockByHeight implements ServiceServer.GetBlockByHeight
func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeightRequest) (*GetBlockByHeightResponse, error) {
blockHeight, err := getBlockHeight(ctx, s.clientCtx)
blockHeight, err := getBlockHeight(ctx, s.rpc)
if err != nil {
return nil, err
}
Expand All @@ -95,12 +97,12 @@ func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeight
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}

protoBlockID, protoBlock, err := GetProtoBlock(ctx, s.clientCtx, &req.Height)
protoBlockID, protoBlock, err := GetProtoBlock(ctx, s.rpc, &req.Height)
if err != nil {
return nil, err
}

sdkBlock, err := convertBlock(protoBlock, s.clientCtx.ConsensusAddressCodec)
sdkBlock, err := convertBlock(protoBlock, s.consensusCodec)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +121,7 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa
return nil, err
}

return ValidatorsOutput(ctx, s.clientCtx, nil, page, limit)
return ValidatorsOutput(ctx, s.rpc, s.consensusCodec, nil, page, limit)
}

func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
Expand All @@ -141,7 +143,7 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida
return nil, err
}

blockHeight, err := getBlockHeight(ctx, s.clientCtx)
blockHeight, err := getBlockHeight(ctx, s.rpc)
if err != nil {
return nil, status.Error(codes.Internal, "failed to parse chain height")
}
Expand All @@ -150,7 +152,7 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}

r, err := ValidatorsOutput(ctx, s.clientCtx, &req.Height, page, limit)
r, err := ValidatorsOutput(ctx, s.rpc, s.consensusCodec, &req.Height, page, limit)
if err != nil {
return nil, err
}
Expand All @@ -162,8 +164,8 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida
}, nil
}

func ValidatorsOutput(ctx context.Context, clientCtx client.Context, height *int64, page, limit int) (*GetLatestValidatorSetResponse, error) {
vs, err := getValidators(ctx, clientCtx, height, page, limit)
func ValidatorsOutput(ctx context.Context, rpc CometRPC, consCodec address.Codec, height *int64, page, limit int) (*GetLatestValidatorSetResponse, error) {
vs, err := getValidators(ctx, rpc, height, page, limit)
if err != nil {
return nil, err
}
Expand All @@ -186,7 +188,7 @@ func ValidatorsOutput(ctx context.Context, clientCtx client.Context, height *int
return nil, err
}

addr, err := clientCtx.ConsensusAddressCodec.BytesToString(v.Address)
addr, err := consCodec.BytesToString(v.Address)
if err != nil {
return nil, err
}
Expand All @@ -204,7 +206,7 @@ func ValidatorsOutput(ctx context.Context, clientCtx client.Context, height *int

// GetNodeInfo implements ServiceServer.GetNodeInfo
func (s queryServer) GetNodeInfo(ctx context.Context, _ *GetNodeInfoRequest) (*GetNodeInfoResponse, error) {
status, err := GetNodeStatus(ctx, s.clientCtx)
status, err := GetNodeStatus(ctx, s.rpc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -285,7 +287,11 @@ func RegisterTendermintService(
iRegistry codectypes.InterfaceRegistry,
queryFn abciQueryFn,
) {
RegisterServiceServer(server, NewQueryServer(clientCtx, iRegistry, queryFn))
node, err := clientCtx.GetNode()
if err != nil {
panic(err)
}
RegisterServiceServer(server, NewQueryServer(node, queryFn, clientCtx.ConsensusAddressCodec))
}

// RegisterGRPCGatewayRoutes mounts the CometBFT service's GRPC-gateway routes on the
Expand Down
10 changes: 2 additions & 8 deletions client/grpc/cmtservice/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import (
"context"

coretypes "github.com/cometbft/cometbft/rpc/core/types"

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

// GetNodeStatus returns the status of the node.
func GetNodeStatus(ctx context.Context, clientCtx client.Context) (*coretypes.ResultStatus, error) {
node, err := clientCtx.GetNode()
if err != nil {
return &coretypes.ResultStatus{}, err
}
return node.Status(ctx)
func GetNodeStatus(ctx context.Context, rpc CometRPC) (*coretypes.ResultStatus, error) {
return rpc.Status(ctx)
}
10 changes: 2 additions & 8 deletions client/grpc/cmtservice/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import (
"context"

coretypes "github.com/cometbft/cometbft/rpc/core/types"

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

func getValidators(ctx context.Context, clientCtx client.Context, height *int64, page, limit int) (*coretypes.ResultValidators, error) {
node, err := clientCtx.GetNode()
if err != nil {
return nil, err
}
return node.Validators(ctx, height, &page, &limit)
func getValidators(ctx context.Context, rpc CometRPC, height *int64, page, limit int) (*coretypes.ResultValidators, error) {
return rpc.Validators(ctx, height, &page, &limit)
}
8 changes: 5 additions & 3 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
"github.com/cosmos/cosmos-sdk/client/rpc"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -41,8 +40,11 @@ func StatusCommand() *cobra.Command {
if err != nil {
return err
}

status, err := cmtservice.GetNodeStatus(context.Background(), clientCtx)
node, err := clientCtx.GetNode()
if err != nil {
return err
}
status, err := node.Status(context.Background())
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c0e84af

Please sign in to comment.