Skip to content

Commit

Permalink
expicit set UniValue type to avoid empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschnelli committed Jun 4, 2015
1 parent 53b4671 commit 6c7bee0
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 92 deletions.
12 changes: 6 additions & 6 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static bool rest_chaininfo(AcceptedConnection* conn,

switch (rf) {
case RF_JSON: {
Array rpcParams;
UniValue rpcParams(UniValue::VARR);
Value chainInfoObject = getblockchaininfo(rpcParams, false);
string strJSON = chainInfoObject.write() + "\n";
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
Expand Down Expand Up @@ -316,7 +316,7 @@ static bool rest_tx(AcceptedConnection* conn,
}

case RF_JSON: {
Object objTx;
UniValue objTx(UniValue::VOBJ);
TxToJSON(tx, hashBlock, objTx);
string strJSON = objTx.write() + "\n";
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
Expand Down Expand Up @@ -491,23 +491,23 @@ static bool rest_getutxos(AcceptedConnection* conn,
}

case RF_JSON: {
Object objGetUTXOResponse;
UniValue objGetUTXOResponse(UniValue::VOBJ);

// pack in some essentials
// use more or less the same output as mentioned in Bip64
objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height()));
objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.Tip()->GetBlockHash().GetHex()));
objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation));

Array utxos;
UniValue utxos(UniValue::VARR);
BOOST_FOREACH (const CCoin& coin, outs) {
Object utxo;
UniValue utxo(UniValue::VOBJ);
utxo.push_back(Pair("txvers", (int32_t)coin.nTxVer));
utxo.push_back(Pair("height", (int32_t)coin.nHeight));
utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue)));

// include the script in a json output
Object o;
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
utxo.push_back(Pair("scriptPubKey", o));
utxos.push_back(utxo);
Expand Down
26 changes: 13 additions & 13 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ double GetDifficulty(const CBlockIndex* blockindex)

Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
{
Object result;
UniValue result(UniValue::VOBJ);
result.push_back(Pair("hash", block.GetHash().GetHex()));
int confirmations = -1;
// Only report confirmations if the block is on the main chain
Expand All @@ -66,12 +66,12 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDe
result.push_back(Pair("height", blockindex->nHeight));
result.push_back(Pair("version", block.nVersion));
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
Array txs;
UniValue txs(UniValue::VARR);
BOOST_FOREACH(const CTransaction&tx, block.vtx)
{
if(txDetails)
{
Object objTx;
UniValue objTx(UniValue::VOBJ);
TxToJSON(tx, uint256(), objTx);
txs.push_back(objTx);
}
Expand Down Expand Up @@ -187,12 +187,12 @@ Value getrawmempool(const Array& params, bool fHelp)
if (fVerbose)
{
LOCK(mempool.cs);
Object o;
UniValue o(UniValue::VOBJ);
BOOST_FOREACH(const PAIRTYPE(uint256, CTxMemPoolEntry)& entry, mempool.mapTx)
{
const uint256& hash = entry.first;
const CTxMemPoolEntry& e = entry.second;
Object info;
UniValue info(UniValue::VOBJ);
info.push_back(Pair("size", (int)e.GetTxSize()));
info.push_back(Pair("fee", ValueFromAmount(e.GetFee())));
info.push_back(Pair("time", e.GetTime()));
Expand Down Expand Up @@ -223,7 +223,7 @@ Value getrawmempool(const Array& params, bool fHelp)
vector<uint256> vtxid;
mempool.queryHashes(vtxid);

Array a;
UniValue a(UniValue::VARR);
BOOST_FOREACH(const uint256& hash, vtxid)
a.push_back(hash.ToString());

Expand Down Expand Up @@ -348,7 +348,7 @@ Value gettxoutsetinfo(const Array& params, bool fHelp)

LOCK(cs_main);

Object ret;
UniValue ret(UniValue::VOBJ);

CCoinsStats stats;
FlushStateToDisk();
Expand Down Expand Up @@ -404,7 +404,7 @@ Value gettxout(const Array& params, bool fHelp)

LOCK(cs_main);

Object ret;
UniValue ret(UniValue::VOBJ);

std::string strHash = params[0].get_str();
uint256 hash(uint256S(strHash));
Expand Down Expand Up @@ -435,7 +435,7 @@ Value gettxout(const Array& params, bool fHelp)
else
ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
Object o;
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
ret.push_back(Pair("scriptPubKey", o));
ret.push_back(Pair("version", coins.nVersion));
Expand Down Expand Up @@ -495,7 +495,7 @@ Value getblockchaininfo(const Array& params, bool fHelp)

LOCK(cs_main);

Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("chain", Params().NetworkIDString()));
obj.push_back(Pair("blocks", (int)chainActive.Height()));
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
Expand Down Expand Up @@ -582,10 +582,10 @@ Value getchaintips(const Array& params, bool fHelp)
setTips.insert(chainActive.Tip());

/* Construct the output array. */
Array res;
UniValue res(UniValue::VARR);
BOOST_FOREACH(const CBlockIndex* block, setTips)
{
Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("height", block->nHeight));
obj.push_back(Pair("hash", block->phashBlock->GetHex()));

Expand Down Expand Up @@ -636,7 +636,7 @@ Value getmempoolinfo(const Array& params, bool fHelp)
+ HelpExampleRpc("getmempoolinfo", "")
);

Object ret;
UniValue ret;
ret.push_back(Pair("size", (int64_t) mempool.size()));
ret.push_back(Pair("bytes", (int64_t) mempool.GetTotalTxSize()));

Expand Down
18 changes: 9 additions & 9 deletions src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Value generate(const Array& params, bool fHelp)
nHeightEnd = nHeightStart+nGenerate;
}
unsigned int nExtraNonce = 0;
Array blockHashes;
UniValue blockHashes(UniValue::VARR);
while (nHeight < nHeightEnd)
{
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
Expand Down Expand Up @@ -247,7 +247,7 @@ Value getmininginfo(const Array& params, bool fHelp)

LOCK(cs_main);

Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("blocks", (int)chainActive.Height()));
obj.push_back(Pair("currentblocksize", (uint64_t)nLastBlockSize));
obj.push_back(Pair("currentblocktx", (uint64_t)nLastBlockTx));
Expand Down Expand Up @@ -519,9 +519,9 @@ Value getblocktemplate(const Array& params, bool fHelp)
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
pblock->nNonce = 0;

Array aCaps; aCaps.push_back("proposal");
UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");

Array transactions;
UniValue transactions(UniValue::VARR);
map<uint256, int64_t> setTxIndex;
int i = 0;
BOOST_FOREACH (CTransaction& tx, pblock->vtx)
Expand All @@ -532,13 +532,13 @@ Value getblocktemplate(const Array& params, bool fHelp)
if (tx.IsCoinBase())
continue;

Object entry;
UniValue entry(UniValue::VOBJ);

entry.push_back(Pair("data", EncodeHexTx(tx)));

entry.push_back(Pair("hash", txHash.GetHex()));

Array deps;
UniValue deps(UniValue::VARR);
BOOST_FOREACH (const CTxIn &in, tx.vin)
{
if (setTxIndex.count(in.prevout.hash))
Expand All @@ -553,20 +553,20 @@ Value getblocktemplate(const Array& params, bool fHelp)
transactions.push_back(entry);
}

Object aux;
UniValue aux(UniValue::VOBJ);
aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));

arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);

static Array aMutable;
static UniValue aMutable(UniValue::VARR);
if (aMutable.empty())
{
aMutable.push_back("time");
aMutable.push_back("transactions");
aMutable.push_back("prevblock");
}

Object result;
UniValue result(UniValue::VOBJ);
result.push_back(Pair("capabilities", aCaps));
result.push_back(Pair("version", pblock->nVersion));
result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
Expand Down
12 changes: 6 additions & 6 deletions src/rpcmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Value getinfo(const Array& params, bool fHelp)
proxyType proxy;
GetProxy(NET_IPV4, proxy);

Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
#ifdef ENABLE_WALLET
Expand Down Expand Up @@ -118,7 +118,7 @@ class DescribeAddressVisitor : public boost::static_visitor<Object>
Object operator()(const CNoDestination &dest) const { return Object(); }

Object operator()(const CKeyID &keyID) const {
Object obj;
UniValue obj(UniValue::VOBJ);
CPubKey vchPubKey;
obj.push_back(Pair("isscript", false));
if (mine == ISMINE_SPENDABLE) {
Expand All @@ -130,7 +130,7 @@ class DescribeAddressVisitor : public boost::static_visitor<Object>
}

Object operator()(const CScriptID &scriptID) const {
Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("isscript", true));
if (mine != ISMINE_NO) {
CScript subscript;
Expand All @@ -141,7 +141,7 @@ class DescribeAddressVisitor : public boost::static_visitor<Object>
ExtractDestinations(subscript, whichType, addresses, nRequired);
obj.push_back(Pair("script", GetTxnOutputType(whichType)));
obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
Array a;
UniValue a(UniValue::VARR);
BOOST_FOREACH(const CTxDestination& addr, addresses)
a.push_back(CBitcoinAddress(addr).ToString());
obj.push_back(Pair("addresses", a));
Expand Down Expand Up @@ -186,7 +186,7 @@ Value validateaddress(const Array& params, bool fHelp)
CBitcoinAddress address(params[0].get_str());
bool isValid = address.IsValid();

Object ret;
UniValue ret(UniValue::VOBJ);
ret.push_back(Pair("isvalid", isValid));
if (isValid)
{
Expand Down Expand Up @@ -312,7 +312,7 @@ Value createmultisig(const Array& params, bool fHelp)
CScriptID innerID(inner);
CBitcoinAddress address(innerID);

Object result;
UniValue result;
result.push_back(Pair("address", address.ToString()));
result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end())));

