Skip to content

Commit 4818124

Browse files
committed
cli: create bitcoin-cli -generate command
1 parent ff41a36 commit 4818124

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <clientversion.h>
1212
#include <optional.h>
1313
#include <rpc/client.h>
14+
#include <rpc/mining.h>
1415
#include <rpc/protocol.h>
1516
#include <rpc/request.h>
1617
#include <util/strencodings.h>
@@ -39,6 +40,9 @@ static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
3940
static const bool DEFAULT_NAMED=false;
4041
static const int CONTINUE_EXECUTION=-1;
4142

43+
/** Default number of blocks to generate for RPC generatetoaddress. */
44+
static const std::string DEFAULT_NBLOCKS = "1";
45+
4246
static void SetupCliArgs()
4347
{
4448
SetupHelpOptions(gArgs);
@@ -50,6 +54,7 @@ static void SetupCliArgs()
5054
gArgs.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5155
gArgs.AddArg("-conf=<file>", strprintf("Specify configuration file. Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5256
gArgs.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
57+
gArgs.AddArg("-generate", strprintf("Generate blocks immediately, equivalent to RPC generatenewaddress followed by RPC generatetoaddress. Optional positional integer arguments are number of blocks to generate (default: %s) and maximum iterations to try (default: %s), equivalent to RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000", DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5358
gArgs.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the results of -getinfo is the result of multiple non-atomic requests. Some entries in the result may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5459
SetupChainParamsBaseOptions();
5560
gArgs.AddArg("-named", strprintf("Pass named instead of positional arguments (default: %s)", DEFAULT_NAMED), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -537,6 +542,22 @@ static UniValue GetNewAddress()
537542
return ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{});
538543
}
539544

545+
/**
546+
* Check bounds and set up args for RPC generatetoaddress params: nblocks, address, maxtries.
547+
* @param[in] address Reference to const string address to insert into the args.
548+
* @param args Reference to vector of string args to modify.
549+
*/
550+
static void SetGenerateToAddressArgs(const std::string& address, std::vector<std::string>& args)
551+
{
552+
if (args.size() > 2) throw std::runtime_error("too many arguments (maximum 2 for nblocks and maxtries)");
553+
if (args.size() == 0) {
554+
args.emplace_back(DEFAULT_NBLOCKS);
555+
} else if (args.at(0) == "0") {
556+
throw std::runtime_error("the first argument (number of blocks to generate, default: " + DEFAULT_NBLOCKS + ") must be an integer value greater than zero");
557+
}
558+
args.emplace(args.begin() + 1, address);
559+
}
560+
540561
static int CommandLineRPC(int argc, char *argv[])
541562
{
542563
std::string strPrint;
@@ -595,6 +616,15 @@ static int CommandLineRPC(int argc, char *argv[])
595616
std::string method;
596617
if (gArgs.IsArgSet("-getinfo")) {
597618
rh.reset(new GetinfoRequestHandler());
619+
} else if (gArgs.GetBoolArg("-generate", false)) {
620+
const UniValue getnewaddress{GetNewAddress()};
621+
const UniValue& error{find_value(getnewaddress, "error")};
622+
if (error.isNull()) {
623+
SetGenerateToAddressArgs(find_value(getnewaddress, "result").get_str(), args);
624+
rh.reset(new GenerateToAddressRequestHandler());
625+
} else {
626+
ParseError(error, strPrint, nRet);
627+
}
598628
} else {
599629
rh.reset(new DefaultRequestHandler());
600630
if (args.size() < 1) {

0 commit comments

Comments
 (0)