Skip to content

Commit

Permalink
cli: create bitcoin-cli -generate command
Browse files Browse the repository at this point in the history
Summary:
This is a backport of [[bitcoin/bitcoin#19133 | core#19133]] [6/10]
bitcoin/bitcoin@4818124

Depends on D9945

Test Plan:
```
src/qt/bitcoin-qt -regtest -server
src/bitcoin-cli -regtest -generate 8
```

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9946
  • Loading branch information
jonatack authored and PiRK committed Aug 26, 2021
1 parent 0edee2d commit 40714b7
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <clientversion.h>
#include <currencyunit.h>
#include <rpc/client.h>
#include <rpc/mining.h>
#include <rpc/protocol.h>
#include <rpc/request.h>
#include <support/events.h>
Expand Down Expand Up @@ -39,6 +40,9 @@ static const int CONTINUE_EXECUTION = -1;
static const std::string ONION{".onion"};
static const size_t ONION_LEN{ONION.size()};

/** Default number of blocks to generate for RPC generatetoaddress. */
static const std::string DEFAULT_NBLOCKS = "1";

static void SetupCliArgs(ArgsManager &argsman) {
SetupHelpOptions(argsman);

Expand All @@ -60,6 +64,17 @@ static void SetupCliArgs(ArgsManager &argsman) {
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-datadir=<dir>", "Specify data directory",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.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);
argsman.AddArg(
"-getinfo",
"Get general information from the remote server. Note that unlike "
Expand Down Expand Up @@ -943,12 +958,35 @@ static void GetWalletBalances(UniValue &result) {
* Call RPC getnewaddress.
* @returns getnewaddress response as a UniValue object.
*/
[[maybe_unused]] static UniValue GetNewAddress() {
static UniValue GetNewAddress() {
std::unique_ptr<BaseRequestHandler> rh{
std::make_unique<DefaultRequestHandler>()};
return ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{});
}

/**
* Check bounds and set up args for RPC generatetoaddress params: nblocks,
* address, maxtries.
* @param[in] address Reference to const string address to insert into the
* args.
* @param args Reference to vector of string args to modify.
*/
static void SetGenerateToAddressArgs(const std::string &address,
std::vector<std::string> &args) {
if (args.size() > 2) {
throw std::runtime_error(
"too many arguments (maximum 2 for nblocks and maxtries)");
}
if (args.size() == 0) {
args.emplace_back(DEFAULT_NBLOCKS);
} else if (args.at(0) == "0") {
throw std::runtime_error(
"the first argument (number of blocks to generate, default: " +
DEFAULT_NBLOCKS + ") must be an integer value greater than zero");
}
args.emplace(args.begin() + 1, address);
}

static int CommandLineRPC(int argc, char *argv[]) {
std::string strPrint;
int nRet = 0;
Expand Down Expand Up @@ -1012,6 +1050,16 @@ static int CommandLineRPC(int argc, char *argv[]) {
std::string method;
if (gArgs.IsArgSet("-getinfo")) {
rh.reset(new GetinfoRequestHandler());
} else if (gArgs.GetBoolArg("-generate", false)) {
const UniValue getnewaddress{GetNewAddress()};
const UniValue &error{find_value(getnewaddress, "error")};
if (error.isNull()) {
SetGenerateToAddressArgs(
find_value(getnewaddress, "result").get_str(), args);
rh.reset(new GenerateToAddressRequestHandler());
} else {
ParseError(error, strPrint, nRet);
}
} else if (gArgs.GetBoolArg("-netinfo", false)) {
rh.reset(new NetinfoRequestHandler());
} else {
Expand Down

0 comments on commit 40714b7

Please sign in to comment.