Skip to content

Conversation

omegajudith
Copy link

Description

Refactors calculate_block_difficulty in the Homestead fork to avoid repeated int() casts while keeping behavior identical.

  • Convert once to Python int for the (signed) offset math.
  • Compute base/offset, apply difficulty bomb, clamp at MINIMUM_DIFFICULTY.
  • Return type remains Uint.

No behavioral change intended; this is a readability/micro-perf cleanup.

Related issue

Refs #1415.

Rationale

  • Fewer casts → clearer intent, slightly cheaper arithmetic in tight loops.
  • Matches repo guidance to tighten math while preserving protocol correctness.

Test plan

  • Static suite locally:
    • codespell: ✅
    • ruff check + ruff format --check: ✅
    • mypy: ✅
    • (Heads-up) ethereum-spec-lint shows a GlacierForksHygiene message locally even with only Homestead touched; opening as Draft to confirm CI behavior and get guidance.
  • Runtime checks:
    • Fast, scoped run: tox -e py3 -- -k 'homestead and difficulty'
    • Full filler run (project default): tox -e py361013 passed, 473 skipped on my box ✅
    • Equivalence sanity: compared new vs old implementation over edge cases + random inputs; no mismatches.

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.

@omegajudith omegajudith marked this pull request as ready for review October 8, 2025 15:28
Copy link
Contributor

@SamWilsn SamWilsn left a 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."""
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.

Comment on lines +745 to +746
pd_int = int(parent_difficulty)
dt_int = int(block_timestamp) - int(parent_timestamp)
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.

Comment on lines -786 to -790
# 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
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.

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?

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?

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.

2 participants