-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gRPC server & reflection #6463
Merged
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
8dd91e3
Add gRPC proxy
aaronc 1e4d01f
Merge branch 'master' into aaronc/5921-grpc-proxy
aaronc ba6ded6
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc 20fd64c
Make GRPC disabled by default
aaronc 2ab5f5d
WIP on integration tests
aaronc 70e5042
WIP on integration tests
aaronc 4771a12
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc 021591f
Start setting up in process tests
aaronc 3176fb4
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc f2671c7
Start setting up in process tests
aaronc 7254946
Make it compile
amaury1093 5834ca6
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 64554c4
Add start server to network util
amaury1093 928edef
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 b7dc4a8
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 973ff20
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 d0c5291
Add Println
amaury1093 978a26f
Use go routine
amaury1093 c93b5e3
Fix scopelint
amaury1093 71c8139
Move to proxy_test
amaury1093 42afb10
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 aaa1ac7
Add response type cache
aaronc b4421f1
Remove proxy
amaury1093 9072a42
Tweaks
amaury1093 59d2fa7
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 f15c036
Use channel to handle error
amaury1093 ff837e8
Use error chan
amaury1093 4080396
Update server/start.go
amaury1093 670c44e
Use %w
amaury1093 c5f850f
Merge branch 'aaronc/5921-grpc-proxy' of ssh://github.com/cosmos/cosm…
amaury1093 323e1c2
Add sdk.Context
amaury1093 8825e99
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 9058b6d
Add comments
amaury1093 0411977
Fix lint
amaury1093 0c90239
Add header and tests
amaury1093 8f15a05
Address comments
amaury1093 f533116
Factorize some code
amaury1093 72db921
Fix lint
amaury1093 d93803b
Add height and prove in req metadata
amaury1093 dbe951d
Merge branch 'master' into aaronc/5921-grpc-proxy
amaury1093 a37c83c
Add reflection test
amaury1093 8513b9d
Fix lint
amaury1093 4f659ce
Put grpc test in server/grpc
amaury1093 ae797c0
Update baseapp/grpcserver.go
alexanderbez 180da4a
Update baseapp/grpcserver.go
alexanderbez b16cbd4
Remove proof header
amaury1093 692b032
Merge branch 'master' into aaronc/5921-grpc-proxy
alexanderbez d6afde0
Merge branch 'master' into aaronc/5921-grpc-proxy
sahith-narahari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package baseapp | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
gogogrpc "github.com/gogo/protobuf/grpc" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
|
||
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// 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(servergrpc.GRPCBlockHeightHeader); len(heightHeaders) > 0 { | ||
height, err = strconv.ParseInt(heightHeaders[0], 10, 64) | ||
if 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something for TODO? |
||
sdkCtx, err := app.createQueryContext(height, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Attach the sdk.Context into the gRPC's context.Context. | ||
grpcCtx = context.WithValue(grpcCtx, sdk.SdkContextKey, sdkCtx) | ||
|
||
// Add relevant gRPC headers | ||
if height == 0 { | ||
height = sdkCtx.BlockHeight() // If height was not set in the request, set it to the latest | ||
} | ||
md = metadata.Pairs(servergrpc.GRPCBlockHeightHeader, strconv.FormatInt(height, 10)) | ||
grpc.SetHeader(grpcCtx, md) | ||
|
||
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, interceptor) | ||
}, | ||
} | ||
} | ||
|
||
newDesc := &grpc.ServiceDesc{ | ||
ServiceName: desc.ServiceName, | ||
HandlerType: desc.HandlerType, | ||
Methods: newMethods, | ||
Streams: desc.Streams, | ||
Metadata: desc.Metadata, | ||
} | ||
|
||
server.RegisterService(newDesc, data.handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package grpc | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
|
||
"github.com/cosmos/cosmos-sdk/server/types" | ||
) | ||
|
||
const ( | ||
// GRPCBlockHeightHeader is the gRPC header for block height. | ||
GRPCBlockHeightHeader = "x-cosmos-block-height" | ||
) | ||
|
||
// StartGRPCServer starts a gRPC server on the given address. | ||
func StartGRPCServer(app types.Application, address string) (*grpc.Server, error) { | ||
grpcSrv := grpc.NewServer() | ||
app.RegisterGRPCServer(grpcSrv) | ||
|
||
// Reflection allows external clients to see what services and methods | ||
// the gRPC server exposes. | ||
reflection.Register(grpcSrv) | ||
amaury1093 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
listener, err := net.Listen("tcp", address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errCh := make(chan error) | ||
go func() { | ||
err = grpcSrv.Serve(listener) | ||
if err != nil { | ||
errCh <- fmt.Errorf("failed to serve: %w", err) | ||
} | ||
}() | ||
|
||
select { | ||
case err := <-errCh: | ||
return nil, err | ||
case <-time.After(5 * time.Second): // assume server started successfully | ||
return grpcSrv, nil | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.