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)