chore: remove duplicate proto files for the same proto file (#21648)
This commit is contained in:
@@ -5,12 +5,10 @@ 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
|
||||
}
|
||||
@@ -18,18 +16,12 @@ func getBlockHeight(ctx context.Context, clientCtx client.Context) (int64, error
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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"
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user