Skip to content

Commit

Permalink
Convert tree to using univalue. Eliminate all json_spirit uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Garzik authored and jonasschnelli committed Jun 4, 2015
1 parent 5e3060c commit 15982a8
Show file tree
Hide file tree
Showing 23 changed files with 321 additions and 205 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ endif

bitcoin_cli_LDADD = \
$(LIBBITCOIN_CLI) \
$(LIBBITCOIN_UNIVALUE) \
$(LIBBITCOIN_UTIL) \
$(LIBSECP256K1)

Expand Down
20 changes: 9 additions & 11 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Object CallRPC(const string& strMethod, const Array& params)

// Parse reply
Value valReply;
if (!read_string(strReply, valReply))
if (!valReply.read(strReply))
throw runtime_error("couldn't parse reply from server");
const Object& reply = valReply.get_obj();
if (reply.empty())
Expand Down Expand Up @@ -176,29 +176,27 @@ int CommandLineRPC(int argc, char *argv[])
const bool fWait = GetBoolArg("-rpcwait", false);
do {
try {
const Object reply = CallRPC(strMethod, params);
// Execute
Object reply = CallRPC(strMethod, params);

// Parse reply
const Value& result = find_value(reply, "result");
const Value& error = find_value(reply, "error");

if (error.type() != null_type) {
if (!error.isNull()) {
// Error
const int code = find_value(error.get_obj(), "code").get_int();
if (fWait && code == RPC_IN_WARMUP)
throw CConnectionFailed("server in warmup");
strPrint = "error: " + write_string(error, false);
strPrint = "error: " + error.write();
int code = error["code"].get_int();
nRet = abs(code);
} else {
// Result
if (result.type() == null_type)
if (result.isNull())
strPrint = "";
else if (result.type() == str_type)
else if (result.isStr())
strPrint = result.get_str();
else
strPrint = write_string(result, true);
strPrint = result.write(2);
}

// Connection succeeded, no need to retry.
break;
}
Expand Down
17 changes: 17 additions & 0 deletions src/json_spirit_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __JSON_SPIRIT_WRAPPER_H__
#define __JSON_SPIRIT_WRAPPER_H__

#include "univalue/univalue.h"

namespace json_spirit {

typedef UniValue Value;
typedef UniValue Array;
typedef UniValue Object;
typedef UniValue::VType Value_type;

}

#define find_value(val,key) (val[key])

#endif // __JSON_SPIRIT_WRAPPER_H__
18 changes: 11 additions & 7 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include "rpcclient.h"
#include "util.h"

#include "json/json_spirit_value.h"

#include <openssl/crypto.h>

#include "univalue/univalue.h"

#ifdef ENABLE_WALLET
#include <db_cxx.h>
#endif
Expand Down Expand Up @@ -167,21 +167,25 @@ void RPCExecutor::request(const QString &command)
std::string strPrint;
// Convert argument list to JSON objects in method-dependent way,
// and pass it along with the method name to the dispatcher.
json_spirit::Value result = tableRPC.execute(
UniValue result = tableRPC.execute(
args[0],
RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));

// Format result reply
if (result.type() == json_spirit::null_type)
if (result.isNull())
strPrint = "";
else if (result.type() == json_spirit::str_type)
else if (result.isStr())
strPrint = result.get_str();
else
strPrint = write_string(result, true);
strPrint = result.write(2);

emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
}
<<<<<<< HEAD
catch (const json_spirit::Object& objError)
=======
catch (UniValue& objError)
>>>>>>> Convert tree to using univalue. Eliminate all json_spirit uses.
{
try // Nice formatting for standard-format error
{
Expand All @@ -191,7 +195,7 @@ void RPCExecutor::request(const QString &command)
}
catch (const std::runtime_error&) // raised when converting to invalid type, i.e. missing code or message
{ // Show raw JSON object
emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(objError.write()));
}
}
catch (const std::exception& e)
Expand Down
16 changes: 11 additions & 5 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <stdint.h>

