[mining] Update difficulty - Add Bitcoin Private's adjustment schedule; display GetNetworkDifficulty in info rpcs

This commit is contained in:
Jon Layton 2018-10-15 21:47:38 -05:00
parent 34a717c976
commit dd8c6df535
3 changed files with 34 additions and 10 deletions

View File

@ -52,31 +52,48 @@ static CUpdatedBlock latestblock;
/* Calculate the difficulty for a given block index, /* Calculate the difficulty for a given block index,
* or the block index of the given chain. * or the block index of the given chain.
*/ */
double GetDifficulty(const CChain& chain, 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) if (blockindex == nullptr)
{ {
if (chain.Tip() == nullptr) if (chainActive.Tip() == nullptr)
return 1.0; return 1.0;
else else
blockindex = chain.Tip(); blockindex = chainActive.Tip();
} }
int nShift = (blockindex->nBits >> 24) & 0xff; uint32_t bits;
double dDiff = if (networkDifficulty) {
(double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff); 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; dDiff *= 256.0;
nShift++; nShift++;
} }
while (nShift > 29) while (nShift > nShiftAmount)
{ {
dDiff /= 256.0; dDiff /= 256.0;
nShift--; nShift--;
} }
// for compatibility purposes, the floor of 1.0 is maintained.
if (dDiff < 1) dDiff = 1.0;
return dDiff; return dDiff;
} }
@ -85,6 +102,11 @@ double GetDifficulty(const CBlockIndex* blockindex)
return GetDifficulty(chainActive, blockindex); return GetDifficulty(chainActive, blockindex);
} }
double GetNetworkDifficulty(const CBlockIndex* blockindex)
{
return GetDifficultyINTERNAL(blockindex, true);
}
UniValue blockheaderToJSON(const CBlockIndex* blockindex) UniValue blockheaderToJSON(const CBlockIndex* blockindex)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
@ -1240,7 +1262,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("blocks", (int)chainActive.Height());
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1); obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1);
obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()); obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex());
obj.pushKV("difficulty", (double)GetDifficulty()); obj.pushKV("difficulty", (double)GetNetworkDifficulty());
obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()); obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast());
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())); obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip()));
obj.pushKV("initialblockdownload", IsInitialBlockDownload()); obj.pushKV("initialblockdownload", IsInitialBlockDownload());

View File

@ -18,6 +18,8 @@ class UniValue;
*/ */
double GetDifficulty(const CBlockIndex* blockindex = nullptr); double GetDifficulty(const CBlockIndex* blockindex = nullptr);
double GetNetworkDifficulty(const CBlockIndex* blockindex = nullptr);
/** Callback for when block tip changed. */ /** Callback for when block tip changed. */
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);

View File

@ -253,7 +253,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("blocks", (int)chainActive.Height());
obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight); obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight);
obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx); obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx);
obj.pushKV("difficulty", (double)GetDifficulty()); obj.pushKV("difficulty", (double)GetNetworkDifficulty());
obj.pushKV("networkhashps", getnetworkhashps(request)); obj.pushKV("networkhashps", getnetworkhashps(request));
obj.pushKV("pooledtx", (uint64_t)mempool.size()); obj.pushKV("pooledtx", (uint64_t)mempool.size());
obj.pushKV("chain", Params().NetworkIDString()); obj.pushKV("chain", Params().NetworkIDString());