> To compensate for increasing hardware speed and varying interest in running nodes over time, the proof-of-work difficulty is determined by a moving average targeting an average number of blocks per hour. If they're generated too fast, the difficulty increases. ~ Satoshi Nakamoto in the Bitcoin Whitepaper
The core function of Bitcoin's mining difficulty is to ensure that blocks take an average of ten minutes to be mined. Transactions are checked multiple times by miners before ultimately being validated and confirmed in the block. This time-based [[Proof of Work]] system delays confirmations such that all transactions included in a block are verified in the correct time-order.
In order to prevent [[Double-spending|double-spending]], and to ensure [[Finality|finality]], Nakamoto-style [[Proof of Work]] trades transaction speed for security. When a transaction is finally added to a block, it has been checked independently by multiple nodes on the Bitcoin network, satisfying a core property of blockchain which is its [[Immutability|immutability]].
From the [Bitcoin Core developer codebase](https://github.com/bitcoin/bitcoin) on GitHub, conditions that result in invalidated transactions are related to time-order:
`TX_PREMATURE_SPEND, //!< transaction spends a coinbase too early, or violates locktime/sequence locks`
`BLOCK_INVALID_HEADER, //!< invalid proof of work or time too old`
`BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)`
To control block interval times, the difficulty is set to adjust every 2,016 blocks, which is roughly every two weeks. This adjustment effectively resets the difficulty target, and thus the computing power required to mine a block, and leads the [[Bitcoin Halving]] set to occur every 210,000 blocks.
>1,008 blocks per week * 52 weeks * 4 years --> 209,664 blocks in between each [[Bitcoin Halving]].
Because the above calculation leaves fewer than 210,000 blocks, difficulty targets ebb and flow. Also - as the network's [[Hashpower]] grows, so does the difficulty of mining Bitcoin. From the chart below, you can see the difficulty rising over a three-year period, and this has been the trend since Bitcoin's inception. Most notably, one might observe the significant drop in difficulty in mid-2021. This was largely a result of a historic change in May of that year: China banned the mining of cryptocurrencies, resulting in the largest migration of computing power in history. This was a major test for the Bitcoin network, and network miners rather remarkably migrated their equipment to other parts of the world.
