Skip to content

refactor(homestead): avoid int() in calculate_block_difficulty - #3677

Closed
trakshan-mishra wants to merge 1 commit into
ethereum:forks/amsterdamfrom
trakshan-mishra:homestead-difficulty-no-int
Closed

trakshan-mishra wants to merge 1 commit into
ethereum:forks/amsterdamfrom
trakshan-mishra:homestead-difficulty-no-int

Conversation

@trakshan-mishra

Copy link
Copy Markdown

Description

Removes the int() conversions from calculate_block_difficulty in the Homestead fork (src/ethereum/forks/homestead/fork.py), expressing the same computation with Uint and U256 so the ethereum-types carry the intended signedness and bitwidth. Closes #1415 for Homestead only.

The docstring and existing comments are unchanged. Variable names are descriptive (no abbreviations). The exponentiation in the difficulty bomb is kept as ** (not a bit shift). No defensive checks are added.

The time-based adjustment is restructured to branch on whether it raises or lowers difficulty, avoiding the signed "factor" (max(1 - delta//10, -99)) that could be negative. The difficulty bomb is gated on block_number >= 200_000, equivalent to the old num_bomb_periods >= 0 check but never producing a negative intermediate.

Validation performed locally:

  • 101,344 differential cases matched — the old and new functions were compared over the edge-case grid (timestamp deltas, block numbers, parent difficulties around the 99-unit clamp and bomb boundaries) plus 100,000 randomized inputs; all produced identical results.
  • 1,653 Homestead tests passed (25 skipped) via uv run fill --fork Homestead -m "not slow" -n auto --dist=loadgroup --skip-index --clean tests.
  • Seven of eight just static checks pass (format-check, lint, spellcheck, typecheck, deadcode, lint-actions, lock-check).

One check fails: lint-spec (ethereum-spec-lint) exits 1 with a single GlacierForksHygiene diagnostic:

ethereum.forks.dao_fork - GlacierForksHygiene:
    `calculate_block_difficulty` in `.fork` has changed. Glacier forks may only differ in difficulty block.

This is because dao_fork is a glacier fork whose calculate_block_difficulty is a copy of Homestead's. The lint compares dao_fork against its predecessor (Homestead) by AST, and since only Homestead is changed here, the two now differ. The diagnostic resolves when the same refactor is applied to dao_fork in a follow-up.

Per CONTRIBUTING.md, this PR follows the recommended two-step workflow: Homestead only as a draft, with the other forks to follow after feedback. Maintainer guidance is requested before expanding to the other forks — specifically on whether to handle dao_fork together with Homestead (to keep lint-spec clean) or separately.

Related Issues or PRs

Refs #1415.

Checklist

  • Ran fast static checks to avoid CI fails, see Code Standards & Verifying Changes: just static
  • PR title has the form <type>(<area>): <title>, where <type> and <area> come from an appropriate C-<type>, respectively A-<area>, label. The title should match the target squash commit message.

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

Calculate the offset and difficulty bomb with ethereum-types instead of
casting to int, so Uint/U256 carry the intended signedness and bitwidth.

Refs ethereum#1415
@danceratopz

Copy link
Copy Markdown
Member

Sorry, we don't have the bandwidth to review agentic-driven drive-by PRs.

@danceratopz danceratopz closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid converting to int in calculate_block_difficulty

2 participants