mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
rpc: parse each batch element into its own request #36642
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36642
Loading…
Reference in a new issue
No description provided.
Delete branch "rpc-batch-reset-id"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
In a JSON-RPC batch, an element that is not an object gets an error reply with the id and version of the element before it.
ExecuteHTTPRPCparses every element of a batch into the same request object, andparse()throws on a non-object before it setsidandm_json_version. For[{"jsonrpc":"2.0","method":"getblockcount","id":"X"}, 2]the reply has two objects with"id":"X", one with a result and one with an error. If the first element is a notification, the error is dropped. I give each element its own copy of the request, which holds only the HTTP fields, so the error is the one the first element of a batch already gets. The new case ininterface_rpc.pyfails on master and passes with this change.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.🔵 Suggestion · PR description describes a parse() reset that the patch no longer makes
The description attributes the fix to resets in parse(), but the patch isolates batch elements with separate request copies.
Location:
src/httprpc.cpp:163(head).Review coverage was incomplete.
Reviewed:
5861dd29a73a· Full review report · Previous reportspermalink
ACK
0811092e11Reviewed and tested, looks good to me.
Small nit: the existing m_json_version reset before the jsonrpc check is now redundant and could be removed.
concept ACK
This is a reasonable follow up to #27101 and fixes something we missed from the jsonrpc 2.0 spec that could confuse consumers of the interface expecting unique IDs in batch responses.
remove the copy of this statement on line 221?
this is a little confusing to read, here and below. what if you just assert the response to the valid request is correct and the second response exists, and doesn't have the same id?
Done: I removed the second
m_json_versionreset and simplified the test's asserts. The batch case now checks the valid reply, then that the error reply has another id and nojsonrpcfield, which fails if the version reset stays where it was; the notification case checks that the error reply is there.@AndreaDiazCorreia, the removed reset is the one from your nit; would you mind taking another look at
2ab7c47f60?The
2here is an invalid request because its not JSON but that's not obvious for review. Especially because there is another2a few lines down that is a critical integer value. If I wrote this test I'd probably use something like a literal string"invalid json"there.Done in
325e600c02: both batches now send"not an object"instead of2, since the element parses as JSON and is rejected only for not being an object.ACK
325e600c02Show Signature
pinheadmz's public key is on openpgp.org
Concept ACK,
Have validated that the supplied patch fixes the bug and removing it will fail the supplied tests.
Have added a small NIT to condense the functional tests a bit.
Suggestion to rewrite these tests to remove all the separate asserts (and remove the new include for
assert_not_equal) and condense them into two, one line asserts:Thanks, I'll take this if I need to push again.
Concept ACK
325e600c02Thanks for finding and working on a fix for this!
While it does make for a more surgical fix, it doesn't feel robust to reset individual fields.
Would prefer something closer to https://github.com/hodlinator/bitcoin/tree/pr/36642_suggestions where in commit db01f652d2e092440a90f9339e86b804327b0faf a fresh
JSONRPCRequestis created for each element in a batch. Maybe it could be simplified a bit (further), curious what others think.Yes, a fresh request per element is sturdier, and I'm glad to take your commits, here or in a follow-up, whichever you and others prefer.
One condition: your first commit leaves three throws outside any try, so they lose their JSON-RPC error replies. A body of
2now gets HTTP 500 with a plainunknown errorinstead of the -32700 "Top-level object parse error". I'd take the commits once the refactor keeps those three replies as they are.@fametrano another reminder to write your comments yourself. Both comments left here so far were not nits, but requests to change your code in this pull request. If you find problems with them, just improve them, don't ask for exact copies from reviewers.
Pushed
5861dd29a7with your suggestion: each batch element now gets its own request.request.cppis back to master.this text is written by a human being ;-)
Done in
5861dd29a7: each batch is now checked with oneassert_equalon the whole response.ACK
5861dd29a7Good catch on the escaping throws in my refactor! Thanks for switching to the more robust approach for the batch requests.
Tangential remark: It's seems unfortunate that our RPC implementation inserts
resultin the error case here, https://www.jsonrpc.org/specification#response_object states:Guess it's expected behavior since we don't yet default to JSON RPC 2.0, but wonder when we should make the switch. Also annoying when the batch request contains proper 2.0 elements.
ACK
5861dd29a7Thanks for taking my suggestions.
EDIT: after further contemplation I think the change should be a followup:
I'm conflicted on one issue if this PR is complete or not, the behavior is different if bitcoind is started with
--rpcwhitelistor not. Normally I would say the PR is incomplete, whitelist or not the response should be the same, but given this is already different on master I'm hesitant.This can also be (partially) correct, if something from the batch cannot be checked by the whitelist -> fail everything. Given the batch spec I would say we have to treat every call on it's own and failing everything for one failed call is not the correct response.
The 500 is in any case not correct
@janb84 see https://github.com/bitcoin/bitcoin/pull/36753 for the rpcwhitelist fix 🤣 There's now three jsonrpc PRs circling around each other
ACK
5861dd29a7built and tested on macos/arm64. the patch is very lean, correct and the best approach (thanks @hodlinator).
Show Signature
pinheadmz's public key is on openpgp.org
This makes sense to merge before the related PRs #36753 and #36758. It makes them easier to understand and fixes a bug where when a batch contains an element that is not a JSON object, the error reply for that element gets the id of the element before it. A client matching replies by id then sees two replies for one call, or, if the element before was a notification, the error is dropped.
Things for followups:
A JSON-RPC 2.0 batch element without an id that fails to parse, like
{"jsonrpc": "2.0", "method": 1}, is treated as a notification and gets no reply. The spec's examples reply to that request with an "Invalid Request" error and"id": null. #36758 currently fixes this for single requests only, and pinheadmz plans to fix batches too after rebasing on this PR, so both follow the same rule (discussion).Batch replies should be the same with and without
-rpcwhitelist, as janb84 pointed out. With a whitelist, a non-object element currently fails the whole batch with HTTP 400, and an element without a method fails it with HTTP 500. #36753 fixes this.JSONRPCRequestholds both fields describing the client connection (URI,authUser,peerAddr,context) and fields parsed from each request. Splitting these into two structs would let each batch element start from a fresh request, as hodlinator suggested, without copying anything, and would make it easy to share the per-element parse and reply code between batch loops. I think I will also do this in #36753.Error replies in the legacy format include
"result": null, which JSON-RPC 2.0 doesn't allow, and an element that isn't an object can only be answered in the legacy format. hodlinator asked when 2.0 should become the default. It seems like we should have a comment somewhere explaining current choice and any future plans to change