From 3da6dc7302899e95a811377c0517836f735c56fe Mon Sep 17 00:00:00 2001 From: coblee Date: Fri, 17 May 2013 00:57:05 -1000 Subject: [PATCH] Litecoin: getnetworkhashps --- src/bitcoinrpc.cpp | 2 ++ src/bitcoinrpc.h | 1 + src/rpcmining.cpp | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 44102e5a50..9741b3389f 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -207,6 +207,7 @@ static const CRPCCommand vRPCCommands[] = { "addnode", &addnode, true, true }, { "getaddednodeinfo", &getaddednodeinfo, true, true }, { "getdifficulty", &getdifficulty, true, false }, + { "getnetworkhashps", &getnetworkhashps, true, false }, { "getgenerate", &getgenerate, true, false }, { "setgenerate", &setgenerate, true, false }, { "gethashespersec", &gethashespersec, true, false }, @@ -1145,6 +1146,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 0) ConvertTo(params[0]); if (strMethod == "setgenerate" && n > 0) ConvertTo(params[0]); if (strMethod == "setgenerate" && n > 1) ConvertTo(params[1]); + if (strMethod == "getnetworkhashps" && n > 0) ConvertTo(params[0]); if (strMethod == "sendtoaddress" && n > 1) ConvertTo(params[1]); if (strMethod == "settxfee" && n > 0) ConvertTo(params[0]); if (strMethod == "getreceivedbyaddress" && n > 1) ConvertTo(params[1]); diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index cf5b137988..5c0cae66e3 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -142,6 +142,7 @@ extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool f extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value gethashespersec(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp); diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index b8b7459634..33dcf22387 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -11,6 +11,42 @@ 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) + return 0; + + // If lookup is -1, then use blocks since last difficulty change. + if (lookup <= 0) + lookup = pindexBest->nHeight % 2016 + 1; + + // If lookup is larger than chain, then set it to chain length. + if (lookup > pindexBest->nHeight) + lookup = pindexBest->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; + } + + return (boost::int64_t)(pow(2.0, 32) / (sum / lookup)); +} + +Value getnetworkhashps(const Array& params, bool fHelp) +{ + if (fHelp || params.size() > 1) + throw runtime_error( + "getnetworkhashps [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."); + + return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120); +} + + Value getgenerate(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) @@ -77,6 +113,7 @@ Value getmininginfo(const Array& params, bool fHelp) obj.push_back(Pair("generate", GetBoolArg("-gen"))); obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1))); obj.push_back(Pair("hashespersec", gethashespersec(params, false))); + obj.push_back(Pair("networkhashps", getnetworkhashps(params, false))); obj.push_back(Pair("pooledtx", (uint64_t)mempool.size())); obj.push_back(Pair("testnet", fTestNet)); return obj;