Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ tests/fixtures
tests/t8n_testdata

fixtures

# test artifacts
logs/
66 changes: 16 additions & 50 deletions src/ethereum/forks/homestead/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,59 +740,25 @@ def calculate_block_difficulty(
parent_timestamp: U256,
parent_difficulty: Uint,
) -> Uint:
"""
Computes difficulty of a block using its header and parent header.

The difficulty is determined by the time the block was created after its
parent. The ``offset`` is calculated using the parent block's difficulty,
``parent_difficulty``, and the timestamp between blocks. This offset is
then added to the parent difficulty and is stored as the ``difficulty``
variable. If the time between the block and its parent is too short, the
offset will result in a positive number thus making the sum of
``parent_difficulty`` and ``offset`` to be a greater value in order to
avoid mass forking. But, if the time is long enough, then the offset
results in a negative value making the block less difficult than
its parent.

The base standard for a block's difficulty is the predefined value
set for the genesis block since it has no parent. So, a block
can't be less difficult than the genesis block, therefore each block's
difficulty is set to the maximum value between the calculated
difficulty and the ``GENESIS_DIFFICULTY``.
"""Homestead difficulty: same behavior, fewer int() casts."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably keep the docstring.

# Convert once to signed ints for the parts that need negatives.
pd_int = int(parent_difficulty)
dt_int = int(block_timestamp) - int(parent_timestamp)
Comment on lines +745 to +746
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid opaque variable names, and instead prefer full words whenever possible.


Parameters
----------
block_number :
Block number of the block.
block_timestamp :
Timestamp of the block.
parent_timestamp :
Timestamp of the parent block.
parent_difficulty :
difficulty of the parent block.
# Base adjustment and signed offset (can be negative).
base = pd_int // 2048
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think base can be negative at this point?

offset = base * max(1 - (dt_int // 10), -99)

Returns
-------
difficulty : `ethereum.base_types.Uint`
Computed difficulty for a block.
# Apply offset.
diff_int = pd_int + offset

"""
offset = (
int(parent_difficulty)
// 2048
* max(1 - int(block_timestamp - parent_timestamp) // 10, -99)
)
difficulty = int(parent_difficulty) + offset
# Historical Note: The difficulty bomb was not present in Ethereum at the
# start of Frontier, but was added shortly after launch. However since the
# bomb has no effect prior to block 200000 we pretend it existed from
# genesis.
# See https://github.com/ethereum/go-ethereum/pull/1588
Comment on lines -786 to -790
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the comments.

# Difficulty bomb.
num_bomb_periods = (int(block_number) // 100000) - 2
if num_bomb_periods >= 0:
difficulty += 2**num_bomb_periods
diff_int += 1 << num_bomb_periods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this to a shift?


# Some clients raise the difficulty to `MINIMUM_DIFFICULTY` prior to adding
# the bomb. This bug does not matter because the difficulty is always much
# greater than `MINIMUM_DIFFICULTY` on Mainnet.
return Uint(max(difficulty, int(MINIMUM_DIFFICULTY)))
# Clamp to minimum and return typed.
min_diff = int(MINIMUM_DIFFICULTY)
if diff_int < min_diff:
diff_int = min_diff
return Uint(diff_int)