From 23319521418691ddfcb228684c900c389f3f114d Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Fri, 7 Jun 2013 05:23:30 +0000 Subject: [PATCH] RPC: getblock(): Accept 2nd "verbose" parameter, similar to getrawtransaction, but defaulting to 1 for backward compatibility --- src/bitcoinrpc.cpp | 1 + src/rpcblockchain.cpp | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 0704173e5..d82a382c4 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1161,6 +1161,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 0) ConvertTo(params[0]); if (strMethod == "listunspent" && n > 1) ConvertTo(params[1]); if (strMethod == "listunspent" && n > 2) ConvertTo(params[2]); + if (strMethod == "getblock" && n > 1) ConvertTo(params[1]); if (strMethod == "getrawtransaction" && n > 1) ConvertTo(params[1]); if (strMethod == "createrawtransaction" && n > 0) ConvertTo(params[0]); if (strMethod == "createrawtransaction" && n > 1) ConvertTo(params[1]); diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 2dfdf5842..2aa400abe 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -141,14 +141,20 @@ Value getblockhash(const Array& params, bool fHelp) Value getblock(const Array& params, bool fHelp) { - if (fHelp || params.size() != 1) + if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( - "getblock \n" - "Returns details of a block with given block-hash."); + "getblock [verbose=true]\n" + "If verbose is false, returns a string that is serialized, hex-encoded data for block .\n" + "If verbose is true, returns an Object with information about block ." + ); std::string strHash = params[0].get_str(); uint256 hash(strHash); + bool fVerbose = true; + if (params.size() > 1) + fVerbose = params[1].get_bool(); + if (mapBlockIndex.count(hash) == 0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); @@ -156,6 +162,14 @@ Value getblock(const Array& params, bool fHelp) CBlockIndex* pblockindex = mapBlockIndex[hash]; block.ReadFromDisk(pblockindex, true); + if (!fVerbose) + { + CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); + ssBlock << block; + std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); + return strHex; + } + return blockToJSON(block, pblockindex); }