-
Notifications
You must be signed in to change notification settings - Fork 347
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?
Homestead: reduce redundant int() casts in calculate_block_difficulty (#1415) #1460
Conversation
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.
Quick initial pass. This review basically amounts to: try not to destroy the surrounding code/docstring/comments.
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.""" |
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.
pd_int = int(parent_difficulty) | ||
dt_int = int(block_timestamp) - int(parent_timestamp) |
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.
We try to avoid opaque variable names, and instead prefer full words whenever possible.
# 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 |
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.
Please keep the comments.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this to a shift?
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 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?
Description
Refactors
calculate_block_difficulty
in the Homestead fork to avoid repeatedint()
casts while keeping behavior identical.int
for the (signed) offset math.MINIMUM_DIFFICULTY
.Uint
.No behavioral change intended; this is a readability/micro-perf cleanup.
Related issue
Refs #1415.
Rationale
Test plan
codespell
: ✅ruff check
+ruff format --check
: ✅mypy
: ✅ethereum-spec-lint
shows aGlacierForksHygiene
message locally even with only Homestead touched; opening as Draft to confirm CI behavior and get guidance.tox -e py3 -- -k 'homestead and difficulty'
✅tox -e py3
→ 61013 passed, 473 skipped on my box ✅Notes
Per maintainer advice, this PR limits changes to Homestead. I’m happy to follow up with the remaining forks after review feedback on this pattern.
@SamWilsn , Homestead-only refactor for #1415. Static is green locally; full tox -e py3 also green. Open as Draft for feedback.