Skip to content

Commit

Permalink
Merge #878: [RPC][Mining] Add ability to switch between mining algori…
Browse files Browse the repository at this point in the history
…thms without restarting wallet

6ed4626 [RPC][Mining] Provide ability to change mining algorithm (Cave Spectre)

Pull request description:

  ### Note
  This is built on top of #876 and also contains a couple corrections to get the `getnetworkhashps` help.

  ### Problem
  In order to switch mining algorithms, you must shut down the wallet and restart with a new `-mine=` flag.

  ### Root Cause
  Never implemented

  ### Solution
  Add `setminingalgo` cli command to allow you to switch between different algorithms.  This RPC command requires you to turn off mining before issuing the command and will error if it does not work.

  ### Testing
  ```
  $ veil-cli help setminingalgo
  setminingalgo algorithm

  Changes your mining algorithm to [algorithm].  Note that mining must be turned off when command is used.

  Arguments:
  1. algorithm   (string, required) Algorithm to mine [progpow, randomx, sha256d].

  Result:
  {
    "success": true|false, (boolean) Status of the switch
    "message": "text",     (text) Informational message about the switch
  }

  Examples:
  > veil-cli setminingalgo sha256d
  > curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "setminingalgo", "params": [sha256d] }' -H 'content-type: text/plain;' http://127.0.0.1:58812/
  ```
  ```
  $ veil-cli setminingalgo randomx
  {
    "success": true,
    "message": "Mining algorithm changed from sha256d to randomx"
  }
   veil-cli getmininginfo
  {
  [...]
    "algorithm": "randomx",
    "difficulty": 0.003707938785486674,
    "networkhashps": 22.73835278085342,
  [...]
  }
  ```
  ```
  $ veil-cli setminingalgo sha257d
  error code: -8
  error message:
  sha257d is not a supported mining type
  $ veil-cli setminingalgo sha256d
  {
    "success": true,
    "message": "Mining algorithm changed from randomx to sha256d"
  }
  $ veil-cli getmininginfo
  {
  [...]
    "algorithm": "sha256d",
    "difficulty": 18.1774015474177,
    "networkhashps": 23.05669884097939,
  [...]
  }
  ```
  ```
  $ veil-cli generatecontinuous true 1
  $ veil-cli setminingalgo progpow
  error code: -8
  error message:
  mining must be stopped to change algorithm
  $ veil-cli generatecontinuous false
  $ veil-cli setminingalgo progpow
  {
    "success": true,
    "message": "Mining algorithm changed from sha256d to progpow"
  }
  $ veil-cli getmininginfo
  {
  [...]
    "algorithm": "progpow",
    "difficulty": 164.7481194885794,
    "networkhashps": 23.16038554216868,
  [...]
  }
  ```

Tree-SHA512: 89c8a5032856afcf48ba0ec6c0c9636d9f451e705efe1b9545c3b1719e7db7897d6721a5bf2663f4e3b0d05600c46acff5b6e9c4dd7d3ec3ab2fc47311a328d4
  • Loading branch information
codeofalltrades committed Dec 10, 2020
2 parents fb2a216 + 6ed4626 commit 8d209f5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const char * RANDOMX_STRING = "randomx";

int nMiningAlgorithm = MINE_RANDOMX;

bool fGenerateActive = false;

bool GenerateActive() { return fGenerateActive; };
void setGenerate(bool fGenerate) { fGenerateActive = fGenerate; };

std::map<uint256, int64_t>mapComputeTimeTransactions;

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

bool fGenerateBitcoins = false;
bool fMintableCoins = false;
int nMintableLastCheck = 0;

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

