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(fw,tests): Add pre.deploy_contract, pre.fund_eoa methods to write tests #584

Merged
merged 50 commits into from
Jun 12, 2024

Conversation

marioevz
Copy link
Member

@marioevz marioevz commented May 31, 2024

🗒️ Description

This PR adds three methods to the Alloc class that allow more versatility and control over the produced tests, by allowing the tests to be written in a way that the required test contracts deploy address is not known ahead of time.

This is done by creating a pre fixture in the test filler that is added to each test during the fill process.

The test needs to add it as a parameter in the following way:

def test_chainid(state_test: StateTestFiller, pre: Alloc):
    ...

During the test filling process, a new Alloc object is created and passed to the test.

Then, to "request" a contract to be placed in the pre-state, the test needs to call pre.deploy_contract function:

contract_address = pre.deploy_contract(Op.SSTORE(1, Op.CHAINID) + Op.STOP)

And the result of the call is the address of deployment.

Similarly, the test does not assume that the transactions are all sent from the same TestAddress and instead requests for a new EOA:

sender = pre.fund_eoa(1_000_000_000_000_000_000_000)

The returned object contains the private key of a funded account, with the requested funding.

Also, when defining the transaction(s) to be sent during the test, the test does no longer need to specify the nonce, since the sender object contains the starting nonce, and the value is automatically increased when the sender is used as a parameter to create a tx:

tx = Transaction(
        ty=0x0,
        chain_id=0x01,
        to=contract_address,
        gas_limit=100000000,
        gas_price=10,
        sender=sender,
    )

Currently deploy_contract accepts both nonce and address parameters, just for the purpose of verifiying that the updated tests have a 1-to-1 fixture outputs. The merged tests though should not use these parameters.

A new parameter --strict-alloc-mode is introduced in the fill parameter that, when enabled, if a test uses either of these parameters, the test filling procedure fails.

This PR updates all tests in the following folders:

  • tests/homestead
  • tests/berlin
  • tests/byzantium
  • tests/constantinople
  • tests/frontier
  • tests/istanbul
  • tests/prague

And up to commit 2aad395 the fixtures produce an exact fixture output as the main branch, but the address and nonce workarounds are removed in commit b267d16 and can be filled using the --strict-alloc-mode parameter.

🔗 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:tests Scope: Test cases scope:docs Scope: Documentation type:refactor Type: Refactor type:feat type: Feature scope:fw Scope: Framework (evm|tools|forks|pytest) labels May 31, 2024
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
src/pytest_plugins/test_filler/test_filler.py Outdated Show resolved Hide resolved
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'm really loving this and where it's heading.

Regarding the comments below, got a feeling I might have missed the mark about Sender semantics below?

Would def like to spend more time with this code.

src/pytest_plugins/test_filler/test_filler.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
@marioevz marioevz changed the title feat(fw,tests): Add pre.deploy_contract, pre.fund_sender methods to write tests feat(fw,tests): Add pre.deploy_contract, pre.fund_eoa methods to write tests Jun 11, 2024
Copy link
Collaborator

@spencer-tb spencer-tb left a comment

Choose a reason for hiding this comment

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

Extra comments after updating Shanghai tests respectively.

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.

LGTM!

src/ethereum_test_tools/common/types.py Outdated Show resolved Hide resolved
@marioevz marioevz merged commit ecf9aca into main Jun 12, 2024
9 checks passed
@marioevz marioevz deleted the fixture-pre branch June 12, 2024 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:docs Scope: Documentation scope:fw Scope: Framework (evm|tools|forks|pytest) scope:tests Scope: Test cases type:feat type: Feature type:refactor Type: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants