Skip to content

Commit 0807f52

Browse files
committed
[RPC] Rework gethashnetworkps and getmininginfo
1 parent 3c45544 commit 0807f52

File tree

3 files changed

+129
-35
lines changed

3 files changed

+129
-35
lines changed

src/miner.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1252,17 +1252,17 @@ int GetMiningAlgorithm() {
12521252
return nMiningAlgorithm;
12531253
}
12541254

1255-
bool SetMiningAlgorithm(const std::string& algo) {
1256-
if (algo == PROGPOW_STRING) {
1257-
nMiningAlgorithm = MINE_PROGPOW;
1258-
return true;
1259-
} else if (algo == SHA256D_STRING) {
1260-
nMiningAlgorithm = MINE_SHA256D;
1261-
return true;
1262-
} else if (algo == RANDOMX_STRING) {
1263-
// Catches the default
1264-
nMiningAlgorithm = MINE_RANDOMX;
1255+
bool SetMiningAlgorithm(const std::string& algo, bool fSet) {
1256+
int setAlgo = -1;
1257+
1258+
if (algo == PROGPOW_STRING) setAlgo = MINE_PROGPOW;
1259+
else if (algo == SHA256D_STRING) setAlgo = MINE_SHA256D;
1260+
else if (algo == RANDOMX_STRING) setAlgo = MINE_RANDOMX;
1261+
1262+
if (setAlgo != -1) {
1263+
if (fSet) nMiningAlgorithm = setAlgo;
12651264
return true;
12661265
}
1266+
12671267
return false;
12681268
}

src/miner.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
22
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Copyright (c) 2019-2020 The Veil developers
34
// Distributed under the MIT software license, see the accompanying
45
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
56

@@ -49,7 +50,7 @@ static std::string GetMiningType(int nPoWType, bool fProofOfStake = false, bool
4950
}
5051

5152
int GetMiningAlgorithm();
52-
bool SetMiningAlgorithm(const std::string& algo);
53+
bool SetMiningAlgorithm(const std::string& algo, bool fSet = true);
5354

5455
// End Pow algorithm to use
5556

src/rpc/mining.cpp

+117-24
Original file line numberDiff line numberDiff line change
@@ -58,63 +58,144 @@ unsigned int ParseConfirmTarget(const UniValue& value)
5858
* or from the last difficulty change if 'lookup' is nonpositive.
5959
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
6060
*/
61-
static UniValue GetNetworkHashPS(int lookup, int height) {
61+
static UniValue GetNetworkHashPS(int lookup, int height, std::string type) {
6262
CBlockIndex *pb = chainActive.Tip();
6363

6464
if (height >= 0 && height < chainActive.Height())
6565
pb = chainActive[height];
6666

67+
// if after fork, set pb to fork height unless specifying pos or x16rt
68+
6769
if (pb == nullptr || !pb->nHeight)
6870
return 0;
6971

7072
// If lookup is -1, then use blocks since last difficulty change.
71-
if (lookup <= 0)
72-
lookup = 1;
73-
74-
// If lookup is larger than chain, then set it to chain length.
75-
if (lookup > pb->nHeight)
76-
lookup = pb->nHeight;
77-
78-
CBlockIndex *pb0 = pb;
79-
int64_t minTime = pb0->GetBlockTime();
80-
int64_t maxTime = minTime;
81-
for (int i = 0; i < lookup; i++) {
82-
pb0 = pb0->pprev;
83-
int64_t time = pb0->GetBlockTime();
84-
minTime = std::min(time, minTime);
85-
maxTime = std::max(time, maxTime);
73+
if (lookup <= 1)
74+
lookup = 2;
75+
76+
CBlockIndex *pbLast = pb;
77+
CBlockIndex *pbFirst = nullptr;
78+
bool found = false;
79+
while (pb->nHeight > 0)
80+
{
81+
// find the last of the algo
82+
if (0 == type.compare("PoS")) {
83+
if (pb->IsProofOfStake()) found=true;
84+
} else if (0 == type.compare("sha256d")) {
85+
if (pb->GetBlockTime() < Params().PowUpdateTimestamp()) return 0;
86+
if (pb->IsSha256DProofOfWork()) found=true;
87+
} else if (0 == type.compare("randomx")) {
88+
if (pb->GetBlockTime() < Params().PowUpdateTimestamp()) return 0;
89+
if (pb->IsRandomXProofOfWork()) found=true;
90+
} else if (0 == type.compare("progpow")) {
91+
if (pb->GetBlockTime() < Params().PowUpdateTimestamp()) return 0;
92+
if (pb->IsProgProofOfWork()) found=true;
93+
} else if (0 == type.compare("xr16t")) {
94+
if (pb->IsX16RTProofOfWork()) found=true;
95+
} else {
96+
// Unknown Algo
97+
return 0;
98+
}
99+
if (found) {
100+
pbLast = pb;
101+
break;
102+
}
103+
pb = pb->pprev;
86104
}
87105

106+
if (!found) return 0;
107+
108+
// find the nth previous
109+
uint32_t nCountBlocks = 1;
110+
while ((nCountBlocks < lookup) && (pb->nHeight > 1))
111+
{
112+
pb = pb->pprev;
113+
if (0 == type.compare("PoS")) {
114+
if (pb->IsProofOfStake()) {
115+
nCountBlocks++;
116+
pbFirst = pb;
117+
}
118+
} else if (0 == type.compare("sha256d")) {
119+
if (pb->IsSha256DProofOfWork()) {
120+
nCountBlocks++;
121+
pbFirst = pb;
122+
}
123+
} else if (0 == type.compare("randomx")) {
124+
if (pb->IsRandomXProofOfWork()) {
125+
nCountBlocks++;
126+
pbFirst = pb;
127+
}
128+
} else if (0 == type.compare("progpow")) {
129+
if (pb->IsProgProofOfWork()) {
130+
nCountBlocks++;
131+
pbFirst = pb;
132+
}
133+
} else { // xr16rt
134+
if (pb->IsX16RTProofOfWork()) {
135+
nCountBlocks++;
136+
pbFirst = pb;
137+
}
138+
}
139+
// Check if we should continue
140+
if (((0 != type.compare("sha256d")) || (0 != type.compare("randomx")) || (0 != type.compare("progpow"))) &&
141+
(pb->GetBlockTime() < Params().PowUpdateTimestamp()))
142+
{
143+
// Don't keep searching, abort now
144+
break;
145+
}
146+
}
147+
148+
// only one was found
149+
if (!pbFirst || !pbLast || (pbFirst == pbLast)) return 0;
150+
151+
int64_t minTime = pbFirst->GetBlockTime();
152+
int64_t maxTime = pbLast->GetBlockTime();
153+
88154
// In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
89155
if (minTime == maxTime)
90156
return 0;
91157

92-
arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
158+
arith_uint256 workDiff = pbLast->nChainWork - pbFirst->nChainWork;
93159
int64_t timeDiff = maxTime - minTime;
94160

95161
return workDiff.getdouble() / timeDiff;
96162
}
97163

98164
static UniValue getnetworkhashps(const JSONRPCRequest& request)
99165
{
100-
if (request.fHelp || request.params.size() > 2)
166+
if (request.fHelp || request.params.size() > 3)
101167
throw std::runtime_error(
102-
"getnetworkhashps ( nblocks height )\n"
168+
"getnetworkhashps ( nblocks height algo )\n"
103169
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
104170
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
105-
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
171+
"Pass in [height] to estimate the network hash at the time when a certain block was found.\n"
172+
"Pass in [algo] to estimate the network hash for a different algo then your wallet is set to.\n"
106173
"\nArguments:\n"
107174
"1. nblocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
108175
"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"
109177
"\nResult:\n"
110178
"x (numeric) Hashes per second estimated\n"
111179
"\nExamples:\n"
112180
+ HelpExampleCli("getnetworkhashps", "")
113181
+ HelpExampleRpc("getnetworkhashps", "")
114182
);
115183

116-
LOCK(cs_main);
117-
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
184+
185+
if (!(request.params[2].isNull())) {
186+
std::string algo = request.params[2].get_str();
187+
// Check if it's a mining algorithm
188+
if (!SetMiningAlgorithm(algo, false))
189+
// check that it's not one of the other two
190+
if (algo.compare("PoS") && algo.compare("xr16t"))
191+
throw JSONRPCError(RPC_INVALID_PARAMETER,
192+
strprintf("%s is not a supported mining type", algo));
193+
}
194+
195+
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120,
196+
!request.params[1].isNull() ? request.params[1].get_int() : -1,
197+
!request.params[2].isNull() ? request.params[2].get_str()
198+
: GetMiningType(GetMiningAlgorithm(), false, false));
118199
}
119200

120201
UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript)
@@ -276,14 +357,26 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
276357
+ HelpExampleRpc("getmininginfo", "")
277358
);
278359

360+
const Consensus::Params& ConsensusParams = Params().GetConsensus();
361+
int nAlgo = GetMiningAlgorithm();
362+
std::string sMiningAlgo = GetMiningType(nAlgo, false, false);
363+
364+
int nBlockType = CBlock::RANDOMX_BLOCK;
365+
if (nAlgo == MINE_PROGPOW) nBlockType = CBlock::PROGPOW_BLOCK;
366+
else if (nAlgo == MINE_SHA256D) nBlockType = CBlock::SHA256D_BLOCK;
279367

280368
LOCK(cs_main);
281369

282370
UniValue obj(UniValue::VOBJ);
371+
372+
double nDiff = GetDifficulty(GetNextWorkRequired(chainActive.Tip(), nullptr, ConsensusParams,
373+
false, nBlockType));
374+
283375
obj.pushKV("blocks", (int)chainActive.Height());
284376
obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight);
285377
obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx);
286-
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip()));
378+
obj.pushKV("algorithm", sMiningAlgo);
379+
obj.pushKV("difficulty", (double)nDiff);
287380
obj.pushKV("networkhashps", getnetworkhashps(request));
288381
obj.pushKV("pooledtx", (uint64_t)mempool.size());
289382
obj.pushKV("chain", Params().NetworkIDString());
@@ -1153,7 +1246,7 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
11531246
static const CRPCCommand commands[] =
11541247
{ // category name actor (function) argNames
11551248
// --------------------- ------------------------ ----------------------- ----------
1156-
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height"} },
1249+
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height","algo"} },
11571250
{ "mining", "getmininginfo", &getmininginfo, {} },
11581251
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
11591252
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },

0 commit comments

Comments
 (0)