Skip to content

Conversation

s3bc40
Copy link

@s3bc40 s3bc40 commented Jul 2, 2025

Related issue: #45

What I did

The main goal was to kill the remaining anvil-zksync process, since they were not garbage collected. But I ended up adapting boa-zksync to the latest anvil-zksync and reviewed the fork system.

Key changes include the refactoring of environment setup functions, enhancements to the AnvilZKsync class for managing test nodes (lifecycle), and updates to dependencies and test configurations.

I tried at first to go for a context manager method inside AnvilZKsync but it was not adapted to our need, and it would have brought too many changes in other tools like Moccasin.

How I did it

Improvements to AnvilZKsync test node management:

  • Introduced a global WeakSet to track active AnvilZKsync instances and ensure proper cleanup using an atexit handler. (boa_zksync/node.py, boa_zksync/node.pyR1-R31)
  • Added dynamic port allocation logic to support parallel testing with pytest-xdist and fallback to a suggested port when available. (boa_zksync/node.py, boa_zksync/node.pyR76-R221)
  • Enhanced the node startup process with a _build_command method for constructing commands and improved error handling during node shutdown. (boa_zksync/node.py, boa_zksync/node.pyR76-R221)

Enhancements to zkSync environment setup:

  • Refactored functions like set_zksync_env, set_zksync_test_env, and set_zksync_fork to improve clarity and ensure consistent environment initialization. Added detailed docstrings for better documentation. (boa_zksync/__init__.py, boa_zksync/init.pyR12-R48)
  • Deprecated the use of node_args in set_zksync_fork and updated the default behavior for block_identifier. (tests/conftest.py, tests/conftest.pyL33-R35)

Updates to forking and RPC handling:

  • Modified the fork and fork_rpc methods to improve handling of RPC connections, ensure proper cleanup of existing nodes, and support new configurations. (boa_zksync/environment.py, [1] [2]
  • Added a utility function is_port_free to check port availability, improving the robustness of test node setup. (boa_zksync/util.py, boa_zksync/util.pyR23-R47)

Dependency and configuration updates:

  • Updated pyproject.toml temporarily since zkvyper does not support vyper>=0.4.2 (I'll go handle this).

Test updates:

  • Adjusted test configurations to align with the refactored environment setup, including changes to the zksync_sepolia_fork fixture. (tests/conftest.py, tests/conftest.pyL33-R35)
  • Updated call trace assertions in test_boa_loads.py to reflect changes in error messages and contract behavior. (tests/test_boa_loads.py, tests/test_boa_loads.pyL134-R151)

How to verify it

Run make test and see it all pass. You should have the latest anvil-zksync, and it should pass with the latest blocks.
You can also run make lint to be sure that the code complies.

Description for the changelog

  • Enhanced AnvilZKsync Management: implemented automatic cleanup of test nodes, enabled dynamic port allocation for parallel testing, and improved overall node startup/shutdown robustness.

  • Streamlined zkSync Environment: refactored environment setup functions (set_zksync_env, set_zksync_fork, etc.) for clearer usage and updated forking defaults.

  • Improved Forking Logic: enhanced fork and fork_rpc methods for better RPC connection handling and more reliable node setup.

  • Dependency & Test Adjustments: included a temporary dependency fix for zkvyper and updated test configurations/assertions to match the new system.

Cute Animal Picture

image

s3bc40 added 5 commits July 1, 2025 19:30
- Removed deprecated anvil-zksync arguments
- Updated test to use "safe" block_identifier by default
- Updated environment.py to use new fork_rpc method
- Updated node.py to handle new fork mode arguments
- Ensured compatibility with latest anvil-zksync changes

Now anvil-zksync can be shut down gracefully without leaving zombie processes.
And the tests are updated to reflect the new behavior of the fork method.

Everything has been adapted to pytest xdist workers with detection of env variable to run tests in parallel, without port conflicts.

Suggested anvil port is now by default 8011 to allow adding the network to MetaMask.
If 8011 is already in use, then we apply free port logic.
Copy link

socket-security bot commented Jul 2, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedvyper@​0.4.3 ⏵ 0.4.2rc199100100100100

View full report

"<unknown>).get_name_of(address) -> ['string'])",
" <Unknown contract 0x0000000000000000000000000000000000008009>",
" <Unknown contract 0x000000000000000000000000000000000000800b>",
# " <Unknown contract 0x000000000000000000000000000000000000800b>",
Copy link
Author

Choose a reason for hiding this comment

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

I let this here, but maybe we can remove it if we follow along with the latest?

Comment on lines +85 to +86
# @dev deprecated in boa, use AnvilZKsync fork mode
# return super().fork(url, reset_traces, block_identifier, debug, **kwargs)
Copy link
Author

Choose a reason for hiding this comment

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

I can remove it after the review

Suggested change
# @dev deprecated in boa, use AnvilZKsync fork mode
# return super().fork(url, reset_traces, block_identifier, debug, **kwargs)

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.

1 participant