Skip to content

Commit

Permalink
rpc: Backport getblock verbosity
Browse files Browse the repository at this point in the history
Github-Pull: PIVX-Project#2779
Rebased-From: 715f2a2
  • Loading branch information
tecnovert authored and Fuzzbawls committed Nov 22, 2022
1 parent fd719a2 commit cce6951
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,16 @@ UniValue getblock(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
throw std::runtime_error(
"getblock \"blockhash\" ( verbose )\n"
"\nIf 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>.\n"
"getblock \"blockhash\" ( verbosity )\n"
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
"If verbosity is 1, returns an Object with information about block <hash>.\n"
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n"

"\nArguments:\n"
"1. \"blockhash\" (string, required) The block hash\n"
"2. verbose (boolean, optional, default=true) True for a json object, false for the hex encoded data\n"
"1. \"blockhash\" (string, required) The block hash\n"
"2. verbosity (numeric, optional, default=1) 0 for hex encoded data, 1 for a json object, and 2 for json object with transaction data\n"

"\nResult (for verbose = true):\n"
"\nResult (for verbosity = 1):\n"
"{\n"
" \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
" \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n"
Expand Down Expand Up @@ -571,9 +572,13 @@ UniValue getblock(const JSONRPCRequest& request)

uint256 hash(ParseHashV(request.params[0], "blockhash"));

bool fVerbose = true;
if (request.params.size() > 1)
fVerbose = request.params[1].get_bool();
int verbosity = 1;
if (!request.params[1].isNull()) {
if(request.params[1].isNum())
verbosity = request.params[1].get_int();
else
verbosity = request.params[1].get_bool() ? 1 : 0;
}

CBlockIndex* pblockindex = LookupBlockIndex(hash);
if (pblockindex == nullptr)
Expand All @@ -583,14 +588,14 @@ UniValue getblock(const JSONRPCRequest& request)
if (!ReadBlockFromDisk(block, pblockindex))
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");

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

return blockToJSON(block, chainActive.Tip(), pblockindex);
return blockToJSON(block, chainActive.Tip(), pblockindex, verbosity >= 2);
}

UniValue getblockheader(const JSONRPCRequest& request)
Expand Down Expand Up @@ -1449,7 +1454,7 @@ static const CRPCCommand commands[] =
// --------------------- ------------------------ ----------------------- ------ --------
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
{ "blockchain", "getbestsaplinganchor", &getbestsaplinganchor, true, {} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose"} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbosity"} },
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getblockcount", &getblockcount, true, {} },
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static const CRPCConvertParam vRPCConvertParams[] = {
{ "getbalance", 1, "include_watchonly" },
{ "getbalance", 2, "include_delegated" },
{ "getbalance", 3, "include_shield" },
{ "getblock", 1, "verbose" },
{ "getblock", 1, "verbosity" },
{ "getblockhash", 0, "height" },
{ "getblockheader", 1, "verbose" },
{ "getblockindexstats", 0, "height" },
Expand Down
11 changes: 11 additions & 0 deletions test/functional/rpc_named_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ def run_test(self):
assert_equal(node.echo(arg9=None), [None]*10)
assert_equal(node.echo(arg0=0,arg3=3,arg9=9), [0] + [None]*2 + [3] + [None]*5 + [9])

# Test getblock verbosity
block = node.getblock(blockhash=h, verbosity=0)
assert(isinstance(block, str))

block = node.getblock(blockhash=h, verbosity=1)
assert(isinstance(block['tx'][0], str))

block = node.getblock(blockhash=h, verbosity=2)
assert('vin' in block['tx'][0])


if __name__ == '__main__':
NamedArgumentTest().main()

0 comments on commit cce6951

Please sign in to comment.