Skip to content

Commit ea18c5a

Browse files
committed
[RPC][Mining] Extract last Hashspeed calculation
1 parent 0697885 commit ea18c5a

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/miner.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,20 @@ CCriticalSection cs_nonce;
843843
static int32_t nNonce_base = 0;
844844
static arith_uint256 nHashes = 0;
845845
static int32_t nTimeStart = 0;
846+
static int32_t nTimeDuration = 0;
847+
848+
double GetHashSpeed() {
849+
LOCK(cs_nonce);
850+
if (!nTimeDuration) return 0;
851+
return arith_uint256(nHashes/nTimeDuration).getdouble();
852+
}
853+
854+
void ClearHashSpeed() {
855+
LOCK(cs_nonce);
856+
nHashes = 0;
857+
nTimeStart = 0;
858+
nTimeDuration = 0;
859+
}
846860

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

1004-
LOCK(cs_nonce);
1005-
nHashes += nTries;
1006-
int32_t nTimeDuration = GetTime() - nTimeStart;
1007-
if (!nTimeDuration) nTimeDuration = 1;
1008-
LogPrint(BCLog::BLOCKCREATION, "%s: PoW Hashspeed %d kh/s\n", __func__,
1009-
arith_uint256(nHashes/1000/nTimeDuration).getdouble());
1018+
double nHashSpeed = 0;
1019+
{
1020+
LOCK(cs_nonce);
1021+
nHashes += nTries;
1022+
nTimeDuration = GetTime() - nTimeStart;
1023+
if (!nTimeDuration) nTimeDuration = 1;
1024+
nHashSpeed = arith_uint256(nHashes/1000/nTimeDuration).getdouble();
1025+
}
1026+
LogPrint(BCLog::BLOCKCREATION, "%s: PoW Hashspeed %d kh/s\n", __func__, nHashSpeed);
1027+
10101028
if (nTries == nInnerLoopCount) {
10111029
continue;
10121030
}
@@ -1114,13 +1132,15 @@ void BitcoinRandomXMiner(std::shared_ptr<CReserveScript> coinbaseScript, int vm_
11141132
}
11151133
}
11161134

1117-
int32_t nTimeDuration = GetTime() - nTimeStart;
11181135
double nHashSpeed = 0;
1119-
if (!nTimeDuration) nTimeDuration = 1;
11201136
{
11211137
LOCK(cs_nonce);
1122-
nHashes += nTries;
1123-
nHashSpeed = arith_uint256(nHashes/nTimeDuration).getdouble();
1138+
nTimeDuration = GetTime() - nTimeStart;
1139+
if (!nTimeDuration) nTimeDuration = 1;
1140+
{
1141+
nHashes += nTries;
1142+
nHashSpeed = arith_uint256(nHashes/nTimeDuration).getdouble();
1143+
}
11241144
}
11251145
LogPrint(BCLog::BLOCKCREATION, "%s: RandomX PoW Hashspeed %d hashes/s\n", __func__, nHashSpeed);
11261146

src/miner.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static std::string GetMiningType(int nPoWType, bool fProofOfStake = false, bool
4949
return "Unknown";
5050
}
5151

52+
double GetHashSpeed();
53+
void ClearHashSpeed();
5254
int GetMiningAlgorithm();
5355
bool SetMiningAlgorithm(const std::string& algo, bool fSet = true);
5456

src/rpc/mining.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
349349
" \"algorithm\": \"xxxx\", (string) Algorithm set to mine (progpow, randomx, sha256d)\n"
350350
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
351351
" \"networkhashps\": nnn, (numeric) The network hashes per second\n"
352+
" \"hashspeed\": nnn, (numeric) The system hashes per second\n"
352353
" \"pooledtx\": n (numeric) The size of the mempool\n"
353354
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
354355
" \"warnings\": \"...\" (string) any network and blockchain warnings\n"
@@ -379,6 +380,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
379380
obj.pushKV("algorithm", sMiningAlgo);
380381
obj.pushKV("difficulty", (double)nDiff);
381382
obj.pushKV("networkhashps", getnetworkhashps(request));
383+
obj.pushKV("hashspeed", GetHashSpeed());
382384
obj.pushKV("pooledtx", (uint64_t)mempool.size());
383385
obj.pushKV("chain", Params().NetworkIDString());
384386
obj.pushKV("warnings", GetWarnings("statusbar"));
@@ -390,7 +392,7 @@ static UniValue setminingalgo(const JSONRPCRequest& request)
390392
if (request.fHelp || request.params.size() != 1)
391393
throw std::runtime_error(
392394
"setminingalgo algorithm\n"
393-
"\nChanges your mining algorithm to [algorithm]. Note that mining must be turned off when command is used.\n"
395+
"\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"
394396
"\nArguments:\n"
395397
"1. algorithm (string, required) Algorithm to mine [progpow, randomx, sha256d].\n"
396398
"\nResult:\n"
@@ -412,6 +414,7 @@ static UniValue setminingalgo(const JSONRPCRequest& request)
412414
if (!SetMiningAlgorithm(sNewAlgo)) {
413415
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s is not a supported mining type", sNewAlgo));
414416
}
417+
ClearHashSpeed();
415418
UniValue result(UniValue::VOBJ);
416419
result.pushKV("success", true);
417420
result.pushKV("message", strprintf("Mining algorithm changed from %s to %s", sOldAlgo, sNewAlgo));

0 commit comments

Comments
 (0)