Skip to content

Comments

fix(evm): pin fork block number to prevent state inconsistency#13085

Merged
mattsse merged 1 commit intomasterfrom
gdzzy/fix-fork-block-consistency
Jan 14, 2026
Merged

fix(evm): pin fork block number to prevent state inconsistency#13085
mattsse merged 1 commit intomasterfrom
gdzzy/fix-fork-block-consistency

Conversation

@grandizzy
Copy link
Collaborator

Summary

When using --fork-url without --block-number, forge fetches the latest block to configure the environment. However, the fork_block_number in EvmOpts remained None, causing subsequent fork operations to potentially fetch state from a different (newer) block if the chain advanced between calls.

Problem

  1. evm_opts.evm_env() fetches latest block → populates env.block_env.number
  2. evm_opts.get_fork() clones evm_opts with fork_block_number: None
  3. Later, create_fork() calls fork_evm_env_with_provider() again, fetching latest (potentially different) block
  4. State fetches could occur at inconsistent blocks

Solution

Update get_fork() to pin fork_block_number to the block already resolved in the environment when it's None. This ensures all fork operations use the same consistent block.

Changes

  • crates/evm/core/src/opts.rs: Modified get_fork() to set fork_block_number from env.evm_env.block_env.number when not explicitly provided

Testing

Added two tests:

  • get_fork_pins_block_number_from_env - verifies block number is pinned when not explicitly set
  • get_fork_preserves_explicit_block_number - verifies explicit block numbers aren't overwritten

Run tests:

cargo test -p foundry-evm-core --lib opts::tests

When using --fork-url without --block-number, forge fetches the latest
block to configure the environment. However, the fork_block_number in
EvmOpts remained None, causing subsequent fork operations to potentially
fetch state from a different (newer) block if the chain advanced.

This fix updates get_fork() to pin fork_block_number to the block that
was already resolved in the environment, ensuring all fork operations
use the same consistent block.
@grandizzy grandizzy marked this pull request as ready for review January 14, 2026 16:26
@mattsse mattsse added this pull request to the merge queue Jan 14, 2026
Merged via the queue into master with commit c9d4110 Jan 14, 2026
16 checks passed
@mattsse mattsse deleted the gdzzy/fix-fork-block-consistency branch January 14, 2026 19:30
@github-project-automation github-project-automation bot moved this to Done in Foundry Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants