This commit is contained in:
Matthew Zipkin 2024-04-29 04:32:49 +02:00 committed by GitHub
commit f483fc26c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 364 additions and 88 deletions

View File

@ -74,6 +74,22 @@ major version via the `-deprecatedrpc=` command line option. The release notes
of a new major release come with detailed instructions on what RPC features
were deprecated and how to re-enable them temporarily.
## JSON-RPC 1.1 vs 2.0
The server recognizes [JSON-RPC v2.0](https://www.jsonrpc.org/specification) requests
and responds accordingly. A 2.0 request is identified by the presence of
`"jsonrpc": "2.0"` in the request body. If that key + value is not present in a request,
the legacy JSON-RPC v1.1 protocol is followed instead, which was the only available
protocol in previous releases.
|| 1.1 | 2.0 |
|-|-|-|
| Request marker | `"version": "1.1"` (or none) | `"jsonrpc": "2.0"` |
| Response marker | (none) | `"jsonrpc": "2.0"` |
| `"error"` and `"result"` fields in response | both present | only one is present |
| HTTP codes in response | `200` unless there is any kind of RPC error (invalid parameters, method not found, etc) | Always `200` unless there is an actual HTTP server error (request parsing error, endpoint not found, etc) |
| Notifications: requests that get no reply | (not supported) | Supported for requests that exclude the "id" field |
## Security
The RPC interface allows other programs to control Bitcoin Core,

View File

@ -0,0 +1,9 @@
JSON-RPC
--------
The JSON-RPC server now recognizes JSON-RPC 2.0 requests and responds with
strict adherence to the specification (https://www.jsonrpc.org/specification):
- Returning HTTP "204 No Content" responses to JSON-RPC 2.0 notifications instead of full responses.
- Returning HTTP "200 OK" responses in all other cases, rather than 404 responses for unknown methods, 500 responses for invalid parameters, etc.
- Returning either "result" fields or "error" fields in JSON-RPC responses, rather than returning both fields with one field set to null.

View File

@ -300,7 +300,7 @@ public:
}
addresses.pushKV("total", total);
result.pushKV("addresses_known", addresses);
return JSONRPCReplyObj(result, NullUniValue, 1);
return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
}
};
@ -369,7 +369,7 @@ public:
}
result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]);
result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]);
return JSONRPCReplyObj(result, NullUniValue, 1);
return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
}
};

View File

