|
15 | 15 | #include "policy/fees.h" |
16 | 16 | #include "policy/policy.h" |
17 | 17 | #include "policy/rbf.h" |
| 18 | +#include "rpc/mining.h" |
18 | 19 | #include "rpc/server.h" |
19 | 20 | #include "script/sign.h" |
20 | 21 | #include "timedata.h" |
@@ -2922,6 +2923,47 @@ UniValue bumpfee(const JSONRPCRequest& request) |
2922 | 2923 | return result; |
2923 | 2924 | } |
2924 | 2925 |
|
| 2926 | +UniValue generate(const JSONRPCRequest& request) |
| 2927 | +{ |
| 2928 | + CWallet * const pwallet = GetWalletForJSONRPCRequest(request); |
| 2929 | + |
| 2930 | + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) |
| 2931 | + return NullUniValue; |
| 2932 | + |
| 2933 | + if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) |
| 2934 | + throw std::runtime_error( |
| 2935 | + "generate nblocks ( maxtries )\n" |
| 2936 | + "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n" |
| 2937 | + "\nArguments:\n" |
| 2938 | + "1. nblocks (numeric, required) How many blocks are generated immediately.\n" |
| 2939 | + "2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n" |
| 2940 | + "\nResult:\n" |
| 2941 | + "[ blockhashes ] (array) hashes of blocks generated\n" |
| 2942 | + "\nExamples:\n" |
| 2943 | + "\nGenerate 11 blocks\n" |
| 2944 | + + HelpExampleCli("generate", "11") |
| 2945 | + ); |
| 2946 | + |
| 2947 | + int nGenerate = request.params[0].get_int(); |
| 2948 | + uint64_t nMaxTries = 1000000; |
| 2949 | + if (request.params.size() > 1) { |
| 2950 | + nMaxTries = request.params[1].get_int(); |
| 2951 | + } |
| 2952 | + |
| 2953 | + std::shared_ptr<CReserveScript> coinbaseScript; |
| 2954 | + pwallet->GetScriptForMining(coinbaseScript); |
| 2955 | + |
| 2956 | + // If the keypool is exhausted, no script is returned at all. Catch this. |
| 2957 | + if (!coinbaseScript) |
| 2958 | + throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); |
| 2959 | + |
| 2960 | + //throw an error if no script was provided |
| 2961 | + if (coinbaseScript->reserveScript.empty()) |
| 2962 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)"); |
| 2963 | + |
| 2964 | + return generateBlocks(coinbaseScript, nGenerate, nMaxTries, true); |
| 2965 | +} |
| 2966 | + |
2925 | 2967 | extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp |
2926 | 2968 | extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp |
2927 | 2969 | extern UniValue importprivkey(const JSONRPCRequest& request); |
@@ -2985,6 +3027,8 @@ static const CRPCCommand commands[] = |
2985 | 3027 | { "wallet", "walletpassphrasechange", &walletpassphrasechange, true, {"oldpassphrase","newpassphrase"} }, |
2986 | 3028 | { "wallet", "walletpassphrase", &walletpassphrase, true, {"passphrase","timeout"} }, |
2987 | 3029 | { "wallet", "removeprunedfunds", &removeprunedfunds, true, {"txid"} }, |
| 3030 | + |
| 3031 | + { "generating", "generate", &generate, true, {"nblocks","maxtries"} }, |
2988 | 3032 | }; |
2989 | 3033 |
|
2990 | 3034 | void RegisterWalletRPCCommands(CRPCTable &t) |
|
0 commit comments