Add algorithm-aware mining difficulty
This commit is contained in:
parent
74ef53ae3d
commit
37d4ae0d1b
4 changed files with 92 additions and 3 deletions
|
|
@ -137,11 +137,46 @@ double GetDifficulty(const CBlockIndex* blockindex)
|
|||
return dDiff;
|
||||
}
|
||||
|
||||
double GetAlgoDifficulty(
|
||||
const CBlockIndex* blockindex,
|
||||
const Consensus::Params& consensusParams,
|
||||
int algo)
|
||||
{
|
||||
assert(blockindex != nullptr);
|
||||
|
||||
if (algo == ALGO_SHA256D) {
|
||||
return GetDifficulty(blockindex);
|
||||
}
|
||||
|
||||
if (algo != ALGO_RANDOMX) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
bool negative;
|
||||
bool overflow;
|
||||
|
||||
arith_uint256 target;
|
||||
target.SetCompact(blockindex->nBits, &negative, &overflow);
|
||||
|
||||
if (negative || overflow || target == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
const arith_uint256 reference =
|
||||
UintToArith256(consensusParams.randomXInitialTarget);
|
||||
|
||||
if (reference == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return reference.getdouble() / target.getdouble();
|
||||
}
|
||||
|
||||
static double GetNextDifficulty(const CBlockIndex* tip, const Consensus::Params& consensusParams, int algo)
|
||||
{
|
||||
CBlockIndex next;
|
||||
next.nBits = GetNextWorkRequired(tip, nullptr, consensusParams, algo);
|
||||
return GetDifficulty(&next);
|
||||
return GetAlgoDifficulty(&next, consensusParams, algo);
|
||||
}
|
||||
|
||||
static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@ class ChainstateManager;
|
|||
class UniValue;
|
||||
struct NodeContext;
|
||||
|
||||
namespace Consensus {
|
||||
struct Params;
|
||||
}
|
||||
|
||||
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
|
||||
|
||||
/**
|
||||
|
|
@ -36,6 +40,17 @@ static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
|
|||
*/
|
||||
double GetDifficulty(const CBlockIndex* blockindex);
|
||||
|
||||
/**
|
||||
* Get difficulty for a specific mining algorithm.
|
||||
*
|
||||
* SHA256D preserves the legacy GetDifficulty() scale.
|
||||
* RandomX uses randomXInitialTarget as difficulty 1.0.
|
||||
*/
|
||||
double GetAlgoDifficulty(
|
||||
const CBlockIndex* blockindex,
|
||||
const Consensus::Params& consensusParams,
|
||||
int algo);
|
||||
|
||||
/** Callback for when block tip changed. */
|
||||
void RPCNotifyBlockChange(const CBlockIndex*);
|
||||
|
||||
|
|
|
|||
|
|
@ -678,9 +678,15 @@ static RPCHelpMan getmininginfo()
|
|||
GetNextWorkRequired(tip, nullptr, consensusParams, ALGO_RANDOMX);
|
||||
|
||||
const double sha_difficulty =
|
||||
GetDifficulty(&sha_next);
|
||||
GetAlgoDifficulty(
|
||||
&sha_next,
|
||||
consensusParams,
|
||||
ALGO_SHA256D);
|
||||
const double randomx_difficulty =
|
||||
GetDifficulty(&randomx_next);
|
||||
GetAlgoDifficulty(
|
||||
&randomx_next,
|
||||
consensusParams,
|
||||
ALGO_RANDOMX);
|
||||
|
||||
difficulties.pushKV(
|
||||
GetMiningAlgoName(ALGO_SHA256D),
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include <chain.h>
|
||||
#include <chainparams.h>
|
||||
#include <pow.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/string.h>
|
||||
|
|
@ -74,4 +76,35 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
|
|||
TestDifficulty(0x12345678, 5913134931067755359633408.0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(get_algo_difficulty)
|
||||
{
|
||||
const auto chainParams =
|
||||
CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
|
||||
const auto& consensus = chainParams->GetConsensus();
|
||||
|
||||
CBlockIndex sha;
|
||||
sha.nBits = UintToArith256(consensus.powLimit).GetCompact();
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
GetAlgoDifficulty(&sha, consensus, ALGO_SHA256D),
|
||||
GetDifficulty(&sha));
|
||||
|
||||
CBlockIndex randomx;
|
||||
randomx.nBits =
|
||||
UintToArith256(consensus.randomXInitialTarget).GetCompact();
|
||||
|
||||
RejectDifficultyMismatch(
|
||||
GetAlgoDifficulty(&randomx, consensus, ALGO_RANDOMX),
|
||||
1.0);
|
||||
|
||||
arith_uint256 harderRandomXTarget =
|
||||
UintToArith256(consensus.randomXInitialTarget);
|
||||
harderRandomXTarget /= 2;
|
||||
randomx.nBits = harderRandomXTarget.GetCompact();
|
||||
|
||||
RejectDifficultyMismatch(
|
||||
GetAlgoDifficulty(&randomx, consensus, ALGO_RANDOMX),
|
||||
2.0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
|||
Loading…
Reference in a new issue