diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 93d3920d21..e0c7ab9f0f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2927,10 +2927,11 @@ UniValue generate(const JSONRPCRequest& request) { CWallet * const pwallet = GetWalletForJSONRPCRequest(request); - if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { return NullUniValue; + } - if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) + if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) { throw std::runtime_error( "generate nblocks ( maxtries )\n" "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n" @@ -2943,25 +2944,28 @@ UniValue generate(const JSONRPCRequest& request) "\nGenerate 11 blocks\n" + HelpExampleCli("generate", "11") ); + } - int nGenerate = request.params[0].get_int(); - uint64_t nMaxTries = 1000000; - if (request.params.size() > 1) { - nMaxTries = request.params[1].get_int(); + int num_generate = request.params[0].get_int(); + uint64_t max_tries = 1000000; + if (request.params.size() > 1 && !request.params[1].isNull()) { + max_tries = request.params[1].get_int(); } - std::shared_ptr coinbaseScript; - pwallet->GetScriptForMining(coinbaseScript); + std::shared_ptr coinbase_script; + pwallet->GetScriptForMining(coinbase_script); // If the keypool is exhausted, no script is returned at all. Catch this. - if (!coinbaseScript) + if (!coinbase_script) { throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); + } //throw an error if no script was provided - if (coinbaseScript->reserveScript.empty()) - throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)"); + if (coinbase_script->reserveScript.empty()) { + throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available"); + } - return generateBlocks(coinbaseScript, nGenerate, nMaxTries, true); + return generateBlocks(coinbase_script, num_generate, max_tries, true); } extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp