Teacher Xiao Zhen from Peking University's "Blockchain Technology and Application" series of course study notes [19] Ethereum - Difficulty Adjustment

Table of contents

1. The difficulty adjustment principle of Ethereum

        1. Difficulty adjustment formula

2. Four Stages of Ethereum Development

3. Code implementation

        1. In the Byzantium phase, the code for mining difficulty adjustment

        2. Calculate the difficulty adjustment of the basic part

        3. Calculation of the difficulty bomb

Fourth, the actual statistics of Ethereum

        1. Difficulty Statistics in Ethereum

        2. Block time statistics

        3. Two blocks 

1. The difficulty adjustment principle of Ethereum

        In order to keep the block generation time at about ten minutes, Bitcoin will adjust the mining difficulty every 2016 blocks. In Ethereum, it is possible to adjust the mining difficulty for each block, and the adjustment method is also relatively complicated. Several versions have been changed, and there are some discrepancies between the Yellow Book of Ethereum and the actual code. Here we follow the principle of taking the code as the standard for learning.

1. Difficulty adjustment formula

Picture 1-1

         This is the formula for difficulty adjustment, where H refers to the current block, Hi is the serial number of this block, and D(H) is the current difficulty of this block, then the difficulty adjustment formula has two parts, the max bracket The first part is called the basic part, the purpose is to maintain the block generation time at about fifteen seconds, followed by ∈ is the second part, also known as the difficulty bomb, mainly for the transition to the proof of rights and interests, ether Fang wants to gradually transfer the consensus mechanism from proof of work to proof of equity.

Figure 1-2
Figure 1-3

        The value of this part of the difficulty bomb increases exponentially. When Ethereum was just launched, the block numbers were relatively small, and the calculated value of the difficulty bomb was very small, basically negligible, so the difficulty adjustment was mainly determined by the basic part (block generation time in the system) to decide. As the block numbers get bigger and bigger, the power of the difficulty bomb begins to show. The idea of ​​the original design is that when the power of the difficulty bomb begins to play out, Ethereum can switch from proof of work to proof of equity (mining becomes more and more difficult, and everyone intends to switch to proof of equity). But the actual situation: there are still many problems to be solved in the consensus mechanism based on proof of rights and interests , which is far from smooth as originally imagined, so the time to switch to proof of rights and interests has been postponed again and again. There is no other way to reach consensus.

        I was worried that everyone would not want to transfer, but now it has become impossible to transfer if you want to transfer. This situation became obvious in April and May of 2017. The block generation time has gradually begun to increase, from 15 seconds to about 30 seconds. , and will continue to grow if no measures are taken. When Ethereum finally decides to calculate the difficulty bomb, it will roll back the block number by 3 million blocks, that is, subtract 3 million from the real block number to calculate a fake block number, and then calculate the difficulty bomb . Buy some time for the upper limit of the proof of stake.

Figure 1-4

        The role of the difficulty bomb is shown in Figure 1-4. It can be seen that in the early days, it was basically negligible, and the difficulty adjustment was basically adjusted according to the block generation time in the system. At about 3.7 million blocks, the power of the difficulty bomb began to rise exponentially. At the peak above (that is, when Ethereum decided to call back the difficulty bomb), three million blocks were reduced. Let’s take a look at the value of the difficulty bomb It just fell down, and the back looks like a flat straight line, but it is actually growing, but because the peak is too high, it can't be seen.

2. Four Stages of Ethereum Development

        The development of Ethereum is divided into four stages, Frontier, Homestead, Metropolis, and Serenity. Metropolis is divided into two stages, Byzantium and Constantinople. We are in the Byzantium stage, and the callback of the difficulty bomb is carried out in the Byzantium stage, as shown in the figure 2-1 shown. EIP (Ethereum Improvement Proposal), BIP (BitCoin Improvement Proposal).

Figure 2-1

        The Ethereum system reduced the block reward from 5ETH to 3ETH at the same time as the difficulty callback (if not adjusted, it would be unfair to the miners before the callback), and the total supply benefiting from the system must be maintained. If the supply is stable and mining becomes easier, the block reward should be reduced accordingly. In Bitcoin, block rewards are halved every once in a while. This practice does not exist in Ethereum. The reduction of 5ETH to 3ETH is a one-off, and it does not mean that it will be done regularly in the future.

3. Code implementation

        

1. In the Byzantium phase, the code for mining difficulty adjustment

        The input is the timestamp of the parent block and the difficulty of the parent block, and the difficulty of the currently mined block is calculated. BigTime in the code is the timestamp of the current block, and bigParentTime is the timestamp of the parent block