@ -73,8 +73,11 @@ static std::vector<std::vector<std::string>> g_rpcauth;
static std::map<std::string, std::set<std::string>> g_rpc_whitelist;
static bool g_rpc_whitelist_default = false;
static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const UniValue& id)
static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const JSONRPCRequest& jreq)
{
// Sending HTTP errors is a legacy JSON-RPC behavior.
Assume(jreq.m_json_version != JSONVersion::JSON_2_0);
// Send error reply from json-rpc error object
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
int code = objError.find_value("code").getInt<int>();
@ -84,7 +87,7 @@ static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const Uni
else if (code == RPC_METHOD_NOT_FOUND)
nStatus = HTTP_NOT_FOUND;
std::string strReply = JSONRPCReply(NullUniValue, objError, id);
std::string strReply = JSONRPCReply(NullUniValue, objError, jreq);
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(nStatus, strReply);
@ -185,7 +188,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
// Set the URI
jreq.URI = req->GetURI();
std::string strReply;
UniValue reply;
bool user_has_whitelist = g_rpc_whitelist.count(jreq.authUser);
if (!user_has_whitelist && g_rpc_whitelist_default) {
LogPrintf("RPC User %s not allowed to call any methods\n", jreq.authUser);
@ -200,13 +203,17 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
req->WriteReply(HTTP_FORBIDDEN);
return false;
}
UniValue result = tableRPC.execute(jreq);
// Send reply
strReply = JSONRPCReply(result, NullUniValue, jreq.id);
reply = JSONRPCExec(jreq);
if (jreq.IsNotification()) {
// Even though we do execute notifications, we do not respond to them
req->WriteReply(HTTP_NO_CONTENT);
return true;
}
// array of requests
} else if (valRequest.isArray()) {
// Check authorization for each request's method
if (user_has_whitelist) {
for (unsigned int reqIdx = 0; reqIdx < valRequest.size(); reqIdx++) {
if (!valRequest[reqIdx].isObject()) {
@ -223,18 +230,43 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
}
}
}
strReply = JSONRPCExecBatch(jreq, valRequest.get_array());
// Execute each request
reply = UniValue{UniValue::VARR};
bool all_notifications = true;
for (unsigned int reqIdx = 0; reqIdx < valRequest.size(); reqIdx++) {
// Batches never throw HTTP errors, they are always just included
// in "HTTP OK" responses. Notifications never get any response.
UniValue response;
try {
jreq.parse(valRequest[reqIdx]);
response = JSONRPCExec(jreq);
} catch (const UniValue& objError) {
response = JSONRPCReplyObj(NullUniValue, objError, jreq.id, jreq.m_json_version);
} catch (const std::exception& e) {
response = JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id, jreq.m_json_version);
}
if (!jreq.IsNotification()) {
reply.push_back(std::move(response));
all_notifications = false;
}
}
// All-notification batch expects no response
if (all_notifications && valRequest.size() > 0) {
req->WriteReply(HTTP_NO_CONTENT);
return true;
}
}
else
throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strReply);
req->WriteReply(HTTP_OK, reply.write() + "\n");
} catch (const UniValue& objError) {
JSONErrorReply(req, objError, jreq.id);
JSONErrorReply(req, objError, jreq);
return false;
} catch (const std::exception& e) {
JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq);
return false;
}
return true;

View File

@ -10,6 +10,7 @@
enum HTTPStatusCode
{
HTTP_OK = 200,
HTTP_NO_CONTENT = 204,
HTTP_BAD_REQUEST = 400,
HTTP_UNAUTHORIZED = 401,
HTTP_FORBIDDEN = 403,

View File

@ -26,6 +26,17 @@
*
* 1.0 spec: http://json-rpc.org/wiki/specification
* 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
*
* If the server receives a request with the JSON-RPC 2.0 marker `{"jsonrpc": "2.0"}`
* then Bitcoin will respond with a strictly specified response.
* It will only return an HTTP error code if an actual HTTP error is encountered
* such as the endpoint is not found (404) or the request is not formatted correctly (500).
* Otherwise the HTTP code is always OK (200) and RPC errors will be included in the
* response body.
*
* 2.0 spec: https://www.jsonrpc.org/specification
*
* Also see http://www.simple-is-better.org/rpc/#differences-between-1-0-and-2-0
*/
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
@ -37,21 +48,28 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
return request;
}
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
UniValue JSONRPCReplyObj(UniValue result, UniValue error, const std::optional<UniValue>& id, JSONVersion json_version)
{
UniValue reply(UniValue::VOBJ);
if (!error.isNull())
reply.pushKV("result", NullUniValue);
else
// Add JSON-RPC version number field in v2 only.
if (json_version == JSONVersion::JSON_2_0) reply.pushKV("jsonrpc", "2.0");
// Add both result and error fields in v1, even though one will be null.
// Omit the null field in v2.
if (error.isNull()) {
reply.pushKV("result", result);
reply.pushKV("error", error);
reply.pushKV("id", id);
if (json_version == JSONVersion::JSON_1_BTC) reply.pushKV("error", NullUniValue);
} else {
if (json_version == JSONVersion::JSON_1_BTC) reply.pushKV("result", NullUniValue);
reply.pushKV("error", error);
}
if (id.has_value()) reply.pushKV("id", id.value());
return reply;
}
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const JSONRPCRequest& jreq)
{
UniValue reply = JSONRPCReplyObj(result, error, id);
UniValue reply = JSONRPCReplyObj(result, error, jreq.id, jreq.m_json_version);
return reply.write() + "\n";
}
@ -170,8 +188,33 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
const UniValue& request = valRequest.get_obj();
// Parse id now so errors from here on will have the id
id = request.find_value("id");
// Check for JSON-RPC 2.0 (default 1.1)
// We must do this before looking for the id field
m_json_version = JSONVersion::JSON_1_BTC;
const UniValue& valJsonRPC = request.find_value("jsonrpc");
if (!valJsonRPC.isNull()) {
if (!valJsonRPC.isStr()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
}
if (valJsonRPC.get_str() == "1.0") {
m_json_version = JSONVersion::JSON_1_BTC;
} else if (valJsonRPC.get_str() == "2.0") {
m_json_version = JSONVersion::JSON_2_0;
} else {
throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
}
}
// Parse id now so errors from here on will have the id.
// In 1.1 a default null value is inserted if no id field was present in the request
// In 2.0 a missing id field is a notification, but `"id": null` is not
if (m_json_version != JSONVersion::JSON_2_0 || request.exists("id")) {
id = request.find_value("id");
} else {
// Because we reuse JSONRPCRequest objects with multiple valRequests in
// a batch request, we may need to reset this optional value.
id.reset();
}
// Parse method
const UniValue& valMethod{request.find_value("method")};

