Skip to content
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(server/v2)!: grpcgateway autoregistration #22941

Merged
merged 29 commits into from
Jan 6, 2025

Conversation

technicallyty
Copy link
Contributor

@technicallyty technicallyty commented Dec 18, 2024

Description

Closes: #22715

This PR is marked breaking as the grpcgateway.New function signature has changed.

This PR implements a http.Handler that routes queries from grpc-gateway requests to the application manager's query router.

It does so by using an internal mapping of http get annotations to query input type names, built from the global gogoproto registry.

The interceptor handles GET requests with wildcard queries (foo/bar/{baz}), catch-all wildcards (foo/bar/{baz=**}, query parameters (foo/bar?baz=hi), and query parameters with nested struct setting (foo/bar?pagination.limit=5), as well as POST requests with JSON bodies. Please let me know if there are other cases i am missing.


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 in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced a custom HTTP handler for the gRPC gateway, simplifying service registration.
    • Added functionality for URI matching and message creation in a gRPC context.
  • Bug Fixes

    • Enhanced error messages for invalid height parameters in gRPC queries.
  • Tests

    • Implemented comprehensive unit tests for URI matching and message handling.
  • Chores

    • Updated module dependencies and cleaned up unnecessary imports.
  • Documentation

    • Updated changelog to reflect new features and improvements.

Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces a comprehensive enhancement to the gRPC gateway functionality in the Cosmos SDK server package. The changes focus on creating a more flexible and automated approach to routing gRPC gateway queries. A new gatewayInterceptor is implemented to handle HTTP requests, with improved mechanisms for matching URLs, creating messages, and routing queries through an application manager. The modifications aim to eliminate manual gRPC gateway registration and provide a more streamlined method for handling HTTP-to-gRPC request translations.

Changes

File Change Summary
server/v2/api/grpcgateway/interceptor.go New file introducing gatewayInterceptor struct for routing gRPC gateway queries
server/v2/api/grpcgateway/server.go Updated server creation method, added GRPCBlockHeightHeader constant
server/v2/api/grpcgateway/uri.go New file with URI matching and message creation functionality
server/v2/api/grpcgateway/uri_test.go Comprehensive test suite for URI matching and message creation
server/v2/go.mod Updated dependencies, added google.golang.org/genproto/googleapis/api
simapp/v2/simdv2/cmd/commands.go Simplified gRPC gateway routes registration
tests/systemtests/distribution_test.go Improved error message handling for distribution module
server/v2/CHANGELOG.md Added entry for new custom HTTP handler for grpc-gateway

Assessment against linked issues

Objective Addressed Explanation
Remove manual gRPC gateway registration
Reduce module dependencies
Simplify gRPC gateway service registration

Possibly related PRs

Suggested labels

backport/v0.52.x

Suggested reviewers

  • kocubinski
  • sontrinh16
  • julienrbrt
  • tac0turtle
  • hieuvubk

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added C:x/authz C:server/v2 Issues related to server/v2 C:server/v2 api labels Dec 18, 2024
@technicallyty technicallyty changed the title feat(server/v2): grpcgateway autoregistration feat(server/v2)!: grpcgateway autoregistration Dec 18, 2024
@technicallyty
Copy link
Contributor Author

@julienrbrt any suggestion on how to test this? i was hoping there were tests i could pattern match from the rest server, but looks like there are none. should i write a systemtest? i did some manual testing of a few endpoints and found success.

"fmt"
"io"
"net/http"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
Comment on lines 47 to 67
for getPattern, queryInputName := range getPatternToQueryInputName {
getPattern = strings.TrimRight(getPattern, "/")

regexPattern, wildcardNames := patternToRegex(getPattern)

regex := regexp.MustCompile(regexPattern)
matches := regex.FindStringSubmatch(uri)

if matches != nil && len(matches) > 1 {
// first match is the full string, subsequent matches are capture groups
params := make(map[string]string)
for i, name := range wildcardNames {
params[name] = matches[i+1]
}

return &uriMatch{
QueryInputName: queryInputName,
Params: params,
}
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
server/v2/api/grpcgateway/uri.go Fixed Show fixed Hide fixed
@julienrbrt
Copy link
Member

julienrbrt commented Dec 18, 2024

@julienrbrt any suggestion on how to test this? i was hoping there were tests i could pattern match from the rest server, but looks like there are none. should i write a systemtest? i did some manual testing of a few endpoints and found success.

As you disabled the manual registration in simapp/v2, this is already tested. We have gRPC gateway system tests, and if they are passing, this means it is working 👌🏾
The system tests obviously do not test every single endpoint. So if they are some you'd like to add to be sure feel free to add, but the ones we have should already give some good confidence that the most used endpoints are working.


for _, mod := range deps.ModuleManager.Modules() {
if gmod, ok := mod.(module.HasGRPCGateway); ok {
gmod.RegisterGRPCGatewayRoutes(deps.ClientContext, server.GRPCGatewayRouter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice follow-up of this is to start de-wire grpc gateway from module on main, by removing

version: v1
plugins:
  - name: gocosmos
    out: ..
    opt: plugins=grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any
-  - name: grpc-gateway
-    out: ..
-    opt: logtostderr=true,allow_colon_final_segments=true

And remove all generated *.gw.go from modules and RegisterGRPCGatewayRoutes.
It will need to wait 1. of #22904 but that's very soon.

Comment on lines +151 to +168
for key, value := range match.Params {
parts := strings.Split(key, ".")
current := nestedParams

// step through nested levels
for i, part := range parts {
if i == len(parts)-1 {
// Last part - set the value
current[part] = value
} else {
// continue nestedness
if _, exists := current[part]; !exists {
current[part] = make(map[string]any)
}
current = current[part].(map[string]any)
}
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
query, err := g.appManager.Query(request.Context(), height, msg)
if err != nil {
// if we couldn't find a handler for this request, just fall back to the gateway mux.
if errors.Is(err, stf.ErrNoHandler) {
Copy link
Contributor Author

@technicallyty technicallyty Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to have stf package imported. I can just do something like strings.Contains(err.Error(), "no handler") instead if this is not good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's not import stf.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
server/v2/api/grpcgateway/interceptor.go (3)

39-50: Validate error handling for getHTTPGetAnnotationMapping.
The function gracefully returns an error if the annotation mapping retrieval fails. This approach is good, but ensure calling code logs relevant context for debugging.


83-92: Check user-supplied block height.
Great validation of user-supplied block height. Make sure to handle negative or extremely large values gracefully.


94-104: Brace for potential error types.
Falling back to the gateway mux when "no handler" is found is appropriate. For more robust error handling, consider typed errors rather than matching strings.

server/v2/api/grpcgateway/server.go (1)

79-83: Interceptor creation is well-placed.
This intercepts all requests via mux.Handle and ensures a single source of truth for custom routing.

Consider adding more explanatory logs so operators can distinguish fallback vs. custom interception in logs.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e3551f0 and 176726b.

📒 Files selected for processing (4)
  • server/v2/CHANGELOG.md (1 hunks)
  • server/v2/api/grpcgateway/interceptor.go (1 hunks)
  • server/v2/api/grpcgateway/server.go (4 hunks)
  • server/v2/go.mod (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/v2/go.mod
🧰 Additional context used
📓 Path-based instructions (3)
server/v2/CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

server/v2/api/grpcgateway/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/interceptor.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (8)
server/v2/api/grpcgateway/interceptor.go (4)

23-36: Clearly structured gatewayInterceptor.
This struct is well-organized, showing clear responsibilities for logging, gateway fallback, and routing. The doc comment also clarifies how queries are routed via the app manager.


52-62: Fallback to gateway mux is logical.
Falling back to the gateway mux for unrecognized endpoints keeps default gRPC-gateway behavior. Good design.


68-76: Consider support for other HTTP methods if required.
Currently, only POST and GET are handled. It might be worth confirming if other methods (PUT, PATCH, DELETE) are needed now or in the future.


109-145: Efficient scanning for custom GET annotations.
The approach to gather HTTP GET annotations from the merged proto registry is efficient. Remember to handle potential duplicates in the final map (should not happen in valid proto, but still a possibility).

server/v2/CHANGELOG.md (1)

25-28: Changelog entry is concise and follows guidelines.
It references GitHub issue #22715 and clearly states the new feature. This is consistent with the recommended changelog structure.

server/v2/api/grpcgateway/server.go (3)

17-17: AppManager import suits new functionality.
Importing "cosmossdk.io/server/v2/appmanager" aligns with the new gateway logic.


Line range hint 41-50: Public New function is well-documented.
The new signature adding appManager is a logical extension, but ensure downstream calls handle any potential nil appManager references.


140-141: Constant definition clarifies usage.
Defining GRPCBlockHeightHeader in one place is a good improvement for maintainability and consistency.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
server/v2/api/grpcgateway/uri_test.go (2)

15-92: Consider adding edge cases to TestMatchURI

While the test cases are comprehensive, consider adding these scenarios:

  1. Empty URI path
  2. URI with special characters in parameters (e.g., URL-encoded values)
  3. URI with multiple catch-all wildcards (to verify proper handling of invalid patterns)
 func TestMatchURI(t *testing.T) {
     testCases := []struct {
         name     string
         uri      string
         mapping  map[string]string
         expected *uriMatch
     }{
+        {
+            name:     "empty path",
+            uri:      "https://localhost:8080",
+            mapping:  map[string]string{"/": "query.Root"},
+            expected: &uriMatch{QueryInputName: "query.Root", Params: map[string]string{}},
+        },
+        {
+            name:     "special characters in parameters",
+            uri:      "https://localhost:8080/foo/bar?q=hello%20world&type=%2Fbank%2Fsend",
+            mapping:  map[string]string{"/foo/bar": "query.Bank"},
+            expected: &uriMatch{QueryInputName: "query.Bank", Params: map[string]string{"q": "hello world", "type": "/bank/send"}},
+        },
+        {
+            name:     "multiple catch-all wildcards should not match",
+            uri:      "https://localhost:8080/foo/bar/baz",
+            mapping:  map[string]string{"/foo/{bar=**}/{baz=**}": "query.Invalid"},
+            expected: nil,
+        },

94-100: Add nil map test case to TestURIMatch_HasParams

Add a test case for nil params map to ensure robust handling.

 func TestURIMatch_HasParams(t *testing.T) {
     u := uriMatch{Params: map[string]string{"foo": "bar"}}
     require.True(t, u.HasParams())

     u = uriMatch{}
     require.False(t, u.HasParams())
+
+    u = uriMatch{Params: nil}
+    require.False(t, u.HasParams())
 }
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 176726b and 10d6778.

📒 Files selected for processing (1)
  • server/v2/api/grpcgateway/uri_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/api/grpcgateway/uri_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

🔇 Additional comments (4)
server/v2/api/grpcgateway/uri_test.go (4)

1-13: LGTM! Clean imports organization

The imports are well-organized with standard library imports separated from third-party imports.


102-125: Add cleanup for registered proto type

The test registers a type with gogoproto but doesn't clean it up, which could affect other tests.


126-206: Add cleanup and enhance error test cases

  1. Add cleanup for registered proto type
  2. Consider adding these error cases:
    • Invalid nested field path
    • Non-existent field
    • Invalid field type conversion for nested fields
 func TestCreateMessage(t *testing.T) {
+    t.Cleanup(func() {
+        gogoproto.RegisterType(nil, dummyProtoName)
+    })
     gogoproto.RegisterType(&DummyProto{}, dummyProtoName)
     
     testCases := []struct {
         // ... existing test cases ...
+        {
+            name: "invalid nested field path",
+            uri: uriMatch{
+                QueryInputName: dummyProtoName,
+                Params: map[string]string{"page..nest.foo": "5"},
+            },
+            expErr: true,
+        },
+        {
+            name: "non-existent field",
+            uri: uriMatch{
+                QueryInputName: dummyProtoName,
+                Params: map[string]string{"nonexistent": "value"},
+            },
+            expErr: true,
+        },
+        {
+            name: "invalid nested field type",
+            uri: uriMatch{
+                QueryInputName: dummyProtoName,
+                Params: map[string]string{"page.nest.foo": "not_a_number"},
+            },
+            expErr: true,
+        },

208-264: Enhance TestCreateMessageFromJson with additional cases

  1. Add cleanup for registered proto type
  2. Add test cases for:
    • Request body read error
    • Empty request body
    • Invalid field types in JSON
 func TestCreateMessageFromJson(t *testing.T) {
+    t.Cleanup(func() {
+        gogoproto.RegisterType(nil, dummyProtoName)
+    })
     gogoproto.RegisterType(&DummyProto{}, dummyProtoName)
     
     testCases := []struct {
         // ... existing test cases ...
+        {
+            name: "empty request body",
+            uri:  uriMatch{QueryInputName: dummyProtoName},
+            request: func() *http.Request {
+                return &http.Request{Body: io.NopCloser(bytes.NewReader(nil))}
+            },
+            expErr: true,
+        },
+        {
+            name: "invalid field types",
+            uri:  uriMatch{QueryInputName: dummyProtoName},
+            request: func() *http.Request {
+                return &http.Request{Body: io.NopCloser(bytes.NewReader([]byte(`{"baz":"not_a_number"}`)))}
+            },
+            expErr: true,
+        },

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👏

I think we should add a doc.go file for this package and explain what syntax it supports (only gets for instance).

Copy link
Contributor

@hieuvubk hieuvubk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@tac0turtle
Copy link
Member

we should create an issue with the follow ups so they dont get lost

@julienrbrt julienrbrt added this pull request to the merge queue Jan 6, 2025
Merged via the queue into main with commit 884a7a5 Jan 6, 2025
75 of 76 checks passed
@julienrbrt julienrbrt deleted the technicallyty/22715-grpc-auto-registration branch January 6, 2025 08:24
@julienrbrt
Copy link
Member

we should create an issue with the follow ups so they dont get lost

Created #23188

@julienrbrt
Copy link
Member

Calling mergify to do a partial backport.

@julienrbrt
Copy link
Member

@Mergifyio backport release/v0.52.x

Copy link
Contributor

mergify bot commented Jan 6, 2025

backport release/v0.52.x

✅ Backports have been created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 api C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

grpc-gateway: do no require manual grpc gateway registration
5 participants