We’re back at it in the Proof of Work difficulty spectrum, this time going through how Ethereum’s difficulty changes over time. This is part 4.2 of the part 4 series, where part 4.1 was about Bitcoin’s PoW difficulty, and the following 4.3 will be about jbc’s PoW difficulty.
To calculate the difficulty for the next Ethereum block, you calculate the time it took to mine the previous block, and if that time difference was greater than the goal time, then the difficulty goes down to make mining the next block quicker. If it was less than the time goal, then difficulty goes up to attempt to mine the next block quicker.
There are three parts to determining the new difficulty:
offset, which determines the standard amount of change from one difficulty 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, Homestead, and Metropolis, but the overall formula for calculating the next difficulty is
target = parent.difficulty + (offset * sign) + bomb
Other Posts in This Series
- Part 1 — Creating, Storing, Syncing, Displaying, Mining, and Proving Work
- Part 2 — Syncing Chains From Different Nodes
- Part 3 — Nodes that Mine
- Part 4.1 — Bitcoin Proof of Work Difficulty Explained
For the following code examples, this will be 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 was grabbed from Etherscan.