Categories

# Mining Difficulty in Ethereum

The mining difficulty in Ethereum states how long it takes to find a suitable hash to mine a block. It is represented as an integer value and stored in each Ethereum block. The difficulty is also an indicator for Ethereum nodes which chain is the longest (or rather heaviest) chain in case of a fork. For further information about the chain-selection, see fork choice rules.

On average it takes 10 to 19 seconds to find a block in Ethereum. If the time is longer or shorter than 10 seconds the difficulty gets adjusted. Here, we explain the formula of how the Ethereum mining difficulty is calculated.

In Ethereum the difficulty gets adjusted in every block (in contrast to Bitcoin’s mining difficulty algorithm where the difficulty adjustment occurs every 2016 blocks).

In the following sections, you find an explanation of how the calculation algorithm works.

There was an update to the formula in the Homestead release. The current formula of the mining difficulty is:

block_diff = parent_diff + parent_diff // 2048 * max(1 – (block_timestamp – parent_timestamp) // 10, -99) + int(2**((block.number // 100000) – 2))

• block_diff: Difficulty to find a block
• parent_diff: Difficulty of the parent block
• //: integer division, e. g. 8  // 3 = 2 (instead of 2.67)

The formula consists of three parts which we explain now.

### Part 1

`parent_diff`

The difficulty of the parent block is the basis for the calculation of the difficulty of the current block. It gets increased or reduced by the following two parts.

### Part 2:

`parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)`

The first division parent_diff // 2048 is the step size. The max(1 – (block_timestamp – parent_timestamp) // 10, -99) choses the higher from two values. Either the result from 1 – (block_timestamp – parent_timestamp) // 10 or -99.

Let us consider four cases

Case 1

Here, the time difference between the blocks is exactly 10 seconds. This means that part 2 is zero.

• Parent_diff = 10,000
• Block_timestamp = 1610784108
• Parent_timestamp = 1610784098

10,000 + 10,000 // 2048 * max(1-(1610784108 – 1610784098)//10, -99) = 10,000 + 4 * 0 = 10,000

Case 2

The time difference between the blocks is larger than 10 seconds (20 seconds in our case).

• Parent_diff = 10,000
• Block_timestamp = 1610784118
• Parent_timestamp = 1610784098

10,000 + 10,000 // 2048 * max(1-(1610784118- 1610784098)//10, -99) = 10,000 + 4 * 0 = 10,000 + 4 *-1 = 9,996

This means that the difficulty gets reduced by 4.

Case 3

The time between two blocks is less than ten seconds. In our case, it is just five seconds.

• Parent_diff = 10,000
• Block_timestamp = 1610784103
• Parent_timestamp = 1610784098

10,000 + 10,000 // 2048 * max(1-(1610784103- 1610784098)//10, -99) = 10,000 + 4 * 0 = 10,000 + 4 *1 = 10,004

This means that the difficulty gets increased by 4.

Case 4

The time between the blocks is very long, so the “max”-term is -99.

• Parent_diff = 10,000
• Block_timestamp = 1610785098
• Parent_timestamp = 1610784098

10,000 + 10,000 // 2048 * max(1-(1610784103- 1610784098)//10, -99) = 10,000 + 4 *-99 = 10,000 -366 = 9,604

This means that the difficulty gets reduced by 366.

### Part 3

This is the so-called difficulty bomb. With each block, the difficulty increases a bit faster, no matter the time between the blocks.

`int(2**((block.number // 100000) - 2))`

The difficulty bomb is explained in another section in depth.

## Calculating the target

The difficulty is used to calculate the target. The target is the value that needs to be undercut in proof of work. The higher the difficulty is, the lower the target.

The algorithm for calculating the target is here: https://eth.wiki/en/concepts/ethash/ethash

Search for mining and look for this line:

`target = zpad(encode_int(2**256 // difficulty), 64)[::-1]`

Basically, a 256 bit number is integer-divided by the difficulty (//). There is some zero-padding and hex encoding done in order to compare it to the hexadecimal block hash.

## Difficulty Bomb/ Ice Age

The difficulty bomb is a term in the Ethereum mining difficulty formula. After a certain block number, the mining difficulty increases exponentially with every block. The increase is designed to be that fast that faster hardware cannot keep up with it. This leads to an ever-increasing block time.

The reason for this difficulty bomb is that every miner should be forced by that to update their software at a certain time. The software (and protocol updates) were planned ahead in order to move towards Ethereum 2.0 and Proof of Stake.

There were many updates already and every time the difficulty bomb was defused by setting the block number higher.

## Total Difficulty in Ethereum

The total difficulty states the total difficulty of the chain until this block. [https://eth.wiki/json-rpc/API]. It is an integer value and it is stored in the block.