Definition: The mining difficulty determines how long it takes in average (number of attempts) to find a suitable input set that results in a valid hash output in order to mine a block. Usually miners (in PoW) or block creators (PoS) are required to find a hash below a certain target.
The higher the mining or network difficulty is the more attempts are necessary in average to find a correct result.
It is closely related to the puzzle friendliness property of a hash function.
A (mining) difficulty can be used in Proof of Work and Proof of Stake blockchains.
Example of Blockchain Mining Difficulty
Let us assume that we want to find an input which results in a SHA256 hash output lower than our target. Our algorithm is fairly simple.
The hiding property of our hash function makes sure that it is impossible to backwards calculate the correct solution. The only way to find a suitable input is to try all possible inputs.
Our Target is:
Now we iterate through some inputs (nonces) and check, if the hash output is lower than our target value.
- Nonce: 1
- SHA256: 6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EAA22F1D49C01E52DDB7875B4B
- Decimal: 48635463943209833381901998330766343617611861281925802850775679724874994024448
- Lower than target? No 🙁
- Nonce: 2
- SHA256: D4735E3A265E16EEE03F59718B9B5D03019C07D8B6C51F90DA3A666EEC13AB35
- Decimal: 96094161643976068547795211782921178242700266527459617249984291792468700037120
- Lower than target? No 🙁
- Nonce: 3
- SHA256: 4E07408562BEDB8B60CE05C1DECFE3AD16B72230967DE01F640B7E4729B49FCE
- Decimal: 35293215426786447886904954653686936637725318876815869978644794420005867356160
- Lower than target? YES! 🙂
You see we needed three attempts to find a suitable input nonce which results in a hash output fulfilling our puzzle.
The lower the target is the more attempts we would need in average to be successful. This means the difficulty is higher.
Application of the Mining Difficulty
The difficulty also has another usage in the chain fork choice rule. In a simplified explanation the longest chain is chosen in case of a fork. But if only the number of blocks were counted an attacker could lower the difficulty and mine more blocks in the same time.
That’s why the chain with the highest cumulated difficulty is chosen in most blockchains. This means the chain with the most work in it is considered as the canonical chain.
Usually there is a difficulty parameter stored in each block. And the higher the cumulated difficulty is the more work has been committed to this branch.
In this figure the upper chain has more blocks than the lower chain. But with 14 its cumulated difficulty is lower (sum of the numbers in the blocks) than the one of the lower chain (17). This would mean that only a few miners mine on the upper chain but more miners work on the lower chain. As a result, the lower chain is more secure and should be chosen as the canonical chain by the algorithm.
The difficulty is important to maintain a constant average block finding time. Over time computers get faster and can try more hashes in the same time (hash rate) and the likelihood of finding the right solution rises. The same happens if more nodes are online. If the difficulty remains constant this would lead to a shorter block finding time. A shorter block time has two effects:
- More orphaned blocks in conjunction with network latency.
- A higher inflation (and a quicker exhaustion of the maximal available amount of coins) due to a quicker distribution of the block reward.
But a dynamic adjustment of the difficulty is also necessary, if the hash rate drops for some reasons. This could be due to economic changes where miners cease their operations, natural disasters where nodes get damaged or internet blockades in some countries.
There are different approaches how to adjust the mining difficulty.
Difficulty Adjustment Models
There are different algorithms how the mining difficulty of a blockchain can be adjusted. There are basically two types of algorithms.
In a periodical approach the mining difficulty is adjusted in certain intervals, e. g. two weeks. At the end of the period the number of blocks created within this period are counted and compared with the expected number of blocks. If more blocks were mined the difficulty rises. If fewer blocks were found the difficulty gets lowered.
Bitcoin for example adjusts its difficulty target that way.
Another approach is to adjust the difficulty with every block. Here each miner calculates the time between the parent block and the actual block. If it is above a certain threshold the difficulty is reduced. If the time was too low the difficulty rises.
Calculation of Bitcoin Mining Difficulty.