mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-18 13:17:55 +02:00
Merge 75118a608f into merged_master (Bitcoin PR bitcoin/bitcoin#27101)
This commit is contained in:
commit
c3eebcf145
13 changed files with 349 additions and 95 deletions
|
|
@ -298,7 +298,7 @@ public:
|
|||
}
|
||||
addresses.pushKV("total", total);
|
||||
result.pushKV("addresses_known", addresses);
|
||||
return JSONRPCReplyObj(result, NullUniValue, 1);
|
||||
return JSONRPCReplyObj(std::move(result), NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -367,7 +367,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, /*id=*/1, JSONRPCVersion::V1_LEGACY);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -631,7 +631,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1);
|
||||
return JSONRPCReplyObj(UniValue{result}, NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
|
||||
}
|
||||
|
||||
const std::string m_help_doc{
|
||||
|
|
@ -718,7 +718,7 @@ public:
|
|||
UniValue result(UniValue::VOBJ);
|
||||
result.pushKV("address", address_str);
|
||||
result.pushKV("blocks", reply.get_obj()["result"]);
|
||||
return JSONRPCReplyObj(result, NullUniValue, 1);
|
||||
return JSONRPCReplyObj(std::move(result), NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
|
||||
}
|
||||
protected:
|
||||
std::string address_str;
|
||||
|
|
|
|||
|
|
@ -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, UniValue objError, const JSONRPCRequest& jreq)
|
||||
{
|
||||
// Sending HTTP errors is a legacy JSON-RPC behavior.
|
||||
Assume(jreq.m_json_version != JSONRPCVersion::V2);
|
||||
|
||||
// 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 = JSONRPCReplyObj(NullUniValue, std::move(objError), jreq.id, jreq.m_json_version).write() + "\n";
|
||||
|
||||
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,23 @@ 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);
|
||||
// Legacy 1.0/1.1 behavior is for failed requests to throw
|
||||
// exceptions which return HTTP errors and RPC errors to the client.
|
||||
// 2.0 behavior is to catch exceptions and return HTTP success with
|
||||
// RPC errors, as long as there is not an actual HTTP server error.
|
||||
const bool catch_errors{jreq.m_json_version == JSONRPCVersion::V2};
|
||||
reply = JSONRPCExec(jreq, catch_errors);
|
||||
|
||||
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 +236,49 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
|
|||
}
|
||||
}
|
||||
}
|
||||
strReply = JSONRPCExecBatch(jreq, valRequest.get_array());
|
||||
|
||||
// Execute each request
|
||||
reply = UniValue::VARR;
|
||||
for (size_t i{0}; i < valRequest.size(); ++i) {
|
||||
// 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[i]);
|
||||
response = JSONRPCExec(jreq, /*catch_errors=*/true);
|
||||
} catch (UniValue& e) {
|
||||
response = JSONRPCReplyObj(NullUniValue, std::move(e), 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));
|
||||
}
|
||||
}
|
||||
// Return no response for an all-notification batch, but only if the
|
||||
// batch request is non-empty. Technically according to the JSON-RPC
|
||||
// 2.0 spec, an empty batch request should also return no response,
|
||||
// However, if the batch request is empty, it means the request did
|
||||
// not contain any JSON-RPC version numbers, so returning an empty
|
||||
// response could break backwards compatibility with old RPC clients
|
||||
// relying on previous behavior. Return an empty array instead of an
|
||||
// empty response in this case to favor being backwards compatible
|
||||
// over complying with the JSON-RPC 2.0 spec in this case.
|
||||
if (reply.size() == 0 && 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);
|
||||
} catch (const UniValue& objError) {
|
||||
JSONErrorReply(req, objError, jreq.id);
|
||||
req->WriteReply(HTTP_OK, reply.write() + "\n");
|
||||
} catch (UniValue& e) {
|
||||
JSONErrorReply(req, std::move(e), 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;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
enum HTTPStatusCode
|
||||
{
|
||||
HTTP_OK = 200,
|
||||
HTTP_NO_CONTENT = 204,
|
||||
HTTP_BAD_REQUEST = 400,
|
||||
HTTP_UNAUTHORIZED = 401,
|
||||
HTTP_FORBIDDEN = 403,
|
||||
|
|
|
|||
|
|
@ -30,6 +30,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)
|
||||
|
|
@ -41,22 +52,23 @@ 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, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version)
|
||||
{
|
||||
UniValue reply(UniValue::VOBJ);
|
||||
if (!error.isNull())
|
||||
reply.pushKV("result", NullUniValue);
|
||||
else
|
||||
reply.pushKV("result", result);
|
||||
reply.pushKV("error", error);
|
||||
reply.pushKV("id", id);
|
||||
return reply;
|
||||
}
|
||||
// Add JSON-RPC version number field in v2 only.
|
||||
if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0");
|
||||
|
||||
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
|
||||
{
|
||||
UniValue reply = JSONRPCReplyObj(result, error, id);
|
||||
return reply.write() + "\n";
|
||||
// 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", std::move(result));
|
||||
if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("error", NullUniValue);
|
||||
} else {
|
||||
if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue);
|
||||
reply.pushKV("error", std::move(error));
|
||||
}
|
||||
if (id.has_value()) reply.pushKV("id", std::move(id.value()));
|
||||
return reply;
|
||||
}
|
||||
|
||||
UniValue JSONRPCError(int code, const std::string& message)
|
||||
|
|
@ -214,7 +226,30 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
|
|||
const UniValue& request = valRequest.get_obj();
|
||||
|
||||
// Parse id now so errors from here on will have the id
|
||||
id = request.find_value("id");
|
||||
if (request.exists("id")) {
|
||||
id = request.find_value("id");
|
||||
} else {
|
||||
id = std::nullopt;
|
||||
}
|
||||
|
||||
// Check for JSON-RPC 2.0 (default 1.1)
|
||||
m_json_version = JSONRPCVersion::V1_LEGACY;
|
||||
const UniValue& jsonrpc_version = request.find_value("jsonrpc");
|
||||
if (!jsonrpc_version.isNull()) {
|
||||
if (!jsonrpc_version.isStr()) {
|
||||
throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
|
||||
}
|
||||
// The "jsonrpc" key was added in the 2.0 spec, but some older documentation
|
||||
// incorrectly included {"jsonrpc":"1.0"} in a request object, so we
|
||||
// maintain that for backwards compatibility.
|
||||
if (jsonrpc_version.get_str() == "1.0") {
|
||||
m_json_version = JSONRPCVersion::V1_LEGACY;
|
||||
} else if (jsonrpc_version.get_str() == "2.0") {
|
||||
m_json_version = JSONRPCVersion::V2;
|
||||
} else {
|
||||
throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
|
||||
}
|
||||
}
|
||||
|
||||
// Parse method
|
||||
const UniValue& valMethod{request.find_value("method")};
|
||||
|
|
|
|||
|
|
@ -7,13 +7,18 @@
|
|||
#define BITCOIN_RPC_REQUEST_H
|
||||
|
||||
#include <any>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <univalue.h>
|
||||
|
||||
enum class JSONRPCVersion {
|
||||
V1_LEGACY,
|
||||
V2
|
||||
};
|
||||
|
||||
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, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
|
||||
UniValue JSONRPCError(int code, const std::string& message);
|
||||
|
||||
/** Generate a new RPC authentication cookie and write it to disk */
|
||||
|
|
@ -28,7 +33,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 +41,10 @@ public:
|
|||
std::string authUser;
|
||||
std::string peerAddr;
|
||||
std::any context;
|
||||
JSONRPCVersion m_json_version = JSONRPCVersion::V1_LEGACY;
|
||||
|
||||
void parse(const UniValue& valRequest);
|
||||
[[nodiscard]] bool IsNotification() const { return !id.has_value() && m_json_version == JSONRPCVersion::V2; };
|
||||
};
|
||||
|
||||
// ELEMENTS:
|
||||
|
|
|
|||
|
|
@ -360,36 +360,22 @@ 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, bool catch_errors)
|
||||
{
|
||||
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 (catch_errors) {
|
||||
try {
|
||||
result = tableRPC.execute(jreq);
|
||||
} catch (UniValue& e) {
|
||||
return JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
|
||||
} catch (const std::exception& e) {
|
||||
return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_MISC_ERROR, e.what()), jreq.id, jreq.m_json_version);
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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, bool catch_errors);
|
||||
|
||||
#endif // BITCOIN_RPC_SERVER_H
|
||||
|
|
|
|||
|
|
@ -177,7 +177,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:7041/\n";
|
||||
}
|
||||
|
||||
|
|
@ -188,7 +188,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";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"}}));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue