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

fix(serverv2/cometbft): properly decode and route gRPC transactions #20808

Merged
merged 7 commits into from
Jul 16, 2024

Conversation

testinginprod
Copy link
Contributor

@testinginprod testinginprod commented Jun 28, 2024

Description

This PR ensures that cometBFT can properly decode gRPC queries of modules by giving comet context on the existence of gRPC queries.


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 GRPC query decoding functionality to enhance request handling.
  • Dependency Updates

    • Upgraded cosmossdk.io/client/v2 to the latest version.
    • Added cosmossdk.io/tools/confix for improved configuration management.
    • Reintroduced github.com/cosmos/gogoproto with updated version and direct dependency notation.

Copy link
Contributor

coderabbitai bot commented Jun 28, 2024

Walkthrough

Walkthrough

The changes introduce the gogoproto package for protocol buffer message handling and incorporate a new GRPCQueryDecoders map to the App struct for decoding gRPC queries. Adjustments were made to the server initialization and dependency management to support these additions.

Changes

File Change Summary
runtime/v2/app.go Added gogoproto import and GRPCQueryDecoders map to decode request bytes into messages.
server/v2/cometbft/server.go Modified NewConsensus function call to include appI.GetGRPCQueryDecoders().
server/v2/types.go Added gogoproto import and GetGRPCQueryDecoders() method.
simapp/v2/go.mod Updated dependencies, including cosmossdk.io and github.com/cosmos/gogoproto.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.


// GRPCQueryDecoders maps gRPC method name to a function that decodes the request
// bytes into a gogoproto.Message, which then can be passed to appmanager.
GRPCQueryDecoders map[string]func(requestBytes []byte) (gogoproto.Message, error)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are registereed from modules exposing gRPC queries and map gRPC method=>something that decodes the req.

