在比特币源代码中在哪里以及如何设置矿池难度(pdi​​ff)?

Where and how pool difficulty(pdiff) is set in bitcoin source code?

我正在使用比特币源代码,想将初始难度设置为 1(我更改了 bdiff,nBits 字段)。所以我也需要更改 pdiff。根据 :

difficulty = difficulty_1_target / current_target (target is a 256 bit number)

difficulty_1_target can be different for various ways to measure difficulty. Traditionally, it represents a hash where the leading 32 bits are zero and the rest are one (this is known as "pool difficulty" or "pdiff"). The Bitcoin protocol represents targets as a custom floating point type with limited precision; as a result, Bitcoin clients often approximate difficulty based on this (this is known as "bdiff").

有人知道 pdiff 存储在哪里吗?它是硬编码的吗?

我找到了解决办法!它不完全是代码中的 pdiff 字段,但 blockchain.cpp 中有一个函数:

double GetDifficulty(const CBlockIndex* blockindex)
{
    if (blockindex == nullptr)
    {
        return 1.0;
    }

    int nShift = (blockindex->nBits >> 24) & 0xff;
    double dDiff =
        (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);

    while (nShift < 29)
    {
        dDiff *= 256.0;
        nShift++;
    }
    while (nShift > 29)
    {
        dDiff /= 256.0;
        nShift--;
    }
    return dDiff;
 }

对于比特币,初始 nBits 等于 0x1d00ffff,所以上面的 dDiff 字段变为 1,nshift 等于 1D。对于我的私有版本,我将 nBits 设置为 0x1f0fffff 并且应该像

一样计算 dDiff
double dDiff =(double)0x000ffff / (double)(blockindex->nBits & 0x00ffffff);

和 nShift 字段对我来说是 0x1f,所以我将 while 条件更改为 while(nShift < 31)while (nShift > 31)。通过 运行 命令 bitcoin-cli getdifficulty 我得到了 1 作为初始难度。