-
Notifications
You must be signed in to change notification settings - Fork 391
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(rpc): Update http batch request to handle singletons #3694
base: master
Are you sure you want to change the base?
Conversation
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🟢 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
duplicated with #3678 |
It looks like we've resolved this issue with the RPC server patch #3678 , but I still think it is worthwhile thinking about how we could make the client more forgiving as well. I'll pass long feedback line in a review. |
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.
Nice first pass, please see my comments in line for a few changes.
@@ -115,7 +116,8 @@ func sendRequestCommon[T requestType, R responseType]( | |||
// Marshal the request | |||
requestBytes, err := json.Marshal(request) | |||
if err != nil { | |||
return nil, fmt.Errorf("unable to JSON-marshal the request, %w", err) | |||
var zero R | |||
return zero, fmt.Errorf("unable to JSON-marshal the request, %w", 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.
This should be expressed as a one-liner. I think nil
should work, but if R needs various return types to be supported, you should be able to handle it by declaring the default value on the same line.
response = newSlice.Interface().(R) | ||
return response, nil | ||
} | ||
} |
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.
Let's try to refactor this without reflection. Take notes from the RPC Server patch on how you could approach this. If the Slice marshalling fails, attempt to unmarshal a single response item and put that in a slice of the responses for the return.
@notJoon I still think this is worth patching so that Batching responses for the client will work, even on older test nets. This follows the robustness principle (Postel's Law). If we can coax a slightly malformed response into a correct one, then it is better for the end-user experience. But, with the server patch, all testnets moving forward should be fine with the server side-only change. |
This pull request updates the HTTP batch request to handle singletons correctly.
Closes #3676
In sendRequestCommon, if the JSON response fails to unmarshal into the expected type, the function checks if the expected response type is a slice. If so, it attempts to unmarshal the JSON as a single element of that slice. When successful, it wraps the single element into a new slice and returns it, thereby gracefully handling cases where a singleton response is provided instead of an array.