Why adjust mining difficulty?
To maintain a fixed block time
What happens if you don’t tune it?
The block time will get shorter and shorter
Will there be any problem if the block production time is too short?
As the strength of the system increases and the mining difficulty remains the same, the block time will get shorter and shorter, and too short a block time will lead to too many forks.
Two nodes at the same time to mine, there will be bifurcation, the shorter the time, this kind of bifurcation will become normal, bifurcation too much for the system to reach a consensus on the more difficult, the system of the force is stronger, the more safe. Fork more finally force will be dispersed, because each node according to the network distribution is different, choose different fork mining, then malicious nodes do not need 51% of the calculation power can launch an attack. To prevent malicious attacks, you need to avoid too many forks. You need to keep the block time within a fixed range.
Bitcoin mining algorithm
H(Block Header) <= target
SHA-256
Bitcoin mining difficulty adjustment
The Bitcoin network rules update the difficulty of mining every 2,016 blocks. How long will it take?
(2016 * 10)/(24 * 60) = 14 days, which is about two weeks.
Adjust the formula
target = target * (actual time / expect time)
Expect time is the expected time: 2016 * 10
Actual Time The actual time of the last 2016 blocks generated in the system
If the actual time is greater than two weeks and the value is a number greater than one, the target becomes larger and less difficult.
If the actual time is less than two weeks, and the following value is a number less than one, the target becomes smaller and the difficulty increases.
In real code there is a limit to how much adjustment you can make, and neither increase nor decrease by more than a factor of 4.
For example, if the actual time is longer than 8 weeks, calculate it as 8 weeks and increase it by 4 times.
The actual time is even shorter than half a week, that is, in less than half a week, the 2016 block will be dug out, and the target threshold will be taken half a week. I’m going to reduce it by a quarter.
In the block header there is an nBits field of 4 bytes, encoded by Target. An honest miner checks this nBits to ensure that all miners are mining at the adjusted difficulty level.
Ethereum mining difficulty adjustment
Regulating general formula
Formula for block difficulty D(H) adjustment
Parameter Description:
D(H) Difficulty of this block
P(H)Hd parent block difficulty, the difficulty of each block is adjusted on the basis of the parent block difficulty
X * ϛ is used to adaptively adjust the block difficulty and maintain a stable block speed
Epsilon bomb
The base part is the lower bound D0 131072
Adaptive difficulty adjustment
X is the unit of adjustment, ϛ the coefficient of adjustment
Y is related to the number of uncles in the parent block, y = 2 if the parent block contains uncle otherwise y = 1
Hs is the timestamp of the local block, and P(H)Hs is the timestamp of the parent block. Both are in seconds and Hs > P(H)Hs is specified
It’s one unit harder when the parent block contains Uncle, because when uncle is included, the amount of newly issued currency increases,
At this time, it is necessary to properly increase the difficulty of mining to maintain the stability of currency circulation.
The upper bound of the adjustment is -99, which is mainly used to prevent hacking or other uncertain situations.
If we look at this formula carefully, if we take y 1 or y 2, it’s a constant minus the value, and if y is bigger than the value, it’s a positive number, which means we’re going to have to increase the difficulty. The reverse is down.
This part is the most important part to stabilize the block speed. If the block time is too short, it will be difficult to adjust, and if the block time is too long, it will be difficult to adjust.
Let’s take an example
Take the parent block without uncle as an example:
The block time is between 1-8 seconds. If the block time is too short, the difficulty will be increased by one unit.
The block time is between 9-17 seconds. The block time is acceptable and remains unchanged.
The block time is between 18 and 26 seconds. If the block time is too long, the difficulty will be reduced by one unit.
If it’s longer than 26 seconds, it’ll go down more units.
Difficulty is the bomb
Why the difficulty bomb?
The reason for the difficulty bomb is to reduce the risk of fork when moving to Pos, where mining is very difficult, so miners have an incentive to move to Pos.
ε is an exponential function of 2, doubling every 100,000 blocks, with a very rapid growth in the late period, which is the origin of the difficulty bomb.
Hi ‘has the real block number minus 3000000 to get, the reason for this is to underestimate the difficulty of developing Pos protocol, which takes about a year and a half longer
15 * 3000000 / (24 * 3600) / 365 = 1.42 years
EIP100: also at this time, the reward for ethereum decreased from 5ETH to 3ETH