-
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
fix(baseapp): hybrid handlers do not drop any #22866
Conversation
📝 WalkthroughWalkthroughThe changes introduced in this pull request include the addition of a new test case in the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant GRPCQueryRouter
participant Handler
Client->>GRPCQueryRouter: Send request with Any message (Dog instance)
GRPCQueryRouter->>Handler: Process request
Handler->>GRPCQueryRouter: Return response with cached value
GRPCQueryRouter->>Client: Send response with cached value
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
@testinginprod your pull request is missing a changelog! |
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (2)
baseapp/internal/protocompat/protocompat.go (1)
236-237
: Use Professional Language in Code CommentsThe comment describes
setPointer
as an "ugly hack," which might not be ideal in a professional codebase.Consider rephrasing the comment for professionalism:
- // since proto.Merge breaks due to the custom cosmos sdk any, we are forced to do this ugly setPointer hack. + // Due to issues with proto.Merge and custom Cosmos SDK Any types, we use setPointer to directly assign values.baseapp/grpcrouter_test.go (1)
121-121
: Enhance Test by Asserting Cached Value EqualityWhile the test confirms that
resp.HasAnimal.Animal.GetCachedValue()
is not nil, asserting that the cached value matches the expected object would strengthen the test.Consider adding the following assertion:
require.Equal(t, anyMsg.GetCachedValue(), resp.HasAnimal.Animal.GetCachedValue())
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
baseapp/grpcrouter_test.go
(1 hunks)baseapp/internal/protocompat/protocompat.go
(3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
baseapp/grpcrouter_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"
baseapp/internal/protocompat/protocompat.go (1)
Pattern **/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
🔇 Additional comments (1)
baseapp/grpcrouter_test.go (1)
99-122
: New Test Case Validates Cached Value Preservation
The added test case "any cached value is not dropped"
effectively ensures that cached values in Any
messages are preserved when processed by hybrid handlers, addressing issue #22779.
// NOTE: using gogoproto.Merge will fail for some reason unknown to me, but | ||
// using proto.Merge with gogo messages seems to work fine. | ||
proto.Merge(msg.(gogoproto.Message), inReq) | ||
setPointer(msg, inReq) |
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.
Ensure Type Safety in setPointer
Function Usage
The setPointer
function is used at lines 126, 132, 164, and 170 to replace proto.Merge
. However, it operates without type or nil checks, which could lead to runtime panics if dst
and src
are nil or of incompatible types.
Consider adding type assertions and nil checks to setPointer
to ensure safe usage. Here's a suggested modification:
func setPointer(dst, src any) {
+ dstValue := reflect.ValueOf(dst)
+ srcValue := reflect.ValueOf(src)
+ if !dstValue.IsValid() || !srcValue.IsValid() {
+ panic("dst and src must be valid")
+ }
+ if dstValue.IsNil() || srcValue.IsNil() {
+ panic("dst and src must be non-nil")
+ }
+ if dstValue.Elem().Type() != srcValue.Elem().Type() {
+ panic("dst and src must have the same type")
+ }
dstValue.Elem().Set(srcValue.Elem())
}
And update the calls to handle potential errors or panics appropriately.
Also applies to: 132-132, 164-164, 170-170
// since proto.Merge breaks due to the custom cosmos sdk any, we are forced to do this ugly setPointer hack. | ||
// ref: https://github.com/cosmos/cosmos-sdk/issues/22779 | ||
func setPointer(dst, src any) { | ||
reflect.ValueOf(dst).Elem().Set(reflect.ValueOf(src).Elem()) | ||
} |
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.
🛠️ Refactor suggestion
Add Type Checks and Error Handling in setPointer
Function
The setPointer
function currently lacks type safety and nil checks, which might cause runtime panics if misused.
Modify setPointer
to include type checks and error handling:
func setPointer(dst, src any) error {
dstValue := reflect.ValueOf(dst)
srcValue := reflect.ValueOf(src)
if !dstValue.IsValid() || !srcValue.IsValid() {
return fmt.Errorf("dst and src must be valid")
}
if dstValue.IsNil() || srcValue.IsNil() {
return fmt.Errorf("dst and src must be non-nil")
}
dstElem := dstValue.Elem()
srcElem := srcValue.Elem()
if dstElem.Type() != srcElem.Type() {
return fmt.Errorf("dst and src must have the same type")
}
dstElem.Set(srcElem)
return nil
}
Ensure that calls to setPointer
handle the returned error:
- setPointer(msg, inReq)
+ if err := setPointer(msg, inReq); err != nil {
+ return err
+ }
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.
IBC is saying it fixed their issue so ACK.
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.
LGTM
(cherry picked from commit 25cbf98)
Description
Closes: #22779
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...
!
in the type prefix if API or client breaking changeCHANGELOG.md
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...
Summary by CodeRabbit
Tests
Chores