0

How can I get this value, as I understand this complexity

https://github.com/bitcoin/bitcoin/blob/master/src/test/pow_tests.cpp#L55

0x1c0168fd

2

Here's the function that implements this. Each annotation refers to the line below.


unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{

If we're on a network without retargeting, don't retarget.

    if (params.fPowNoRetargeting)
        return pindexLast->nBits;

Don't adjust up or down by more than 4x.

    // Limit adjustment step
    int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
    if (nActualTimespan < params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan > params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;

Take the old nBits in compact form, and turn it into 256-bit form. I explain that in more detail here: Difficulty target representation in bitcoin wiki

    bnNew.SetCompact(pindexLast->nBits);

Multiply by the timespan (the time between the first block in the retargeting period, and the last block in the retargeting period.) Then, divide by the targeted timespan.

    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

If the result would be less than difficulty 1, change to difficulty 1 instead.

    if (bnNew > bnPowLimit)
        bnNew = bnPowLimit;

Re-encode as compact form for bignums. This is the reverse of SetCompact(int32_t).

    return bnNew.GetCompact();
}

Let's also work through the example you give. 0x1c05a3f4 should change to 0x1c0168fd.

0x1c05a3f4
1c          05a3f4
^ exponent  ^ mantissa

To decimal:

369652 * 256^28

Let's work out the timespans, actual and targeted

Actual: 1279297671 - 1279008237 = 289434
Target: 2016*600 = 1209600
Actual/Target ~= 0.239

Note that Target is more than 4x larger than Actual. We need to cap this, so we don't raise the difficulty too fast.

Capped timespan: Target/4 = 302400

Final calculation:

369652 * 256^28 * (302400/1209600) = 92413 * 256^28

Back to hex:

1c          0168fd
^ exponent  ^ mantissa

Re-encode:

0x1c0168fd

...and that is in fact correct.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.