diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6a2fc2833..4d68642ee 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -57,31 +57,61 @@ static CUpdatedBlock latestblock; /* Calculate the difficulty for a given block index. */ -double GetDifficulty(const CBlockIndex* blockindex) +double GetDifficultyINTERNAL(const CBlockIndex* blockindex, bool networkDifficulty) { + // Floating point number that is a multiple of the minimum difficulty, + // minimum difficulty = 1.0. if (blockindex == nullptr) { - return 1.0; + if (chainActive.Tip() == nullptr) + return 1.0; + else + blockindex = chainActive.Tip(); } - int nShift = (blockindex->nBits >> 24) & 0xff; - double dDiff = - (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff); + uint32_t bits; + if (networkDifficulty) { + bits = GetNextWorkRequired(blockindex, nullptr, Params().GetConsensus()); + } else { + bits = blockindex->nBits; + } - while (nShift < 29) + // instead of using powLimit, network difficulty is calculated using a legacy powLimit value to maintain comparability with the network difficulty of other Equihash-based coins for mining profitability calculations. + uint32_t powLimitLegacy = UintToArith256(Params().GetConsensus().prePowLimit).GetCompact(); + int nShift = (bits >> 24) & 0xff; + int nShiftAmount = (powLimitLegacy >> 24) & 0xff; + + double dDiff = + (double)(powLimitLegacy & 0x00ffffff) / + (double)(bits & 0x00ffffff); + + while (nShift < nShiftAmount) { dDiff *= 256.0; nShift++; } - while (nShift > 29) + while (nShift > nShiftAmount) { dDiff /= 256.0; nShift--; } + // for compatibility purposes, the floor of 1.0 is maintained. + if (dDiff < 1) dDiff = 1.0; + return dDiff; } +double GetDifficulty(const CBlockIndex* blockindex) +{ + return GetDifficulty(chainActive, blockindex); +} + +double GetNetworkDifficulty(const CBlockIndex* blockindex) +{ + return GetDifficultyINTERNAL(blockindex, true); +} + UniValue blockheaderToJSON(const CBlockIndex* blockindex) { AssertLockHeld(cs_main); @@ -1250,7 +1280,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1); obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()); - obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip())); + obj.pushKV("difficulty", (double)GetNetworkDifficulty()); obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()); obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())); obj.pushKV("initialblockdownload", IsInitialBlockDownload()); diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index add335eb8..1209a3431 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -23,6 +23,8 @@ static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; */ double GetDifficulty(const CBlockIndex* blockindex); +double GetNetworkDifficulty(const CBlockIndex* blockindex = nullptr); + /** Callback for when block tip changed. */ void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 0ef4ba0e6..1cd9ff9da 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -254,7 +254,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request) obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight); obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx); - obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip())); + obj.pushKV("difficulty", (double)GetNetworkDifficulty()); obj.pushKV("networkhashps", getnetworkhashps(request)); obj.pushKV("pooledtx", (uint64_t)mempool.size()); obj.pushKV("chain", Params().NetworkIDString());