Skip to content

Commit

Permalink
drop duplicate registrations of proto
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Sep 11, 2024
1 parent 9dd6a99 commit b31abf8
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 8,016 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)
}
48 changes: 26 additions & 22 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmtservice
import (
"context"

"cosmossdk.io/core/address"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
gogogrpc "github.com/cosmos/gogoproto/grpc"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
Expand All @@ -28,28 +29,27 @@ 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,
}
}

// 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 +61,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 +72,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 +86,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 +95,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 +119,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 +141,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 +150,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 +162,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 +186,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 +204,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 +285,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
11 changes: 3 additions & 8 deletions client/grpc/cmtservice/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ 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)
}
7 changes: 5 additions & 2 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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 := cmtservice.GetNodeStatus(context.Background(), node)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b31abf8

Please sign in to comment.