rpc: parse each batch element into its own request #36642

Manually merged
fametrano merged 1 commit from rpc-batch-reset-id into master 2026-11-10 14:24:54 +00:00 AGit
fametrano commented 2026-10-24 06:49:59 +00:00 (Migrated from github.com)

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.

ExecuteHTTPRPC parses every element of a batch into the same request object, and parse() throws on a non-object before it sets id and m_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 in interface_rpc.py fails on master and passes with this change.

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. `ExecuteHTTPRPC` parses every element of a batch into the same request object, and `parse()` throws on a non-object before it sets `id` and `m_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 in `interface_rpc.py` fails on master and passes with this change.
DrahtBot commented 2026-10-24 06:50:09 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK hodlinator, janb84, pinheadmz
Stale ACK AndreaDiazCorreia

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36642 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36642.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36642#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [hodlinator](https://github.com/bitcoin/bitcoin/pull/36642#pullrequestreview-5823153433), [janb84](https://github.com/bitcoin/bitcoin/pull/36642#issuecomment-6737550388), [pinheadmz](https://github.com/bitcoin/bitcoin/pull/36642#pullrequestreview-5832020545) | | Stale ACK | [AndreaDiazCorreia](https://github.com/bitcoin/bitcoin/pull/36642#pullrequestreview-5646637313) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 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 reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:055d1cd96b176486294e0a3d052a7e506a00c1f0c7c35fbf579b09cfe6d615cd:237cc81563f53f26a84ba90acce49f0597b0e28b0a77aa425401db6e90431c0b --> <!-- ralph-base:49c3c7fe09ac311ea9ce37599a0df3e4065657f9 --> <!-- ralph-head:5861dd29a73a9f0a21851a83adfa21d4637282b5 --> <!-- ralph-case:e8b53fe95de19f202769bb2c86b9f8afa66b219e66c589a73546221a64f7f44d --> **🔵 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/5861dd29a73a9f0a21851a83adfa21d4637282b5/src/httprpc.cpp#L163>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`5861dd29a73a`](<https://git.fish.foo/bitcoin/bitcoin/commit/5861dd29a73a9f0a21851a83adfa21d4637282b5>) · [Full review report](<https://review.fish.foo/traces/36642-5861dd29a73a9f0a21851a83adfa21d4637282b5-2366-5.html>) · [Previous reports](<https://review.fish.foo/traces/36642/>) [permalink](<https://review.fish.foo/traces/36642-5861dd29a73a9f0a21851a83adfa21d4637282b5-2366-5.html>)
AndreaDiazCorreia (Migrated from github.com) reviewed 2026-10-26 02:05:40 +00:00
AndreaDiazCorreia (Migrated from github.com) left a comment

ACK 0811092e11

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

ACK 0811092e117f08ac9ad0e608f796cd8ac014abd0 Reviewed 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.
pinheadmz (Migrated from github.com) reviewed 2026-10-26 20:05:20 +00:00
pinheadmz (Migrated from github.com) left a comment

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.

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.
pinheadmz (Migrated from github.com) commented 2026-10-26 20:01:01 +00:00

remove the copy of this statement on line 221?

remove the copy of this statement on line 221?
pinheadmz (Migrated from github.com) commented 2026-10-26 20:03:37 +00:00

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?

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?
fametrano commented 2026-11-01 18:41:36 +00:00 (Migrated from github.com)

Done: I removed the second m_json_version reset and simplified the test's asserts. The batch case now checks the valid reply, then that the error reply has another id and no jsonrpc field, 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?

Done: I removed the second `m_json_version` reset and simplified the test's asserts. The batch case now checks the valid reply, then that the error reply has another id and no `jsonrpc` field, 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?
pinheadmz (Migrated from github.com) reviewed 2026-11-02 19:17:56 +00:00
pinheadmz (Migrated from github.com) commented 2026-11-02 19:17:56 +00:00

The 2 here is an invalid request because its not JSON but that's not obvious for review. Especially because there is another 2 a 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.

The `2` here is an invalid request because its not JSON but that's not obvious for review. Especially because there is another `2` a 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.
fametrano (Migrated from github.com) reviewed 2026-11-03 22:44:49 +00:00
fametrano (Migrated from github.com) commented 2026-11-03 22:44:49 +00:00

Done in 325e600c02: both batches now send "not an object" instead of 2, since the element parses as JSON and is rejected only for not being an object.

Done in 325e600c02: both batches now send `"not an object"` instead of `2`, since the element parses as JSON and is rejected only for not being an object.
pinheadmz (Migrated from github.com) approved these changes 2026-11-04 14:48:31 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK 325e600c02

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 325e600c02b0f2dcb28848d66e53e6e053f0b499
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrrRaIbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6IioP/jEZHsTmwDSmS3GvMOYw
qNPF7kCAXObPLr2JP7J3Ewx0aN09itYXoYYUkmp94NSbzRNdwWC7e2Ak5wKYsVud
AflT+cqT2Xoo0G8fZJBKcPAgqM+4u+DavuHpLuMfbPVoSYnt0rB5pTvZMuutLxYx
edBFiHkTjPrtGy4SyESz+QBFlxf6c4kt++B3KXNwsyZwpgd3gNnjPrqcGIXfECBE
rGuIEhFj/2L9T+I7oeMW/+XtgZKNLP+PA3AEhK2StXaK0mzc2Y8K7kvpt7GE30TV
XWBWF7oyQLeBL+qs22bDy+m/acUcM5GWz00dzjCF+P6oG/N85hJAVq3Qp+skGJ45
YxjKC0/BY3s1em0CYjLhboqozMjnEdIu+Rw6awbgXyaSLus1Nq5far9n0mlDMErD
umYgaY0sJZ0lfGwyB7twnTS8tlI09f/8HMWjG8EJJeeaHsguY0VWYt/QxSxb82pq
OfL5vK/0xf5KVnoyYrQKCix+9ToOb2yMaD8GEu/4HdPlnzjGBF+81kwbU4F0GGq5
cc6RKufK9ejMZalmdbd+p2ccE1QseD7vMmu6Sy3n9V58O1xi6JuGJ5U107ylLiga
JZkKgAHzj4XL0tHlWuYf9M8L4OwxiQPPSyKZhnlyE+Tjyj48X5xebxWkTCfv+hCu
u6rmS9Kx4djQ6WtyCgyj/HOV
=xEBx
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK 325e600c02b0f2dcb28848d66e53e6e053f0b499 <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 325e600c02b0f2dcb28848d66e53e6e053f0b499 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrrRaIbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6IioP/jEZHsTmwDSmS3GvMOYw qNPF7kCAXObPLr2JP7J3Ewx0aN09itYXoYYUkmp94NSbzRNdwWC7e2Ak5wKYsVud AflT+cqT2Xoo0G8fZJBKcPAgqM+4u+DavuHpLuMfbPVoSYnt0rB5pTvZMuutLxYx edBFiHkTjPrtGy4SyESz+QBFlxf6c4kt++B3KXNwsyZwpgd3gNnjPrqcGIXfECBE rGuIEhFj/2L9T+I7oeMW/+XtgZKNLP+PA3AEhK2StXaK0mzc2Y8K7kvpt7GE30TV XWBWF7oyQLeBL+qs22bDy+m/acUcM5GWz00dzjCF+P6oG/N85hJAVq3Qp+skGJ45 YxjKC0/BY3s1em0CYjLhboqozMjnEdIu+Rw6awbgXyaSLus1Nq5far9n0mlDMErD umYgaY0sJZ0lfGwyB7twnTS8tlI09f/8HMWjG8EJJeeaHsguY0VWYt/QxSxb82pq OfL5vK/0xf5KVnoyYrQKCix+9ToOb2yMaD8GEu/4HdPlnzjGBF+81kwbU4F0GGq5 cc6RKufK9ejMZalmdbd+p2ccE1QseD7vMmu6Sy3n9V58O1xi6JuGJ5U107ylLiga JZkKgAHzj4XL0tHlWuYf9M8L4OwxiQPPSyKZhnlyE+Tjyj48X5xebxWkTCfv+hCu u6rmS9Kx4djQ6WtyCgyj/HOV =xEBx -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
janb84 (Migrated from github.com) reviewed 2026-11-05 09:27:32 +00:00
janb84 (Migrated from github.com) left a comment

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.

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.
janb84 (Migrated from github.com) commented 2026-11-05 09:23:23 +00:00

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:

        response, status = send_json_rpc(self.nodes[0], [{"jsonrpc": "2.0", "method": "getblockcount", "id": "X"}, "not an object"])
        assert_equal(status, 200)
        assert_equal(response, [
            {"jsonrpc": "2.0", "result": 0, "id": "X"},
            {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Invalid Request object"}, "id": None},
        ])

        # The error is not dropped as if it were the reply to the notification before it
        response, status = send_json_rpc(self.nodes[0], [{"jsonrpc": "2.0", "method": "getblockcount"}, "not an object"])
        assert_equal(status, 200)
        assert_equal(response, [
            {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Invalid Request object"}, "id": None},
        ])
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: ```suggestion response, status = send_json_rpc(self.nodes[0], [{"jsonrpc": "2.0", "method": "getblockcount", "id": "X"}, "not an object"]) assert_equal(status, 200) assert_equal(response, [ {"jsonrpc": "2.0", "result": 0, "id": "X"}, {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Invalid Request object"}, "id": None}, ]) # The error is not dropped as if it were the reply to the notification before it response, status = send_json_rpc(self.nodes[0], [{"jsonrpc": "2.0", "method": "getblockcount"}, "not an object"]) assert_equal(status, 200) assert_equal(response, [ {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Invalid Request object"}, "id": None}, ]) ```
fametrano (Migrated from github.com) reviewed 2026-11-05 22:38:55 +00:00
fametrano (Migrated from github.com) commented 2026-11-05 22:38:55 +00:00

Thanks, I'll take this if I need to push again.

Thanks, I'll take this if I need to push again.
hodlinator (Migrated from github.com) reviewed 2026-11-06 15:31:13 +00:00
hodlinator (Migrated from github.com) left a comment

Concept ACK 325e600c02

Thanks for finding and working on a fix for this!

Concept ACK 325e600c02b0f2dcb28848d66e53e6e053f0b499 Thanks for finding and working on a fix for this!
Lines 206-209
hodlinator (Migrated from github.com) commented 2026-11-06 15:25:10 +00:00

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 JSONRPCRequest is created for each element in a batch. Maybe it could be simplified a bit (further), curious what others think.

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 `JSONRPCRequest` is created for each element in a batch. Maybe it could be simplified a bit (further), curious what others think.
fametrano (Migrated from github.com) reviewed 2026-11-07 06:59:57 +00:00
Lines 206-209
fametrano (Migrated from github.com) commented 2026-11-07 06:59:57 +00:00

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 2 now gets HTTP 500 with a plain unknown error instead of the -32700 "Top-level object parse error". I'd take the commits once the refactor keeps those three replies as they are.

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 `2` now gets HTTP 500 with a plain `unknown error` instead of the -32700 "Top-level object parse error". I'd take the commits once the refactor keeps those three replies as they are.
sedited (Migrated from github.com) reviewed 2026-11-07 07:59:35 +00:00
Lines 206-209
sedited (Migrated from github.com) commented 2026-11-07 07:59:35 +00:00

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

@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.
fametrano (Migrated from github.com) reviewed 2026-11-07 17:36:19 +00:00
Lines 206-209
fametrano (Migrated from github.com) commented 2026-11-07 17:36:19 +00:00

Pushed 5861dd29a7 with your suggestion: each batch element now gets its own request. request.cpp is back to master.

this text is written by a human being ;-)

Pushed 5861dd29a7 with your suggestion: each batch element now gets its own request. `request.cpp` is back to master. this text is written by a human being ;-)
fametrano (Migrated from github.com) reviewed 2026-11-07 17:36:21 +00:00
fametrano (Migrated from github.com) commented 2026-11-07 17:36:21 +00:00

Done in 5861dd29a7: each batch is now checked with one assert_equal on the whole response.

Done in 5861dd29a7: each batch is now checked with one `assert_equal` on the whole response.
hodlinator (Migrated from github.com) approved these changes 2026-11-09 08:36:44 +00:00
hodlinator (Migrated from github.com) left a comment
ACK 5861dd29a73a9f0a21851a83adfa21d4637282b5
Lines 206-209
hodlinator (Migrated from github.com) commented 2026-11-09 08:17:09 +00:00

Good catch on the escaping throws in my refactor! Thanks for switching to the more robust approach for the batch requests.

Good catch on the escaping throws in my refactor! Thanks for switching to the more robust approach for the batch requests.
hodlinator (Migrated from github.com) commented 2026-11-09 08:30:15 +00:00

Tangential remark: It's seems unfortunate that our RPC implementation inserts result in the error case here, https://www.jsonrpc.org/specification#response_object states:

result ... This member MUST NOT exist if there was an error invoking the method.

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.

Tangential remark: It's seems unfortunate that our RPC implementation inserts `result` in the error case here, https://www.jsonrpc.org/specification#response_object states: > result ... This member MUST NOT exist if there was an error invoking the method. 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.
janb84 commented 2026-11-09 10:17:19 +00:00 (Migrated from github.com)

ACK 5861dd29a7

Thanks 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 --rpcwhitelist or 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.

Batch Without a whitelist With a whitelist
Only valid elements 200, array of replies 200, array of replies
Contains "not an object" 200, array, error only for that element 400, one error object, nothing runs
Contains an object without method 200, array, "Missing method" for that element 500, one error object, nothing runs

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

ACK 5861dd29a73a9f0a21851a83adfa21d4637282b5 Thanks 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 `--rpcwhitelist` or 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. Batch | Without a whitelist | With a whitelist -- | -- | -- Only valid elements | 200, array of replies | 200, array of replies Contains "not an object" | 200, array, error only for that element | 400, one error object, nothing runs Contains an object without method | 200, array, "Missing method" for that element | 500, one error object, nothing runs This _can_ also be (partially) correct, if something from the batch cannot be checked by the whitelist -> fail everything. Given the [batch](https://www.jsonrpc.org/specification#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
pinheadmz commented 2026-11-09 20:02:06 +00:00 (Migrated from github.com)

@janb84 see https://github.com/bitcoin/bitcoin/pull/36753 for the rpcwhitelist fix 🤣 There's now three jsonrpc PRs circling around each other

@janb84 see https://github.com/bitcoin/bitcoin/pull/36753 for the rpcwhitelist fix 🤣 There's now three jsonrpc PRs circling around each other
pinheadmz (Migrated from github.com) approved these changes 2026-11-09 20:10:43 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK 5861dd29a7

built and tested on macos/arm64. the patch is very lean, correct and the best approach (thanks @hodlinator).

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 5861dd29a73a9f0a21851a83adfa21d4637282b5
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmryKDgbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6hjkP/0LuARbf5pjU8wGQmtW5
gFDP5qB+8p90+TcqHY/6IbeCdnyYB+XFtWfgCQPH0QwZQVI1kU1VSByE24cDWXTi
rJbu0OXGPcdy7ha0fRwm4OJw1yFxXR1slgEPYmnLGIUkvXKcUIuw/qNigS/qGYF5
2jtufqXEiTvQdz0f9I/1Hx4LTv7YR1lmH1z03kVsZv9PRjsDj4Gbw6DUswiYOmef
WzEHZ9nBjlMLoEvdH3//YaVhnPSsoNj8N+vgu7Cp/7ik36RGmhZ/ANRa/h6uW3sV
bKAeo2iHZthgj1q07fpcKDCaKRJrsacr7ALQvUa4eOOxfm4oP5q9Zr9LTn2AuMAL
F2HtGHSulD0v6lX5toGjmiAPKLGPxGo2pepkiZSnCTpaXyYua8Dq7f+BDhJJm3cO
y08eSmNXFlN0EntsCc/Y7T0RSRB7mmA37IBgpcWB+FXIePO0O3kTWzjQSzj0xF7g
GNm/3+7acdKHdGwo/ph9p2hPV77bZAd/scMbuuDM9r1Gfemulw3Je3OMSzedUMwg
zdJPweD+/l1UaXigUEMnf7aWEW6LHNDk5r4IEXx0PuTJLg07JcNOOziq7SpGOuoZ
exg8ij68GTIedIn/8TrqX8txqDnQZvqGF5/McWtaJqA62f96Ji5T31eo1ZRoBzrA
l3lSEdu/xBQbyB6YKhctsmwt
=KFjP
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK 5861dd29a73a9f0a21851a83adfa21d4637282b5 built and tested on macos/arm64. the patch is very lean, correct and the best approach (thanks @hodlinator). <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 5861dd29a73a9f0a21851a83adfa21d4637282b5 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmryKDgbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6hjkP/0LuARbf5pjU8wGQmtW5 gFDP5qB+8p90+TcqHY/6IbeCdnyYB+XFtWfgCQPH0QwZQVI1kU1VSByE24cDWXTi rJbu0OXGPcdy7ha0fRwm4OJw1yFxXR1slgEPYmnLGIUkvXKcUIuw/qNigS/qGYF5 2jtufqXEiTvQdz0f9I/1Hx4LTv7YR1lmH1z03kVsZv9PRjsDj4Gbw6DUswiYOmef WzEHZ9nBjlMLoEvdH3//YaVhnPSsoNj8N+vgu7Cp/7ik36RGmhZ/ANRa/h6uW3sV bKAeo2iHZthgj1q07fpcKDCaKRJrsacr7ALQvUa4eOOxfm4oP5q9Zr9LTn2AuMAL F2HtGHSulD0v6lX5toGjmiAPKLGPxGo2pepkiZSnCTpaXyYua8Dq7f+BDhJJm3cO y08eSmNXFlN0EntsCc/Y7T0RSRB7mmA37IBgpcWB+FXIePO0O3kTWzjQSzj0xF7g GNm/3+7acdKHdGwo/ph9p2hPV77bZAd/scMbuuDM9r1Gfemulw3Je3OMSzedUMwg zdJPweD+/l1UaXigUEMnf7aWEW6LHNDk5r4IEXx0PuTJLg07JcNOOziq7SpGOuoZ exg8ij68GTIedIn/8TrqX8txqDnQZvqGF5/McWtaJqA62f96Ji5T31eo1ZRoBzrA l3lSEdu/xBQbyB6YKhctsmwt =KFjP -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
ryanofsky (Migrated from github.com) reviewed 2026-11-10 14:24:15 +00:00
ryanofsky (Migrated from github.com) left a comment

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:

  1. 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).

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

  3. JSONRPCRequest holds 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.

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

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: 1. 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](https://www.jsonrpc.org/specification#examples) reply to that request with an "Invalid Request" error and `"id": null`. [#36758](https://github.com/bitcoin/bitcoin/pull/36758) currently fixes this for single requests only, and pinheadmz [plans to fix batches too](https://github.com/bitcoin/bitcoin/pull/36758#discussion_r4517275774) after rebasing on this PR, so both follow the same rule ([discussion](https://github.com/bitcoin/bitcoin/pull/36758#discussion_r4498229560)). 2. Batch replies should be the same with and without `-rpcwhitelist`, as [janb84 pointed out](https://github.com/bitcoin/bitcoin/pull/36642#issuecomment-6737550388). 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](https://github.com/bitcoin/bitcoin/pull/36753) fixes this. 3. `JSONRPCRequest` holds 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](https://github.com/bitcoin/bitcoin/pull/36642#discussion_r4494106574), 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](https://github.com/bitcoin/bitcoin/pull/36753). 4. 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](https://github.com/bitcoin/bitcoin/pull/36642#discussion_r4510779588) 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
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36642
No description provided.