-
Notifications
You must be signed in to change notification settings - Fork 348
Homestead: reduce redundant int() casts in calculate_block_difficulty (#1415) #1460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: forks/osaka
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,3 +60,6 @@ tests/fixtures | |
tests/t8n_testdata | ||
|
||
fixtures | ||
|
||
# test artifacts | ||
logs/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.""" | ||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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.