Skip to content

chore(refactor): split transactions.py:calculate_intrinsic_cost() into helper functions for maintainability#2714

Open
shrirajpawar4 wants to merge 4 commits intoethereum:forks/amsterdamfrom
shrirajpawar4:fix/refactoring-into-helper-functions
Open

chore(refactor): split transactions.py:calculate_intrinsic_cost() into helper functions for maintainability#2714
shrirajpawar4 wants to merge 4 commits intoethereum:forks/amsterdamfrom
shrirajpawar4:fix/refactoring-into-helper-functions

Conversation

@shrirajpawar4
Copy link
Copy Markdown

🗒️ Description

Refactor Amsterdam transaction intrinsic-gas accounting into small neutral helper functions to reduce merge-conflict pressure in transactions.py.

This change keeps behavior unchanged and only restructures the intrinsic-cost pipeline in src/ethereum/forks/amsterdam/transactions.py. calculate_intrinsic_cost() now delegates to helper seams
for:

  • calldata cost
  • contract creation cost
  • access-list cost
  • authorization cost
  • state-dependent intrinsic cost
  • floor-gas calculation

Amsterdam behavior is preserved:

  • access lists still affect intrinsic gas
  • access lists still contribute zero floor tokens
  • set-code transactions still charge authorization intrinsic gas
  • public function signatures remain unchanged

A lightweight regression test file was added to cover legacy calldata, contract creation, access-list transactions, and set-code authorization gas.

🔗 Related Issues or PRs

Fixes #2708.

✅ Checklist

  • All: Ran fast static checks to avoid unnecessary CI fails, see also Code Standards and [Enabling Pre-commit Checks](https://
    eest.ethereum.org/main/dev/precommit/):
    just static
  • All: PR title adheres to the repo standard - it will be used as the squash commit
    message and should start type(scope):.
  • All: Considered updating the online docs in the ./docs/ directory.
  • All: Set appropriate labels for the changes (only maintainers can apply labels).
  • Tests: Ran mkdocs serve locally and verified the auto-generated docs for new tests in the Test Case Reference are correctly formatted.
  • Tests: For PRs implementing a missed test case, update the post-mortem document to add an entry the list.
  • Ported Tests: All converted JSON/YML tests from ethereum/tests or tests/static have been assigned @ported_from marker.

Verification run:

UV_CACHE_DIR=/tmp/uv-cache just static
UV_CACHE_DIR=/tmp/uv-cache uv run pytest \
    tests/json_loader/test_transaction_codec.py \
    tests/json_loader/test_transaction_intrinsic_cost.py -q

Cute Animal Picture

image

@felix314159
Copy link
Copy Markdown
Contributor

ty for taking a look at this. i think you can further extend this: where is calculate_floor_tokens_in_calldata()? it should get its own dedicated helper instead of being implicitly tied to count_tokens_in_data(). additionally, right now calculate_intrinsic_access_list_cost() still mixes two concepts in one helper: intrinsic access-list gas and floor-token-derived gas. right now it still returns access_list_cost + tokens_in_access_list * GAS_TX_DATA_TOKEN_FLOOR, which means future access-list floor-token work still crosses seams instead of being isolated in floor-token helpers

@shrirajpawar4
Copy link
Copy Markdown
Author

Thanks for the review @felix314159 . I extended the split so the seams are now concept-pure:

  • added calculate_floor_tokens_in_calldata()
  • made calculate_intrinsic_access_list_cost() return only intrinsic access-list gas
  • kept floor-token-derived gas isolated to the floor-token helpers and calculate_data_floor_gas_cost()

So future access-list floor-token work should no longer cross the intrinsic access-list seam.

@felix314159 felix314159 changed the title fix(refactor): split transactions.py into helper functions for maintainbility chore(refactor): split transactions.py:calculate_intrinsic_cost() into helper functions for maintainability Apr 20, 2026
@felix314159
Copy link
Copy Markdown
Contributor

thanks for the swift commit! there is a bit more to do:

  • extract seams from validate_transaction()
  • let calculate_intrinsic_cost() assemble regular/state/floor through helpers instead of flattening back to one Uint too early

the context of the issue you are solving is that the script .github/scripts/build_devnet_branch.py should be able to take forks/amsterdam as base fork and then merge EIP branches like 7981 and 8037 into it without a merge conflict. so far your PR is great for the 7981 part, but for 8037 the problem is that validate_transaction() is still too monolithic and we should split it too

@shrirajpawar4
Copy link
Copy Markdown
Author

this makes sense.

My current changes are scoped narrow, but I see your point that the actual merge-base goal is broader.
I’ll update the PR in that direction.

I understand the remaining work as:

  • extract independent seams from validate_transaction()
  • let calculate_intrinsic_cost() compose regular/state/floor through grouped helpers.

am i missing anything?

@felix314159
Copy link
Copy Markdown
Contributor

whether you missed something or not can only be answered after seeing the code :D but your plan looks reasonable

…refactoring-into-helper-functions

# Conflicts:
#	src/ethereum/forks/amsterdam/transactions.py
@shrirajpawar4
Copy link
Copy Markdown
Author

hey @felix314159
Updated this branch to the latest forks/amsterdam and kept the refactor aligned with the current upstream GasCosts structure.

Done:

  • split validate_transaction() into dedicated validation helpers
  • made calculate_intrinsic_cost() compose regular/state/floor via helpers
  • kept floor-token accounting separate from intrinsic access-list gas
  • added focused regression tests

Verification:

  • UV_CACHE_DIR=/tmp/uv-cache uv run pytest tests/json_loader/test_transaction_intrinsic_cost.py tests/json_loader/test_transaction_validation.py
  • UV_CACHE_DIR=/tmp/uv-cache uv run ruff check src/ethereum/forks/amsterdam/transactions.py tests/json_loader/test_transaction_intrinsic_cost.py tests/json_loader/test_transaction_validation.py

I also checked current upstream/eips/amsterdam/eip-7981 and upstream/eips/amsterdam/eip-8037 against this branch. Both still conflict in src/ethereum/forks/amsterdam/transactions.py; from the
merge hunks, those conflicts are now due to the current EIP branches targeting older / broader transaction-cost API shapes, not because validate_transaction() or calculate_intrinsic_cost() remain
monolithic here.

If you think there is a refactor that would reduce these conflicts, I’m happy to make a followup change here.

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.

Split transactions.py:calculate_intrinsic_cost() into many smaller functions to avoid merge-conflicts due to 8037

2 participants