When generating a new block, how is the nbits field in block header calculated? Is nbits calculated for every new block like:
nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160
Or, nbits is only updated every 2016 blocks like:
if(currentblocknum%2016==0) nbits=nbits of last block * (timstamp of lastblock-timestamp of (lastblocknum-2016))/20160 else nbits=nbits of lastblock
And when a node receives a new block, it will calculate the correct nbits itself for the new block (instead of using the nbits in the received block) before verifying the hash result, right?
I have not read the source code of bitcoin, and am confused by the introduction of nbits in some books.