From de5250f9388dafb2ca5741af7686b1a5a5f6a376 Mon Sep 17 00:00:00 2001 From: pooler Date: Sun, 4 Aug 2013 23:09:05 +0200 Subject: [PATCH 1/2] Add a height parameter to getnetworkhashps --- src/bitcoinrpc.cpp | 1 + src/rpcmining.cpp | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 960ce6ccd..1372548b7 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1144,6 +1144,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 0) ConvertTo(params[0]); if (strMethod == "getaddednodeinfo" && n > 0) ConvertTo(params[0]); if (strMethod == "getnetworkhashps" && n > 0) ConvertTo(params[0]); + if (strMethod == "getnetworkhashps" && n > 1) ConvertTo(params[1]); if (strMethod == "sendtoaddress" && n > 1) ConvertTo(params[1]); if (strMethod == "settxfee" && n > 0) ConvertTo(params[0]); if (strMethod == "setmininput" && n > 0) ConvertTo(params[0]); diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 000b52b04..fb722776a 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -12,24 +12,28 @@ using namespace json_spirit; using namespace std; // Litecoin: Return average network hashes per second based on last number of blocks. -Value GetNetworkHashPS(int lookup) { - if (pindexBest == NULL) +Value GetNetworkHashPS(int lookup, int height) { + CBlockIndex *pb = pindexBest; + + if (height >= 0 && height < nBestHeight) + pb = FindBlockByHeight(height); + + if (pb == NULL || !pb->nHeight) return 0; // If lookup is -1, then use blocks since last difficulty change. 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 > pindexBest->nHeight) - lookup = pindexBest->nHeight; + if (lookup > pb->nHeight) + lookup = pb->nHeight; - CBlockIndex* pindexPrev = pindexBest; double sum = 0.0; for (int i = 0; i < lookup; i++) { - sum += (pindexPrev->GetBlockTime() - pindexPrev->pprev->GetBlockTime()) / GetDifficulty(pindexPrev); - pindexPrev = pindexPrev->pprev; + sum += (pb->GetBlockTime() - pb->pprev->GetBlockTime()) / GetDifficulty(pb); + pb = pb->pprev; } 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) { - if (fHelp || params.size() > 1) + if (fHelp || params.size() > 2) throw runtime_error( - "getnetworkhashps [blocks]\n" + "getnetworkhashps [blocks] [height]\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) From 7446c4adc982cd41a5671c61f12a5e6c4b83e4a4 Mon Sep 17 00:00:00 2001 From: pooler Date: Tue, 6 Aug 2013 11:32:21 +0200 Subject: [PATCH 2/2] Update description of GetNetworkHashPS() --- src/rpcmining.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index fb722776a..67ebcb7c8 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -11,7 +11,9 @@ using namespace json_spirit; using namespace std; -// Litecoin: Return average network hashes per second based on last number of blocks. +// Return average network hashes per second based on the last 'lookup' blocks, +// or from the last difficulty change if 'lookup' is nonpositive. +// If 'height' is nonnegative, compute the estimate at the time when a given block was found. Value GetNetworkHashPS(int lookup, int height) { CBlockIndex *pb = pindexBest;