Skip to content

Commit

Permalink
Merge #883: [RPC][Mining] Extract last Hashspeed calculation
Browse files Browse the repository at this point in the history
ea18c5a [RPC][Mining] Extract last Hashspeed calculation (Cave Spectre)

Pull request description:

  ### Problem
  Users want to be able to see their current hashing speed

  ### Solution
  Extract the last calculated hashspeed and provide it in `getmininginfo`.  To note, this only pulls the last calculation that was done, and is not a real-time pull.  It will be the average from the start of mining to the last calculation, from the time the first `generatecontinuous true` was run.  In other words, this will take into account any 'breaks' taken in mining.  e.g. if you have a script that turns off mining for a certain time; the hash speed calculations are still being run; and the break will be calculated into your overall hash rate.

  Logic was added to `setminingalgo` to reset the statistical counters.  So after `setminingalgo` is issued, the statistics will restart on the next `generatecontinuous`.  This can be used to reset your counters even if you're not changing algorithms by setting your mining algo to the same algo:

  ```
  $ ./veil-cli -regtest getmininginfo
  {
  [...]
    "hashspeed": 743034,
  [...]
  }
  $ ./veil-cli -regtest setminingalgo sha256d
  {
    "success": true,
    "message": "Mining algorithm changed from sha256d to sha256d"
  }
  $ ./veil-cli -regtest getmininginfo
  {
  [...]
    "hashspeed": 0,
  }
  ```

  Note that for simplicity sake, the calculation is "hashes per second", so that it can remain a numerical value and users can wrap the use however they wish if they want to convert it to kh/s or mh/s [or whatever they desire]

Tree-SHA512: 484be9de3f40acf76b60db16ba071a2585f1bce7b5852f6371f66d9ff0089b50576893d44981055526d81626c543d00701e38ada603535cc1ade4601627751a3
  • Loading branch information
codeofalltrades committed Dec 14, 2020
2 parents 0697885 + ea18c5a commit 1194909
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
40 changes: 30 additions & 10 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,20 @@ CCriticalSection cs_nonce;
static int32_t nNonce_base = 0;
static arith_uint256 nHashes = 0;
static int32_t nTimeStart = 0;
static int32_t nTimeDuration = 0;

double GetHashSpeed() {
LOCK(cs_nonce);
if (!nTimeDuration) return 0;
return arith_uint256(nHashes/nTimeDuration).getdouble();
}

void ClearHashSpeed() {
LOCK(cs_nonce);
nHashes = 0;
nTimeStart = 0;
nTimeDuration = 0;
}

void BitcoinMiner(std::shared_ptr<CReserveScript> coinbaseScript, bool fProofOfStake = false, bool fProofOfFullNode = false) {
LogPrintf("Veil Miner started\n");
Expand Down Expand Up @@ -1001,12 +1015,16 @@ void BitcoinMiner(std::shared_ptr<CReserveScript> coinbaseScript, bool fProofOfS
return;
}

LOCK(cs_nonce);
nHashes += nTries;
int32_t nTimeDuration = GetTime() - nTimeStart;
if (!nTimeDuration) nTimeDuration = 1;
LogPrint(BCLog::BLOCKCREATION, "%s: PoW Hashspeed %d kh/s\n", __func__,
arith_uint256(nHashes/1000/nTimeDuration).getdouble());
double nHashSpeed = 0;
{
LOCK(cs_nonce);
nHashes += nTries;
nTimeDuration = GetTime() - nTimeStart;
if (!nTimeDuration) nTimeDuration = 1;
nHashSpeed = arith_uint256(nHashes/1000/nTimeDuration).getdouble();
}
LogPrint(BCLog::BLOCKCREATION, "%s: PoW Hashspeed %d kh/s\n", __func__, nHashSpeed);

if (nTries == nInnerLoopCount) {
continue;
}
Expand Down Expand Up @@ -1114,13 +1132,15 @@ void BitcoinRandomXMiner(std::shared_ptr<CReserveScript> coinbaseScript, int vm_
}
}

int32_t nTimeDuration = GetTime() - nTimeStart;
double nHashSpeed = 0;
if (!nTimeDuration) nTimeDuration = 1;
{
LOCK(cs_nonce);
nHashes += nTries;
nHashSpeed = arith_uint256(nHashes/nTimeDuration).getdouble();
nTimeDuration = GetTime() - nTimeStart;
if (!nTimeDuration) nTimeDuration = 1;
{
nHashes += nTries;
nHashSpeed = arith_uint256(nHashes/nTimeDuration).getdouble();
}
}
LogPrint(BCLog::BLOCKCREATION, "%s: RandomX PoW Hashspeed %d hashes/s\n", __func__, nHashSpeed);

Expand Down
2 changes: 2 additions & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static std::string GetMiningType(int nPoWType, bool fProofOfStake = false, bool
return "Unknown";
}

double GetHashSpeed();
void ClearHashSpeed();
int GetMiningAlgorithm();
bool SetMiningAlgorithm(const std::string& algo, bool fSet = true);

Expand Down
5 changes: 4 additions & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
" \"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"
" \"hashspeed\": nnn, (numeric) The system hashes per second\n"
" \"pooledtx\": n (numeric) The size of the mempool\n"
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
" \"warnings\": \"...\" (string) any network and blockchain warnings\n"
Expand Down Expand Up @@ -379,6 +380,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
obj.pushKV("algorithm", sMiningAlgo);
obj.pushKV("difficulty", (double)nDiff);
obj.pushKV("networkhashps", getnetworkhashps(request));
obj.pushKV("hashspeed", GetHashSpeed());
obj.pushKV("pooledtx", (uint64_t)mempool.size());
obj.pushKV("chain", Params().NetworkIDString());
obj.pushKV("warnings", GetWarnings("statusbar"));
Expand All @@ -390,7 +392,7 @@ 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"
"\nChanges your mining algorithm to [algorithm]. Note that mining must be turned off when command is used. This will also have the side effect of clearing your mining statistics.\n"
"\nArguments:\n"
"1. algorithm (string, required) Algorithm to mine [progpow, randomx, sha256d].\n"
"\nResult:\n"
Expand All @@ -412,6 +414,7 @@ static UniValue setminingalgo(const JSONRPCRequest& request)
if (!SetMiningAlgorithm(sNewAlgo)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s is not a supported mining type", sNewAlgo));
}
ClearHashSpeed();
UniValue result(UniValue::VOBJ);
result.pushKV("success", true);
result.pushKV("message", strprintf("Mining algorithm changed from %s to %s", sOldAlgo, sNewAlgo));
Expand Down

0 comments on commit 1194909

Please sign in to comment.