fix(evm): pin fork block number to prevent state inconsistency#13085
Merged
fix(evm): pin fork block number to prevent state inconsistency#13085
Conversation
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.
mattsse
approved these changes
Jan 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When using
--fork-urlwithout--block-number, forge fetches the latest block to configure the environment. However, thefork_block_numberinEvmOptsremainedNone, causing subsequent fork operations to potentially fetch state from a different (newer) block if the chain advanced between calls.Problem
evm_opts.evm_env()fetches latest block → populatesenv.block_env.numberevm_opts.get_fork()clonesevm_optswithfork_block_number: Nonecreate_fork()callsfork_evm_env_with_provider()again, fetching latest (potentially different) blockSolution
Update
get_fork()to pinfork_block_numberto the block already resolved in the environment when it'sNone. This ensures all fork operations use the same consistent block.Changes
crates/evm/core/src/opts.rs: Modifiedget_fork()to setfork_block_numberfromenv.evm_env.block_env.numberwhen not explicitly providedTesting
Added two tests:
get_fork_pins_block_number_from_env- verifies block number is pinned when not explicitly setget_fork_preserves_explicit_block_number- verifies explicit block numbers aren't overwrittenRun tests:
cargo test -p foundry-evm-core --lib opts::tests