diff options
Diffstat (limited to 'src/rpcblockchain.cpp')
-rw-r--r-- | src/rpcblockchain.cpp | 73 |
1 files changed, 70 insertions, 3 deletions
diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 2aa400abe0..a0ddc4d9ee 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -9,6 +9,8 @@ using namespace json_spirit; using namespace std; +void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out); + double GetDifficulty(const CBlockIndex* blockindex) { // Floating point number that is a multiple of the minimum difficulty, @@ -63,8 +65,9 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex) if (blockindex->pprev) result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); - if (blockindex->pnext) - result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex())); + CBlockIndex *pnext = blockindex->GetNextInMainChain(); + if (pnext) + result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); return result; } @@ -160,7 +163,7 @@ Value getblock(const Array& params, bool fHelp) CBlock block; CBlockIndex* pblockindex = mapBlockIndex[hash]; - block.ReadFromDisk(pblockindex, true); + block.ReadFromDisk(pblockindex); if (!fVerbose) { @@ -173,7 +176,71 @@ Value getblock(const Array& params, bool fHelp) return blockToJSON(block, pblockindex); } +Value gettxoutsetinfo(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 0) + throw runtime_error( + "gettxoutsetinfo\n" + "Returns statistics about the unspent transaction output set."); + + Object ret; + + CCoinsStats stats; + if (pcoinsTip->GetStats(stats)) { + ret.push_back(Pair("height", (boost::int64_t)stats.nHeight)); + ret.push_back(Pair("bestblock", stats.hashBlock.GetHex())); + ret.push_back(Pair("transactions", (boost::int64_t)stats.nTransactions)); + ret.push_back(Pair("txouts", (boost::int64_t)stats.nTransactionOutputs)); + ret.push_back(Pair("bytes_serialized", (boost::int64_t)stats.nSerializedSize)); + ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex())); + ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount))); + } + return ret; +} +Value gettxout(const Array& params, bool fHelp) +{ + if (fHelp || params.size() < 2 || params.size() > 3) + throw runtime_error( + "gettxout <txid> <n> [includemempool=true]\n" + "Returns details about an unspent transaction output."); + + Object ret; + std::string strHash = params[0].get_str(); + uint256 hash(strHash); + int n = params[1].get_int(); + bool fMempool = true; + if (params.size() > 2) + fMempool = params[2].get_bool(); + + CCoins coins; + if (fMempool) { + LOCK(mempool.cs); + CCoinsViewMemPool view(*pcoinsTip, mempool); + if (!view.GetCoins(hash, coins)) + return Value::null; + mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool + } else { + if (!pcoinsTip->GetCoins(hash, coins)) + return Value::null; + } + if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull()) + return Value::null; + + ret.push_back(Pair("bestblock", pcoinsTip->GetBestBlock()->GetBlockHash().GetHex())); + if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT) + ret.push_back(Pair("confirmations", 0)); + else + ret.push_back(Pair("confirmations", pcoinsTip->GetBestBlock()->nHeight - coins.nHeight + 1)); + ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue))); + Object o; + ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o); + ret.push_back(Pair("scriptPubKey", o)); + ret.push_back(Pair("version", coins.nVersion)); + ret.push_back(Pair("coinbase", coins.fCoinBase)); + + return ret; +} |