diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 62cbbd670f791..63f66016c30b7 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -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 .\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 .\n" + "If verbosity is 2, returns an Object with information about block 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" @@ -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) @@ -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) @@ -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"} }, diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 53dec7d0b193d..e2a70b39e9807 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -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" }, diff --git a/test/functional/rpc_named_arguments.py b/test/functional/rpc_named_arguments.py index b5242e3a33c46..0c98dc923ad2b 100755 --- a/test/functional/rpc_named_arguments.py +++ b/test/functional/rpc_named_arguments.py @@ -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()