## Description Implements part of #7258 Check some of currently unchecked errors. - [x] baseapp - [x] client - [x] codec - [x] crypto - [x] server - [x] simapp - [ ] snapshots - [ ] store - [x] testutil - [ ] types - [ ] modules --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
101 lines
3.4 KiB
Go
101 lines
3.4 KiB
Go
package baseapp
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
gogogrpc "github.com/gogo/protobuf/grpc"
|
|
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
|
)
|
|
|
|
// GRPCQueryRouter returns the GRPCQueryRouter of a BaseApp.
|
|
func (app *BaseApp) GRPCQueryRouter() *GRPCQueryRouter { return app.grpcQueryRouter }
|
|
|
|
// RegisterGRPCServer registers gRPC services directly with the gRPC server.
|
|
func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
|
|
// Define an interceptor for all gRPC queries: this interceptor will create
|
|
// a new sdk.Context, and pass it into the query handler.
|
|
interceptor := func(grpcCtx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
// If there's some metadata in the context, retrieve it.
|
|
md, ok := metadata.FromIncomingContext(grpcCtx)
|
|
if !ok {
|
|
return nil, status.Error(codes.Internal, "unable to retrieve metadata")
|
|
}
|
|
|
|
// Get height header from the request context, if present.
|
|
var height int64
|
|
if heightHeaders := md.Get(grpctypes.GRPCBlockHeightHeader); len(heightHeaders) == 1 {
|
|
height, err = strconv.ParseInt(heightHeaders[0], 10, 64)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrapf(
|
|
sdkerrors.ErrInvalidRequest,
|
|
"Baseapp.RegisterGRPCServer: invalid height header %q: %v", grpctypes.GRPCBlockHeightHeader, err)
|
|
}
|
|
if err := checkNegativeHeight(height); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Create the sdk.Context. Passing false as 2nd arg, as we can't
|
|
// actually support proofs with gRPC right now.
|
|
sdkCtx, err := app.createQueryContext(height, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Add relevant gRPC headers
|
|
if height == 0 {
|
|
height = sdkCtx.BlockHeight() // If height was not set in the request, set it to the latest
|
|
}
|
|
|
|
// Attach the sdk.Context into the gRPC's context.Context.
|
|
grpcCtx = context.WithValue(grpcCtx, sdk.SdkContextKey, sdkCtx)
|
|
|
|
md = metadata.Pairs(grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(height, 10))
|
|
if err = grpc.SetHeader(grpcCtx, md); err != nil {
|
|
app.logger.Error("failed to set gRPC header", "err", err)
|
|
}
|
|
|
|
return handler(grpcCtx, req)
|
|
}
|
|
|
|
// Loop through all services and methods, add the interceptor, and register
|
|
// the service.
|
|
for _, data := range app.GRPCQueryRouter().serviceData {
|
|
desc := data.serviceDesc
|
|
newMethods := make([]grpc.MethodDesc, len(desc.Methods))
|
|
|
|
for i, method := range desc.Methods {
|
|
methodHandler := method.Handler
|
|
newMethods[i] = grpc.MethodDesc{
|
|
MethodName: method.MethodName,
|
|
Handler: func(srv interface{}, ctx context.Context, dec func(interface{}) error, _ grpc.UnaryServerInterceptor) (interface{}, error) {
|
|
return methodHandler(srv, ctx, dec, grpcmiddleware.ChainUnaryServer(
|
|
grpcrecovery.UnaryServerInterceptor(),
|
|
interceptor,
|
|
))
|
|
},
|
|
}
|
|
}
|
|
|
|
newDesc := &grpc.ServiceDesc{
|
|
ServiceName: desc.ServiceName,
|
|
HandlerType: desc.HandlerType,
|
|
Methods: newMethods,
|
|
Streams: desc.Streams,
|
|
Metadata: desc.Metadata,
|
|
}
|
|
|
|
server.RegisterService(newDesc, data.handler)
|
|
}
|
|
}
|