Skip to content

Commit

Permalink
Merge #2779: rpc: Backport getblock verbosity
Browse files Browse the repository at this point in the history
b512d94 Remove semicolon and add "verbose" back to vRPCConvertParams. (tecnovert)
8488f7e Test for hex strings and remove stray newline from rpc_named_arguments. (tecnovert)
f4943e5 Fix missing help text and add backwards compatibility. (tecnovert)
715f2a2 rpc: Backport getblock verbosity (tecnovert)

Pull request description:

  Changes the boolean 'verbose' parameter of the getblock rpc command to an integer.
  Enables getblock output to include full transaction details.

  Backported from Bitcoin v15
  https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-0.15.0.md

ACKs for top commit:
  PeterL73:
    tACK b512d94
  Fuzzbawls:
    ACK b512d94

Tree-SHA512: 0e86da1ed3416a83b74125918fd0f3b4de18b12b7750de20788a95dec03698845ac45ab1c7cdda304430175105fab2e2e6091ecb0d18898be13255ffec954fd4
  • Loading branch information
Fuzzbawls committed Nov 20, 2022
2 parents 768dfb5 + b512d94 commit abb888b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
42 changes: 28 additions & 14 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,19 @@ 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 = 0):\n"
"\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\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 All @@ -560,8 +564,14 @@ UniValue getblock(const JSONRPCRequest& request)
" }\n"
"}\n"

"\nResult (for verbose=false):\n"
"\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n"
"\nResult (for verbosity = 2):\n"
"{\n"
" ..., Same output as verbosity = 1.\n"
" \"tx\" : [ (array of Objects) The transactions in the format of the getrawtransaction RPC. Different from verbosity = 1 \"tx\" result.\n"
" ,...\n"
" ],\n"
" ,... Same output as verbosity = 1.\n"
"}\n"

"\nExamples:\n" +
HelpExampleCli("getblock", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") +
Expand All @@ -571,9 +581,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 +597,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 +1463,7 @@ static const CRPCCommand commands[] =
// --------------------- ------------------------ ----------------------- ------ --------
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
{ "blockchain", "getbestsaplinganchor", &getbestsaplinganchor, true, {} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose"} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose|verbosity"} },
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getblockcount", &getblockcount, true, {} },
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static const CRPCConvertParam vRPCConvertParams[] = {
{ "getbalance", 1, "include_watchonly" },
{ "getbalance", 2, "include_delegated" },
{ "getbalance", 3, "include_shield" },
{ "getblock", 1, "verbosity" },
{ "getblock", 1, "verbose" },
{ "getblockhash", 0, "height" },
{ "getblockheader", 1, "verbose" },
Expand Down
14 changes: 12 additions & 2 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <boost/signals2/signal.hpp>
#include <boost/thread.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

#include <memory> // for unique_ptr
#include <unordered_map>
Expand Down Expand Up @@ -450,8 +452,16 @@ static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, c
}
// Process expected parameters.
int hole = 0;
for (const std::string &argName: argNames) {
auto fr = argsIn.find(argName);
for (const std::string &argNamePattern: argNames) {
std::vector<std::string> vargNames;
boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of("|"));
auto fr = argsIn.end();
for (const std::string & argName : vargNames) {
fr = argsIn.find(argName);
if (fr != argsIn.end()) {
break;
}
}
if (fr != argsIn.end()) {
for (int i = 0; i < hole; ++i) {
// Fill hole between specified parameters with JSON nulls,
Expand Down
14 changes: 14 additions & 0 deletions test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run_test(self):
self._test_getblockchaininfo()
self._test_gettxoutsetinfo()
self._test_getblockheader()
self._test_getblock()
#self._test_getdifficulty()
self.nodes[0].verifychain(0)

Expand Down Expand Up @@ -108,5 +109,18 @@ def _test_getdifficulty(self):
# binary => decimal => binary math is why we do this check
assert abs(difficulty * 2**31 - 1) < 0.0001

def _test_getblock(self):
node = self.nodes[0]

# Test getblock verbosity
besthash = node.getbestblockhash()
assert_is_hex_string(node.getblock(blockhash=besthash, verbose=False))
assert_is_hex_string(node.getblock(blockhash=besthash, verbosity=0))

assert_is_hash_string(node.getblock(besthash, 1)['tx'][0])
assert_is_hash_string(node.getblock(besthash, True)['tx'][0])
assert_is_hex_string(node.getblock(besthash, 2)['tx'][0]['vin'][0]['coinbase'])


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

0 comments on commit abb888b

Please sign in to comment.