@@ -30,6 +31,9 @@ import (
var _ abci.Application = (*Consensus[transaction.Tx])(nil)

type Consensus[T transaction.Tx] struct {
// legacy support for gRPC
grpcQueryDecoders map[string]func(requestBytes []byte) (gogoproto.Message, error)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -5,6 +5,7 @@
"encoding/json"
"errors"
"fmt"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
return fmt.Errorf("unable to find message in gogotype registry: %w", err)
}
decoderFunc := func(bytes []byte) (gogoproto.Message, error) {
msg := reflect.New(typ.Elem()).Interface().(gogoproto.Message)
Copy link
Member

Choose a reason for hiding this comment

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

are we OK with reflection on the query path? it will be a reflect call on each query. I'm not sure of the performance overheard, should we measure? are there any security concerns? This would mean that v2 doesn't use the generated grpc router code at all, right?

alternatives include:

  • modifying codegen to produce some decoding like what's happening in v1
  • find a way to for this routing layer to re-use the grpc generated code

Copy link
Member

Choose a reason for hiding this comment

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

with the de design of handlers ideally we can move away from using the grpc generated code. I dont think its worth going and changing the generator as well.

@tac0turtle
Copy link
Member

is this still needed?

@testinginprod testinginprod marked this pull request as ready for review July 15, 2024 08:18
@testinginprod testinginprod requested a review from a team as a code owner July 15, 2024 08:18
@testinginprod testinginprod requested a review from julienrbrt July 15, 2024 08:18
Copy link
Contributor

@testinginprod your pull request is missing a changelog!

Comment on lines 262 to 264
func (app *SimApp[T]) GetGRPCQueryDecoders() map[string]func(requestBytes []byte) (gogoproto.Message, error) {
return app.GRPCQueryDecoders
}
Copy link
Member

Choose a reason for hiding this comment

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

anyway to avoid this? seems the value is public so we should be able to get it directly

Copy link
Member

Choose a reason for hiding this comment

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

let's have runtime expose it instead of simapp indeed

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: 2

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ab6400f and 038c17f.

Files selected for processing (7)
  • runtime/v2/app.go (2 hunks)
  • runtime/v2/manager.go (4 hunks)
  • server/v2/cometbft/abci.go (6 hunks)
  • server/v2/cometbft/server.go (1 hunks)
  • server/v2/types.go (2 hunks)
  • simapp/v2/app_di.go (3 hunks)
  • simapp/v2/go.mod (1 hunks)
Files skipped from review due to trivial changes (1)
  • simapp/v2/go.mod
Additional context used
Path-based instructions (6)
server/v2/types.go (1)

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

runtime/v2/app.go (1)

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

server/v2/cometbft/server.go (1)

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

simapp/v2/app_di.go (1)

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

server/v2/cometbft/abci.go (1)

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

runtime/v2/manager.go (1)

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

GitHub Check: CodeQL
runtime/v2/manager.go

[notice] 8-8: Sensitive package import
Certain system packages contain functions which may be a possible source of non-determinism

Additional comments not posted (12)
server/v2/types.go (2)

19-19: Approved: Addition of GetGRPCQueryDecoders method.

The method GetGRPCQueryDecoders is correctly added to the AppI interface. Ensure its implementation in the respective structs.


20-20: Approved: Addition of GetStore method.

The method GetStore is correctly added to the AppI interface. Ensure its implementation in the respective structs.

runtime/v2/app.go (1)

70-72: Approved: Addition of GRPCQueryDecoders field.

The GRPCQueryDecoders field is correctly added to the App struct. Ensure its initialization and usage in the respective methods.

server/v2/cometbft/server.go (1)

65-65: Approved: Modification of NewConsensus function call.

The NewConsensus function call is correctly modified to include appI.GetGRPCQueryDecoders(). Ensure the grpcQueryDecoders map is properly initialized and used.

simapp/v2/app_di.go (3)

229-230: Approved: Modification of NewSimApp function call.

The NewSimApp function call is correctly modified to include app.App.GetStore(). Ensure the GetStore method is properly implemented and used.


260-261: Approved: Addition of GetStore method.

The method GetStore is correctly added to the SimApp struct. Ensure its implementation in the respective structs.


262-264: Approved: Addition of GetGRPCQueryDecoders method.

The method GetGRPCQueryDecoders is correctly added to the SimApp struct. Ensure its implementation in the respective structs.

server/v2/cometbft/abci.go (2)

36-36: Well-Integrated Addition of grpcQueryDecoders Map

The addition of the grpcQueryDecoders map to the Consensus struct is well-integrated and follows the existing conventions.


63-84: Consistent Changes to NewConsensus Function

The changes to the NewConsensus function are consistent with the addition of the grpcQueryDecoders map, and the new parameter is correctly passed and initialized.

However, ensure that all calls to NewConsensus are updated to include the new parameter.

runtime/v2/manager.go (3)

559-560: Well-Integrated Addition of grpcQueryDecoders Map

The addition of the grpcQueryDecoders map to the configurator struct is well-integrated and follows the existing conventions.


570-571: Consistent Changes to registerServices Function

The changes to the registerServices function are consistent with the addition of the grpcQueryDecoders map, and the new map is correctly initialized and assigned.


659-665: Consistent Changes to registerMethod Function

The changes to the registerMethod function's return signature are consistent with the addition of error handling. The updated return signature appears to be correctly handled.

However, ensure that all calls to registerMethod are updated to handle the new return signature.

Verification successful

Consistent Changes to registerMethod Function

The changes to the registerMethod function's return signature are consistent with the addition of error handling. The updated return signature is correctly handled in the calling code.

  • Lines in runtime/v2/manager.go show proper error handling after calling registerMethod.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all calls to `registerMethod` handle the new return signature.

# Test: Search for the function calls. Expect: Only occurances of the new signature.
rg --type go -A 5 $'registerMethod('

Length of output: 118


Script:

#!/bin/bash
# Description: Verify that all calls to `registerMethod` handle the new return signature.

# Test: Search for the function calls. Expect: Only occurrences of the new signature.
rg --type go -A 5 'registerMethod('

Length of output: 118


Script:

#!/bin/bash
# Description: Verify that all calls to `registerMethod` handle the new return signature.

# Test: Search for the function calls. Expect: Only occurrences of the new signature.
rg --type go -A 5 'registerMethod\('

Length of output: 961

Comment on lines +167 to +176
func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (resp *abciproto.QueryResponse, err error) {
// check if it's a gRPC method
grpcQueryDecoder, isGRPC := c.grpcQueryDecoders[req.Path]
if isGRPC {
protoRequest, err := grpcQueryDecoder(req.Data)
if err != nil {
return nil, fmt.Errorf("unable to decode gRPC request with path %s from ABCI.Query: %w", req.Path, err)
}
res, err := c.app.Query(ctx, uint64(req.Height), protoRequest)

Copy link
Contributor

Choose a reason for hiding this comment

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

Robust Handling of gRPC Queries in Query Function

The changes to the Query function introduce a new code path for handling gRPC queries using the grpcQueryDecoders map. The error handling and logic appear robust and consistent with the existing code.

Consider adding unit tests to cover the new gRPC query handling code path.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Comment on lines +617 to +625
typ := gogoproto.MessageType(requestFullName)
if typ == nil {
return fmt.Errorf("unable to find message in gogotype registry: %w", err)
}
decoderFunc := func(bytes []byte) (gogoproto.Message, error) {
msg := reflect.New(typ.Elem()).Interface().(gogoproto.Message)
return msg, gogoproto.Unmarshal(bytes, msg)
}
c.grpcQueryDecoders[md.MethodName] = decoderFunc
Copy link
Contributor

Choose a reason for hiding this comment

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

Robust Handling of gRPC Query Decoders in registerQueryHandlers Function

The changes to the registerQueryHandlers function introduce a new code path for registering gRPC query decoders. The error handling and logic appear robust and consistent with the existing code.

Consider adding unit tests to cover the new gRPC query decoder registration code path.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

ACK, we should come back to this at a later point but we should get tests working so we can move the needle forward

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.

lgtm!, two nits

@@ -225,6 +226,8 @@ func NewSimApp[T transaction.Tx](
panic(err)
}

app.App.GetStore()
Copy link
Member

Choose a reason for hiding this comment

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

to delete

Comment on lines 262 to 264
func (app *SimApp[T]) GetGRPCQueryDecoders() map[string]func(requestBytes []byte) (gogoproto.Message, error) {
return app.GRPCQueryDecoders
}
Copy link
Member

Choose a reason for hiding this comment

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

let's have runtime expose it instead of simapp indeed

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 038c17f and 5f4d30e.

Files selected for processing (2)
  • runtime/v2/app.go (3 hunks)
  • simapp/v2/app_di.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • runtime/v2/app.go
  • simapp/v2/app_di.go

@tac0turtle tac0turtle enabled auto-merge July 16, 2024 09:57
@tac0turtle tac0turtle added this pull request to the merge queue Jul 16, 2024
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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5f4d30e and 0f8f6fc.

Files selected for processing (4)
  • runtime/v2/app.go (3 hunks)
  • server/v2/cometbft/server.go (1 hunks)
  • server/v2/types.go (2 hunks)
  • simapp/v2/go.mod (9 hunks)
Files skipped from review as they are similar to previous changes (3)
  • runtime/v2/app.go
  • server/v2/cometbft/server.go
  • server/v2/types.go
Additional comments not posted (3)
simapp/v2/go.mod (3)

7-7: Verify the version string for cosmossdk.io/client/v2.

The version string v2.0.0-00010101000000-000000000000 appears to be non-standard. Please confirm if this is a placeholder or if there's a specific reason for such formatting.


16-16: Assess the addition and version of cosmossdk.io/tools/confix.

A new dependency cosmossdk.io/tools/confix has been added with a placeholder version. Please confirm the necessity of this dependency and the reason for the specific version string.


93-93: Confirm the direct usage of github.com/cosmos/gogoproto.

The // indirect comment has been removed from github.com/cosmos/gogoproto. Please confirm if this dependency is now being used directly in the project, and if so, ensure that all necessary imports and usages are correctly handled.

Merged via the queue into main with commit 86ea861 Jul 16, 2024
72 checks passed
@tac0turtle tac0turtle deleted the tip/fix_comet_query branch July 16, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants