<p>The function is <a href="https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L49" rel="noreferrer"><code>CalculateNextWorkRequired</code> in <code>pow.cpp</code> L#49</a>:</p>

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

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

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;
    bnNew.SetCompact(pindexLast-&gt;nBits);
    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

    if (bnNew &gt; bnPowLimit)
        bnNew = bnPowLimit;

    return bnNew.GetCompact();
}
</code></pre>

<p><strong>Explanation</strong>:<br>
1. If retargeting is disabled, return the last difficulty.<br>
2. Calculate the timespan between the last block and 2016 blocks ago<br>
3. Truncate the timespan to no less than 1/4 of the target timespan (3.5 days) or no greater than 4x the target timespan (8 weeks).<br>
4. Multiply the last difficulty target by the ratio actualTimespan:targetTimespan<br>
5. Truncate to the maximum allowed target (bnPowLimit) if the resulting target is too high (very low difficulty)  </p>

CC BY-SA 4.0
2018-12-14T06:09:58.563Z
thank you. When would retargeting be disabled ? How does this pop in ?
fritz
CC BY-SA 4.0
2018-12-14T06:10:34.103Z
And where in this code is the legendary broken and unfixed bug that causes updates every 2015 rather than 2016 blocks ?
fritz
CC BY-SA 4.0
2018-12-14T06:18:26.420Z
https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/chainparams.cpp#L294
JBaczuk
CC BY-SA 4.0
2018-12-14T06:20:14.350Z
https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L41
JBaczuk
CC BY-SA 4.0
2018-12-13T14:40:19.417Z
2018-12-13T14:40:19.417Z
JBaczuk
<p>Can someone point me to the source code and explain how the average is calculated for the adjustment of difficulty that takes place every 2016 ? 
Update: i have consulted with previous questions but it has not been documented properly and I am not expert so would be much appreciated.</p>

<bitcoin-core-development><difficulty><difficulty-retargetting>
where is the adjustment calculation for the target value in the source code?
fritz
# where is the adjustment calculation for the target value in the source code?

(Asked by: username_0 on 2018-12-13)

Can someone point me to the source code and explain how the average is calculated for the adjustment of difficulty that takes place every 2016 ?
Update: i have consulted with previous questions but it has not been documented properly and I am not expert so would be much appreciated.

**Tags:** bitcoin-core-development, difficulty, difficulty-retargetting


---

## 1 answers

### Answer by username_1 on 2018-12-13. Score: 5

The function is CalculateNextWorkRequired in pow.cpp L#49:
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();
}

Explanation:
1. If retargeting is disabled, return the last difficulty.
2. Calculate the timespan between the last block and 2016 blocks ago
3. Truncate the timespan to no less than 1/4 of the target timespan (3.5 days) or no greater than 4x the target timespan (8 weeks).
4. Multiply the last difficulty target by the ratio actualTimespan:targetTimespan
5. Truncate to the maximum allowed target (bnPowLimit) if the resulting target is too high (very low difficulty)

> **Comments:**
> **username_0**: thank you. When would retargeting be disabled ? How does this pop in ?  
> **username_0**: And where in this code is the legendary broken and unfixed bug that causes updates every 2015 rather than 2016 blocks ?  
> **username_1**: https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/chainparams.cpp#L294  
> **username_1**: https://github.com/bitcoin/bitcoin/blob/v0.17.0.1/src/pow.cpp#L41  

---