View File

@ -7,13 +7,20 @@
#define BITCOIN_RPC_REQUEST_H
#include <any>
#include <optional>
#include <string>
#include <univalue.h>
class JSONRPCRequest;
enum class JSONVersion {
JSON_1_BTC,
JSON_2_0
};
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
UniValue JSONRPCReplyObj(UniValue result, UniValue error, const std::optional<UniValue>& id, JSONVersion json_version = JSONVersion::JSON_1_BTC);
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const JSONRPCRequest& jreq);
UniValue JSONRPCError(int code, const std::string& message);
/** Generate a new RPC authentication cookie and write it to disk */
@ -28,7 +35,7 @@ std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in);
class JSONRPCRequest
{
public:
UniValue id;
std::optional<UniValue> id = UniValue::VNULL;
std::string strMethod;
UniValue params;
enum Mode { EXECUTE, GET_HELP, GET_ARGS } mode = EXECUTE;
@ -36,8 +43,10 @@ public:
std::string authUser;
std::string peerAddr;
std::any context;
JSONVersion m_json_version = JSONVersion::JSON_1_BTC;
void parse(const UniValue& valRequest);
[[nodiscard]] bool IsNotification() const { return !id.has_value() && m_json_version == JSONVersion::JSON_2_0; };
};
#endif // BITCOIN_RPC_REQUEST_H

View File

