How to build Your Own Blockchain part 4.2-ethereum Proof of Work Difficulty

Source: Internet
Author: User
Tags chop pow

We ' re back on it in the Proof of Work difficulty spectrum, this time going through how Ethereum ' s difficulty Time. This is part 4.2 of the "Part 4 series" Where part 4.1 was about Bitcoin ' PoW difficulty, and the following 4.3 would be ab Out JBC ' s PoW difficulty.

Tl;dr

To calculate the difficulty for the next Ethereum blocks, you calculate the time it took to mine the previous blocks, and if That's time difference is greater than the goal time, then the difficulty goes down to make mining the next block quicker. If It is less than the time goal, then difficulty goes the "to attempt" the next block mine.

There are three parts to determining the new Difficulty:offset, which determines the standard amount of change from one D Ifficulty to the next; Sign, which determines if the difficulty should go up or down; And bomb, which adds on extra difficulty depending on the block ' s number.

These numbers are calculated slightly differently for the different, forks, Frontier, and Homestead, Metropolis the But Erall formula for calculating the next difficulty is

target = parent.difficulty + (offset * sign) + bomb
Other Posts in this SeriesPart 1-creating, storing, Syncing, displaying, Mining, and proving Work part 2-syncing chains to different Par T 3-nodes that Mine part 4.1-bitcoin Proof of Work Difficulty explained Pre Notes

For the following code examples, this is the class of the block.

Class block ():
  def __init__ (self, number, timestamp, difficulty, Uncles=none):
    self.number =
    number Self.timestamp = Timestamp
    self.difficulty = difficulty
    Self.uncles = Uncles

The ' Data I use ' to ' show the ' code is correct ' grabbed from Etherscan.

The code doesn ' t include edge cases in calculating the difficulty either. For the most part, edge cases aren ' t involved in calculating the difficulty target. By isn't including them, it makes the following code much more easier to understand. I ' ll talk about the edge cases at the "end of" sure they ' re not completely ignored.

Finally, for the beginning fork, I talk about what the different variables and functions perform. For later forks, Homestead and Metropolis, I only talk about the changes.

Also, here's all of the code I threw in a Github repo! If you don ' t want to write all the code yourself, your should at least clone it locally and run it yourself. 1000% feel free to make pull requests if your want to add more parts to it or I formatted the code wrong. The beginning-frontier

In the beginning, there was Frontier. I ' ll jump right with giving a bullet point section going over the config variables. Diff_adjustment_cutoff-represents the seconds that Ethereum are aiming to mine a blocks at. Block_diff_factor-helps calculate how much of the current difficulty can be change. Expdiff_period-denotes after how many blocks the bomb amount is updated. Expdiff_free_periods-how many expdiff_periods are ignored before the including in bomb difficulty.

And now descriptions of the functions.

Calc_frontier_sign -calculates whether the next difficulty value should down. In the case of Frontier, if the previous block is mined quicker than the seconds diff_adjustment_cutoff, then the Sign is 1, meaning we want the difficulty to is higher to make it more difficult with the goal this next block B E mined more slow. If the previous block is mined longer than seconds, then the sign'll be-1 and the next difficulty would be lower. The overall point of the, goal for block mining, is ~12.5 seconds. Take a look At vitalik Buterin ' s post where he talks about choosing seconds on the minimum average block mini Ng time.

Calc_frontier_offset -offset is the value this determines how much or how little the difficulty'll change. In the Frontier, this is the parent's difficulty integer devided by The block_diff_factor. Since it ' s devided by 2048, which are 2^11, offset can also be calculated by parent_difficulty >> 11&n Bsp;if you want to look at the it in terms of shifting bits. since 1.0/2048 == 0.00048828125, it means that the offset would only change the difficulty by 0.0488%  ;p er change. Not so much, which is good because we don ' t want the difficulty to change a ton with each different mined block. But if the time becomes consistently under seconds cutoff, the difficulty'll slowly grow to compensate.

