Skip to content

[Flow EVM] Add bootstrap option to enable the EVM testing helpers#8487

Open
m-Peter wants to merge 1 commit intomasterfrom
mpeter/evm-testing-helpers-bootstrap-setting
Open

[Flow EVM] Add bootstrap option to enable the EVM testing helpers#8487
m-Peter wants to merge 1 commit intomasterfrom
mpeter/evm-testing-helpers-bootstrap-setting

Conversation

@m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Mar 13, 2026

Follow up to #8391

The current way of injecting the EVM testing helpers, works when the Cadence testing framework is run with Flow Emulator, but not against forked networks (testnet/mainnet). That's because these forked networks are bootstrapped with their appropriate chain ID, and this makes the existing defensive check to fail.

To overcome that, a new BootstrapProcedureOption is added, namely WithEVMTestHelpersEnabled. This achieves the same defensive check, and allows 3rd party tools to control whether the EVM testing helpers should be enabled or not.

Summary by CodeRabbit

  • New Features

    • Added ability to enable or disable EVM test helper functions via bootstrap configuration options.
  • Bug Fixes

    • Removed network restrictions from contract handler operations, allowing SetState, GetState, and RunTxAs to function on all network types instead of emulator-only.

@m-Peter m-Peter self-assigned this Mar 13, 2026
@m-Peter m-Peter requested a review from a team as a code owner March 13, 2026 14:31
@github-actions
Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 37.50000% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
fvm/bootstrap.go 20.00% 4 Missing ⚠️
utils/unittest/execution_state.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 13, 2026

📝 Walkthrough

Walkthrough

This PR introduces configuration-driven EVM test helper injection by adding an explicit evmTestHelpersEnabled boolean flag to bootstrap procedures, replacing chain ID-based detection. It removes emulator-only runtime guards, refactors handler tests to use Emulator exclusively, and updates the contract code generation signature accordingly.

Changes

Cohort / File(s) Summary
Bootstrap Configuration
fvm/bootstrap.go
Adds evmTestHelpersEnabled field to BootstrapParams and introduces WithEVMTestHelpersEnabled() option function to configure EVM test helper injection at bootstrap time.
EVM Test Helpers
fvm/evm/evm_test.go
Adds variadic bootstrapOpts parameter to RunWithNewEnvironment and RunContractWithNewEnvironment functions; updates numerous test invocations to pass WithEVMTestHelpersEnabled for selective test helper enablement.
Handler Implementation
fvm/evm/handler/handler.go
Removes emulator-only runtime checks (panic guards) from SetState, GetState, and RunTxAs methods, allowing these operations across all networks.
Handler Test Refactoring
fvm/evm/handler/handler_test.go
Consolidates test setup to use Emulator-based flows exclusively; updates NewBlockStore constructor calls to pass flow.Emulator instead of chain IDs; simplifies test harness structure.
Contract Code Generation
fvm/evm/stdlib/contract.go, contract_test.go, type.go
Removes chainID parameter from ContractCode function signature; replaces emulator chain ID check with explicit evmTestHelpersEnabled boolean flag to control test helper injection.
Error Types
fvm/evm/types/errors.go
Removes public error variable ErrUnsupportedNetworkOperation from exported error pool.
Test Execution State
utils/unittest/execution_state.go
Updates default genesis state commitment hex for unknown chain IDs from 29a8579af083bf1044232edec7c58d26e577fe34c064db738e69250d8d3e828e to 6f96de6f0bc152c1d03383f220b62c0c0edc1aa11da57b6db3d62807a0bdde0d.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Feature

Suggested reviewers

  • zhangchiqing
  • turbolent
  • holyfuchs
  • fxamacker

Poem

🐰 A flag hops in, the chains set free,
Test helpers now where they should be,
No more guards that block the way,
Bootstrap options light the day! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a bootstrap option to enable EVM testing helpers, which directly aligns with the PR's core objective.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mpeter/evm-testing-helpers-bootstrap-setting
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can scan for known vulnerabilities in your dependencies using OSV Scanner.

OSV Scanner will automatically detect and report security vulnerabilities in your project's dependencies. No additional configuration is required.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
fvm/evm/stdlib/contract_test.go (1)

354-359: Consider adding one complementary evmTestHelpersEnabled=false deployment case.

