1

When I read this book Master Bitcoin, I have a problem in understanding the "Difficulty" in Chapter 8.

There is a equation of difficulty:

New Difficulty = Old Difficulty * (Actual Time of Last 2016 Blocks / 20160 minutes)

I feel confused.
According to the formula, if Actual Time was longer than 20160 minutes, the New Difficulty would be bigger than Old Difficulty, and then Actual Time would be longer and longer.

It looks very strange. For me the equation should be:

New Difficulty = Old Difficulty * (20160 minutes / Actual Time of Last 2016 Blocks)

However, I found this fomula is consistent with that in bitcoin code.

unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
    if (params.fPowNoRetargeting)
        return pindexLast->nBits;

    // 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;
    bnNew.SetCompact(pindexLast->nBits);
    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

    if (bnNew > bnPowLimit)
        bnNew = bnPowLimit;

    return bnNew.GetCompact();
}

Then I found this answer.
This is what I understand, but why it is different from the equation of the book and the code.

update

I think this was just an ambiguous expression.
The better expression should be:

New Difficulty Target = Old Difficulty Target * (Actual Time of Last 2016 Blocks / 20160 minutes)

1 Answer 1

3

The code you're citing does not actually deal with difficulty. It deals with targets. The target is the maximum 256-bit value than the block hash is allowed to have.

The difficulty is defined as 2^208 * 65535 / target.

As for the book, that looks wrong indeed.

2
  • Thanks for answer, as you said, the description of the book is wrong?
    – Ello
    Jun 6, 2017 at 4:57
  • 3
    The book is wrong. Jun 6, 2017 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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