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: precompile Solidity testing in pure Go #1234

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

ARR4N
Copy link
Contributor

@ARR4N ARR4N commented Jul 12, 2024

Why this should be merged

Prerequisite for #1228. Also paves the way for pure-Solidity (similar to Forge) testing of contracts intended to be deployed with custom precompiles.

How this works

  1. The new evmsim package wraps geth's SimulatedBackend to provide a real EVM with an interface allowing use with contract bindings generated with abigen. It provides some additional convenience functions that make using the simulator easier. Most importantly, enabling individual precompiles is trivial.

  2. The new dstest parses transaction receipts to find events emitted by Solidity-based tests inheriting from DSTest; the existing precompile tests use this, as do Forge tests. All failing DSTest assertions emit such events.

  3. The existing Solidity contracts for testing the allowlist precompile are run through solc | abigen to produce Go bindings that are used with evmsim to mirror the behaviour of the equivalent Hardhat test. Failures are detected (and reported) with the dstest package. Unlike the Hardhat tests, these are run in the same process and on the subnet-evm VM implementation.

Tip

Review the code in the order described above: testing/evmsim, then testing/dstest, then contracts/*_test.go. Apologies for adding so much functionality in one PR! It started as (3) but that necessitated (2) and then (1).

How this was tested

Unit testing of dstest against an evmsim backend running a Solidity contract that emits DSTest events. The allowlist tests were also deliberately broken (and then reverted) to confirm that the Go tests fail.

How is this documented

Full godoc commentary.

TODO Before review

  • Abstract //go:generate solc | abigen into script
  • Pre-merge check that generated files are up to date
  • Move precompile configuration into evmsim.New()

return fmt.Errorf("abigen: %w", err)
}

re := regexp.MustCompile(`github.com/ethereum/go-ethereum/(accounts|core)/`)

Check failure

Code scanning / CodeQL

Missing regular expression anchor High

When this is used as a regular expression on a URL, it may match anywhere, and arbitrary hosts may come before or after it.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return fmt.Errorf("abigen: %w", err)
}

re := regexp.MustCompile(`github.com/ethereum/go-ethereum/(accounts|core)/`)

Check failure

Code scanning / CodeQL

Incomplete regular expression for hostnames High

This regular expression has an unescaped dot before 'com', so it might match more hosts than expected when
the regular expression is used
.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

ARR4N added 12 commits July 16, 2024 15:40
…le `contract Empty`

Repeated runs of `go generate ./contracts/...` could result in different outputs. I suspect that it's because of a Solidity bug (they call it a feature though) that appends a hash of the complete source file from which a contract was compiled. In this case it would have been from the different context in which the otherwise same `Example` contracts were defined; in the worst case a single-character change in a comment results in different addresses (i.e. a bug) `</rant>`.
@ARR4N ARR4N marked this pull request as ready for review July 17, 2024 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress 🏗
Development

Successfully merging this pull request may close these issues.

2 participants