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(rpc): Update http batch request to handle singletons #3694

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

masonmcbride
Copy link

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.

@github-actions github-actions bot added the 📦 🌐 tendermint v2 Issues or PRs tm2 related label Feb 6, 2025
@Gno2D2 Gno2D2 requested a review from a team February 6, 2025 09:39
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Feb 6, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Feb 6, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
  • The pull request description provides enough details (checked by @n2p5)
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)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: masonmcbride/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission
The pull request description provides enough details

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 Not (🔴 Pull request author is a member of the team: core-contributors)
    └── 🟢 Not (🔴 Pull request author is user: dependabot[bot])

Can be checked by

  • team core-contributors

@masonmcbride masonmcbride changed the title Update http batch request to handle singletons fix(rpc): Update http batch request to handle singletons Feb 6, 2025
@notJoon
Copy link
Member

notJoon commented Feb 6, 2025

duplicated with #3678

@n2p5
Copy link
Contributor

n2p5 commented Feb 6, 2025

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.

Copy link
Contributor

@n2p5 n2p5 left a 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)
Copy link
Contributor

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
}
}
Copy link
Contributor

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.

@n2p5
Copy link
Contributor

n2p5 commented Feb 6, 2025

@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.

@Gno2D2 Gno2D2 requested a review from a team February 6, 2025 18:30
@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Feb 6, 2025
@masonmcbride
Copy link
Author

masonmcbride commented Feb 7, 2025

I updated both the server side (Omar's solution) and the client side (mine) functions associated with batch requests so that they have the same functionality but are more readable. On the server side, it tries to unmarshall into a slice of Responses and if it can't it tried to unmarshall into a single Response but both cases perform the same process which is abstracted out as processRequest. On the client side, sendRequestCommon has R ResponseType which is a *types.RPCResponse | types.RPCResponses so it uses a switch case on the return type and exhausts the cases for a clear program flow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related
Projects
Status: Triage
Development

Successfully merging this pull request may close these issues.

bug: RPC HTTP Batch doesn't support 1 item responses
4 participants