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

Recompile test contracts with latest Solidity version + large refactor #2797

Merged

Conversation

fselmo
Copy link
Collaborator

@fselmo fselmo commented Jan 27, 2023

I apologize for the beefiness ahead of time but most of this is refactoring to accommodate the new contract sources.


What was wrong?

closes #2301
contributes quite a bit to #2460

How was it fixed?

  • Write a script that compiles test contracts in the Solidity version specified and generates test data for testing.
  • Move all contracts to the web3/_utils/contract_sources folder.
  • Compiled contract data is generated in web3/_utils/contract_sources/contract_data and neatly assigned to constants for testing.
  • Re-compile all contracts using Solidity 0.8.17 (latest at the time of writing).
  • Refactor core test fixtures, eliminate fixtures that were not necessary. Use consistent casing for pytest fixture names (this is a large chunk of the refactor).
  • Re-generate the integration test fixture to account for bytecode changes in integration tests.
  • Add documentation for script and update test docs in general.

I found one improvement along the way due to a failing test:

  • When checking a contract's ABI for whether a function is payable against a transaction that has a non-zero value, we weren't considering the case of "stateMutability": "nonpayable"... we were only checking the payable: {boolean} part of the ABI. This change should clarify what I mean.

Todo:

Cute Animal Picture

20230122_141541

@fselmo fselmo force-pushed the recompile-test-contracts-with-latest-solidity branch from 0da1539 to c918271 Compare January 27, 2023 20:56
- Update test contracts to compile with Solidity v0.8.17
- Refactor some of the logic using test contracts for efficiency
@fselmo fselmo force-pushed the recompile-test-contracts-with-latest-solidity branch from c918271 to 39a02f9 Compare January 27, 2023 21:08
@fselmo fselmo marked this pull request as ready for review January 27, 2023 21:18
@fselmo fselmo changed the title Recompile test contracts with latest solidity Recompile test contracts with latest Solidity version + large refactor Jan 27, 2023
@fselmo fselmo requested review from pacrob and kclowes January 27, 2023 21:37
@fselmo fselmo force-pushed the recompile-test-contracts-with-latest-solidity branch from 39a02f9 to 6267e4a Compare January 27, 2023 21:40
- Create script to compile all test contracts at once, with the exception of the FixedReflector contract since no source can be found for it.
- Re-organize all contracts as ``.sol`` files with defined Solidity contracts - move all to `web3/_utils/contract_sources`.
- Refactor pytest fixtures with appropriate casing since a lot of these were being redefined.
- Remove unnecessary pytest fixtures and import constants where appropriate, in an effort to reduce test times and make code more readable (one single source for a constant in most cases).
- Run the new test contract compiler script for Solidity version 0.8.17.

Integration tests:

- Re-generate the fixture to account for new bytecodes for contracts.

Fixed test errors:

- Validating the ``value`` field for a transaction against the ``payable`` value in the abi needed one more check against the ``stateMutability`` value of ``nonpayable``.
- Clean up contract compiler script
@fselmo fselmo force-pushed the recompile-test-contracts-with-latest-solidity branch from 6267e4a to 9504205 Compare January 27, 2023 21:49
Copy link
Collaborator

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

Thank you for this refactor and for the script! Generally, this looks really good. I left a few nitpicks, and I noticed that in some places, we have fixtures for our constants (for ex. SOME_CONTRACT_BYTECODE) and sometimes we don't - is there a pattern to leaving those that I didn't pick up on? I commented on it in tests/core/contracts/test_contract_deployment.py, but noticed it elsewhere too. Let me know if that doesn't make sense.

@@ -7,10 +7,17 @@
from web3 import (
constants,
)
from web3._utils.contract_sources.contract_data.constructor_contracts import (
CONSTRUCTOR_WITH_ADDRESS_ARGUMENT_CONTRACT_RUNTIME,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: for consistency's sake, do you think it's worth making these constants into fixtures like you did with math_contract_runtime?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The distinction I made was whether or not it's imported in many files as a constant or just one or maybe two test files. I'd be good with making this a session-scoped fixture, or keep it as a constant and reduce some of the clutter in the contracts conftest file. Do you have a preference here? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it. If it's used in just 1-2 files, I think we can leave it!

tests/core/contracts/test_contract_deployment.py Outdated Show resolved Hide resolved
tests/core/contracts/test_contract_transact_interface.py Outdated Show resolved Hide resolved
tests/core/contracts/test_contract_transact_interface.py Outdated Show resolved Hide resolved
@@ -217,8 +219,8 @@ def setup_chain_state(w3):
# Math Contract
#
math_contract_factory = w3.eth.contract(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should standardize this naming convention - sometimes this w3.eth.contractobject is called math_contract_instance and sometimes math_contract_factory. I slightly prefer factory I think, but it's not a strong preference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

factory is present in the integration tests, I'm good with it. I can standardize it 👍🏼

f"# source: web3/_utils/contract_sources/{dot_sol_filename}:{c}"
)
if len(contract_source) > 88:
contract_source += " # noqa: E501"
Copy link
Collaborator

Choose a reason for hiding this comment

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

very clever :)

fselmo added a commit to fselmo/web3.py that referenced this pull request Jan 30, 2023
@fselmo fselmo force-pushed the recompile-test-contracts-with-latest-solidity branch from 56ab08d to e224442 Compare January 30, 2023 21:54
@fselmo fselmo merged commit 256a028 into ethereum:master Jan 30, 2023
@fselmo fselmo deleted the recompile-test-contracts-with-latest-solidity branch April 25, 2023 16:48
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.

enable_strict_bytes_type_checking() broken with solidty>=0.5.0
2 participants