#include "json/json_spirit_value.h"
#include "json_spirit_wrapper.h"

using namespace json_spirit;
using namespace std;
Expand Down Expand Up @@ -206,7 +206,13 @@ Value getrawmempool(const Array& params, bool fHelp)
if (mempool.exists(txin.prevout.hash))
setDepends.insert(txin.prevout.hash.ToString());
}
Array depends(setDepends.begin(), setDepends.end());

UniValue depends;
BOOST_FOREACH(const string& dep, setDepends)
{
depends.push_back(dep);
}

info.push_back(Pair("depends", depends));
o.push_back(Pair(hash.ToString(), info));
}
Expand Down Expand Up @@ -412,14 +418,14 @@ Value gettxout(const Array& params, bool fHelp)
LOCK(mempool.cs);
CCoinsViewMemPool view(pcoinsTip, mempool);
if (!view.GetCoins(hash, coins))
return Value::null;
return NullUniValue;
mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool
} else {
if (!pcoinsTip->GetCoins(hash, coins))
return Value::null;
return NullUniValue;
}
if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull())
return Value::null;
return NullUniValue;

BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
CBlockIndex *pindex = it->second;
Expand Down
2 changes: 1 addition & 1 deletion src/rpcclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
// parse string as JSON, insert bool/number/object/etc. value
else {
Value jVal;
if (!read_string(strVal, jVal))
if (!jVal.read(strVal))
throw runtime_error(string("Error parsing JSON:")+strVal);
params.push_back(jVal);
}
Expand Down
4 changes: 1 addition & 3 deletions src/rpcclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#ifndef BITCOIN_RPCCLIENT_H
#define BITCOIN_RPCCLIENT_H

#include "json/json_spirit_reader_template.h"
#include "json/json_spirit_utils.h"
#include "json/json_spirit_writer_template.h"
#include "json_spirit_wrapper.h"

json_spirit::Array RPCConvertValues(const std::string& strMethod, const std::vector<std::string>& strParams);

Expand Down
19 changes: 9 additions & 10 deletions src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

#include <boost/assign/list_of.hpp>

#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"
#include "json_spirit_wrapper.h"

using namespace json_spirit;
using namespace std;
Expand Down Expand Up @@ -216,7 +215,7 @@ Value setgenerate(const Array& params, bool fHelp)
mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);

return Value::null;
return NullUniValue;
}
#endif

Expand Down Expand Up @@ -382,14 +381,14 @@ Value getblocktemplate(const Array& params, bool fHelp)
LOCK(cs_main);

std::string strMode = "template";
Value lpval = Value::null;
Value lpval = NullUniValue;
if (params.size() > 0)
{
const Object& oparam = params[0].get_obj();
const Value& modeval = find_value(oparam, "mode");
if (modeval.type() == str_type)
if (modeval.isStr())
strMode = modeval.get_str();
else if (modeval.type() == null_type)
else if (modeval.isNull())
{
/* Do nothing */
}
Expand Down Expand Up @@ -439,14 +438,14 @@ Value getblocktemplate(const Array& params, bool fHelp)

static unsigned int nTransactionsUpdatedLast;

if (lpval.type() != null_type)
if (!lpval.isNull())
{
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
uint256 hashWatchedChain;
boost::system_time checktxtime;
unsigned int nTransactionsUpdatedLastLP;

if (lpval.type() == str_type)
if (lpval.isStr())
{
// Format: <hashBestChain><nTransactionsUpdatedLast>
std::string lpstr = lpval.get_str();
Expand Down Expand Up @@ -686,7 +685,7 @@ Value estimatefee(const Array& params, bool fHelp)
+ HelpExampleCli("estimatefee", "6")
);

RPCTypeCheck(params, boost::assign::list_of(int_type));
RPCTypeCheck(params, boost::assign::list_of(UniValue::VNUM));

int nBlocks = params[0].get_int();
if (nBlocks < 1)
Expand Down Expand Up @@ -718,7 +717,7 @@ Value estimatepriority(const Array& params, bool fHelp)
+ HelpExampleCli("estimatepriority", "6")
);

RPCTypeCheck(params, boost::assign::list_of(int_type));
RPCTypeCheck(params, boost::assign::list_of(UniValue::VNUM));

int nBlocks = params[0].get_int();
if (nBlocks < 1)
Expand Down
5 changes: 2 additions & 3 deletions src/rpcmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#include <stdint.h>

#include <boost/assign/list_of.hpp>
#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"
#include "json_spirit_wrapper.h"

using namespace json_spirit;
using namespace std;
Expand Down Expand Up @@ -204,7 +203,7 @@ Value validateaddress(const Array& params, bool fHelp)
if (mine != ISMINE_NO) {
ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? true: false));
Object detail = boost::apply_visitor(DescribeAddressVisitor(mine), dest);
ret.insert(ret.end(), detail.begin(), detail.end());
ret.pushKVs(detail);
}
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
Expand Down
8 changes: 4 additions & 4 deletions src/rpcnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <boost/foreach.hpp>