Line 358 now always enables helpers in the shared deploy path. A focused disabled-path test would keep toggle behavior fully covered.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@fvm/evm/stdlib/contract_test.go` around lines 354 - 359, The shared deploy
path currently always enables helpers by calling stdlib.ContractCode(..., true);
add a complementary test case that deploys the same contract using
evmTestHelpersEnabled=false to exercise the disabled helpers path. Locate the
test that builds/deploys via stdlib.ContractCode (the call with the three
contractsAddress args) and add a parallel subtest or table entry that passes
false instead of true, ensuring the deployment, assertions, and teardown mirror
the existing enabled case so toggle behavior is fully covered.
fvm/evm/evm_test.go (1)

312-313: Add a forked-network regression case.

These new helper-gating checks still bootstrap flow.Emulator.Chain(), so they never exercise the path that was previously broken on forked networks. A single flow.Testnet.Chain() or flow.Mainnet.Chain() case with WithEVMTestHelpersEnabled(cadence.NewBool(true)) would lock down the actual fix instead of only re-testing emulator behavior.

Also applies to: 316-387, 484-486, 489-648

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@fvm/evm/evm_test.go` around lines 312 - 313, The tests currently only
exercise the emulator chain; add at least one forked-network regression case
that uses flow.Testnet.Chain() or flow.Mainnet.Chain() together with the
existing helper flag (WithEVMTestHelpersEnabled(cadence.NewBool(true))) to
ensure the broken path on forked networks is covered; update the test
table/cases around the EVM test helper sections (the blocks invoking
flow.Emulator.Chain() and the test entries at the ranges mentioned) to include a
parallel case that swaps Chain() to flow.Testnet.Chain() (or
flow.Mainnet.Chain()) while keeping the same setup and assertions so the fix is
validated on a forked network.
fvm/evm/handler/handler_test.go (1)

1300-1304: Propagate this chain fix into SetupHandler too.

These tests now align NewBlockStore(flow.Emulator, ...) with NewContractHandler(flow.Emulator, ...), but SetupHandler at Line 1415 still hard-codes defaultChainID. That leaves most of the integrated tests in this file on the old mixed emulator/testnet setup and can hide block-store regressions.

♻️ Suggested follow-up
 func SetupHandler(t testing.TB, backend types.Backend, rootAddr flow.Address) *handler.ContractHandler {
 	return handler.NewContractHandler(
 		flow.Emulator,
 		rootAddr,
 		flowTokenAddress,
 		rootAddr,
-		handler.NewBlockStore(defaultChainID, backend, rootAddr),
+		handler.NewBlockStore(flow.Emulator, backend, rootAddr),
 		handler.NewAddressAllocator(),
 		backend,
 		emulator.NewEmulator(backend, rootAddr),
 	)
 }

Also applies to: 1322-1326, 1354-1373

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@fvm/evm/handler/handler_test.go` around lines 1300 - 1304, Tests were updated
to construct the block store and contract handler with flow.Emulator
(handler.NewBlockStore(flow.Emulator, ...) and
handler.NewContractHandler(flow.Emulator, ...)) but SetupHandler still
hard-codes defaultChainID, leaving tests using a mixed chain; update
SetupHandler to accept or derive the chain parameter and pass flow.Emulator into
the same calls (use handler.NewBlockStore(chain, ...) and
handler.NewContractHandler(chain, ...)) instead of defaultChainID so the handler
setup matches the test constructors (search for SetupHandler, defaultChainID,
NewBlockStore and NewContractHandler to locate the spots to change).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@fvm/bootstrap.go`:
- Line 89: Add unit tests that cover the new evmTestHelpersEnabled bootstrap
flag: write one test that constructs the bootstrap with default options and
asserts evmTestHelpersEnabled is false, and a second test that passes the
explicit option/enabler (the same option used where evmTestHelpersEnabled is
set) and asserts it becomes true; locate the code paths around the
evmTestHelpersEnabled field and the bootstrap constructor/option function (the
option setter used in the diff) and exercise both branches (default and explicit
true) to ensure patch coverage for the flag.

In `@utils/unittest/execution_state.go`:
- Line 95: Add a unit test that exercises the fallback branch of the function
that returns the default commitment (the branch that currently returns
"6f96de6f0bc152c1d03383f220b62c0c0edc1aa11da57b6db3d62807a0bdde0d"): call that
function with a chain ID that is not Mainnet, Testnet, or Sandboxnet (an “other”
chain ID) and assert the returned string equals the hex literal; place the test
alongside existing execution_state tests in the same package and use the same
test helpers/setup so this branch is covered by patch-level unit tests.

---

Nitpick comments:
In `@fvm/evm/evm_test.go`:
- Around line 312-313: The tests currently only exercise the emulator chain; add
at least one forked-network regression case that uses flow.Testnet.Chain() or
flow.Mainnet.Chain() together with the existing helper flag
(WithEVMTestHelpersEnabled(cadence.NewBool(true))) to ensure the broken path on
forked networks is covered; update the test table/cases around the EVM test
helper sections (the blocks invoking flow.Emulator.Chain() and the test entries
at the ranges mentioned) to include a parallel case that swaps Chain() to
flow.Testnet.Chain() (or flow.Mainnet.Chain()) while keeping the same setup and
assertions so the fix is validated on a forked network.

