-
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
feat: wire new handlers to grpc #22333
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8d2dd2a
initial commit
1f1dd1b
fix service
56a87f4
add list query handlers
d959c05
update service and refactors
1f9ff63
update service and refactors
f08976a
lint
76a4019
change cosmossdk.io proto buf package route
e4bf954
return error on registering v2 service
6863035
remove cosmos sdk replacement
5248508
Merge remote-tracking branch 'origin/main' into feat/grpc-v2
466e937
include core
72a7f3c
lint and proto
d520f90
not needed imports
c1646fd
lint
c312f13
Merge branch 'main' into feat/grpc-v2
julienrbrt af319d3
fixes
julienrbrt 31419b2
go mod tidy
julienrbrt 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
syntax = "proto3"; | ||
package cosmos.base.grpc.v2; | ||
|
||
import "google/protobuf/any.proto"; | ||
|
||
option go_package = "cosmossdk.io/server/v2/api/grpc"; | ||
|
||
// Service defines the gRPC service for query server for v2 | ||
service Service { | ||
// Query queries the server with a request, the request can be any sdk Msg. | ||
rpc Query(QueryRequest) returns (QueryResponse) {} | ||
|
||
// ListQueryHandlers lists all the available query handlers. | ||
rpc ListQueryHandlers(ListQueryHandlersRequest) returns (ListQueryHandlersResponse) {} | ||
} | ||
|
||
// QueryRequest is the request for the Query method | ||
message QueryRequest { | ||
google.protobuf.Any request = 1; | ||
} | ||
|
||
// QueryResponse is the response for the Query method | ||
message QueryResponse { | ||
google.protobuf.Any response = 1; | ||
} | ||
|
||
// ListQueryHandlersRequest is the request for the ListQueryHandlers method | ||
message ListQueryHandlersRequest {} | ||
|
||
// ListQueryHandlersResponse is the response for the ListQueryHandlers method | ||
message ListQueryHandlersResponse { | ||
repeated Handler handlers = 1; | ||
} | ||
|
||
// Handler defines a query handler | ||
message Handler { | ||
string request_name = 1; | ||
string response_name = 2; | ||
} |
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,73 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/gogoproto/proto" | ||
gogoproto "github.com/cosmos/gogoproto/types/any" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
appmodulev2 "cosmossdk.io/core/appmodule/v2" | ||
"cosmossdk.io/core/transaction" | ||
) | ||
|
||
// v2Service implements the gRPC service interface for handling queries and listing handlers. | ||
type v2Service struct { | ||
queryHandlers map[string]appmodulev2.Handler | ||
queryable interface { | ||
Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error) | ||
} | ||
} | ||
|
||
// Query handles incoming query requests by unmarshaling the request, processing it, | ||
// and returning the response in an Any protobuf message. | ||
func (s v2Service) Query(ctx context.Context, request *QueryRequest) (*QueryResponse, error) { | ||
if request == nil || request.Request == nil { | ||
return nil, status.Error(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
msgName := request.Request.TypeUrl | ||
|
||
handler, exists := s.queryHandlers[msgName] | ||
if !exists { | ||
return nil, status.Errorf(codes.NotFound, "handler not found for %s", msgName) | ||
} | ||
|
||
protoMsg := handler.MakeMsg() | ||
if err := proto.Unmarshal(request.Request.Value, protoMsg); err != nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "failed to unmarshal request: %v", err) | ||
} | ||
|
||
queryResp, err := s.queryable.Query(ctx, 0, protoMsg) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "query failed: %v", err) | ||
} | ||
|
||
respBytes, err := proto.Marshal(queryResp) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to marshal response: %v", err) | ||
} | ||
|
||
anyResp := &gogoproto.Any{ | ||
TypeUrl: "/" + proto.MessageName(queryResp), | ||
Value: respBytes, | ||
} | ||
|
||
return &QueryResponse{Response: anyResp}, nil | ||
} | ||
|
||
func (s v2Service) ListQueryHandlers(_ context.Context, _ *ListQueryHandlersRequest) (*ListQueryHandlersResponse, error) { | ||
var handlerDescriptors []*Handler | ||
for handlerName := range s.queryHandlers { | ||
msg := s.queryHandlers[handlerName].MakeMsg() | ||
resp := s.queryHandlers[handlerName].MakeMsgResp() | ||
|
||
handlerDescriptors = append(handlerDescriptors, &Handler{ | ||
RequestName: proto.MessageName(msg), | ||
ResponseName: proto.MessageName(resp), | ||
}) | ||
} | ||
Comment on lines
+62
to
+70
Check warning Code scanning / CodeQL Iteration over map Warning
Iteration over map may be a possible source of non-determinism
|
||
|
||
return &ListQueryHandlersResponse{Handlers: handlerDescriptors}, nil | ||
} |
Oops, something went wrong.
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.
Avoid exposing internal error details to clients
Returning detailed error messages can leak internal information and pose security risks. Consider returning generic error messages to the client and logging the detailed errors internally.
Apply this diff to adjust the error messages:
Also applies to: 52-53, 57-58