mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Merge #19133: rpc, cli, test: add bitcoin-cli -generate command
22cb303cf0rpc: add missing space in JSON parsing error message, update test (Jon Atack)bf53ebef06test: add multiwallet tests for bitcoin-cli -generate (Jon Atack)4b859cfff9cli: add multiwallet capability to GetNewAddress and -generate (Jon Atack)18f93545a1test: add tests for bitcoin-cli -generate (Jon Atack)4818124137cli: create bitcoin-cli -generate command (Jon Atack)ff41a36900cli: extract ParseResult() and ParseError() (Jon Atack)f4185b26d9cli: create GenerateToAddressRequestHandler class (Harris)f7c65a3350cli: create GetNewAddress() (Jon Atack)9be7fd35c5rpc: make generatetoaddress locals const (Jon Atack)cb00510dbarpc: create rpc/mining.h, hoist default max tries values to constant (Jon Atack) Pull request description: This PR continues and completes the work begun in #17700 working on issue #16000 to create a client-side version of RPC `generate`. Basically, `bitcoin-cli -generate` wraps calling `generatenewaddress` followed by `generatetoaddress [nblocks] [maxtries]` and prints the following: ``` $ bitcoin-cli -generate { "address": "bcrt1qn4aszr2y2xvpa70y675a76wsu70wlkwvdyyln6" "blocks": [ "01d2ebcddf663da90b28da7f6805115e2ba7818f16fe747258836646a43a0bb5", ] } $ bitcoin-cli -rpcwallet=wallet-name -generate 3 100 { "address": "bcrt1q4cunfw0gnsj7g7e6mk0v0uuvvau9mwr09dj45l", "blocks": [ "7a6650ca5e0c614992ee64fb148a7e5e022af842e4b6003f81abd8baf1e75136", "01d2ebcddf663da90b28da7f6805115e2ba7818f16fe747258836646a43a0bb5", "3f8795ec40b1ad812b818c177680841be319a3f6753d4e32dc7dfb5bafe5d00e" ] } ``` Help doc: ``` $ bitcoin-cli -h | grep -A5 "\-generate" -generate Generate blocks immediately, equivalent to RPC generatenewaddress followed by RPC generatetoaddress. Optional positional arguments are number of blocks to generate (default: 1) and maximum iterations to try (default: 1000000), equivalent to RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000 ``` Quite a bit of test coverage turned out to be needed to cover the change and the different cases (arguments, multiwallet mode) and error-handling. This PR also improves some things that working on these changes brought to light. Credit to Harris Brakmić for the initial work in #17700. ACKs for top commit: adamjonas: utACK22cb303cf0meshcollider: utACK22cb303cf0Tree-SHA512: 94f67f632fe093d076f614e0ecff09ce7342ac6e424579200d5211a6615260e438d857861767fb788950ec6da0b26ef56dc8268c430012a3b3d4822b24ca6fbf
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <policy/fees.h>
|
||||
#include <pow.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/mining.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/util.h>
|
||||
#include <script/descriptor.h>
|
||||
@@ -207,7 +208,7 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
|
||||
{
|
||||
{"num_blocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
|
||||
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor to send the newly generated bitcoin to."},
|
||||
{"maxtries", RPCArg::Type::NUM, /* default */ "1000000", "How many iterations to try."},
|
||||
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
|
||||
},
|
||||
RPCResult{
|
||||
RPCResult::Type::ARR, "", "hashes of blocks generated",
|
||||
@@ -221,7 +222,7 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
|
||||
.Check(request);
|
||||
|
||||
const int num_blocks{request.params[0].get_int()};
|
||||
const int64_t max_tries{request.params[2].isNull() ? 1000000 : request.params[2].get_int()};
|
||||
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
|
||||
|
||||
CScript coinbase_script;
|
||||
std::string error;
|
||||
@@ -242,7 +243,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
{
|
||||
{"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
|
||||
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The address to send the newly generated bitcoin to."},
|
||||
{"maxtries", RPCArg::Type::NUM, /* default */ "1000000", "How many iterations to try."},
|
||||
{"maxtries", RPCArg::Type::NUM, /* default */ ToString(DEFAULT_MAX_TRIES), "How many iterations to try."},
|
||||
},
|
||||
RPCResult{
|
||||
RPCResult::Type::ARR, "", "hashes of blocks generated",
|
||||
@@ -257,11 +258,8 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
},
|
||||
}.Check(request);
|
||||
|
||||
int nGenerate = request.params[0].get_int();
|
||||
uint64_t nMaxTries = 1000000;
|
||||
if (!request.params[2].isNull()) {
|
||||
nMaxTries = request.params[2].get_int();
|
||||
}
|
||||
const int num_blocks{request.params[0].get_int()};
|
||||
const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].get_int()};
|
||||
|
||||
CTxDestination destination = DecodeDestination(request.params[1].get_str());
|
||||
if (!IsValidDestination(destination)) {
|
||||
@@ -273,7 +271,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||
|
||||
CScript coinbase_script = GetScriptForDestination(destination);
|
||||
|
||||
return generateBlocks(chainman, mempool, coinbase_script, nGenerate, nMaxTries);
|
||||
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
|
||||
}
|
||||
|
||||
static UniValue generateblock(const JSONRPCRequest& request)
|
||||
@@ -371,7 +369,7 @@ static UniValue generateblock(const JSONRPCRequest& request)
|
||||
}
|
||||
|
||||
uint256 block_hash;
|
||||
uint64_t max_tries{1000000};
|
||||
uint64_t max_tries{DEFAULT_MAX_TRIES};
|
||||
unsigned int extra_nonce{0};
|
||||
|
||||
if (!GenerateBlock(EnsureChainman(request.context), block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
|
||||
|
||||
Reference in New Issue
Block a user