From cc5173a20534bd761caca6c5291cadc42a0ba5d7 Mon Sep 17 00:00:00 2001 From: svost Date: Thu, 30 Dec 2021 23:54:16 +0300 Subject: [PATCH] Cleanup --- src/bitcoinrpc.cpp | 68 ++++++++++++++++++++++++++-------------------------- src/bitcoinrpc.h | 12 ++++---- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 06263b0..b411be4 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -4,20 +4,21 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "init.h" -#include "util.h" -#include "sync.h" -#include "interface.h" #include "base58.h" #include "bitcoinrpc.h" #include "db.h" +#include "interface.h" +#include "sync.h" +#include "util.h" #undef printf -#include -#include #include +#include +#include #include +#include #define printf OutputDebugStringF @@ -113,7 +114,7 @@ std::string HexBits(unsigned int nBits) // Utilities: convert hex-encoded Values // (throws error if not hex). // -uint256 ParseHashV(const Value& v, std::string strName) +uint256 ParseHashV(const Value& v, const std::string& strName) { std::string strHex; if (v.type() == str_type) @@ -125,12 +126,12 @@ uint256 ParseHashV(const Value& v, std::string strName) return result; } -uint256 ParseHashO(const Object& o, std::string strKey) +uint256 ParseHashO(const Object& o, const std::string& strKey) { return ParseHashV(find_value(o, strKey), strKey); } -std::vector ParseHexV(const Value& v, std::string strName) +std::vector ParseHexV(const Value& v, const std::string& strName) { std::string strHex; if (v.type() == str_type) @@ -140,7 +141,7 @@ std::vector ParseHexV(const Value& v, std::string strName) return ParseHex(strHex); } -std::vector ParseHexO(const Object& o, std::string strKey) +std::vector ParseHexO(const Object& o, const std::string& strKey) { return ParseHexV(find_value(o, strKey), strKey); } @@ -150,14 +151,14 @@ std::vector ParseHexO(const Object& o, std::string strKey) /// Note: This interface may still be subject to change. /// -std::string CRPCTable::help(std::string strCommand) const +std::string CRPCTable::help(const std::string& strCommand) const { std::string strRet; std::set setDone; - for (std::map::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi) + for (const auto & mapCommand : mapCommands) { - const CRPCCommand *pcmd = mi->second; - std::string strMethod = mi->first; + const CRPCCommand *pcmd = mapCommand.second; + std::string strMethod = mapCommand.first; // We already filter duplicates, but these deprecated screw up the sort order if (strMethod.find("label") != std::string::npos) continue; @@ -170,7 +171,7 @@ std::string CRPCTable::help(std::string strCommand) const if (setDone.insert(pfn).second) (*pfn)(params, true); } - catch (std::exception& e) + catch (const std::exception& e) { // Help text is returned in an exception std::string strHelp = std::string(e.what()); @@ -194,7 +195,7 @@ Value help(const Array& params, bool fHelp) "List commands, or get help for a command."); std::string strCommand; - if (params.size() > 0) + if (!params.empty()) strCommand = params[0].get_str(); return tableRPC.help(strCommand); @@ -209,7 +210,7 @@ Value stop(const Array& params, bool fHelp) " is true or false to detach the database or not for this stop only\n" "Stop NovaCoin server (and possibly override the detachdb config value)."); // Shutdown will take long enough that the response should get back - if (params.size() > 0) + if (!params.empty()) bitdb.SetDetach(params[0].get_bool()); StartShutdown(); return "NovaCoin server stopping"; @@ -315,19 +316,18 @@ static const CRPCCommand vRPCCommands[] = CRPCTable::CRPCTable() { - unsigned int vcidx; - for (vcidx = 0; vcidx < (sizeof(vRPCCommands) / sizeof(vRPCCommands[0])); vcidx++) + for (const auto & vRPCCommand : vRPCCommands) { const CRPCCommand *pcmd; - pcmd = &vRPCCommands[vcidx]; + pcmd = &vRPCCommand; mapCommands[pcmd->name] = pcmd; } } -const CRPCCommand *CRPCTable::operator[](std::string name) const +const CRPCCommand *CRPCTable::operator[](const std::string& name) const { - std::map::const_iterator it = mapCommands.find(name); + auto it = mapCommands.find(name); if (it == mapCommands.end()) return nullptr; return (*it).second; @@ -446,7 +446,7 @@ static Object JSONRPCExecOne(const Value& req) { rpc_result = JSONRPCReplyObj(Value::null, objError, jreq.id); } - catch (std::exception& e) + catch (const std::exception& e) { rpc_result = JSONRPCReplyObj(Value::null, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id); @@ -458,8 +458,8 @@ static Object JSONRPCExecOne(const Value& req) static std::string JSONRPCExecBatch(const Array& vReq) { Array ret; - for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++) - ret.push_back(JSONRPCExecOne(vReq[reqIdx])); + for (const auto & reqIdx : vReq) + ret.push_back(JSONRPCExecOne(reqIdx)); return write_string(Value(ret), false) + "\n"; } @@ -496,11 +496,11 @@ void StartRPCServer() std::string host = GetArg("-rpchost", "127.0.0.1"); int port = GetArg("-rpcport", GetDefaultRPCPort()); - g_server = std::unique_ptr(new ix::HttpServer(port, host)); + g_server = std::make_unique(port, host); LOCK(cs_THREAD_RPCHANDLER); - g_server->setOnConnectionCallback([](ix::HttpRequestPtr request, std::shared_ptr connectionState) -> ix::HttpResponsePtr { + g_server->setOnConnectionCallback([](const ix::HttpRequestPtr& request, const std::shared_ptr& connectionState) -> ix::HttpResponsePtr { ix::WebSocketHttpHeaders headers; headers["Server"] = std::string("novacoin-json-rpc/") + FormatFullVersion(); @@ -510,12 +510,12 @@ void StartRPCServer() { printf("ThreadRPCServer incorrect password attempt from %s\n", connectionState->getRemoteIp().c_str()); connectionState->setTerminated(); - return std::make_shared(401, "Unauthorized", ix::HttpErrorCode::Ok, headers, "Not authorized"); + return std::make_shared(HTTP_UNAUTHORIZED, "Unauthorized", ix::HttpErrorCode::Ok, headers, "Not authorized"); } if (request->method != "POST") { connectionState->setTerminated(); - return std::make_shared(400, "Bad request", ix::HttpErrorCode::Ok, headers, "Bad request"); + return std::make_shared(HTTP_BAD_REQUEST, "Bad request", ix::HttpErrorCode::Ok, headers, "Bad request"); } JSONRequest jreq; @@ -552,11 +552,11 @@ void StartRPCServer() } catch(Object& objError) { - return std::make_shared(500, "Internal Server Error", ix::HttpErrorCode::Ok, headers, ErrorReply(objError, jreq.id)); + return std::make_shared(HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error", ix::HttpErrorCode::Ok, headers, ErrorReply(objError, jreq.id)); } - catch(std::exception& e) + catch(const std::exception& e) { - return std::make_shared(500, "Internal Server Error", ix::HttpErrorCode::Ok, headers, ErrorReply(JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id)); + return std::make_shared(HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error", ix::HttpErrorCode::Ok, headers, ErrorReply(JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id)); } }); @@ -581,7 +581,7 @@ void StopRPCServer() vnThreadsRunning[THREAD_RPCLISTENER]--; } -json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_spirit::Array ¶ms) const + json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_spirit::Array ¶ms) const { // Find method const CRPCCommand *pcmd = tableRPC[strMethod]; @@ -608,7 +608,7 @@ json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_s } return result; } - catch (std::exception& e) + catch (const std::exception& e) { throw JSONRPCError(RPC_MISC_ERROR, e.what()); } @@ -661,7 +661,7 @@ Object CallRPC(const std::string& strMethod, const Array& params) // Receive reply if (nStatus == HTTP_UNAUTHORIZED) throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); - else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR) + else if (nStatus >= HTTP_BAD_REQUEST && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR) throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus)); else if (strReply.empty()) throw std::runtime_error("no response from server"); diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 5f76f6f..d0a3712 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -109,8 +109,8 @@ private: std::map mapCommands; public: CRPCTable(); - const CRPCCommand* operator[](std::string name) const; - std::string help(std::string name) const; + const CRPCCommand* operator[](const std::string& name) const; + std::string help(const std::string& name) const; /** * Execute a method. @@ -146,10 +146,10 @@ extern void EnsureWalletIsUnlocked(); // Utilities: convert hex-encoded Values // (throws error if not hex). // -extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName); -extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey); -extern std::vector ParseHexV(const json_spirit::Value& v, std::string strName); -extern std::vector ParseHexO(const json_spirit::Object& o, std::string strKey); +extern uint256 ParseHashV(const json_spirit::Value& v, const std::string& strName); +extern uint256 ParseHashO(const json_spirit::Object& o, const std::string& strKey); +extern std::vector ParseHexV(const json_spirit::Value& v, const std::string& strName); +extern std::vector ParseHexO(const json_spirit::Object& o, const std::string& strKey); extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp); -- 1.7.1