From e016663524a4c0cbdf991383fea029c3c38d2d93 Mon Sep 17 00:00:00 2001 From: Capri Date: Wed, 16 Sep 2026 15:41:51 +0000 Subject: [PATCH] Simplify difficulty calculation API --- src/rpc/blockchain.cpp | 34 +++++++++------------------------- src/rpc/blockchain.h | 4 ++-- src/rpc/mining.cpp | 4 ++-- src/test/blockchain_tests.cpp | 2 +- 4 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 590e3ec..355d479 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -113,27 +113,16 @@ CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context) /* Calculate the difficulty for a given block index. */ -double GetDifficulty(const CBlockIndex* tip, const CBlockIndex* blockindex) +double GetDifficulty(const CBlockIndex* blockindex) { - unsigned int nBits; - unsigned int powLimit = InitialDifficulty(Params().GetConsensus()); - if (blockindex == nullptr) - { - if (tip == nullptr) - nBits = powLimit; - else - { - // Vexta is SHA256D-only, so difficulty is based on the active chain tip. - nBits = tip->nBits; - } - } - else - nBits = blockindex->nBits; - + assert(blockindex != nullptr); + + const unsigned int nBits = blockindex->nBits; + int nShift = (nBits >> 24) & 0xff; double dDiff = (double)0x0000ffff / (double)(nBits & 0x00ffffff); - + while (nShift < 29) { dDiff *= 256.0; @@ -144,20 +133,15 @@ double GetDifficulty(const CBlockIndex* tip, const CBlockIndex* blockindex) dDiff /= 256.0; nShift--; } - - return dDiff; -} -double GetDifficulty(const CBlockIndex* blockindex) -{ - return GetDifficulty(NULL, blockindex); + return dDiff; } static double GetNextDifficulty(const CBlockIndex* tip, const Consensus::Params& consensusParams, int algo) { CBlockIndex next; next.nBits = GetNextWorkRequired(tip, nullptr, consensusParams, algo); - return GetDifficulty(nullptr, &next); + return GetDifficulty(&next); } static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next) @@ -215,7 +199,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex result.pushKV("mediantime", (int64_t)blockindex->GetMedianTimePast()); result.pushKV("nonce", (uint64_t)blockindex->nNonce); result.pushKV("bits", strprintf("%08x", blockindex->nBits)); - result.pushKV("difficulty", GetDifficulty(tip, blockindex)); + result.pushKV("difficulty", GetDifficulty(blockindex)); result.pushKV("chainwork", blockindex->nChainWork.GetHex()); result.pushKV("nTx", (uint64_t)blockindex->nTx); diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index 709af25..078b3b5 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -29,12 +29,12 @@ struct NodeContext; static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; /** - * Get the difficulty of the net wrt to the given block index. + * Get the difficulty represented by the given block index nBits. * * @return A floating point number that is a multiple of the main net minimum * difficulty (4295032833 hashes). */ -double GetDifficulty(const CBlockIndex* tip = NULL, const CBlockIndex* blockindex = nullptr); +double GetDifficulty(const CBlockIndex* blockindex); /** Callback for when block tip changed. */ void RPCNotifyBlockChange(const CBlockIndex*); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 8bf488a..8ae2f25 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -665,9 +665,9 @@ static RPCHelpMan getmininginfo() GetNextWorkRequired(tip, nullptr, consensusParams, ALGO_RANDOMX); const double sha_difficulty = - GetDifficulty(nullptr, &sha_next); + GetDifficulty(&sha_next); const double randomx_difficulty = - GetDifficulty(nullptr, &randomx_next); + GetDifficulty(&randomx_next); difficulties.pushKV( GetMiningAlgoName(ALGO_SHA256D), diff --git a/src/test/blockchain_tests.cpp b/src/test/blockchain_tests.cpp index 5c0390e..30999e2 100644 --- a/src/test/blockchain_tests.cpp +++ b/src/test/blockchain_tests.cpp @@ -41,7 +41,7 @@ static void RejectDifficultyMismatch(double difficulty, double expected_difficul static void TestDifficulty(uint32_t nbits, double expected_difficulty) { CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits); - double difficulty = GetDifficulty(NULL, block_index); + double difficulty = GetDifficulty(block_index); delete block_index; RejectDifficultyMismatch(difficulty, expected_difficulty);