while (fGenerateBitcoins || (fProofOfStake && enablewallet))
while (GenerateActive() || (fProofOfStake && enablewallet))
{
boost::this_thread::interruption_point();
#ifdef ENABLE_WALLET
Expand Down Expand Up @@ -926,7 +930,9 @@ void BitcoinMiner(std::shared_ptr<CReserveScript> coinbaseScript, bool fProofOfS
}
#endif

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

while (fGenerateBitcoins)
while (GenerateActive())
{
boost::this_thread::interruption_point();

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

if (nThreads < 0) {
// In regtest threads defaults to 1
Expand Down
3 changes: 3 additions & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class BlockAssembler
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
};

bool GenerateActive();
void setGenerate(bool fGenerate);

/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
int64_t UpdateTime(CBlock* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
Expand Down
49 changes: 41 additions & 8 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
"\nArguments:\n"
"1. nblocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
"2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
"3. algo (numeric, optional, default="+GetMiningType(GetMiningAlgorithm(), false, false)+") Algo to calculate [randomx, sha256d, progpow, xr16t, PoS].\n"
"3. algo (string, optional, default="+GetMiningType(GetMiningAlgorithm(), false, false)+") Algo to calculate [randomx, sha256d, progpow, xr16t, PoS].\n"
"\nResult:\n"
"x (numeric) Hashes per second estimated\n"
"\nExamples:\n"
Expand Down Expand Up @@ -346,6 +346,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
" \"blocks\": nnn, (numeric) The current block\n"
" \"currentblockweight\": nnn, (numeric) The last block weight\n"
" \"currentblocktx\": nnn, (numeric) The last block transaction\n"
" \"algorithm\": \"xxxx\", (string) Algorithm set to mine (progpow, randomx, sha256d)\n"
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
" \"networkhashps\": nnn, (numeric) The network hashes per second\n"
" \"pooledtx\": n (numeric) The size of the mempool\n"
Expand Down Expand Up @@ -384,6 +385,39 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
return obj;
}

static UniValue setminingalgo(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1)
throw std::runtime_error(
"setminingalgo algorithm\n"
"\nChanges your mining algorithm to [algorithm]. Note that mining must be turned off when command is used.\n"
"\nArguments:\n"
"1. algorithm (string, required) Algorithm to mine [progpow, randomx, sha256d].\n"
"\nResult:\n"
"{\n"
" \"success\": true|false, (boolean) Status of the switch\n"
" \"message\": \"text\", (text) Informational message about the switch\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("setminingalgo", "sha256d")
+ HelpExampleRpc("setminingalgo", "sha256d")
);

if (GenerateActive())
throw JSONRPCError(RPC_INVALID_PARAMETER, "mining must be stopped to change algorithm");

std::string sOldAlgo = GetMiningType(GetMiningAlgorithm(), false, false);
std::string sNewAlgo = request.params[0].get_str();
// Check if it's a mining algorithm
if (!SetMiningAlgorithm(sNewAlgo)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s is not a supported mining type", sNewAlgo));
}
UniValue result(UniValue::VOBJ);
result.pushKV("success", true);
result.pushKV("message", strprintf("Mining algorithm changed from %s to %s", sOldAlgo, sNewAlgo));
return result;

}

// NOTE: Unlike wallet RPC (which use VEIL values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
static UniValue prioritisetransaction(const JSONRPCRequest& request)
Expand Down Expand Up @@ -1246,20 +1280,19 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
static const CRPCCommand commands[] =
{ // category name actor (function) argNames
// --------------------- ------------------------ ----------------------- ----------
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height","algo"} },
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
{ "mining", "getmininginfo", &getmininginfo, {} },
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height","algo"} },
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },
{ "mining", "pprpcsb", &pprpcsb, {"header_hash", "mix_hash", "nonce"} },

{ "mining", "setminingalgo", &setminingalgo, {"algo"} },
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },

{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },
{ "hidden", "estimatefee", &estimatefee, {} },

{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },

{ "hidden", "estimatefee", &estimatefee, {} },
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },
};

void RegisterMiningRPCCommands(CRPCTable &t)
Expand Down

0 comments on commit 8d209f5

Please sign in to comment.