rpc: Add "jsonrpc" field and drop null "result"/"error" fields

Only for JSON-RPC 2.0 requests.
This commit is contained in:
Matthew Zipkin 2023-07-07 14:41:23 -04:00
parent 2ca1460ae3
commit 466b90562f
No known key found for this signature in database
GPG key ID: E7E2984B6289C93A
6 changed files with 28 additions and 18 deletions

View file

@ -302,7 +302,7 @@ public:
} }
addresses.pushKV("total", total); addresses.pushKV("total", total);
result.pushKV("addresses_known", addresses); result.pushKV("addresses_known", addresses);
return JSONRPCReplyObj(std::move(result), NullUniValue, 1); return JSONRPCReplyObj(std::move(result), NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
} }
}; };
@ -371,7 +371,7 @@ public:
} }
result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]); result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]);
result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]); result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]);
return JSONRPCReplyObj(std::move(result), NullUniValue, 1); return JSONRPCReplyObj(std::move(result), NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
} }
}; };
@ -623,7 +623,7 @@ public:
} }
} }
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1); return JSONRPCReplyObj(UniValue{result}, NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
} }
const std::string m_help_doc{ const std::string m_help_doc{
@ -709,7 +709,7 @@ public:
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.pushKV("address", address_str); result.pushKV("address", address_str);
result.pushKV("blocks", reply.get_obj()["result"]); result.pushKV("blocks", reply.get_obj()["result"]);
return JSONRPCReplyObj(std::move(result), NullUniValue, 1); return JSONRPCReplyObj(std::move(result), NullUniValue, /*id=*/1, JSONRPCVersion::V1_LEGACY);
} }
protected: protected:
std::string address_str; std::string address_str;

View file

@ -73,7 +73,7 @@ static std::vector<std::vector<std::string>> g_rpcauth;
static std::map<std::string, std::set<std::string>> g_rpc_whitelist; static std::map<std::string, std::set<std::string>> g_rpc_whitelist;
static bool g_rpc_whitelist_default = false; static bool g_rpc_whitelist_default = false;
static void JSONErrorReply(HTTPRequest* req, UniValue objError, UniValue id) static void JSONErrorReply(HTTPRequest* req, UniValue objError, const JSONRPCRequest& jreq)
{ {
// Send error reply from json-rpc error object // Send error reply from json-rpc error object
int nStatus = HTTP_INTERNAL_SERVER_ERROR; int nStatus = HTTP_INTERNAL_SERVER_ERROR;
@ -84,7 +84,7 @@ static void JSONErrorReply(HTTPRequest* req, UniValue objError, UniValue id)
else if (code == RPC_METHOD_NOT_FOUND) else if (code == RPC_METHOD_NOT_FOUND)
nStatus = HTTP_NOT_FOUND; nStatus = HTTP_NOT_FOUND;
std::string strReply = JSONRPCReplyObj(NullUniValue, std::move(objError), std::move(id)).write() + "\n"; std::string strReply = JSONRPCReplyObj(NullUniValue, std::move(objError), jreq.id, jreq.m_json_version).write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
req->WriteReply(nStatus, strReply); req->WriteReply(nStatus, strReply);
@ -231,9 +231,9 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
jreq.parse(valRequest[i]); jreq.parse(valRequest[i]);
reply.push_back(JSONRPCExec(jreq)); reply.push_back(JSONRPCExec(jreq));
} catch (UniValue& e) { } catch (UniValue& e) {
reply.push_back(JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id)); reply.push_back(JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version));
} catch (const std::exception& e) { } catch (const std::exception& e) {
reply.push_back(JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id)); reply.push_back(JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id, jreq.m_json_version));
} }
} }
} }
@ -243,10 +243,10 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, reply.write() + "\n"); req->WriteReply(HTTP_OK, reply.write() + "\n");
} catch (UniValue& e) { } catch (UniValue& e) {
JSONErrorReply(req, std::move(e), jreq.id); JSONErrorReply(req, std::move(e), jreq);
return false; return false;
} catch (const std::exception& e) { } 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 false;
} }
return true; return true;

View file

@ -37,14 +37,21 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
return request; return request;
} }
UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id) UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id, JSONRPCVersion jsonrpc_version)
{ {
UniValue reply(UniValue::VOBJ); UniValue reply(UniValue::VOBJ);
if (!error.isNull()) // Add JSON-RPC version number field in v2 only.
reply.pushKV("result", NullUniValue); if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0");
else
// 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)); reply.pushKV("result", std::move(result));
reply.pushKV("error", std::move(error)); 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));
}
reply.pushKV("id", std::move(id)); reply.pushKV("id", std::move(id));
return reply; return reply;
} }

View file

@ -17,7 +17,7 @@ enum class JSONRPCVersion {
}; };
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id); UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id); UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id, JSONRPCVersion jsonrpc_version);
UniValue JSONRPCError(int code, const std::string& message); UniValue JSONRPCError(int code, const std::string& message);
/** Generate a new RPC authentication cookie and write it to disk */ /** Generate a new RPC authentication cookie and write it to disk */

View file

@ -364,7 +364,7 @@ UniValue JSONRPCExec(const JSONRPCRequest& jreq)
// but inside a batch, we just include the error object and return HTTP 200 // but inside a batch, we just include the error object and return HTTP 200
UniValue result = tableRPC.execute(jreq); UniValue result = tableRPC.execute(jreq);
return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id); return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
} }
/** /**

View file

@ -48,7 +48,10 @@ def format_request(options, idx, fields):
def format_response(options, idx, fields): def format_response(options, idx, fields):
response = {} response = {}
response.update(id=None if options.notification else idx) response.update(id=None if options.notification else idx)
response.update(result=None, error=None) if options.version == 2:
response.update(jsonrpc="2.0")
else:
response.update(result=None, error=None)
response.update(fields) response.update(fields)
if options.response_fields: if options.response_fields:
response.update(options.response_fields) response.update(options.response_fields)