bitcoin/src/rpc
Ryan Ofsky 75118a608f
Merge bitcoin/bitcoin#27101: Support JSON-RPC 2.0 when requested by client
cbc6c440e3 doc: add comments and release-notes for JSON-RPC 2.0 (Matthew Zipkin)
e7ee80dcf2 rpc: JSON-RPC 2.0 should not respond to "notifications" (Matthew Zipkin)
bf1a1f1662 rpc: Avoid returning HTTP errors for JSON-RPC 2.0 requests (Matthew Zipkin)
466b90562f rpc: Add "jsonrpc" field and drop null "result"/"error" fields (Matthew Zipkin)
2ca1460ae3 rpc: identify JSON-RPC 2.0 requests (Matthew Zipkin)
a64a2b77e0 rpc: refactor single/batch requests (Matthew Zipkin)
df6e3756d6 rpc: Avoid copies in JSONRPCReplyObj() (Matthew Zipkin)
09416f9ec4 test: cover JSONRPC 2.0 requests, batches, and notifications (Matthew Zipkin)
4202c170da test: refactor interface_rpc.py (Matthew Zipkin)

Pull request description:

  Closes https://github.com/bitcoin/bitcoin/issues/2960

  Bitcoin Core's JSONRPC server behaves with a special blend of 1.0, 1.1 and 2.0 behaviors. This introduces compliance issues with more strict clients. There are the major misbehaviors that I found:
  - returning non-200 HTTP codes for RPC errors like "Method not found" (this is not a server error or an HTTP error)
  - returning both `"error"` and `"result"` fields together in a response object.
  - different error-handling behavior for single and batched RPC requests (batches contain errors in the response but single requests will actually throw HTTP errors)

  https://github.com/bitcoin/bitcoin/pull/15495 added regression tests after a discussion in https://github.com/bitcoin/bitcoin/pull/15381 to kinda lock in our RPC behavior to preserve backwards compatibility.

  https://github.com/bitcoin/bitcoin/pull/12435 was an attempt to allow strict 2.0 compliance behind a flag, but was abandoned.

  The approach in this PR is not strict and preserves backwards compatibility in a familiar bitcoin-y way: all old behavior is preserved, but new rules are applied to clients that opt in. One of the rules in the [JSON RPC 2.0 spec](https://www.jsonrpc.org/specification#request_object) is that the kv pair `"jsonrpc": "2.0"` must be present in the request. Well, let's just use that to trigger strict 2.0 behavior! When that kv pair is included in a request object, the [response will adhere to strict JSON-RPC 2.0 rules](https://www.jsonrpc.org/specification#response_object), essentially:

  - always return HTTP 200 "OK" unless there really is a server error or malformed request
  - either return `"error"` OR `"result"` but never both
  - same behavior for single and batch requests

  If this is merged next steps can be:

  - Refactor bitcoin-cli to always use strict 2.0
  - Refactor the python test framework to always use strict 2.0 for everything
  - Begin deprecation process for 1.0/1.1 behavior (?)

  If we can one day remove the old 1.0/1.1 behavior we can clean up the rpc code quite a bit.

ACKs for top commit:
  cbergqvist:
    re ACK cbc6c440e3
  ryanofsky:
    Code review ACK cbc6c440e3. Just suggested changes since the last review: changing uncaught exception error code from PARSE_ERROR to MISC_ERROR, renaming a few things, and adding comments.
  tdb3:
    re ACK for cbc6c440e3

Tree-SHA512: 0b702ed32368b34b29ad570d090951a7aeb56e3b0f2baf745bd32fdc58ef68fee6b0b8fad901f1ca42573ed714b150303829cddad4a34ca7ad847350feeedb36
2024-05-16 10:18:04 -04:00
..
blockchain.cpp rpc: move UniValue in blockToJSON 2024-05-13 14:30:44 +01:00
blockchain.h refactor: rpc: Pass CBlockIndex by reference instead of pointer 2023-12-07 12:05:21 +01:00
client.cpp Merge bitcoin/bitcoin#29130: wallet: Add `createwalletdescriptor` and `gethdkeys` RPCs for adding new automatically generated descriptors 2024-03-29 06:39:57 -04:00
client.h refactor: rpc: hide and rename ParseNonRFCJSONValue() 2023-03-23 18:18:46 +00:00
external_signer.cpp scripted-diff: Add IWYU pragma keep to bitcoin-config.h includes 2024-05-01 08:33:04 +02:00
fees.cpp refactor: Simply include CTxMemPool::Options in CTxMemPool directly rather than duplicating definition 2024-05-06 20:34:10 +00:00
mempool.cpp Merge bitcoin/bitcoin#29086: refactor: Simply include CTxMemPool::Options in CTxMemPool directly rather than duplicating definition 2024-05-14 20:00:34 -04:00
mempool.h rpc: Move mempool RPCs to new file 2022-03-11 17:46:58 +01:00
mining.cpp kernel: Remove key module from kernel library 2024-05-09 15:56:08 +02:00
mining.h
net.cpp refactor: Simply include CTxMemPool::Options in CTxMemPool directly rather than duplicating definition 2024-05-06 20:34:10 +00:00
node.cpp scripted-diff: Add IWYU pragma keep to bitcoin-config.h includes 2024-05-01 08:33:04 +02:00
output_script.cpp rpc: check and throw specific pubkey parsing errors in `HexToPubKey` 2024-02-09 13:35:23 +01:00
protocol.h rpc: JSON-RPC 2.0 should not respond to "notifications" 2024-05-14 11:28:43 -04:00
rawtransaction.cpp Merge bitcoin/bitcoin#28890: rpc: Remove deprecated -rpcserialversion 2024-01-05 10:42:10 +00:00
rawtransaction_util.cpp refactor: move parsing to new function 2024-01-19 15:04:56 +01:00
rawtransaction_util.h refactor: move parsing to new function 2024-01-19 15:04:56 +01:00
register.h scripted-diff: Add IWYU pragma keep to bitcoin-config.h includes 2024-05-01 08:33:04 +02:00
request.cpp doc: add comments and release-notes for JSON-RPC 2.0 2024-05-14 11:28:48 -04:00
request.h rpc: JSON-RPC 2.0 should not respond to "notifications" 2024-05-14 11:28:43 -04:00
server.cpp Merge bitcoin/bitcoin#27101: Support JSON-RPC 2.0 when requested by client 2024-05-16 10:18:04 -04:00
server.h Merge bitcoin/bitcoin#27101: Support JSON-RPC 2.0 when requested by client 2024-05-16 10:18:04 -04:00
server_util.cpp refactor: add and use EnsureAnyAddrman in rpc 2023-10-04 08:53:51 +05:30
server_util.h refactor: add and use EnsureAnyAddrman in rpc 2023-10-04 08:53:51 +05:30
signmessage.cpp rpc: access some args by name 2024-03-01 13:51:21 +00:00
txoutproof.cpp refactor: Use Txid in CMerkleBlock 2023-11-28 17:49:41 +01:00
util.cpp Merge bitcoin/bitcoin#27101: Support JSON-RPC 2.0 when requested by client 2024-05-16 10:18:04 -04:00
util.h rpc: return warnings as an array instead of just a single one 2024-05-01 14:44:57 +01:00