Calc_frontier_bomb-the bomb. The bomb adds an amount of difficulty this doubles after every. In the Frontier world, this value is incredibly small. For example, in block 1500000, the bomb is 2 * * ((1500000//100000)-2) = = 2 * * 15 = 32768. The difficulty of the the IS 34982465665323. That ' s a huge difference meaning the bomb took on zero affect. This'll change later.

Calc_frontier_difficulty-once you have the values for sign, offset, and bomb, the new difficulty is (Parent.difficulty + Offset * sign) + bomb. Let's say that's the time it took to mine the parent's block was seconds. In this case, the difficulty'll go down by offset *-1, and then add the small amount of the bomb on the end. If the time to mine the parent's block was 8 seconds, the difficulty would increase by offset + bomb.

In order to understand it fully, go through the code this follows and look at the calculations.

Config = dict (diff_adjustment_cutoff=13, block_diff_factor=2048, expdiff_period=100000, expdiff_free_periods=2,) 

def calc_frontier_offset (parent_difficulty): offset = parent_difficulty//config[' block_diff_factor '] return offset def calc_frontier_sign (Parent_timestamp, child_timestamp): Time_diff = Child_timestamp-parent_timestamp if Time_di FF < config[' Diff_adjustment_cutoff ']: sign = 1 else:sign =-1 return sign def calc_frontier_bomb (Parent_ 
  Number): Period_count = (parent.number + 1)//config[' expdiff_period '] period_count-= config[' Expdiff_free_periods '] Bomb = 2** (Period_count) return bomb def calc_frontier_difficulty (parent, child_timestamp): offset = Calc_frontier _offset (parent.difficulty) sign = calc_frontier_sign (Parent.timestamp, child_timestamp) bomb = Calc_frontier_bomb (par Ent.number) target = (parent.difficulty + offset * sign) + bomb return offset, sign, bomb, target
The middle-homestead

The Homestead fork, which took place at block number 1150000 in March to 2016, has a couple big changes with calculating t He sign.

Calc_homestead_sign-instead of has a single number, diff_adjustment_cutoff which are different for the Homestead Fork, That makes the "difficulty go" or down, Homestead takes a slightly different. If you look in the code, you'll be in the "that" there are groupings of the sign rather than either 1 or-1. If the Time_diff between grandparent and parent is in [0,9], sign'll be 1, meaning this difficulty needs to increase. If the Time_diff is [10,19], the sign would be 0 meaning this difficulty should as it is. If the Time_diff is in the range [m], then the sign becomes-1. If Time_diff is in the range [30,39], then the sign is-2, etc.

This does two things. The Homestead doesn ' t want to equate a blocks that took the seconds as the mine as a block that being sec Onds to mine. If It took a blocks seconds, then the next difficulty in fact does need. Secondly, instead of Diff_adjustment_cutoff representing the goal time, which switches the aim point to be The mid poi NT of the range of time_diffs with a sign of 0. [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]. Meaning ~14.5 seconds, not including the bomb.

Config = dict (homestead_diff_adjustment_cutoff=10, block_diff_factor=2048, expdiff_period=100000, EXPDIFF_FREE_PE riods=2,) def calc_homestead_offset (parent_difficulty): offset = parent_difficulty//config[' Block_diff_factor '] re
  Turn offset def calc_homestead_sign (Parent_timestamp, child_timestamp): Time_diff = Child_timestamp-parent_timestamp Sign = 1-(Time_diff//config[' Homestead_diff_adjustment_cutoff ']) return sign def calc_homestead_bomb (Parent_numbe r): Period_count = (parent_number + 1)//config[' Expdiff_period '] # or Parent.number + 1 >> One if you are bit no tation Period_count-= config[' expdiff_free_periods '] bomb = 2** (Period_count) return bomb def Calc_homestead_diffi Culty (Parent, child_timestamp): offset = calc_homestead_offset (parent.difficulty) sign = calc_homestead_sign (parent.t Imestamp, child_timestamp) bomb = Calc_homestead_bomb (parent.number) target = (parent.difficulty + offset * sign) + bo MB return offset, sign, Bomb, Target 
The current-metropolis

There are a couple differences from Homestead. The ' Diff_adjustment_cutoff is ' now 9, which means that, without uncles, the target block time is midpoint of [9, one, one, each, or ~13 seconds.)

The second takes into account whether or not there are uncles in the block. and uncle in Ethereum language refers to a point in time where two nodes a child block from the mine same. So if you are mining a child blocks from a parent that has a "sibling", your ' re able to pick one of the siblings to mine from , but also include that to you noticed. In this case, Ethereum wants to make the new difficulty larger, buy another offset, to make sure this there is a less like Ly chance for two natural forks to get much longer.

The

Now's biggest difference is dissolving the impact of the bombs. Check out the "code For calc_metropolis_bomb where not" Do we subtract the value of expdiff_free_periods , but Also metropolis_delay_periods which is a time periods. A huge number. Instead of talking about the bombs here, I'll have a section devoted to that after this.

Config = dict (metropolis_diff_adjustment_cutoff=9, block_diff_factor=2048, expdiff_period=100000, EXPDIFF_FREE_PE Riods=2, metropolis_delay_periods=30, Def calc_metropolis_offset (parent_difficulty): offset = parent_difficulty/ config[' block_diff_factor '] return offset def calc_metropolis_sign (Parent_timestamp, Child_timestamp): If Parent.uncl  Es:uncles = 2 Else:uncles = 1 Time_diff = Child_timestamp-parent_timestamp sign = uncles-(Time_diff// config[' Metropolis_diff_adjustment_cutoff ']) return sign def calc_metropolis_bomb (parent_number): Period_count = (PA  Rent_number + 1)//config[' expdiff_period '] period_count-= config[' metropolis_delay_periods '] #chop off, meaning go Back 3M blocks in time period_count-= config[' Expdiff_free_periods ' #chop off 2 more for good measure bomb = 2** (PE Riod_count) return bomb def calc_metropolis_difficulty (parent, child_timestamp): offset = calc_metropolis_offset (pare nt.difficulty) sign =Calc_metropolis_sign (Parent_timestamp, child_timestamp) bomb = Calc_metropolis_bomb (parent.number) target = (parent.d Ifficulty + offset * sign) + bomb return offset, sign, bomb, target
going deeper with the bombs

If you look in the difficulty charts from online, you'll look a recent amount of huge increasing jumps every 100000 blocks, and then a giant drop off about a month ago. Screenshot time for those not wanting to click the link:

Each horizontal line indicates a 3 second the change in time it takes to mine a block.

What's the point of the "having a" bomb like this? A big goal of ethereum be to get rid of Proof of Work, which requires energy and time to create and validate a new block, Into Proof of Stake, which is described in the Ethereum Wiki. In order to force nodes to the Proof of the Stake implementation, a "bomb" that doubles it impact on the difficulty E Very 100000 blocks would soon make it take "a new block," mine nodes on the "Old Running Fork '" "won to Run anymore. This is where the term ' Ice age ' comes from; The block chain would is frozen in time.

This is a good way to manage future changes, but also "we run into" issue that the new Proof of Stake implementation, CA lled Casper, wasn ' t ready in time before the giant difficulty-spikes in-that graph. That's where the Metropolis fork came into play-it eliminated the effect the bomb has on the difficulty and but in a few ye Ars It'll come back in play where the switch to Casper'll (hopefully) is ready to roll. That being said, predicting then features like this fork are the for ready and production are adoption, so if difficult Isn ' t ready to is pushed quick enough, you can create another fork this moves the bomb back into time again.

Bomb Math

Besides the description of what the bombs does to the difficulty as seen on those graphs, it's worth it to show the math Beh IND Why the difficulty is rising to those levels, and what so means for how long it takes to mine a.

Going back

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.