Skip to content

Commit

Permalink
Merge pull request #2747 from luke-jr/getblock_verbose0
Browse files Browse the repository at this point in the history
Add verbose flag to getblock RPC so it is possible to get hex dumps of blocks
  • Loading branch information
Jeff Garzik committed Jun 10, 2013
2 parents f59530c + 2331952 commit 543d70a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "listunspent" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "listunspent" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "listunspent" && n > 2) ConvertTo<Array>(params[2]);
if (strMethod == "getblock" && n > 1) ConvertTo<bool>(params[1]);
if (strMethod == "getrawtransaction" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "createrawtransaction" && n > 0) ConvertTo<Array>(params[0]);
if (strMethod == "createrawtransaction" && n > 1) ConvertTo<Object>(params[1]);
Expand Down
20 changes: 17 additions & 3 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,35 @@ 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 <hash>\n"
"Returns details of a block with given block-hash.");
"getblock <hash> [verbose=true]\n"
"If verbose is false, returns a string that is serialized, hex-encoded data for block <hash>.\n"
"If verbose is true, returns an Object with information about block <hash>."
);

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");

CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];
block.ReadFromDisk(pblockindex);

if (!fVerbose)
{
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << block;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
return strHex;
}

return blockToJSON(block, pblockindex);
}

Expand Down

0 comments on commit 543d70a

Please sign in to comment.