Skip to content

Commit 6ed4626

Browse files
committed
[RPC][Mining] Provide ability to change mining algorithm
1 parent 0807f52 commit 6ed4626

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

src/miner.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ const char * RANDOMX_STRING = "randomx";
5454

5555
int nMiningAlgorithm = MINE_RANDOMX;
5656

57+
bool fGenerateActive = false;
58+
59+
bool GenerateActive() { return fGenerateActive; };
60+
void setGenerate(bool fGenerate) { fGenerateActive = fGenerate; };
61+
5762
std::map<uint256, int64_t>mapComputeTimeTransactions;
5863

5964
// Unconfirmed transactions in the memory pool often depend on other
@@ -831,7 +836,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
831836
pblock->hashWitnessMerkleRoot = BlockWitnessMerkleRoot(*pblock, &malleated);
832837
}
833838

834-
bool fGenerateBitcoins = false;
835839
bool fMintableCoins = false;
836840
int nMintableLastCheck = 0;
837841

@@ -851,7 +855,7 @@ void BitcoinMiner(std::shared_ptr<CReserveScript> coinbaseScript, bool fProofOfS
851855
enablewallet = !gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET);
852856
#endif
853857

854-
while (fGenerateBitcoins || (fProofOfStake && enablewallet))
858+
while (GenerateActive() || (fProofOfStake && enablewallet))
855859
{
856860
boost::this_thread::interruption_point();
857861
#ifdef ENABLE_WALLET
@@ -926,7 +930,9 @@ void BitcoinMiner(std::shared_ptr<CReserveScript> coinbaseScript, bool fProofOfS
926930
}
927931
#endif
928932

929-
if (fGenerateBitcoins && !fProofOfStake) { // If the miner was turned on and we are in IsInitialBlockDownload(), sleep 60 seconds, before trying again
933+
if (GenerateActive() && !fProofOfStake) {
934+
// If the miner was turned on and we are in IsInitialBlockDownload(),
935+
// sleep 60 seconds, before trying again
930936
if (IsInitialBlockDownload() && !gArgs.GetBoolArg("-genoverride", false)) {
931937
MilliSleep(60000);
932938
continue;
@@ -1024,11 +1030,11 @@ void BitcoinRandomXMiner(std::shared_ptr<CReserveScript> coinbaseScript, int vm_
10241030
static const int nInnerLoopCount = RANDOMX_INNER_LOOP_COUNT;
10251031
bool fBlockFoundAlready = false;
10261032

1027-
while (fGenerateBitcoins)
1033+
while (GenerateActive())
10281034
{
10291035
boost::this_thread::interruption_point();
10301036

1031-
if (fGenerateBitcoins) { // If the miner was turned on and we are in IsInitialBlockDownload(), sleep 60 seconds, before trying again
1037+
if (GenerateActive()) { // If the miner was turned on and we are in IsInitialBlockDownload(), sleep 60 seconds, before trying again
10321038
if (IsInitialBlockDownload() && !gArgs.GetBoolArg("-genoverride", false)) {
10331039
MilliSleep(60000);
10341040
continue;
@@ -1209,7 +1215,7 @@ void GenerateBitcoins(bool fGenerate, int nThreads, std::shared_ptr<CReserveScri
12091215
error("%s: pthreadGroupPoW is null! Cannot mine.", __func__);
12101216
return;
12111217
}
1212-
fGenerateBitcoins = fGenerate;
1218+
setGenerate(fGenerate);
12131219

12141220
if (nThreads < 0) {
12151221
// In regtest threads defaults to 1

src/miner.h

+3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ class BlockAssembler
241241
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
242242
};
243243

244+
bool GenerateActive();
245+
void setGenerate(bool fGenerate);
246+
244247
/** Modify the extranonce in a block */
245248
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
246249
int64_t UpdateTime(CBlock* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

src/rpc/mining.cpp

+41-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
173173
"\nArguments:\n"
174174
"1. nblocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
175175
"2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
176-
"3. algo (numeric, optional, default="+GetMiningType(GetMiningAlgorithm(), false, false)+") Algo to calculate [randomx, sha256d, progpow, xr16t, PoS].\n"
176+
"3. algo (string, optional, default="+GetMiningType(GetMiningAlgorithm(), false, false)+") Algo to calculate [randomx, sha256d, progpow, xr16t, PoS].\n"
177177
"\nResult:\n"
178178
"x (numeric) Hashes per second estimated\n"
179179
"\nExamples:\n"
@@ -346,6 +346,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
346346
" \"blocks\": nnn, (numeric) The current block\n"
347347
" \"currentblockweight\": nnn, (numeric) The last block weight\n"
348348
" \"currentblocktx\": nnn, (numeric) The last block transaction\n"
349+
" \"algorithm\": \"xxxx\", (string) Algorithm set to mine (progpow, randomx, sha256d)\n"
349350
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
350351
" \"networkhashps\": nnn, (numeric) The network hashes per second\n"
351352
" \"pooledtx\": n (numeric) The size of the mempool\n"
@@ -384,6 +385,39 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
384385
return obj;
385386
}
386387

388+
static UniValue setminingalgo(const JSONRPCRequest& request)
389+
{
390+
if (request.fHelp || request.params.size() != 1)
391+
throw std::runtime_error(
392+
"setminingalgo algorithm\n"
393+
"\nChanges your mining algorithm to [algorithm]. Note that mining must be turned off when command is used.\n"
394+
"\nArguments:\n"
395+
"1. algorithm (string, required) Algorithm to mine [progpow, randomx, sha256d].\n"
396+
"\nResult:\n"
397+
"{\n"
398+
" \"success\": true|false, (boolean) Status of the switch\n"
399+
" \"message\": \"text\", (text) Informational message about the switch\n"
400+
"}\n"
401+
"\nExamples:\n"
402+
+ HelpExampleCli("setminingalgo", "sha256d")
403+
+ HelpExampleRpc("setminingalgo", "sha256d")
404+
);
405+
406+
if (GenerateActive())
407+
throw JSONRPCError(RPC_INVALID_PARAMETER, "mining must be stopped to change algorithm");
408+
409+
std::string sOldAlgo = GetMiningType(GetMiningAlgorithm(), false, false);
410+
std::string sNewAlgo = request.params[0].get_str();
411+
// Check if it's a mining algorithm
412+
if (!SetMiningAlgorithm(sNewAlgo)) {
413+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s is not a supported mining type", sNewAlgo));
414+
}
415+
UniValue result(UniValue::VOBJ);
416+
result.pushKV("success", true);
417+
result.pushKV("message", strprintf("Mining algorithm changed from %s to %s", sOldAlgo, sNewAlgo));
418+
return result;
419+
420+
}
387421

388422
// NOTE: Unlike wallet RPC (which use VEIL values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
389423
static UniValue prioritisetransaction(const JSONRPCRequest& request)
@@ -1246,20 +1280,19 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
12461280
static const CRPCCommand commands[] =
12471281
{ // category name actor (function) argNames
12481282
// --------------------- ------------------------ ----------------------- ----------
1249-
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height","algo"} },
1283+
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
12501284
{ "mining", "getmininginfo", &getmininginfo, {} },
1285+
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height","algo"} },
12511286
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
1252-
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
1253-
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },
12541287
{ "mining", "pprpcsb", &pprpcsb, {"header_hash", "mix_hash", "nonce"} },
1255-
1288+
{ "mining", "setminingalgo", &setminingalgo, {"algo"} },
1289+
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },
12561290

12571291
{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },
1258-
{ "hidden", "estimatefee", &estimatefee, {} },
1259-
1260-
{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },
12611292

1293+
{ "hidden", "estimatefee", &estimatefee, {} },
12621294
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
1295+
{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },
12631296
};
12641297

12651298
void RegisterMiningRPCCommands(CRPCTable &t)

0 commit comments

Comments
 (0)