Expand Down
32 changes: 16 additions & 16 deletions src/rpcnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ Value getpeerinfo(const Array& params, bool fHelp)
vector<CNodeStats> vstats;
CopyNodeStats(vstats);

Array ret;
UniValue ret(UniValue::VARR);

BOOST_FOREACH(const CNodeStats& stats, vstats) {
Object obj;
UniValue obj(UniValue::VOBJ);
CNodeStateStats statestats;
bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
obj.push_back(Pair("id", stats.nodeid));
Expand Down Expand Up @@ -151,7 +151,7 @@ Value getpeerinfo(const Array& params, bool fHelp)
obj.push_back(Pair("banscore", statestats.nMisbehavior));
obj.push_back(Pair("synced_headers", statestats.nSyncHeight));
obj.push_back(Pair("synced_blocks", statestats.nCommonHeight));
Array heights;
UniValue heights(UniValue::VARR);
BOOST_FOREACH(int height, statestats.vHeightInFlight) {
heights.push_back(height);
}
Expand Down Expand Up @@ -271,12 +271,12 @@ Value getaddednodeinfo(const Array& params, bool fHelp)
throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
}

Array ret;
UniValue ret(UniValue::VARR);
if (!fDns)
{
BOOST_FOREACH(string& strAddNode, laddedNodes)
{
Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("addednode", strAddNode));
ret.push_back(obj);
}
Expand All @@ -291,26 +291,26 @@ Value getaddednodeinfo(const Array& params, bool fHelp)
laddedAddreses.push_back(make_pair(strAddNode, vservNode));
else
{
Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("addednode", strAddNode));
obj.push_back(Pair("connected", false));
Array addresses;
UniValue addresses(UniValue::VARR);
obj.push_back(Pair("addresses", addresses));
}
}

LOCK(cs_vNodes);
for (list<pair<string, vector<CService> > >::iterator it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
{
Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("addednode", it->first));

Array addresses;
UniValue addresses(UniValue::VARR);
bool fConnected = false;
BOOST_FOREACH(CService& addrNode, it->second)
{
bool fFound = false;
Object node;
UniValue node(UniValue::VOBJ);
node.push_back(Pair("address", addrNode.ToString()));
BOOST_FOREACH(CNode* pnode, vNodes)
if (pnode->addr == addrNode)
Expand Down Expand Up @@ -350,7 +350,7 @@ Value getnettotals(const Array& params, bool fHelp)
+ HelpExampleRpc("getnettotals", "")
);

Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("totalbytesrecv", CNode::GetTotalBytesRecv()));
obj.push_back(Pair("totalbytessent", CNode::GetTotalBytesSent()));
obj.push_back(Pair("timemillis", GetTimeMillis()));
Expand All @@ -359,14 +359,14 @@ Value getnettotals(const Array& params, bool fHelp)

static Array GetNetworksInfo()
{
Array networks;
UniValue networks(UniValue::VARR);
for(int n=0; n<NET_MAX; ++n)
{
enum Network network = static_cast<enum Network>(n);
if(network == NET_UNROUTABLE)
continue;
proxyType proxy;
Object obj;
UniValue obj(UniValue::VOBJ);
GetProxy(network, proxy);
obj.push_back(Pair("name", GetNetworkName(network)));
obj.push_back(Pair("limited", IsLimited(network)));
Expand Down Expand Up @@ -418,7 +418,7 @@ Value getnetworkinfo(const Array& params, bool fHelp)

LOCK(cs_main);

Object obj;
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("subversion",
FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>())));
Expand All @@ -428,12 +428,12 @@ Value getnetworkinfo(const Array& params, bool fHelp)
obj.push_back(Pair("connections", (int)vNodes.size()));
obj.push_back(Pair("networks", GetNetworksInfo()));
obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
Array localAddresses;
UniValue localAddresses(UniValue::VARR);
{
LOCK(cs_mapLocalHost);
BOOST_FOREACH(const PAIRTYPE(CNetAddr, LocalServiceInfo) &item, mapLocalHost)
{
Object rec;
UniValue rec(UniValue::VOBJ);
rec.push_back(Pair("address", item.first.ToString()));
rec.push_back(Pair("port", item.second.nPort));
rec.push_back(Pair("score", item.second.nScore));
Expand Down
Loading

0 comments on commit 6c7bee0

Please sign in to comment.