diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -7,9 +7,12 @@ #include +class CBlockIndex; class Config; class JSONRPCRequest; UniValue getblockchaininfo(const Config &config, const JSONRPCRequest &request); +double GetDifficulty(const CBlockIndex *blockindex); + #endif // BITCOIN_RPCBLOCKCHAIN_H diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -44,25 +44,15 @@ void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex); -double GetDifficulty(const CBlockIndex *blockindex) { - // Floating point number that is a multiple of the minimum difficulty, - // minimum difficulty = 1.0. - if (blockindex == nullptr) { - if (chainActive.Tip() == nullptr) { - return 1.0; - } - - blockindex = chainActive.Tip(); - } - - int nShift = (blockindex->nBits >> 24) & 0xff; - - double dDiff = double(0x0000ffff) / double(blockindex->nBits & 0x00ffffff); +static double GetDifficultyFromBits(uint32_t nBits) { + int nShift = (nBits >> 24) & 0xff; + double dDiff = 0x0000ffff / double(nBits & 0x00ffffff); while (nShift < 29) { dDiff *= 256.0; nShift++; } + while (nShift > 29) { dDiff /= 256.0; nShift--; @@ -71,6 +61,16 @@ return dDiff; } +double GetDifficulty(const CBlockIndex *blockindex) { + // Floating point number that is a multiple of the minimum difficulty, + // minimum difficulty = 1.0. + if (blockindex == nullptr) { + return 1.0; + } + + return GetDifficultyFromBits(blockindex->nBits); +} + UniValue blockheaderToJSON(const CBlockIndex *blockindex) { UniValue result(UniValue::VOBJ); result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex())); @@ -365,7 +365,7 @@ } LOCK(cs_main); - return GetDifficulty(); + return GetDifficulty(chainActive.Tip()); } std::string EntryDescriptionString() { @@ -1306,7 +1306,7 @@ Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1)); obj.push_back( Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex())); - obj.push_back(Pair("difficulty", double(GetDifficulty()))); + obj.push_back(Pair("difficulty", double(GetDifficulty(chainActive.Tip())))); obj.push_back( Pair("mediantime", int64_t(chainActive.Tip()->GetMedianTimePast()))); obj.push_back( diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -17,6 +17,7 @@ #include "net.h" #include "policy/policy.h" #include "pow.h" +#include "rpc/blockchain.h" #include "rpc/server.h" #include "txmempool.h" #include "util.h" @@ -24,11 +25,11 @@ #include "validation.h" #include "validationinterface.h" +#include + #include #include -#include - /** * 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 @@ -286,7 +287,7 @@ obj.push_back(Pair("blocks", int(chainActive.Height()))); obj.push_back(Pair("currentblocksize", uint64_t(nLastBlockSize))); obj.push_back(Pair("currentblocktx", uint64_t(nLastBlockTx))); - obj.push_back(Pair("difficulty", double(GetDifficulty()))); + obj.push_back(Pair("difficulty", double(GetDifficulty(chainActive.Tip())))); obj.push_back(Pair("blockprioritypercentage", uint8_t(GetArg("-blockprioritypercentage", DEFAULT_BLOCK_PRIORITY_PERCENTAGE)))); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -11,6 +11,7 @@ #include "init.h" #include "net.h" #include "netbase.h" +#include "rpc/blockchain.h" #include "rpc/server.h" #include "timedata.h" #include "util.h" @@ -21,10 +22,10 @@ #include "wallet/walletdb.h" #endif -#include - #include +#include + /** * @note Do not add or change anything in the information returned by this * method. `getinfo` exists for backwards-compatibility only. It combines @@ -106,7 +107,7 @@ CConnman::CONNECTIONS_ALL))); obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string()))); - obj.push_back(Pair("difficulty", (double)GetDifficulty())); + obj.push_back(Pair("difficulty", double(GetDifficulty(chainActive.Tip())))); obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET)); #ifdef ENABLE_WALLET diff --git a/src/rpc/server.h b/src/rpc/server.h --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -219,7 +219,6 @@ extern int64_t nWalletUnlockTime; extern Amount AmountFromValue(const UniValue &value); extern UniValue ValueFromAmount(const Amount &amount); -extern double GetDifficulty(const CBlockIndex *blockindex = nullptr); extern std::string HelpRequiringPassphrase(); extern std::string HelpExampleCli(const std::string &methodname, const std::string &args);