#include "json/json_spirit_value.h"
#include "json_spirit_wrapper.h"

using namespace json_spirit;
using namespace std;
Expand Down Expand Up @@ -59,7 +59,7 @@ Value ping(const Array& params, bool fHelp)
pNode->fPingQueued = true;
}

return Value::null;
return NullUniValue;
}

static void CopyNodeStats(std::vector<CNodeStats>& vstats)
Expand Down Expand Up @@ -190,7 +190,7 @@ Value addnode(const Array& params, bool fHelp)
{
CAddress addr;
OpenNetworkConnection(addr, NULL, strNode.c_str());
return Value::null;
return NullUniValue;
}

LOCK(cs_vAddedNodes);
Expand All @@ -212,7 +212,7 @@ Value addnode(const Array& params, bool fHelp)
vAddedNodes.erase(it);
}

return Value::null;
return NullUniValue;
}

Value getaddednodeinfo(const Array& params, bool fHelp)
Expand Down
10 changes: 5 additions & 5 deletions src/rpcprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/shared_ptr.hpp>
#include "json/json_spirit_writer_template.h"
#include "json_spirit_wrapper.h"

using namespace std;
using namespace json_spirit;
Expand Down Expand Up @@ -260,14 +260,14 @@ string JSONRPCRequest(const string& strMethod, const Array& params, const Value&
request.push_back(Pair("method", strMethod));
request.push_back(Pair("params", params));
request.push_back(Pair("id", id));
return write_string(Value(request), false) + "\n";
return request.write() + "\n";
}

Object JSONRPCReplyObj(const Value& result, const Value& error, const Value& id)
{
Object reply;
if (error.type() != null_type)
reply.push_back(Pair("result", Value::null));
if (!error.isNull())
reply.push_back(Pair("result", NullUniValue));
else
reply.push_back(Pair("result", result));
reply.push_back(Pair("error", error));
Expand All @@ -278,7 +278,7 @@ Object JSONRPCReplyObj(const Value& result, const Value& error, const Value& id)
string JSONRPCReply(const Value& result, const Value& error, const Value& id)
{
Object reply = JSONRPCReplyObj(result, error, id);
return write_string(Value(reply), false) + "\n";
return reply.write() + "\n";
}

Object JSONRPCError(int code, const string& message)
Expand Down
4 changes: 1 addition & 3 deletions src/rpcprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

#include "json/json_spirit_reader_template.h"
#include "json/json_spirit_utils.h"
#include "json/json_spirit_writer_template.h"
#include "json_spirit_wrapper.h"

//! HTTP status codes
enum HTTPStatusCode
Expand Down
Loading

0 comments on commit 15982a8

Please sign in to comment.