@@ -58,63 +58,144 @@ unsigned int ParseConfirmTarget(const UniValue& value)
58
58
* or from the last difficulty change if 'lookup' is nonpositive.
59
59
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
60
60
*/
61
- static UniValue GetNetworkHashPS (int lookup, int height) {
61
+ static UniValue GetNetworkHashPS (int lookup, int height, std::string type ) {
62
62
CBlockIndex *pb = chainActive.Tip ();
63
63
64
64
if (height >= 0 && height < chainActive.Height ())
65
65
pb = chainActive[height];
66
66
67
+ // if after fork, set pb to fork height unless specifying pos or x16rt
68
+
67
69
if (pb == nullptr || !pb->nHeight )
68
70
return 0 ;
69
71
70
72
// 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 ;
86
104
}
87
105
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
+
88
154
// In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
89
155
if (minTime == maxTime)
90
156
return 0 ;
91
157
92
- arith_uint256 workDiff = pb ->nChainWork - pb0 ->nChainWork ;
158
+ arith_uint256 workDiff = pbLast ->nChainWork - pbFirst ->nChainWork ;
93
159
int64_t timeDiff = maxTime - minTime;
94
160
95
161
return workDiff.getdouble () / timeDiff;
96
162
}
97
163
98
164
static UniValue getnetworkhashps (const JSONRPCRequest& request)
99
165
{
100
- if (request.fHelp || request.params .size () > 2 )
166
+ if (request.fHelp || request.params .size () > 3 )
101
167
throw std::runtime_error (
102
- " getnetworkhashps ( nblocks height )\n "
168
+ " getnetworkhashps ( nblocks height algo )\n "
103
169
" \n Returns the estimated network hashes per second based on the last n blocks.\n "
104
170
" 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 "
106
173
" \n Arguments:\n "
107
174
" 1. nblocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n "
108
175
" 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 "
109
177
" \n Result:\n "
110
178
" x (numeric) Hashes per second estimated\n "
111
179
" \n Examples:\n "
112
180
+ HelpExampleCli (" getnetworkhashps" , " " )
113
181
+ HelpExampleRpc (" getnetworkhashps" , " " )
114
182
);
115
183
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 ));
118
199
}
119
200
120
201
UniValue generateBlocks (std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript)
@@ -276,14 +357,26 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
276
357
+ HelpExampleRpc (" getmininginfo" , " " )
277
358
);
278
359
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;
279
367
280
368
LOCK (cs_main);
281
369
282
370
UniValue obj (UniValue::VOBJ);
371
+
372
+ double nDiff = GetDifficulty (GetNextWorkRequired (chainActive.Tip (), nullptr , ConsensusParams,
373
+ false , nBlockType));
374
+
283
375
obj.pushKV (" blocks" , (int )chainActive.Height ());
284
376
obj.pushKV (" currentblockweight" , (uint64_t )nLastBlockWeight);
285
377
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);
287
380
obj.pushKV (" networkhashps" , getnetworkhashps (request));
288
381
obj.pushKV (" pooledtx" , (uint64_t )mempool.size ());
289
382
obj.pushKV (" chain" , Params ().NetworkIDString ());
@@ -1153,7 +1246,7 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
1153
1246
static const CRPCCommand commands[] =
1154
1247
{ // category name actor (function) argNames
1155
1248
// --------------------- ------------------------ ----------------------- ----------
1156
- { " mining" , " getnetworkhashps" , &getnetworkhashps, {" nblocks" ," height" } },
1249
+ { " mining" , " getnetworkhashps" , &getnetworkhashps, {" nblocks" ," height" , " algo " } },
1157
1250
{ " mining" , " getmininginfo" , &getmininginfo, {} },
1158
1251
{ " mining" , " prioritisetransaction" , &prioritisetransaction, {" txid" ," dummy" ," fee_delta" } },
1159
1252
{ " mining" , " getblocktemplate" , &getblocktemplate, {" template_request" } },
0 commit comments