Skip to content

Commit dabab06

Browse files
committed
Merge #19455: rpc generate: print useful help and error message
f0aa8ae test: add rpc_generate functional test (Jon Atack) 92d94ff rpc: print useful help and error message for generate (Jon Atack) 8d32d20 test: consider generate covered in _get_uncovered_rpc_commands() (Jon Atack) Pull request description: This was a requested follow-up to #19133 and #17700 to alleviate confusion and head-scratching by people following tutorials that use `generate`. See bitcoin/bitcoin#19455 (comment) below, bitcoin/bitcoin#19133 (comment) and bitcoin/bitcoin#17700 (comment). before ``` $ bitcoin-cli help generate help: unknown command: generate $ bitcoin-cli generate error code: -32601 error message: Method not found ``` after ``` $ bitcoin-cli help generate generate ( nblocks maxtries ) has been replaced by the -generate cli option. Refer to -help for more information. $ bitcoin-cli generate error code: -32601 error message: generate ( nblocks maxtries ) has been replaced by the -generate cli option. Refer to -help for more information. ``` In the general help it remains hidden, as requested by laanwj. ``` $ bitcoin-cli help == Generating == generateblock "output" ["rawtx/txid",...] generatetoaddress nblocks "address" ( maxtries ) generatetodescriptor num_blocks "descriptor" ( maxtries ) ``` ACKs for top commit: adamjonas: utACK f0aa8ae pinheadmz: ACK f0aa8ae Tree-SHA512: d083652589ad3e8228c733455245001db22397559c3946e7e573cf9bd01c46e9e88b72d934728ec7f4361436ae4c74adb8f579670b09f479011924357e729af5
2 parents c0b1706 + f0aa8ae commit dabab06

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/rpc/mining.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
236236
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
237237
}
238238

239+
static UniValue generate(const JSONRPCRequest& request)
240+
{
241+
const std::string help_str{"generate ( nblocks maxtries ) has been replaced by the -generate cli option. Refer to -help for more information."};
242+
243+
if (request.fHelp) {
244+
throw std::runtime_error(help_str);
245+
} else {
246+
throw JSONRPCError(RPC_METHOD_NOT_FOUND, help_str);
247+
}
248+
}
249+
239250
static UniValue generatetoaddress(const JSONRPCRequest& request)
240251
{
241252
RPCHelpMan{"generatetoaddress",
@@ -1198,6 +1209,7 @@ static const CRPCCommand commands[] =
11981209
{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },
11991210

12001211
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
1212+
{ "hidden", "generate", &generate, {} },
12011213
};
12021214
// clang-format on
12031215
for (const auto& c : commands) {

test/functional/rpc_generate.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test generate RPC."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import (
9+
assert_equal,
10+
assert_raises_rpc_error,
11+
)
12+
13+
14+
class RPCGenerateTest(BitcoinTestFramework):
15+
def set_test_params(self):
16+
self.num_nodes = 1
17+
18+
def run_test(self):
19+
message = (
20+
"generate ( nblocks maxtries ) has been replaced by the -generate "
21+
"cli option. Refer to -help for more information."
22+
)
23+
24+
self.log.info("Test rpc generate raises with message to use cli option")
25+
assert_raises_rpc_error(-32601, message, self.nodes[0].rpc.generate)
26+
27+
self.log.info("Test rpc generate help prints message to use cli option")
28+
assert_equal(message, self.nodes[0].help("generate"))
29+
30+
self.log.info("Test rpc generate is a hidden command not discoverable in general help")
31+
assert message not in self.nodes[0].help()
32+
33+
34+
if __name__ == "__main__":
35+
RPCGenerateTest().main()

test/functional/test_runner.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
'p2p_eviction.py',
195195
'rpc_signmessage.py',
196196
'rpc_generateblock.py',
197+
'rpc_generate.py',
197198
'wallet_balance.py',
198199
'feature_nulldummy.py',
199200
'mempool_accept.py',
@@ -713,14 +714,16 @@ def _get_uncovered_rpc_commands(self):
713714
Return a set of currently untested RPC commands.
714715
715716
"""
716-
# This is shared from `test/functional/test-framework/coverage.py`
717+
# This is shared from `test/functional/test_framework/coverage.py`
717718
reference_filename = 'rpc_interface.txt'
718719
coverage_file_prefix = 'coverage.'
719720

720721
coverage_ref_filename = os.path.join(self.dir, reference_filename)
721722
coverage_filenames = set()
722723
all_cmds = set()
723-
covered_cmds = set()
724+
# Consider RPC generate covered, because it is overloaded in
725+
# test_framework/test_node.py and not seen by the coverage check.
726+
covered_cmds = set({'generate'})
724727

725728
if not os.path.isfile(coverage_ref_filename):
726729
raise RuntimeError("No coverage reference found")

0 commit comments

Comments
 (0)