Skip to content

regression(cheatcodes): vm.chainId failed to set blockId on nightly with --isolate #10586

@QYuQianchen

Description

@QYuQianchen

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge Version: 1.2.0-nightly Commit SHA: a93c000 Build Timestamp: 2025-05-21T06:02:36.119934000Z (1747807356) Build Profile: maxperf

What version of Foundryup are you on?

foundryup: 1.0.1

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

When a function in a smart contract uses block.chainid in its computation, it expects a different result when the chainId is different. However, after using vm.chainId in the unit test, the function call fails with [Revert] EvmError: Revert
A minimum reproducible example is below:

pragma solidity ^0.8;

abstract contract ChainIdExampleEvents {
    event ChainId(uint256 indexed chainId);
}

contract ChainIdExample is ChainIdExampleEvents{
    function chainIdTest() public {
        emit ChainId(block.chainid);
    }
}

and test file

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.6.0 <0.9.0;

import { Test } from "forge-std/Test.sol";
import { ChainIdExample, ChainIdExampleEvents } from "../src/ChainIdExample.sol";


contract ChainIdExampleTest is Test, ChainIdExampleEvents {
    ChainIdExample public chainIdExample;

    function setUp() public {
        chainIdExample = new ChainIdExample();
    }

    function test_GetChainId() public {
        uint256 chainId = block.chainid;
        vm.expectEmit(true, true, false, false, address(chainIdExample));
        emit ChainId(chainId);
        chainIdExample.chainIdTest();
    }

    function test_GetChainIdAfterSet() public {
        uint256 chainId = block.chainid;
        vm.assertGt(address(chainIdExample).code.length, 0);
        chainIdExample.chainIdTest();
        vm.chainId(100);
        vm.assertGt(address(chainIdExample).code.length, 0);
        chainIdExample.chainIdTest();
    }
}

Run with command forge test -vvvv --gas-report --match-test test_GetChainId --force

The unit test test_GetChainIdAfterSet fails with:

[FAIL: EvmError: Revert] test_GetChainIdAfterSet() (gas: 32160)
Traces:
  [123709] ChainIdExampleTest::setUp()
    ├─ [86261] → new ChainIdExample@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   └─ ← [Return] 152 bytes of code
    └─ ← [Stop]

  [32160] ChainIdExampleTest::test_GetChainIdAfterSet()
    ├─ [0] VM::assertGt(152, 0) [staticcall]
    │   └─ ← [Return]
    ├─ [22334] ChainIdExample::chainIdTest()
    │   ├─ emit ChainId(chainId: 31337 [3.133e4])
    │   └─ ← [Stop]
    ├─ [0] VM::chainId(100)
    │   └─ ← [Return]
    ├─ [0] VM::assertGt(152, 0) [staticcall]
    │   └─ ← [Return]
    ├─ [0] ChainIdExample::chainIdTest()
    │   └─ ← [Revert] EvmError: Revert
    └─ ← [Revert] EvmError: Revert

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions