Skip to content
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

feat/pydantic: Use Pydantic library for types #486

Merged
merged 27 commits into from
Apr 4, 2024
Merged

feat/pydantic: Use Pydantic library for types #486

merged 27 commits into from
Apr 4, 2024

Conversation

marioevz
Copy link
Member

@marioevz marioevz commented Mar 26, 2024

🗒️ Description

Simplify code by using Pydantic library.

Affected Files Description

src/ethereum_test_tools/common/base_types.py

A pydantic schema was defined as ToStringSchema which all base types inherit in order to be properly serializable.

src/ethereum_test_tools/common/types.py

The following types have been transformed into pydantic base models:

  • Storage
  • Account
  • Alloc
  • Withdrawal
  • Environment
  • Transaction
  • AccessList

Types Storage and Alloc were defined as RootModel types due to the nature of them containing non-fixed keys in their dictionaries.

Type generics were used to simplify the creation of inherited types where the only change was the number encoding, such as WithdrawalGeneric, EnvironmentGeneric and TransactionGeneric, which have been used to create FixtureWithdrawal, FixtureEnvironment, and FixtureTransaction, respectively.

Many type methods have been refactored into cached properties, such as the transaction's RLP, in order to reduce the number of times this same value has to be computed.

Models TransactionReceipt, RejectedTransaction, Result, TransitionToolOutput, are types that are used to parse the responses from the transition tool.

src/ethereum_test_tools/spec/state/types.py, src/ethereum_test_tools/spec/state/state_test.py

State test types and its fixture types have been refactored to use pydantic models.

src/ethereum_test_tools/spec/blockchain/types.py

Blockchain test types and its fixture types have been refactored to use pydantic models.

TODOs

There is still room for improvement but this can be left for to subsequent PRs:

  • Properly define type schemas (Add examples) and try to use pydantic primitive types to speed up the test generation.
  • Refactor the types into a different library to be able to reuse common types in the evm_transition_tool library.

🔗 Related Issues

None

✅ Checklist

  • All: Set appropriate labels for the changes.
  • All: Considered squashing commits to improve commit history.
  • All: Added an entry to CHANGELOG.md.
  • All: Considered updating the online docs in the ./docs/ directory.
  • Tests: All converted JSON/YML tests from ethereum/tests have been added to converted-ethereum-tests.txt.
  • Tests: Included the type and version of evm t8n tool used to locally execute test cases: e.g., ref with commit hash or geth 1.13.1-stable-3f40e65.
  • Tests: Ran mkdocs serve locally and verified the auto-generated docs for new tests in the Test Case Reference are correctly formatted.

@marioevz marioevz added scope:tools Scope: ethereum_test_tools package scope:pytest Scope: Pytest plugins scope:tests Scope: Test cases type:refactor Type: Refactor type:feat type: Feature scope:fw Scope: Framework (evm|tools|forks|pytest) labels Mar 26, 2024
@marioevz
Copy link
Member Author

1100+ less lines of code overall.

Copy link
Member

@danceratopz danceratopz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it. The code is much more readable. The removal of all the json_encoder defs in types.py is such a joy.

The suggestions below are mainly a few additions to docstrings/comments.

src/ethereum_test_tools/common/types.py Show resolved Hide resolved
tests/shanghai/eip4895_withdrawals/test_withdrawals.py Outdated Show resolved Hide resolved
tests/cancun/eip7516_blobgasfee/test_blobgasfee_opcode.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/__init__.py Show resolved Hide resolved
src/ethereum_test_tools/__init__.py Show resolved Hide resolved
docs/CHANGELOG.md Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/base_types.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
@marioevz marioevz merged commit 4a65686 into main Apr 4, 2024
9 checks passed
@marioevz marioevz deleted the feat/pydantic branch May 3, 2024 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:fw Scope: Framework (evm|tools|forks|pytest) scope:pytest Scope: Pytest plugins scope:tests Scope: Test cases scope:tools Scope: ethereum_test_tools package type:feat type: Feature type:refactor Type: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants