1. If block_time > last_block_time+1200, then difficulty=1
2. Otherwise, difficulty=network_diff
Note that every miner can control "block_time", because it is just what is put in block header. By default, Bitcoin Core gives you the earliest time (based on the current time, the MTP rule, and the timewarp 600 seconds rule). But: you can put some later timestamp there, to trigger "difficulty=1" condition.
60942/2016=30+...
30*2016=60480 //latest adjustment
bits_in_block(60480)=0x1913e3b7
bits_to_target(0x1913e3b7)=0000000000000013e3b700000000000000000000000000000000000000000000
bits_to_target(0x1d00ffff)=00000000ffff0000000000000000000000000000000000000000000000000000
difficulty=00000000ffff0000000000000000000000000000000000000000000000000000/0000000000000013e3b700000000000000000000000000000000000000000000
difficulty=26959535291011309493156476344723991336010898738574164086137773096960/124848484694520496450254989037678616522135653265297473798144
difficulty=215938025+81833825167668903265531020299488556990381026556677309071360/124848484694520496450254989037678616522135653265297473798144
difficulty=215938025+(2^176*3*5*7*79*103/2^176*3^3*23*2099)
difficulty=215938025+284795/434493
difficulty=215938025.6554651053066447560720195722370671104022389313521736828901731443...
https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L14
{
assert(pindexLast != nullptr);
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
// Only change once per difficulty adjustment interval
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
{
if (params.fPowAllowMinDifficultyBlocks)
{
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
return nProofOfWorkLimit;
else
{
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
}
return pindexLast->nBits;
}
// Go back by what we want to be 14 days worth of blocks
int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
assert(nHeightFirst >= 0);
const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
assert(pindexFirst);
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
}