Skip to content

Commit cce6951

Browse files
tecnovertFuzzbawls
authored andcommitted
rpc: Backport getblock verbosity
Github-Pull: #2779 Rebased-From: 715f2a2
1 parent fd719a2 commit cce6951

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/rpc/blockchain.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,16 @@ UniValue getblock(const JSONRPCRequest& request)
527527
{
528528
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
529529
throw std::runtime_error(
530-
"getblock \"blockhash\" ( verbose )\n"
531-
"\nIf verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
532-
"If verbose is true, returns an Object with information about block <hash>.\n"
530+
"getblock \"blockhash\" ( verbosity )\n"
531+
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
532+
"If verbosity is 1, returns an Object with information about block <hash>.\n"
533+
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n"
533534

534535
"\nArguments:\n"
535-
"1. \"blockhash\" (string, required) The block hash\n"
536-
"2. verbose (boolean, optional, default=true) True for a json object, false for the hex encoded data\n"
536+
"1. \"blockhash\" (string, required) The block hash\n"
537+
"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"
537538

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

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

574-
bool fVerbose = true;
575-
if (request.params.size() > 1)
576-
fVerbose = request.params[1].get_bool();
575+
int verbosity = 1;
576+
if (!request.params[1].isNull()) {
577+
if(request.params[1].isNum())
578+
verbosity = request.params[1].get_int();
579+
else
580+
verbosity = request.params[1].get_bool() ? 1 : 0;
581+
}
577582

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

586-
if (!fVerbose) {
591+
if (verbosity <= 0) {
587592
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
588593
ssBlock << block;
589594
std::string strHex = HexStr(ssBlock);
590595
return strHex;
591596
}
592597

593-
return blockToJSON(block, chainActive.Tip(), pblockindex);
598+
return blockToJSON(block, chainActive.Tip(), pblockindex, verbosity >= 2);
594599
}
595600

596601
UniValue getblockheader(const JSONRPCRequest& request)
@@ -1449,7 +1454,7 @@ static const CRPCCommand commands[] =
14491454
// --------------------- ------------------------ ----------------------- ------ --------
14501455
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
14511456
{ "blockchain", "getbestsaplinganchor", &getbestsaplinganchor, true, {} },
1452-
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose"} },
1457+
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbosity"} },
14531458
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
14541459
{ "blockchain", "getblockcount", &getblockcount, true, {} },
14551460
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },

src/rpc/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static const CRPCConvertParam vRPCConvertParams[] = {
5353
{ "getbalance", 1, "include_watchonly" },
5454
{ "getbalance", 2, "include_delegated" },
5555
{ "getbalance", 3, "include_shield" },
56-
{ "getblock", 1, "verbose" },
56+
{ "getblock", 1, "verbosity" },
5757
{ "getblockhash", 0, "height" },
5858
{ "getblockheader", 1, "verbose" },
5959
{ "getblockindexstats", 0, "height" },

test/functional/rpc_named_arguments.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,16 @@ def run_test(self):
3030
assert_equal(node.echo(arg9=None), [None]*10)
3131
assert_equal(node.echo(arg0=0,arg3=3,arg9=9), [0] + [None]*2 + [3] + [None]*5 + [9])
3232

33+
# Test getblock verbosity
34+
block = node.getblock(blockhash=h, verbosity=0)
35+
assert(isinstance(block, str))
36+
37+
block = node.getblock(blockhash=h, verbosity=1)
38+
assert(isinstance(block['tx'][0], str))
39+
40+
block = node.getblock(blockhash=h, verbosity=2)
41+
assert('vin' in block['tx'][0])
42+
43+
3344
if __name__ == '__main__':
3445
NamedArgumentTest().main()

0 commit comments

Comments
 (0)