In `@fvm/evm/handler/handler_test.go`:
- Around line 1300-1304: Tests were updated to construct the block store and
contract handler with flow.Emulator (handler.NewBlockStore(flow.Emulator, ...)
and handler.NewContractHandler(flow.Emulator, ...)) but SetupHandler still
hard-codes defaultChainID, leaving tests using a mixed chain; update
SetupHandler to accept or derive the chain parameter and pass flow.Emulator into
the same calls (use handler.NewBlockStore(chain, ...) and
handler.NewContractHandler(chain, ...)) instead of defaultChainID so the handler
setup matches the test constructors (search for SetupHandler, defaultChainID,
NewBlockStore and NewContractHandler to locate the spots to change).

In `@fvm/evm/stdlib/contract_test.go`:
- Around line 354-359: The shared deploy path currently always enables helpers
by calling stdlib.ContractCode(..., true); add a complementary test case that
deploys the same contract using evmTestHelpersEnabled=false to exercise the
disabled helpers path. Locate the test that builds/deploys via
stdlib.ContractCode (the call with the three contractsAddress args) and add a
parallel subtest or table entry that passes false instead of true, ensuring the
deployment, assertions, and teardown mirror the existing enabled case so toggle
behavior is fully covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0b52bba4-9d52-472e-8132-1ab2a2ce48cc

📥 Commits

Reviewing files that changed from the base of the PR and between 076aa90 and 851ee82.

📒 Files selected for processing (9)
  • fvm/bootstrap.go
  • fvm/evm/evm_test.go
  • fvm/evm/handler/handler.go
  • fvm/evm/handler/handler_test.go
  • fvm/evm/stdlib/contract.go
  • fvm/evm/stdlib/contract_test.go
  • fvm/evm/stdlib/type.go
  • fvm/evm/types/errors.go
  • utils/unittest/execution_state.go
💤 Files with no reviewable changes (2)
  • fvm/evm/types/errors.go
  • fvm/evm/handler/handler.go

@jribbink
Copy link
Contributor

jribbink commented Mar 13, 2026

It worked for me with a local CLI build, added fvm.WithEVMTestHelpersEnabled to the emulator and ran against FYV fork tests.

jribbink@Jordans-MacBook-Pro FlowVaults-sc % ../flow-cli/cmd/flow/flow test cadence/tests/forked_rebalance_scenario1_test.cdc

// ...

Test results: "cadence/tests/forked_rebalance_scenario1_test.cdc"
- PASS: test_ForkedRebalanceYieldVaultScenario1


jribbink@Jordans-MacBook-Pro FlowVaults-sc % ../flow-cli/cmd/flow/flow version -v                                           
Version: v2.15.1

Flow Package Dependencies 
github.com/onflow/atree v0.12.1
github.com/onflow/cadence v1.9.10
github.com/onflow/cadence-tools/languageserver v1.9.4
github.com/onflow/cadence-tools/lint v1.7.5
github.com/onflow/cadence-tools/test v1.11.1
github.com/onflow/crypto v0.25.4
github.com/onflow/fcl-dev-wallet v0.9.1
github.com/onflow/fixed-point v0.1.1
github.com/onflow/flixkit-go/v2 v2.7.4
github.com/onflow/flow-core-contracts/lib/go/contracts v1.9.3
github.com/onflow/flow-core-contracts/lib/go/templates v1.9.3
github.com/onflow/flow-emulator v1.17.0
github.com/onflow/flow-evm-bridge v0.1.0
github.com/onflow/flow-evm-gateway v1.4.8-0.20260311083540-75525b282115
github.com/onflow/flow-ft/lib/go/contracts v1.0.1
github.com/onflow/flow-ft/lib/go/templates v1.0.1
github.com/onflow/flow-go v0.47.0-ledger-service.1.0.20260313142257-851ee8283f60
github.com/onflow/flow-go-sdk v1.9.16
github.com/onflow/flow-nft/lib/go/contracts v1.3.0
github.com/onflow/flow-nft/lib/go/templates v1.3.0
github.com/onflow/flow/protobuf/go/flow v0.4.20
github.com/onflow/flowkit/v2 v2.11.1
github.com/onflow/go-ethereum v1.15.10
github.com/onflow/nft-storefront/lib/go/contracts v1.0.0
github.com/onflow/sdks v0.6.0-preview.1
github.com/onflow/wal v1.0.2

jribbink@Jordans-MacBook-Pro FlowVaults-sc %

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants