From 0ab8ba1ac65b70f044a5e323b13d098cef33695a Mon Sep 17 00:00:00 2001 From: Karl-Johan Alm Date: Wed, 19 Jun 2019 16:49:38 +0900 Subject: rpc: fix RPC help requirements for getblocktemplate First argument is optional, and defaults to {mode:template}. --- src/rpc/mining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 86b0845738..b3b567a31e 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -302,7 +302,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request) " https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n" " https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki\n", { - {"template_request", RPCArg::Type::OBJ, RPCArg::Optional::NO, "A json object in the following spec", + {"template_request", RPCArg::Type::OBJ, "{}", "A json object in the following spec", { {"mode", RPCArg::Type::STR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "This must be set to \"template\", \"proposal\" (see BIP 23), or omitted"}, {"capabilities", RPCArg::Type::ARR, /* treat as named arg */ RPCArg::Optional::OMITTED_NAMED_ARG, "A list of strings", -- cgit v1.2.3 From 5c5e32bbe3dfa790dd8bb421fbd6301ae10b09f5 Mon Sep 17 00:00:00 2001 From: Karl-Johan Alm Date: Thu, 20 Jun 2019 02:39:38 +0900 Subject: rpc: migrate JSONRPCRequest functionality into request.cpp --- src/Makefile.am | 5 +- src/bitcoin-cli.cpp | 1 + src/rest.cpp | 1 + src/rpc/protocol.cpp | 150 -------------------------------- src/rpc/protocol.h | 23 ----- src/rpc/rawtransaction_util.cpp | 2 +- src/rpc/request.cpp | 184 ++++++++++++++++++++++++++++++++++++++++ src/rpc/request.h | 42 +++++++++ src/rpc/server.cpp | 33 ------- src/rpc/server.h | 18 +--- src/rpc/util.h | 1 + 11 files changed, 235 insertions(+), 225 deletions(-) delete mode 100644 src/rpc/protocol.cpp create mode 100644 src/rpc/request.cpp create mode 100644 src/rpc/request.h diff --git a/src/Makefile.am b/src/Makefile.am index 39e8d3d689..e4c542fa25 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -175,9 +175,10 @@ BITCOIN_CORE_H = \ rpc/blockchain.h \ rpc/client.h \ rpc/protocol.h \ - rpc/server.h \ rpc/rawtransaction_util.h \ rpc/register.h \ + rpc/request.h \ + rpc/server.h \ rpc/util.h \ scheduler.h \ script/descriptor.h \ @@ -481,7 +482,7 @@ libbitcoin_util_a_SOURCES = \ interfaces/handler.cpp \ logging.cpp \ random.cpp \ - rpc/protocol.cpp \ + rpc/request.cpp \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 38010c461e..d3419520a7 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include diff --git a/src/rest.cpp b/src/rest.cpp index ab409947d3..eba7aae50f 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp deleted file mode 100644 index 33b0130a94..0000000000 --- a/src/rpc/protocol.cpp +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright (c) 2010 Satoshi Nakamoto -// Copyright (c) 2009-2018 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include - -#include -#include -#include -#include -#include - -/** - * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, - * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were - * unspecified (HTTP errors and contents of 'error'). - * - * 1.0 spec: http://json-rpc.org/wiki/specification - * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html - */ - -UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id) -{ - UniValue request(UniValue::VOBJ); - request.pushKV("method", strMethod); - request.pushKV("params", params); - request.pushKV("id", id); - return request; -} - -UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const 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); - 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); - error.pushKV("code", code); - error.pushKV("message", message); - return error; -} - -/** Username used when cookie authentication is in use (arbitrary, only for - * recognizability in debugging/logging purposes) - */ -static const std::string COOKIEAUTH_USER = "__cookie__"; -/** Default name for auth cookie file */ -static const std::string COOKIEAUTH_FILE = ".cookie"; - -/** Get name of RPC authentication cookie file */ -static fs::path GetAuthCookieFile(bool temp=false) -{ - std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE); - if (temp) { - arg += ".tmp"; - } - return AbsPathForConfigVal(fs::path(arg)); -} - -bool GenerateAuthCookie(std::string *cookie_out) -{ - const size_t COOKIE_SIZE = 32; - unsigned char rand_pwd[COOKIE_SIZE]; - GetRandBytes(rand_pwd, COOKIE_SIZE); - std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE); - - /** the umask determines what permissions are used to create this file - - * these are set to 077 in init.cpp unless overridden with -sysperms. - */ - fsbridge::ofstream file; - fs::path filepath_tmp = GetAuthCookieFile(true); - file.open(filepath_tmp); - if (!file.is_open()) { - LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string()); - return false; - } - file << cookie; - file.close(); - - fs::path filepath = GetAuthCookieFile(false); - if (!RenameOver(filepath_tmp, filepath)) { - LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string()); - return false; - } - LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); - - if (cookie_out) - *cookie_out = cookie; - return true; -} - -bool GetAuthCookie(std::string *cookie_out) -{ - fsbridge::ifstream file; - std::string cookie; - fs::path filepath = GetAuthCookieFile(); - file.open(filepath); - if (!file.is_open()) - return false; - std::getline(file, cookie); - file.close(); - - if (cookie_out) - *cookie_out = cookie; - return true; -} - -void DeleteAuthCookie() -{ - try { - fs::remove(GetAuthCookieFile()); - } catch (const fs::filesystem_error& e) { - LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e)); - } -} - -std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num) -{ - if (!in.isArray()) { - throw std::runtime_error("Batch must be an array"); - } - std::vector batch(num); - for (size_t i=0; i= num) { - throw std::runtime_error("Batch member id larger than size"); - } - batch[id] = rec; - } - return batch; -} diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h index 6bcbccbd4f..ef6537e4ec 100644 --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -6,15 +6,6 @@ #ifndef BITCOIN_RPC_PROTOCOL_H #define BITCOIN_RPC_PROTOCOL_H -#include - -#include -#include -#include -#include - -#include - //! HTTP status codes enum HTTPStatusCode { @@ -92,18 +83,4 @@ enum RPCErrorCode RPC_FORBIDDEN_BY_SAFE_MODE = -2, //!< Server is in safe mode, and command is not allowed in safe mode }; -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 JSONRPCError(int code, const std::string& message); - -/** Generate a new RPC authentication cookie and write it to disk */ -bool GenerateAuthCookie(std::string *cookie_out); -/** Read the RPC authentication cookie from disk */ -bool GetAuthCookie(std::string *cookie_out); -/** Delete RPC authentication cookie from disk */ -void DeleteAuthCookie(); -/** Parse JSON-RPC batch reply into a vector */ -std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num); - #endif // BITCOIN_RPC_PROTOCOL_H diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp index 14fcb628eb..69ed7ffcbb 100644 --- a/src/rpc/rawtransaction_util.cpp +++ b/src/rpc/rawtransaction_util.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp new file mode 100644 index 0000000000..56cac6661e --- /dev/null +++ b/src/rpc/request.cpp @@ -0,0 +1,184 @@ +// Copyright (c) 2010 Satoshi Nakamoto +// Copyright (c) 2009-2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +#include +#include +#include +#include + +/** + * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, + * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were + * unspecified (HTTP errors and contents of 'error'). + * + * 1.0 spec: http://json-rpc.org/wiki/specification + * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html + */ + +UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id) +{ + UniValue request(UniValue::VOBJ); + request.pushKV("method", strMethod); + request.pushKV("params", params); + request.pushKV("id", id); + return request; +} + +UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const 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); + 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); + error.pushKV("code", code); + error.pushKV("message", message); + return error; +} + +/** Username used when cookie authentication is in use (arbitrary, only for + * recognizability in debugging/logging purposes) + */ +static const std::string COOKIEAUTH_USER = "__cookie__"; +/** Default name for auth cookie file */ +static const std::string COOKIEAUTH_FILE = ".cookie"; + +/** Get name of RPC authentication cookie file */ +static fs::path GetAuthCookieFile(bool temp=false) +{ + std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE); + if (temp) { + arg += ".tmp"; + } + return AbsPathForConfigVal(fs::path(arg)); +} + +bool GenerateAuthCookie(std::string *cookie_out) +{ + const size_t COOKIE_SIZE = 32; + unsigned char rand_pwd[COOKIE_SIZE]; + GetRandBytes(rand_pwd, COOKIE_SIZE); + std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE); + + /** the umask determines what permissions are used to create this file - + * these are set to 077 in init.cpp unless overridden with -sysperms. + */ + fsbridge::ofstream file; + fs::path filepath_tmp = GetAuthCookieFile(true); + file.open(filepath_tmp); + if (!file.is_open()) { + LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string()); + return false; + } + file << cookie; + file.close(); + + fs::path filepath = GetAuthCookieFile(false); + if (!RenameOver(filepath_tmp, filepath)) { + LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string()); + return false; + } + LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); + + if (cookie_out) + *cookie_out = cookie; + return true; +} + +bool GetAuthCookie(std::string *cookie_out) +{ + fsbridge::ifstream file; + std::string cookie; + fs::path filepath = GetAuthCookieFile(); + file.open(filepath); + if (!file.is_open()) + return false; + std::getline(file, cookie); + file.close(); + + if (cookie_out) + *cookie_out = cookie; + return true; +} + +void DeleteAuthCookie() +{ + try { + fs::remove(GetAuthCookieFile()); + } catch (const fs::filesystem_error& e) { + LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e)); + } +} + +std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num) +{ + if (!in.isArray()) { + throw std::runtime_error("Batch must be an array"); + } + std::vector batch(num); + for (size_t i=0; i= num) { + throw std::runtime_error("Batch member id larger than size"); + } + batch[id] = rec; + } + return batch; +} + +void JSONRPCRequest::parse(const UniValue& valRequest) +{ + // Parse request + if (!valRequest.isObject()) + 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 = find_value(request, "id"); + + // Parse method + UniValue valMethod = find_value(request, "method"); + if (valMethod.isNull()) + throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method"); + if (!valMethod.isStr()) + throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string"); + strMethod = valMethod.get_str(); + if (fLogIPs) + LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod), + this->authUser, this->peerAddr); + else + LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser); + + // Parse params + UniValue valParams = find_value(request, "params"); + if (valParams.isArray() || valParams.isObject()) + params = valParams; + else if (valParams.isNull()) + params = UniValue(UniValue::VARR); + else + throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object"); +} diff --git a/src/rpc/request.h b/src/rpc/request.h new file mode 100644 index 0000000000..99eb4f9354 --- /dev/null +++ b/src/rpc/request.h @@ -0,0 +1,42 @@ +// Copyright (c) 2010 Satoshi Nakamoto +// Copyright (c) 2009-2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_REQUEST_H +#define BITCOIN_RPC_REQUEST_H + +#include + +#include + +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 JSONRPCError(int code, const std::string& message); + +/** Generate a new RPC authentication cookie and write it to disk */ +bool GenerateAuthCookie(std::string *cookie_out); +/** Read the RPC authentication cookie from disk */ +bool GetAuthCookie(std::string *cookie_out); +/** Delete RPC authentication cookie from disk */ +void DeleteAuthCookie(); +/** Parse JSON-RPC batch reply into a vector */ +std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num); + +class JSONRPCRequest +{ +public: + UniValue id; + std::string strMethod; + UniValue params; + bool fHelp; + std::string URI; + std::string authUser; + std::string peerAddr; + + JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {} + void parse(const UniValue& valRequest); +}; + +#endif // BITCOIN_RPC_REQUEST_H diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index ca17d379bc..8b7b517473 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -329,39 +329,6 @@ bool RPCIsInWarmup(std::string *outStatus) return fRPCInWarmup; } -void JSONRPCRequest::parse(const UniValue& valRequest) -{ - // Parse request - if (!valRequest.isObject()) - 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 = find_value(request, "id"); - - // Parse method - UniValue valMethod = find_value(request, "method"); - if (valMethod.isNull()) - throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method"); - if (!valMethod.isStr()) - throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string"); - strMethod = valMethod.get_str(); - if (fLogIPs) - LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod), - this->authUser, this->peerAddr); - else - LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser); - - // Parse params - UniValue valParams = find_value(request, "params"); - if (valParams.isArray() || valParams.isObject()) - params = valParams; - else if (valParams.isNull()) - params = UniValue(UniValue::VARR); - else - throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object"); -} - bool IsDeprecatedRPCEnabled(const std::string& method) { const std::vector enabled_methods = gArgs.GetArgs("-deprecatedrpc"); diff --git a/src/rpc/server.h b/src/rpc/server.h index 431ff0bb7c..b060db5bf9 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -7,13 +7,14 @@ #define BITCOIN_RPC_SERVER_H #include -#include +#include #include #include #include #include #include +#include #include @@ -27,21 +28,6 @@ namespace RPCServer void OnStopped(std::function slot); } -class JSONRPCRequest -{ -public: - UniValue id; - std::string strMethod; - UniValue params; - bool fHelp; - std::string URI; - std::string authUser; - std::string peerAddr; - - JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {} - void parse(const UniValue& valRequest); -}; - /** Query whether RPC is running */ bool IsRPCRunning(); diff --git a/src/rpc/util.h b/src/rpc/util.h index 8762281d73..29bd629fea 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -9,6 +9,7 @@ #include #include #include +#include #include