Simplify difficulty calculation API

This commit is contained in:
Capri 2026-09-16 15:41:51 +00:00
commit e016663524
Signed by: VextaProject
SSH key fingerprint: SHA256:739BZv8LvIDY2tR7lXLy/IQ6MaGTQtYcG9GNvCjmpVM
4 changed files with 14 additions and 30 deletions

View file

@ -113,22 +113,11 @@ CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
/* Calculate the difficulty for a given block index. /* Calculate the difficulty for a given block index.
*/ */
double GetDifficulty(const CBlockIndex* tip, const CBlockIndex* blockindex) double GetDifficulty(const CBlockIndex* blockindex)
{ {
unsigned int nBits; assert(blockindex != nullptr);
unsigned int powLimit = InitialDifficulty(Params().GetConsensus());
if (blockindex == nullptr) const unsigned int nBits = blockindex->nBits;
{
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;
int nShift = (nBits >> 24) & 0xff; int nShift = (nBits >> 24) & 0xff;
double dDiff = double dDiff =
@ -148,16 +137,11 @@ double GetDifficulty(const CBlockIndex* tip, const CBlockIndex* blockindex)
return dDiff; return dDiff;
} }
double GetDifficulty(const CBlockIndex* blockindex)
{
return GetDifficulty(NULL, blockindex);
}
static double GetNextDifficulty(const CBlockIndex* tip, const Consensus::Params& consensusParams, int algo) static double GetNextDifficulty(const CBlockIndex* tip, const Consensus::Params& consensusParams, int algo)
{ {
CBlockIndex next; CBlockIndex next;
next.nBits = GetNextWorkRequired(tip, nullptr, consensusParams, algo); 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) 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("mediantime", (int64_t)blockindex->GetMedianTimePast());
result.pushKV("nonce", (uint64_t)blockindex->nNonce); result.pushKV("nonce", (uint64_t)blockindex->nNonce);
result.pushKV("bits", strprintf("%08x", blockindex->nBits)); 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("chainwork", blockindex->nChainWork.GetHex());
result.pushKV("nTx", (uint64_t)blockindex->nTx); result.pushKV("nTx", (uint64_t)blockindex->nTx);

View file

@ -29,12 +29,12 @@ struct NodeContext;
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; 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 * @return A floating point number that is a multiple of the main net minimum
* difficulty (4295032833 hashes). * difficulty (4295032833 hashes).
*/ */
double GetDifficulty(const CBlockIndex* tip = NULL, const CBlockIndex* blockindex = nullptr); double GetDifficulty(const CBlockIndex* blockindex);
/** Callback for when block tip changed. */ /** Callback for when block tip changed. */
void RPCNotifyBlockChange(const CBlockIndex*); void RPCNotifyBlockChange(const CBlockIndex*);

View file

@ -665,9 +665,9 @@ static RPCHelpMan getmininginfo()
GetNextWorkRequired(tip, nullptr, consensusParams, ALGO_RANDOMX); GetNextWorkRequired(tip, nullptr, consensusParams, ALGO_RANDOMX);
const double sha_difficulty = const double sha_difficulty =
GetDifficulty(nullptr, &sha_next); GetDifficulty(&sha_next);
const double randomx_difficulty = const double randomx_difficulty =
GetDifficulty(nullptr, &randomx_next); GetDifficulty(&randomx_next);
difficulties.pushKV( difficulties.pushKV(
GetMiningAlgoName(ALGO_SHA256D), GetMiningAlgoName(ALGO_SHA256D),

View file

@ -41,7 +41,7 @@ static void RejectDifficultyMismatch(double difficulty, double expected_difficul
static void TestDifficulty(uint32_t nbits, double expected_difficulty) static void TestDifficulty(uint32_t nbits, double expected_difficulty)
{ {
CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits); CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
double difficulty = GetDifficulty(NULL, block_index); double difficulty = GetDifficulty(block_index);
delete block_index; delete block_index;
RejectDifficultyMismatch(difficulty, expected_difficulty); RejectDifficultyMismatch(difficulty, expected_difficulty);