Add a height parameter to getnetworkhashps

This commit is contained in:
pooler 2013-08-04 23:09:05 +02:00
parent 1b5cb0866e
commit de5250f938
2 changed files with 18 additions and 12 deletions

View File

@ -1144,6 +1144,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "stop" && n > 0) ConvertTo<bool>(params[0]); if (strMethod == "stop" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "getaddednodeinfo" && n > 0) ConvertTo<bool>(params[0]); if (strMethod == "getaddednodeinfo" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "getnetworkhashps" && n > 0) ConvertTo<boost::int64_t>(params[0]); if (strMethod == "getnetworkhashps" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "getnetworkhashps" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "sendtoaddress" && n > 1) ConvertTo<double>(params[1]); if (strMethod == "sendtoaddress" && n > 1) ConvertTo<double>(params[1]);
if (strMethod == "settxfee" && n > 0) ConvertTo<double>(params[0]); if (strMethod == "settxfee" && n > 0) ConvertTo<double>(params[0]);
if (strMethod == "setmininput" && n > 0) ConvertTo<double>(params[0]); if (strMethod == "setmininput" && n > 0) ConvertTo<double>(params[0]);

View File

@ -12,24 +12,28 @@ using namespace json_spirit;
using namespace std; using namespace std;
// Litecoin: Return average network hashes per second based on last number of blocks. // Litecoin: Return average network hashes per second based on last number of blocks.
Value GetNetworkHashPS(int lookup) { Value GetNetworkHashPS(int lookup, int height) {
if (pindexBest == NULL) CBlockIndex *pb = pindexBest;
if (height >= 0 && height < nBestHeight)
pb = FindBlockByHeight(height);
if (pb == NULL || !pb->nHeight)
return 0; return 0;
// If lookup is -1, then use blocks since last difficulty change. // If lookup is -1, then use blocks since last difficulty change.
if (lookup <= 0) if (lookup <= 0)
lookup = pindexBest->nHeight % 2016 + 1; lookup = pb->nHeight % 2016 + 1;
// If lookup is larger than chain, then set it to chain length. // If lookup is larger than chain, then set it to chain length.
if (lookup > pindexBest->nHeight) if (lookup > pb->nHeight)
lookup = pindexBest->nHeight; lookup = pb->nHeight;
CBlockIndex* pindexPrev = pindexBest;
double sum = 0.0; double sum = 0.0;
for (int i = 0; i < lookup; i++) for (int i = 0; i < lookup; i++)
{ {
sum += (pindexPrev->GetBlockTime() - pindexPrev->pprev->GetBlockTime()) / GetDifficulty(pindexPrev); sum += (pb->GetBlockTime() - pb->pprev->GetBlockTime()) / GetDifficulty(pb);
pindexPrev = pindexPrev->pprev; pb = pb->pprev;
} }
return (boost::int64_t)(pow(2.0, 32) / (sum / lookup)); return (boost::int64_t)(pow(2.0, 32) / (sum / lookup));
@ -37,13 +41,14 @@ Value GetNetworkHashPS(int lookup) {
Value getnetworkhashps(const Array& params, bool fHelp) Value getnetworkhashps(const Array& params, bool fHelp)
{ {
if (fHelp || params.size() > 1) if (fHelp || params.size() > 2)
throw runtime_error( throw runtime_error(
"getnetworkhashps [blocks]\n" "getnetworkhashps [blocks] [height]\n"
"Returns the estimated network hashes per second based on the last 120 blocks.\n" "Returns the estimated network hashes per second based on the last 120 blocks.\n"
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change."); "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
"Pass in [height] to estimate the network speed at the time when a certain block was found.");
return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120); return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
} }
Value getmininginfo(const Array& params, bool fHelp) Value getmininginfo(const Array& params, bool fHelp)