Skip to content

Commit 33b63d5

Browse files
committed
[RPC][Mining] Add sanity checks to generatecontinuous
1 parent 6ed4626 commit 33b63d5

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/rpc/client.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
3131
{ "generate", 1, "maxtries" },
3232
{ "generatecontinuous", 0, "fGenerate"},
3333
{ "generatecontinuous", 1, "threads"},
34+
{ "generatecontinuous", 2, "override"},
3435
{ "generatetoaddress", 0, "nblocks" },
3536
{ "generatetoaddress", 2, "maxtries" },
3637
{ "getnetworkhashps", 0, "nblocks" },

src/wallet/rpcwallet.cpp

+60-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <shutdown.h>
2626
#include <timedata.h>
2727
#include <util.h>
28+
#include <miner.h>
2829
#include <utilmoneystr.h>
2930
#include <wallet/coincontrol.h>
3031
#include <wallet/feebumper.h>
@@ -4449,11 +4450,21 @@ UniValue generatecontinuous(const JSONRPCRequest& request)
44494450
return NullUniValue;
44504451
}
44514452

4452-
if (request.fHelp || request.params.size() > 2) {
4453+
if (request.fHelp || request.params.size() > 3) {
44534454
throw std::runtime_error(
4454-
"generatecontinuous (true|false) (threads)\n"
4455+
"generatecontinuous <activate> (threads) (override)\n"
44554456
"\nMine blocks continuously while the request is running.\n"
4456-
"\n(note for Randomx, any value under 4 will use 4 threads)\n"
4457+
"\nArguments:\n"
4458+
"1. activate (boolean, required) Enable or disable mining\n"
4459+
"2. threads (int, required) for enabling, number of threads\n"
4460+
"3. override (boolean, optional) override thread warnings\n"
4461+
"\nResult:\n"
4462+
"{\n"
4463+
" \"success\": true|false, (boolean) Status of the request\n"
4464+
" \"algorithm\": \"string\", (string) Algorithm being mined\n"
4465+
" \"threads\": nnn, (int) Number of threads being used\n"
4466+
" \"message\": \"text\", (string) Informational message\n"
4467+
"}\n"
44574468
"\nExamples:\n"
44584469
+ HelpExampleCli("generatecontinuous", "true 4")
44594470
+ HelpExampleRpc("generateoontinuous", "true, 4")
@@ -4470,20 +4481,64 @@ UniValue generatecontinuous(const JSONRPCRequest& request)
44704481
if (request.params.size() > 1)
44714482
nThreads = request.params[1].get_int();
44724483

4484+
bool fOverride = false;
4485+
if (request.params.size() > 2)
4486+
fOverride = request.params[2].get_bool();
4487+
44734488
std::shared_ptr<CReserveScript> coinbase_script;
44744489
pwallet->GetScriptForMining(coinbase_script);
44754490

44764491
// If the keypool is exhausted, no script is returned at all. Catch this.
44774492
if (!coinbase_script) {
4478-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
4493+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT,
4494+
"Error: Keypool ran out, please call keypoolrefill first");
44794495
}
44804496

44814497
//throw an error if no script was provided
44824498
if (coinbase_script->reserveScript.empty()) {
44834499
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available");
44844500
}
44854501

4486-
return generateBlocks(fGenerate, nThreads, coinbase_script);
4502+
int nAlgo = GetMiningAlgorithm();
4503+
std::string sAlgo = GetMiningType(nAlgo, false, false);
4504+
std:string sWarning = "";
4505+
4506+
if (fGenerate) {
4507+
if (GenerateActive())
4508+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Mining already active");
4509+
4510+
int nCores = GetNumCores();
4511+
if ((nAlgo == MINE_SHA256D) && (nThreads > (nCores - 1)))
4512+
sWarning = strprintf("Available cores: %d, limit sha256d to %d threads",
4513+
nCores, nCores-1);
4514+
4515+
if ((nAlgo == MINE_RANDOMX) && (nThreads < 4)) {
4516+
sWarning = "RandomX must be at least 4 threads";
4517+
// Note this changes the nThreads input, for accuracy of the result
4518+
// message, So this check needs to be below the threads check above
4519+
nThreads = 4;
4520+
}
4521+
4522+
if (!fOverride && sWarning.compare(""))
4523+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: %s", sWarning.c_str()));
4524+
}
4525+
4526+
UniValue result(UniValue::VOBJ);
4527+
result.pushKV("success", true);
4528+
result.pushKV("algorithm", sAlgo);
4529+
if (!fGenerate) {
4530+
result.pushKV("threads", 0);
4531+
result.pushKV("message", "Mining stopped");
4532+
} else {
4533+
result.pushKV("threads", nThreads);
4534+
if (sWarning.compare(""))
4535+
result.pushKV("message", strprintf("Warning: %s", sWarning.c_str()));
4536+
else
4537+
result.pushKV("message", "Mining started");
4538+
}
4539+
4540+
generateBlocks(fGenerate, nThreads, coinbase_script);
4541+
return result;
44874542
}
44884543

44894544
UniValue rescanblockchain(const JSONRPCRequest& request)

0 commit comments

Comments
 (0)