From 15982a8b69ec6ab3c3a6bf71fc6a9b681d3ff541 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 20 Aug 2014 15:15:16 -0400 Subject: Convert tree to using univalue. Eliminate all json_spirit uses. --- src/rpcserver.cpp | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src/rpcserver.cpp') diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 3f74517a67..da0a8048b2 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -27,7 +27,8 @@ #include #include #include -#include "json/json_spirit_writer_template.h" + +#include "json_spirit_wrapper.h" using namespace boost::asio; using namespace json_spirit; @@ -89,30 +90,30 @@ void RPCTypeCheck(const Array& params, break; const Value& v = params[i]; - if (!((v.type() == t) || (fAllowNull && (v.type() == null_type)))) + if (!((v.type() == t) || (fAllowNull && (v.isNull())))) { string err = strprintf("Expected type %s, got %s", - Value_type_name[t], Value_type_name[v.type()]); + uvTypeName(t), uvTypeName(v.type())); throw JSONRPCError(RPC_TYPE_ERROR, err); } i++; } } -void RPCTypeCheck(const Object& o, - const map& typesExpected, +void RPCTypeCheckObj(const UniValue& o, + const map& typesExpected, bool fAllowNull) { BOOST_FOREACH(const PAIRTYPE(string, Value_type)& t, typesExpected) { const Value& v = find_value(o, t.first); - if (!fAllowNull && v.type() == null_type) + if (!fAllowNull && v.isNull()) throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first)); - if (!((v.type() == t.second) || (fAllowNull && (v.type() == null_type)))) + if (!((v.type() == t.second) || (fAllowNull && (v.isNull())))) { string err = strprintf("Expected type %s for %s, got %s", - Value_type_name[t.second], t.first, Value_type_name[v.type()]); + uvTypeName(t.second), t.first, uvTypeName(v.type())); throw JSONRPCError(RPC_TYPE_ERROR, err); } } @@ -142,7 +143,7 @@ Value ValueFromAmount(const CAmount& amount) uint256 ParseHashV(const Value& v, string strName) { string strHex; - if (v.type() == str_type) + if (v.isStr()) strHex = v.get_str(); if (!IsHex(strHex)) // Note: IsHex("") is false throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); @@ -157,7 +158,7 @@ uint256 ParseHashO(const Object& o, string strKey) vector ParseHexV(const Value& v, string strName) { string strHex; - if (v.type() == str_type) + if (v.isStr()) strHex = v.get_str(); if (!IsHex(strHex)) throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); @@ -417,7 +418,7 @@ void ErrorReply(std::ostream& stream, const Object& objError, const Value& id) int code = find_value(objError, "code").get_int(); if (code == RPC_INVALID_REQUEST) nStatus = HTTP_BAD_REQUEST; else if (code == RPC_METHOD_NOT_FOUND) nStatus = HTTP_NOT_FOUND; - string strReply = JSONRPCReply(Value::null, objError, id); + string strReply = JSONRPCReply(NullUniValue, objError, id); stream << HTTPReply(nStatus, strReply, false) << std::flush; } @@ -828,14 +829,14 @@ public: string strMethod; Array params; - JSONRequest() { id = Value::null; } + JSONRequest() { id = NullUniValue; } void parse(const Value& valRequest); }; void JSONRequest::parse(const Value& valRequest) { // Parse request - if (valRequest.type() != obj_type) + if (!valRequest.isObject()) throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object"); const Object& request = valRequest.get_obj(); @@ -844,9 +845,9 @@ void JSONRequest::parse(const Value& valRequest) // Parse method Value valMethod = find_value(request, "method"); - if (valMethod.type() == null_type) + if (valMethod.isNull()) throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method"); - if (valMethod.type() != str_type) + if (!valMethod.isStr()) throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string"); strMethod = valMethod.get_str(); if (strMethod != "getblocktemplate") @@ -854,9 +855,9 @@ void JSONRequest::parse(const Value& valRequest) // Parse params Value valParams = find_value(request, "params"); - if (valParams.type() == array_type) + if (valParams.isArray()) params = valParams.get_array(); - else if (valParams.type() == null_type) + else if (valParams.isNull()) params = Array(); else throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array"); @@ -872,15 +873,15 @@ static Object JSONRPCExecOne(const Value& req) jreq.parse(req); Value result = tableRPC.execute(jreq.strMethod, jreq.params); - rpc_result = JSONRPCReplyObj(result, Value::null, jreq.id); + rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id); } catch (const Object& objError) { - rpc_result = JSONRPCReplyObj(Value::null, objError, jreq.id); + rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id); } catch (const std::exception& e) { - rpc_result = JSONRPCReplyObj(Value::null, + rpc_result = JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id); } @@ -893,7 +894,7 @@ static string JSONRPCExecBatch(const Array& vReq) for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++) ret.push_back(JSONRPCExecOne(vReq[reqIdx])); - return write_string(Value(ret), false) + "\n"; + return ret.write() + "\n"; } static bool HTTPReq_JSONRPC(AcceptedConnection *conn, @@ -925,7 +926,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn, { // Parse request Value valRequest; - if (!read_string(strRequest, valRequest)) + if (!valRequest.read(strRequest)) throw JSONRPCError(RPC_PARSE_ERROR, "Parse error"); // Return immediately if in warmup @@ -938,16 +939,16 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn, string strReply; // singleton request - if (valRequest.type() == obj_type) { + if (valRequest.isObject()) { jreq.parse(valRequest); Value result = tableRPC.execute(jreq.strMethod, jreq.params); // Send reply - strReply = JSONRPCReply(result, Value::null, jreq.id); + strReply = JSONRPCReply(result, NullUniValue, jreq.id); // array of requests - } else if (valRequest.type() == array_type) + } else if (valRequest.isArray()) strReply = JSONRPCExecBatch(valRequest.get_array()); else throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error"); -- cgit v1.2.3 From 6c7bee062437acbc078533fdcf8e53794031bf99 Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Sun, 10 May 2015 14:48:35 +0200 Subject: expicit set UniValue type to avoid empty values --- src/rpcserver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rpcserver.cpp') diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index da0a8048b2..3a54f1670b 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -866,7 +866,7 @@ void JSONRequest::parse(const Value& valRequest) static Object JSONRPCExecOne(const Value& req) { - Object rpc_result; + UniValue rpc_result(UniValue::VOBJ); JSONRequest jreq; try { -- cgit v1.2.3 From 3df0411ad9fd75fb27af53e44835d41f5480fe3f Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 13 May 2015 21:29:19 +0200 Subject: remove JSON Spirit UniValue wrapper --- src/rpcserver.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/rpcserver.cpp') diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 3a54f1670b..cb746508b1 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -135,7 +135,7 @@ CAmount AmountFromValue(const Value& value) return nAmount; } -Value ValueFromAmount(const CAmount& amount) +UniValue ValueFromAmount(const CAmount& amount) { return (double)amount / (double)COIN; } @@ -196,7 +196,7 @@ string CRPCTable::help(string strCommand) const continue; try { - Array params; + UniValue params; rpcfn_type pfn = pcmd->actor; if (setDone.insert(pfn).second) (*pfn)(params, true); @@ -229,7 +229,7 @@ string CRPCTable::help(string strCommand) const return strRet; } -Value help(const Array& params, bool fHelp) +UniValue help(const Array& params, bool fHelp) { if (fHelp || params.size() > 1) throw runtime_error( @@ -249,7 +249,7 @@ Value help(const Array& params, bool fHelp) } -Value stop(const Array& params, bool fHelp) +UniValue stop(const Array& params, bool fHelp) { // Accept the deprecated and ignored 'detach' boolean argument if (fHelp || params.size() > 1) @@ -825,9 +825,9 @@ void RPCRunLater(const std::string& name, boost::function func, int6 class JSONRequest { public: - Value id; + UniValue id; string strMethod; - Array params; + UniValue params; JSONRequest() { id = NullUniValue; } void parse(const Value& valRequest); @@ -844,7 +844,7 @@ void JSONRequest::parse(const Value& valRequest) id = find_value(request, "id"); // Parse method - Value valMethod = find_value(request, "method"); + UniValue valMethod = find_value(request, "method"); if (valMethod.isNull()) throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method"); if (!valMethod.isStr()) @@ -854,7 +854,7 @@ void JSONRequest::parse(const Value& valRequest) LogPrint("rpc", "ThreadRPCServer method=%s\n", SanitizeString(strMethod)); // Parse params - Value valParams = find_value(request, "params"); + UniValue valParams = find_value(request, "params"); if (valParams.isArray()) params = valParams.get_array(); else if (valParams.isNull()) @@ -864,7 +864,7 @@ void JSONRequest::parse(const Value& valRequest) } -static Object JSONRPCExecOne(const Value& req) +static UniValue JSONRPCExecOne(const Value& req) { UniValue rpc_result(UniValue::VOBJ); @@ -872,7 +872,7 @@ static Object JSONRPCExecOne(const Value& req) try { jreq.parse(req); - Value result = tableRPC.execute(jreq.strMethod, jreq.params); + UniValue result = tableRPC.execute(jreq.strMethod, jreq.params); rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id); } catch (const Object& objError) @@ -890,7 +890,7 @@ static Object JSONRPCExecOne(const Value& req) static string JSONRPCExecBatch(const Array& vReq) { - Array ret; + UniValue ret; for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++) ret.push_back(JSONRPCExecOne(vReq[reqIdx])); @@ -925,7 +925,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn, try { // Parse request - Value valRequest; + UniValue valRequest; if (!valRequest.read(strRequest)) throw JSONRPCError(RPC_PARSE_ERROR, "Parse error"); @@ -942,7 +942,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn, if (valRequest.isObject()) { jreq.parse(valRequest); - Value result = tableRPC.execute(jreq.strMethod, jreq.params); + UniValue result = tableRPC.execute(jreq.strMethod, jreq.params); // Send reply strReply = JSONRPCReply(result, NullUniValue, jreq.id); @@ -1005,7 +1005,7 @@ void ServiceConnection(AcceptedConnection *conn) } } -json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_spirit::Array ¶ms) const +UniValue CRPCTable::execute(const std::string &strMethod, const UniValue ¶ms) const { // Find method const CRPCCommand *pcmd = tableRPC[strMethod]; -- cgit v1.2.3 From 9a8897f4ac992741e153d88b54bd2cde877c713d Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Mon, 18 May 2015 14:02:18 +0200 Subject: Remove JSON Spirit wrapper, remove JSON Spirit leftovers - implement find_value() function for UniValue - replace all Array/Value/Object types with UniValues, remove JSON Spirit to UniValue wrapper - remove JSON Spirit sources --- src/rpcserver.cpp | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'src/rpcserver.cpp') diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index cb746508b1..18e74e03cf 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -28,10 +28,9 @@ #include #include -#include "json_spirit_wrapper.h" +#include "univalue/univalue.h" using namespace boost::asio; -using namespace json_spirit; using namespace RPCServer; using namespace std; @@ -79,17 +78,17 @@ void RPCServer::OnPostCommand(boost::function slot) g_rpcSignals.PostCommand.connect(boost::bind(slot, _1)); } -void RPCTypeCheck(const Array& params, - const list& typesExpected, +void RPCTypeCheck(const UniValue& params, + const list& typesExpected, bool fAllowNull) { unsigned int i = 0; - BOOST_FOREACH(Value_type t, typesExpected) + BOOST_FOREACH(UniValue::VType t, typesExpected) { if (params.size() <= i) break; - const Value& v = params[i]; + const UniValue& v = params[i]; if (!((v.type() == t) || (fAllowNull && (v.isNull())))) { string err = strprintf("Expected type %s, got %s", @@ -104,9 +103,9 @@ void RPCTypeCheckObj(const UniValue& o, const map& typesExpected, bool fAllowNull) { - BOOST_FOREACH(const PAIRTYPE(string, Value_type)& t, typesExpected) + BOOST_FOREACH(const PAIRTYPE(string, UniValue::VType)& t, typesExpected) { - const Value& v = find_value(o, t.first); + const UniValue& v = find_value(o, t.first); if (!fAllowNull && v.isNull()) throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first)); @@ -124,7 +123,7 @@ static inline int64_t roundint64(double d) return (int64_t)(d > 0 ? d + 0.5 : d - 0.5); } -CAmount AmountFromValue(const Value& value) +CAmount AmountFromValue(const UniValue& value) { double dAmount = value.get_real(); if (dAmount <= 0.0 || dAmount > 21000000.0) @@ -140,7 +139,7 @@ UniValue ValueFromAmount(const CAmount& amount) return (double)amount / (double)COIN; } -uint256 ParseHashV(const Value& v, string strName) +uint256 ParseHashV(const UniValue& v, string strName) { string strHex; if (v.isStr()) @@ -151,11 +150,11 @@ uint256 ParseHashV(const Value& v, string strName) result.SetHex(strHex); return result; } -uint256 ParseHashO(const Object& o, string strKey) +uint256 ParseHashO(const UniValue& o, string strKey) { return ParseHashV(find_value(o, strKey), strKey); } -vector ParseHexV(const Value& v, string strName) +vector ParseHexV(const UniValue& v, string strName) { string strHex; if (v.isStr()) @@ -164,7 +163,7 @@ vector ParseHexV(const Value& v, string strName) throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); return ParseHex(strHex); } -vector ParseHexO(const Object& o, string strKey) +vector ParseHexO(const UniValue& o, string strKey) { return ParseHexV(find_value(o, strKey), strKey); } @@ -229,7 +228,7 @@ string CRPCTable::help(string strCommand) const return strRet; } -UniValue help(const Array& params, bool fHelp) +UniValue help(const UniValue& params, bool fHelp) { if (fHelp || params.size() > 1) throw runtime_error( @@ -249,7 +248,7 @@ UniValue help(const Array& params, bool fHelp) } -UniValue stop(const Array& params, bool fHelp) +UniValue stop(const UniValue& params, bool fHelp) { // Accept the deprecated and ignored 'detach' boolean argument if (fHelp || params.size() > 1) @@ -411,7 +410,7 @@ bool HTTPAuthorized(map& mapHeaders) return TimingResistantEqual(strUserPass, strRPCUserColonPass); } -void ErrorReply(std::ostream& stream, const Object& objError, const Value& id) +void ErrorReply(std::ostream& stream, const UniValue& objError, const UniValue& id) { // Send error reply from json-rpc error object int nStatus = HTTP_INTERNAL_SERVER_ERROR; @@ -830,15 +829,15 @@ public: UniValue params; JSONRequest() { id = NullUniValue; } - void parse(const Value& valRequest); + void parse(const UniValue& valRequest); }; -void JSONRequest::parse(const Value& valRequest) +void JSONRequest::parse(const UniValue& valRequest) { // Parse request if (!valRequest.isObject()) throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object"); - const Object& request = valRequest.get_obj(); + const UniValue& request = valRequest.get_obj(); // Parse id now so errors from here on will have the id id = find_value(request, "id"); @@ -858,13 +857,13 @@ void JSONRequest::parse(const Value& valRequest) if (valParams.isArray()) params = valParams.get_array(); else if (valParams.isNull()) - params = Array(); + params = UniValue(UniValue::VARR); else throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array"); } -static UniValue JSONRPCExecOne(const Value& req) +static UniValue JSONRPCExecOne(const UniValue& req) { UniValue rpc_result(UniValue::VOBJ); @@ -875,7 +874,7 @@ static UniValue JSONRPCExecOne(const Value& req) UniValue result = tableRPC.execute(jreq.strMethod, jreq.params); rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id); } - catch (const Object& objError) + catch (const UniValue& objError) { rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id); } @@ -888,7 +887,7 @@ static UniValue JSONRPCExecOne(const Value& req) return rpc_result; } -static string JSONRPCExecBatch(const Array& vReq) +static string JSONRPCExecBatch(const UniValue& vReq) { UniValue ret; for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++) @@ -955,7 +954,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn, conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, strReply.size()) << strReply << std::flush; } - catch (const Object& objError) + catch (const UniValue& objError) { ErrorReply(conn->stream(), objError, jreq.id); return false; -- cgit v1.2.3 From 8f7e4abbe6d6c814f5f3c069e5b3067920689dbe Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Tue, 2 Jun 2015 11:41:00 +0200 Subject: fix rpc batching univalue issue --- src/rpcserver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rpcserver.cpp') diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 18e74e03cf..a92ec0b29b 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -889,7 +889,7 @@ static UniValue JSONRPCExecOne(const UniValue& req) static string JSONRPCExecBatch(const UniValue& vReq) { - UniValue ret; + UniValue ret(UniValue::VARR); for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++) ret.push_back(JSONRPCExecOne(vReq[reqIdx])); -- cgit v1.2.3