mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-21 07:39:08 +01:00
rpc: JSON-RPC 2.0 should not respond to "notifications"
For JSON-RPC 2.0 requests we need to distinguish between a missing "id" field and "id":null. This is accomplished by making the JSONRPCRequest id property a std::optional<UniValue> with a default value of UniValue::VNULL. A side-effect of this change for non-2.0 requests is that request which do not specify an "id" field will no longer return "id": null in the response.
This commit is contained in:
@@ -37,7 +37,7 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
|
||||
return request;
|
||||
}
|
||||
|
||||
UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id, JSONRPCVersion jsonrpc_version)
|
||||
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version)
|
||||
{
|
||||
UniValue reply(UniValue::VOBJ);
|
||||
// Add JSON-RPC version number field in v2 only.
|
||||
@@ -52,7 +52,7 @@ UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id, JSONRPCVe
|
||||
if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue);
|
||||
reply.pushKV("error", std::move(error));
|
||||
}
|
||||
reply.pushKV("id", std::move(id));
|
||||
if (id.has_value()) reply.pushKV("id", std::move(id.value()));
|
||||
return reply;
|
||||
}
|
||||
|
||||
@@ -172,7 +172,11 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user