@ -362,36 +362,26 @@ bool IsDeprecatedRPCEnabled(const std::string& method)
return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
}
static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
UniValue JSONRPCExec(const JSONRPCRequest& jreq)
{
UniValue rpc_result(UniValue::VOBJ);
try {
jreq.parse(req);
UniValue result = tableRPC.execute(jreq);
rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
}
catch (const UniValue& objError)
{
rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id);
}
catch (const std::exception& e)
{
rpc_result = JSONRPCReplyObj(NullUniValue,
JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
UniValue result;
if (jreq.m_json_version == JSONVersion::JSON_2_0) {
// JSONRPC 2.0 behavior: only throw HTTP error if the server is actually
// broken. Otherwise errors are sent back in "HTTP OK" responses.
try {
result = tableRPC.execute(jreq);
} catch (const UniValue& objError) {
return JSONRPCReplyObj(NullUniValue, objError, jreq.id, jreq.m_json_version);
} catch (const std::exception& e) {
return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id, jreq.m_json_version);
}
} else {
// Legacy Bitcoin JSONRPC 1.0/1.2 behavior:
// Single requests may throw HTTP errors, handled by caller or client
result = tableRPC.execute(jreq);
}
return rpc_result;
}
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq)
{
UniValue ret(UniValue::VARR);
for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++)
ret.push_back(JSONRPCExecOne(jreq, vReq[reqIdx]));
return ret.write() + "\n";
return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
}
/**

View File

@ -179,6 +179,6 @@ extern CRPCTable tableRPC;
void StartRPC();
void InterruptRPC();
void StopRPC();
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
UniValue JSONRPCExec(const JSONRPCRequest& jreq);
#endif // BITCOIN_RPC_SERVER_H

View File

@ -172,7 +172,7 @@ std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList&
std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
{
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
"\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
}
@ -183,7 +183,7 @@ std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList&
params.pushKV(param.first, param.second);
}
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
"\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
}

View File

@ -552,7 +552,7 @@ BOOST_AUTO_TEST_CASE(help_example)
// test different argument types
const RPCArgList& args = {{"foo", "bar"}, {"b", true}, {"n", 1}};
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", args), "> bitcoin-cli -named test foo=bar b=true n=1\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
// test shell escape
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b'ar"}}), "> bitcoin-cli -named test foo='b'''ar'\n");
@ -565,7 +565,7 @@ BOOST_AUTO_TEST_CASE(help_example)
obj_value.pushKV("b", false);
obj_value.pushKV("n", 1);
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", obj_value}}), "> bitcoin-cli -named test name='{\"foo\":\"bar\",\"b\":false,\"n\":1}'\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
// test array params
UniValue arr_value(UniValue::VARR);
@ -573,7 +573,7 @@ BOOST_AUTO_TEST_CASE(help_example)
arr_value.push_back(false);
arr_value.push_back(1);
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", arr_value}}), "> bitcoin-cli -named test name='[\"bar\",false,1]'\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
// test types don't matter for shell
BOOST_CHECK_EQUAL(HelpExampleCliNamed("foo", {{"arg", true}}), HelpExampleCliNamed("foo", {{"arg", "true"}}));

View File

@ -4,22 +4,55 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Tests some generic aspects of the RPC interface."""
import json
import os
from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_greater_than_or_equal
from threading import Thread
import subprocess
RPC_INVALID_ADDRESS_OR_KEY = -5
RPC_INVALID_PARAMETER = -8
RPC_METHOD_NOT_FOUND = -32601
RPC_INVALID_REQUEST = -32600
RPC_PARSE_ERROR = -32700
def expect_http_status(expected_http_status, expected_rpc_code,
fcn, *args):
try:
fcn(*args)
raise AssertionError(f"Expected RPC error {expected_rpc_code}, got none")
except JSONRPCException as exc:
assert_equal(exc.error["code"], expected_rpc_code)
assert_equal(exc.http_status, expected_http_status)
class RequestBuilder:
def __init__(self):
self.id_count = 0
def rpc_request(self, fields, version=None, notification=False):
req = dict(**fields)
if not version:
req["version"] = "1.1"
if version == 1:
req["jsonrpc"] = "1.0"
if version == 2:
req["jsonrpc"] = "2.0"
if not notification:
req["id"] = self.id_count
self.id_count += 1
return req
def send_raw_rpc(node, raw_body: bytes) -> tuple[object, int]:
return node._request("POST", "/", raw_body)
def send_json_rpc(node, body: object) -> tuple[object, int]:
raw = json.dumps(body).encode("utf-8")
return send_raw_rpc(node, raw)
def expect_http_rpc_status(expected_http_status, expected_rpc_error_code, node, method, params, version=1, notification=False):
req = RequestBuilder().rpc_request({"method": method, "params": params}, version, notification)
response, status = send_json_rpc(node, req)
if expected_rpc_error_code is not None:
assert_equal(response["error"]["code"], expected_rpc_error_code)
assert_equal(status, expected_http_status)
def test_work_queue_getblock(node, got_exceeded_error):
@ -49,36 +82,170 @@ class RPCInterfaceTest(BitcoinTestFramework):
assert_equal(info['logpath'], os.path.join(self.nodes[0].chain_path, 'debug.log'))
def test_batch_request(self):
self.log.info("Testing basic JSON-RPC batch request...")
results = self.nodes[0].batch([
commands = [
# A basic request that will work fine.
{"method": "getblockcount", "id": 1},
# Request that will fail. The whole batch request should still
# work fine.
{"method": "invalidmethod", "id": 2},
{"method": "getblockcount"},
# Request that will fail. The whole batch request should still work fine.
{"method": "invalidmethod"},
# Another call that should succeed.
{"method": "getblockhash", "id": 3, "params": [0]},
])
{"method": "getblockhash", "params": [0]},
# Invalid request format
{"pizza": "sausage"}
]
builder = RequestBuilder()
result_by_id = {}
for res in results:
result_by_id[res["id"]] = res
self.log.info("Testing empty batch request...")
body = []
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
assert_equal(
response,
[]
)
assert_equal(result_by_id[1]['error'], None)
assert_equal(result_by_id[1]['result'], 0)
self.log.info("Testing default JSON-RPC 1.1 batch request...")
body = []
for cmd in commands:
body.append(builder.rpc_request(cmd))
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
# JSON 1.1: Every response has a "result" and an "error"
assert_equal(
response,
[
{"result": 0, "error": None, "id": 0},
{"result": None, "error": {"code": RPC_METHOD_NOT_FOUND, "message": "Method not found"}, "id": 1},
{"result": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", "error": None, "id": 2},
{"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Missing method"}, "id": 3}
]
)
assert_equal(result_by_id[2]['error']['code'], -32601)
assert_equal(result_by_id[2]['result'], None)
self.log.info("Testing basic JSON-RPC 1.0 batch request...")
body = []
for cmd in commands:
body.append(builder.rpc_request(cmd, version=1))
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
# JSON 1.1: Every response has a "result" and an "error"
assert_equal(
response,
[
{"result": 0, "error": None, "id": 4},
{"result": None, "error": {"code": RPC_METHOD_NOT_FOUND, "message": "Method not found"}, "id": 5},
{"result": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", "error": None, "id": 6},
{"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "Missing method"}, "id": 7}
]
)
assert_equal(result_by_id[3]['error'], None)
assert result_by_id[3]['result'] is not None
self.log.info("Testing basic JSON-RPC 2.0 batch request...")
body = []
for cmd in commands:
body.append(builder.rpc_request(cmd, version=2))
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
# JSON 2.0: Each response has either a "result" or an "error"
assert_equal(
response,
[
{"jsonrpc": "2.0", "result": 0, "id": 8},
{"jsonrpc": "2.0", "error": {"code": RPC_METHOD_NOT_FOUND, "message": "Method not found"}, "id": 9},
{"jsonrpc": "2.0", "result": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", "id": 10},
{"jsonrpc": "2.0", "error": {"code": RPC_INVALID_REQUEST, "message": "Missing method"}, "id": 11}
]
)
self.log.info("Testing mixed JSON-RPC 1.1/2.0 batch request...")
body = []
version = 1
for cmd in commands:
body.append(builder.rpc_request(cmd, version=version))
version = 2 if version == 1 else 1
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
# Responses respect the version of the individual request in the batch
assert_equal(
response,
[
{"result": 0, "error": None, "id": 12},
{"jsonrpc": "2.0", "error": {"code": RPC_METHOD_NOT_FOUND, "message": "Method not found"}, "id": 13},
{"result": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", "error": None, "id": 14},
{"jsonrpc": "2.0", "error": {"code": RPC_INVALID_REQUEST, "message": "Missing method"}, "id": 15}
]
)
self.log.info("Testing JSON-RPC 2.0 batch with notifications...")
body = []
notification = False
for cmd in commands:
body.append(builder.rpc_request(cmd, version=2, notification=notification))
notification = True
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 200)
# Notifications are never responded to, even if there is an error
assert_equal(
response,
[
{"jsonrpc": "2.0", "result": 0, "id": 16}
]
)
self.log.info("Testing JSON-RPC 2.0 batch of ALL notifications...")
body = []
for cmd in commands:
body.append(builder.rpc_request(cmd, version=2, notification=True))
response, status = send_json_rpc(self.nodes[0], body)
assert_equal(status, 204) # HTTP_NO_CONTENT
assert_equal(response, None) # not even an empty array
def test_http_status_codes(self):
self.log.info("Testing HTTP status codes for JSON-RPC requests...")
self.log.info("Testing HTTP status codes for JSON-RPC 1.1 requests...")
# OK
expect_http_rpc_status(200, None, self.nodes[0], "getblockhash", [0])
# Errors
expect_http_rpc_status(404, RPC_METHOD_NOT_FOUND, self.nodes[0], "invalidmethod", [])
expect_http_rpc_status(500, RPC_INVALID_PARAMETER, self.nodes[0], "getblockhash", [42])
# force-send invalidly formatted request
response, status = send_raw_rpc(self.nodes[0], b"this is bad")
assert_equal(response["id"], None)
assert_equal(response["error"]["code"], RPC_PARSE_ERROR)
assert_equal(status, 500)
expect_http_status(404, -32601, self.nodes[0].invalidmethod)
expect_http_status(500, -8, self.nodes[0].getblockhash, 42)
self.log.info("Testing HTTP status codes for JSON-RPC 2.0 requests...")
# OK
expect_http_rpc_status(200, None, self.nodes[0], "getblockhash", [0], 2, False)
# RPC errors but not HTTP errors
expect_http_rpc_status(200, RPC_METHOD_NOT_FOUND, self.nodes[0], "invalidmethod", [], 2, False)
expect_http_rpc_status(200, RPC_INVALID_PARAMETER, self.nodes[0], "getblockhash", [42], 2, False)
# force-send invalidly formatted requests
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": 2, "method": "getblockcount"})
assert_equal(response["id"], None)
assert_equal(response["error"]["code"], RPC_INVALID_REQUEST)
assert_equal(response["error"]["message"], "jsonrpc field must be a string")
assert_equal(status, 400)
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": "3.0", "method": "getblockcount"})
assert_equal(response["id"], None)
assert_equal(response["error"]["code"], RPC_INVALID_REQUEST)
assert_equal(response["error"]["message"], "JSON-RPC version not supported")
assert_equal(status, 400)
self.log.info("Testing HTTP status codes for JSON-RPC 2.0 notifications...")
# Not notification: id exists
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": "2.0", "id": None, "method": "getblockcount"})
assert_equal(response["result"], 0)
assert_equal(status, 200)
# Not notification: JSON 1.1
expect_http_rpc_status(200, None, self.nodes[0], "getblockcount", [], 1)
# Not notification: has "id" field
expect_http_rpc_status(200, None, self.nodes[0], "getblockcount", [], 2, False)
block_count = self.nodes[0].getblockcount()
# Notification response status code: HTTP_NO_CONTENT
expect_http_rpc_status(204, None, self.nodes[0], "generatetoaddress", [1, "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqdku202"], 2, True)
# The command worked even though there was no response
assert_equal(block_count + 1, self.nodes[0].getblockcount())
# No error response for notifications even if they are invalid
expect_http_rpc_status(204, None, self.nodes[0], "generatetoaddress", [1, "invalid_address"], 2, True)
# Sanity check: command was not executed
assert_equal(block_count + 1, self.nodes[0].getblockcount())
def test_work_queue_exceeded(self):
self.log.info("Testing work queue exceeded...")

View File

@ -160,6 +160,15 @@ class AuthServiceProxy():
raise JSONRPCException({
'code': -342, 'message': 'missing HTTP response from server'})
# Check for no-content HTTP status code, which can be returned when an
# RPC client requests a JSON-RPC 2.0 "notification" with no response.
# Currently this is only possible if clients call the _request() method
# directly to send a raw request.
if http_response.status == HTTPStatus.NO_CONTENT:
if len(http_response.read()) != 0:
raise JSONRPCException({'code': -342, 'message': 'Content received with NO CONTENT status code'})
return None, http_response.status
content_type = http_response.getheader('Content-Type')
if content_type != 'application/json':
raise JSONRPCException(