-
Notifications
You must be signed in to change notification settings - Fork 389
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): always return array reponses for batch requests #3678
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
|
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Thanks for this quick PR. 👍
I just tested it locally, and it looks like this script is still failing.
Could you take a look at the comments and let me know if I'm missing something?
@@ -145,6 +145,9 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger *slog.Logger) http.H | |||
requests types.RPCRequests | |||
responses types.RPCResponses | |||
) | |||
|
|||
isRPCRequests := true |
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.
I don't understand the purpose of this variable. If the point is to know if there is only one request (so only one response) why don't you use a len(requests)
instead?
Or event better, a len(responses)
in your case (since 1 request == 1 response if I read the for loop bellow correctly).
If there's another purpose, please change the variable name and add a comment to explain it.
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.
Thanks for the review! ^^
In this PR, I've updated the logic for handling batch requests. Previously, if a batch request contained only a single item, the endpoint would return just a single response object instead of an array containing that response. This was inconsistent with the expected behavior where even a single-item batch should yield an array of responses.
With my changes, regardless of the number of items in the batch request, the API now consistently returns an array.
if len(responses) == 0 { | ||
return | ||
} | ||
|
||
if isRPCRequests { | ||
WriteRPCResponseArrayHTTP(w, responses) | ||
return | ||
} | ||
|
||
WriteRPCResponseHTTP(w, responses[0]) |
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 code looks like it could be replaced by something like:
if len(responses) == 1 {
WriteRPCResponseHTTP(w, responses[0])
} else if if len(responses) > 1 {
WriteRPCResponseArrayHTTP(w, responses)
}
Or maybe it would better to check this directly inside WriteRPCResponseArrayHTTP
function and to just let the code as it is in this file.
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.
I just checked the WriteRPCResponseArrayHTTP
and it already check this
gno/tm2/pkg/bft/rpc/lib/server/http_server.go
Lines 122 to 124 in d75f1a2
if len(res) == 1 { | |
WriteRPCResponseHTTP(w, res[0]) | |
} else { |
I'm not sure that these changes in the code don't have any impact then.
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.
:o I forget to commit this 7453add is why it failing sorry ^^
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.
I haven't tried re-running the script with this change because I really feel like this commit won't fix it.
Did you try it on your side? Is it working?
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.
Can you add a unit test that covers the same case as the issue's script please?
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.
Edited the test + add a subtest here: b37dc8c
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.
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.
Ok cool :) I'll check it
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.
Ok, it works well, GG 👍
So your fix is not about the number of responses you have to send but whether you received an array or not. If you get an array of size 1, you should return an array of size 1, right?
If that's the case, could you change your boolean name to something like isRPCRequestArray
and add a comment right above your last if explaining that for a request in the form of an array, even if it contains only one element, your response must also be an array.
Thanks!
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.
So your fix is not about the number of responses you have to send but whether you received an array or not. If you get an array of size 1, you should return an array of size 1, right?
Yes
If that's the case, could you change your boolean name to something like isRPCRequestArray and add a comment right above your last if explaining that for a request in the form of an array, even if it contains only one element, your response must also be an array.
I used "isRPCRequests" with an added "s", but "isRPCRequestArray" is clearer.
I’ve added a comment here: 50fa2cd.
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.
Edit: sorry I spammed the Submit Review
button.
closes: #3676
Currently, when a batch request contains only a single item, our implementation returns a single response object instead of an array.
What This PR Does:
My changes update the logic so that even if the batch request includes only one item, the endpoint will still return an array containing that single response.