Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix missing help text and add backwards compatibility.
  • Loading branch information
tecnovert committed Nov 2, 2022
commit f4943e53734d3caada54852ced1254f262576f87
17 changes: 13 additions & 4 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,15 @@ UniValue getblock(const JSONRPCRequest& request)
"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"
"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. 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 verbosity = 0):\n"
"\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n"

"\nResult (for verbosity = 1):\n"

This comment was marked as resolved.

"{\n"
" \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
Expand All @@ -561,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 Down Expand Up @@ -1454,7 +1463,7 @@ static const CRPCCommand commands[] =
// --------------------- ------------------------ ----------------------- ------ --------
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
{ "blockchain", "getbestsaplinganchor", &getbestsaplinganchor, true, {} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbosity"} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbose|verbosity"} },
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getblockcount", &getblockcount, true, {} },
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },
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
13 changes: 13 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,17 @@ 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(isinstance(node.getblock(blockhash=besthash, verbose=False), str))
assert(isinstance(node.getblock(blockhash=besthash, verbosity=0), str))
assert(isinstance(node.getblock(besthash, 1)['tx'][0], str))
assert(isinstance(node.getblock(besthash, True)['tx'][0], str))
assert('vin' in node.getblock(besthash, 2)['tx'][0])
Copy link
Copy Markdown

@PeterL73 PeterL73 Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
I think this covers the basics

Would love to see some stricter tests then just checking if a string is returned
Maybe something like:

assert_is_hex_string(node.getblock(besthash, False))
assert_is_hex_string(node.getblock(besthash, 0))

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



if __name__ == '__main__':
BlockchainTest().main()
10 changes: 0 additions & 10 deletions test/functional/rpc_named_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ 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])


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/functional/rpc_named_arguments.py is now unchanged and could be removed from this PR

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