aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthew Zipkin <pinheadmz@gmail.com>2024-01-23 10:33:26 -0500
committerRyan Ofsky <ryan@ofsky.org>2024-03-07 19:13:15 -0500
commitdf6e3756d6feaf1856e7886820b70874209fd90b (patch)
treee2645fd3585367a5169e07deadbff7d8d3f9e375 /src
parent09416f9ec445e4d6bb277400758083b0b4e8b174 (diff)
rpc: Avoid copies in JSONRPCReplyObj()
Change parameters from const references to values, so they can be moved into the reply instead of copied. Also update callers to move instead of copy.
Diffstat (limited to 'src')
-rw-r--r--src/bitcoin-cli.cpp6
-rw-r--r--src/httprpc.cpp10
-rw-r--r--src/rpc/request.cpp14
-rw-r--r--src/rpc/request.h3
-rw-r--r--src/rpc/server.cpp6
5 files changed, 16 insertions, 23 deletions
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index 45db7a9a66..3591f7248b 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -302,7 +302,7 @@ public:
}
addresses.pushKV("total", total);
result.pushKV("addresses_known", addresses);
- return JSONRPCReplyObj(result, NullUniValue, 1);
+ return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
}
};
@@ -371,7 +371,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);
}
};
@@ -709,7 +709,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, 1);
}
protected:
std::string address_str;
diff --git a/src/httprpc.cpp b/src/httprpc.cpp
index c72dbf10bc..aef20c24fc 100644
--- a/src/httprpc.cpp
+++ b/src/httprpc.cpp
@@ -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 bool g_rpc_whitelist_default = false;
-static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const UniValue& id)
+static void JSONErrorReply(HTTPRequest* req, UniValue objError, UniValue id)
{
// Send error reply from json-rpc error object
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
@@ -84,7 +84,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), std::move(id)).write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(nStatus, strReply);
@@ -203,7 +203,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
UniValue result = tableRPC.execute(jreq);
// Send reply
- strReply = JSONRPCReply(result, NullUniValue, jreq.id);
+ strReply = JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id).write() + "\n";
// array of requests
} else if (valRequest.isArray()) {
@@ -230,8 +230,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strReply);
- } catch (const UniValue& objError) {
- JSONErrorReply(req, objError, jreq.id);
+ } catch (UniValue& e) {
+ JSONErrorReply(req, std::move(e), jreq.id);
return false;
} catch (const std::exception& e) {
JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp
index b7acd62ee3..12726ecc88 100644
--- a/src/rpc/request.cpp
+++ b/src/rpc/request.cpp
@@ -37,24 +37,18 @@ 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, UniValue id)
{
UniValue reply(UniValue::VOBJ);
if (!error.isNull())
reply.pushKV("result", NullUniValue);
else
- reply.pushKV("result", result);
- reply.pushKV("error", error);
- reply.pushKV("id", id);
+ reply.pushKV("result", std::move(result));
+ reply.pushKV("error", std::move(error));
+ reply.pushKV("id", std::move(id));
return reply;
}
-std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
-{
- UniValue reply = JSONRPCReplyObj(result, error, id);
- return reply.write() + "\n";
-}
-
UniValue JSONRPCError(int code, const std::string& message)
{
UniValue error(UniValue::VOBJ);
diff --git a/src/rpc/request.h b/src/rpc/request.h
index a682c58d96..116ebb8aac 100644
--- a/src/rpc/request.h
+++ b/src/rpc/request.h
@@ -12,8 +12,7 @@
#include <univalue.h>
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, UniValue id);
UniValue JSONRPCError(int code, const std::string& message);
/** Generate a new RPC authentication cookie and write it to disk */
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 4883341522..fd18a7f9ec 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -366,11 +366,11 @@ static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
jreq.parse(req);
UniValue result = tableRPC.execute(jreq);
- rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
+ rpc_result = JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id);
}
- catch (const UniValue& objError)
+ catch (UniValue& e)
{
- rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id);
+ rpc_result = JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id);
}
catch (const std::exception& e)
{