//calcDifficultyByzantium is the difficulty adjustment algorithm.It returns
//the difficulty that a new block should have when created at time given the
//parent block's time and difficulty.The calculation uses the Byzantium rules.
func calcDifficultyByzantium(time-uint64,parent *types.Header) *big.Int {
    //https://github.com/ ethereum/EIPs/issues/100.
    //algorithm:
    //diff = (parent_diff +
    //        (parent_diff [ 2048*
    //            max((2 if len(parent.uncles) else 1) - ((timestamp -parent.timestamp)// 9),-99))
    //        ) + 2^(periodCount - 2)
    bigTime := new(big.Int).Setuint64(time)
    bigParentTime := new(big.Int).Set(parent.Time)
    × := new(big.Int)
    y := new(big.Int)

2. Calculate the difficulty adjustment of the basic part

//(2 if len(parent_uncles) else 1) - (timestamp - parent_timestamp) //9
x.Sub(bigTime,bigParentTime)
x.Div(x,big9)
if parent.UncleHash == types.EmptyUncleHash {
    x.Sub(big1,x)
} else {
    x.Sub(big2,x)
}
//max((2-if-len(parent_uncles) else 1) - (timestamp - -parent_timestamp)//9, -99)
if x.cmp(bigMinus99) < 0 {
    x.Set(bigMinus99)
}
//parent_diff + (parent_diff / 2848 *,
//max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp)// 9),-99))
y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
x.Mul(y,x)
x.Add(parent.Difficulty, x)
//minimum  difficulty can ever be (before exponential factor)
if x Cmp(params.MinimumDifficulty) < 0 {
    x.Set(params.MinimumDifficulty)
}

        注:DifficultyBoundDivisor = big.NewInt(2048), inimumDifficulty= big.NewInt(131072)。

        The first line is to subtract the current timestamp from the parent block’s timestamp to calculate the block time, and then the second line is divided by 9 and rounded down. Determine whether there is an uncle block, if yes, subtract the previous number x from 2, if not, subtract the previous number x from 1, and then compare it with the negative 99, there is a limit to the downward adjustment , can not be smaller than -99, the next calculation is the degree of difficulty adjustment, the difficulty of the parent block divided by this DifficultyBoundDivisor is actually 2048, and then multiplied by the previously calculated coefficient, added to the difficulty of the parent block Go, the difficulty adjustment of the basic part has a lower limit, no matter how small the difficulty is, it cannot be less than that D0, and the MinimumDifficulty is that D0=131072.

3. Calculation of the difficulty bomb

//calculate a fake block number for the ice age delay:
//    https : /lgithub.com/ ethereum/EIPs/pull/669
//    fake_block_number = min(0,block.number - 3000000
fakeBlockNumber := new(big.Int)
if parent.Number.Cmp(big2999999) >= 0 {
    fakeBlockNumber = fakeBlockNumber.Sub(parent.Number , big2999999)}
//for the exponential factor
periodCount := fakeBlockNumber
periodCount.Div(periodCount,expDiffPeriod)
//the exponential factor, commonly referred to as "the bomb"
//diff = diff + 2^(periodCount - 2)
if periodCount.Cmp(big1) > 0 {
    y.Sub(periodCount,big2)
    y.Exp( big2,y,nil)
    x.Add(x,y)
}

        注:expDiffPeriod = big.NewInt(100000)。

Fourth, the actual statistics of Ethereum

1. Difficulty Statistics in Ethereum

        Seeing this part of the curve, it looks like an exponential shape. The peak position is that Ethereum decided to roll back the difficulty bomb, rolling back three million blocks, so the difficulty of mining dropped immediately. Currently, Ethereum’s Mining difficulty is basically regionally stable.

Pic 4-1

 

2. Block time statistics

        Regardless of individual fluctuations, in general, the block generation time is stable at around 15s. It shows that in the early days of Ethereum, the adjustment of mining difficulty was mainly based on stabilizing the block generation time, which achieved the expected effect. Also in the middle of 2017, the block generation time has increased significantly, which is the effect of this difficulty bomb.

Figure 4-2

3. Two blocks 

        For Ethereum, the longest legal chain should actually be called the most difficult legal chain, which is the legal chain with the highest total difficulty. The difficulty of each block reflects the workload required to dig out this block, and the total difficulty is the highest. , that is, the total workload required to dig out all the blocks on this chain is the largest. Generally speaking, the workload required to dig out the later blocks is relatively large.

Figure 4-3

Guess you like

Origin blog.csdn.net/YSL_Lsy_/article/details/126482714