From From EIP 2, the Homestead difficulty adjustment algorithm is:
block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99) + int(2**((block.number // 100000) - 2))
where // is the integer division operator, eg. 6 // 2 = 3, 7 // 2 = 3, 8 // 2 = 4.
How does this work?
(This question was prompted by the question What was the first block mined with Homestead?)
Other related Q&As:
Best Answer
Summary
If the timestamp difference
(block_timestamp - parent_timestamp)
is:parent_diff // 2048 * 1
parent_diff // 2048 * -1
to a max downward adjustment ofparent_diff // 2048 * -99
This is consistent with the statement from ethdocs.org - Ethereum Homestead - The Homestead Release:
And from Ethereum Network Status, the average block time currently is 13.86 seconds.
Details
The difficulty adjustment formula:
can be broken down into the following parts:
Sub-formula B - The difficulty bomb part, which increases the difficulty exponentially every 100,000 blocks.
The difficulty bomb won't be discussed here as it is already covered in the following Q&As:
Sub-formula A - The difficulty adjustment part, which increases or decreases the block difficulty depending on the time between the current block timestamp and the parent block timestamp:
Subformula A1 - Lets separate out part of Subformula A
and consider what the adjustment effect is due to the timestamp difference between the current block and the parent block:
When
(block_timestamp - parent_timestamp)
ismax(1 - 0, -99) = 1
+parent_diff // 2048 * 1
max(1 - 1, -99) = 0
+parent_diff // 2048 * 0
max(1 - 2, -99) = -1
+parent_diff // 2048 * -1
max(1 - 3, -99) = -2
+parent_diff // 2048 * -2
max(1 - 100, -99) = -99
+parent_diff // 2048 * -99
max(1 - {number greater than 100}, -99) = -99
+parent_diff // 2048 * -99
So, if the timestamp difference
(block_timestamp - parent_timestamp)
is:parent_diff // 2048 * 1
parent_diff // 2048 * -1
to a max downward adjustment ofparent_diff // 2048 * -99
The Source Code
From Go Ethereum - core/block_validator.go, lines 264-311: