-
Notifications
You must be signed in to change notification settings - Fork 362
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
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.
src/ethereum/forks/homestead/fork.py
Outdated
| 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.
src/ethereum/forks/homestead/fork.py
Outdated
| 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.
src/ethereum/forks/homestead/fork.py
Outdated
| 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?
…eum#1415) - Preserve docstrings and historical comments - Keep function identical across early PoW forks (Frontier→Spurious Dragon)
|
Hey @SamWilsn - Specifically: The branch is passing all relevant checks locally (ruff check/format passed, and the scoped test run Ready for a second review! |
Some of the changes you made are unnecessary, especially in the inline comments and the task is till unsolved the init is still there. |
|
@SamWilsn , wish u could check and we have time to resolve |
| """ | ||
| Computes difficulty of a block using its header and parent header. | ||
| The difficulty of a block is determined by the time the block was created |
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.
This for example
Description
Refactors
calculate_block_difficultyin the Homestead fork to avoid repeatedint()casts while keeping behavior identical.intfor 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-lintshows aGlacierForksHygienemessage 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.