Skip to content

Commit 5a4b19e

Browse files
authored
fix: vm.chainId regression in isolation mode on nightly post revm bump (#10589)
* reapply refactor * reset env * add repro * add isolation test into repro * add previous trace for comparison * nit remove unused event * move ecx as_db_env_and_journal into with_stack and resets as well * revert to use *env directly
1 parent 9663a39 commit 5a4b19e

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

crates/evm/evm/src/inspectors/stack.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ impl InspectorStackRefMut<'_> {
616616
let cached_env = Env::from(ecx.cfg.clone(), ecx.block.clone(), ecx.tx.clone());
617617

618618
ecx.block.basefee = 0;
619+
ecx.tx.chain_id = Some(ecx.cfg.chain_id);
619620
ecx.tx.caller = caller;
620621
ecx.tx.kind = kind;
621622
ecx.tx.data = input;

crates/forge/tests/it/repros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,6 @@ test_repro!(10527);
410410

411411
// https://github.com/foundry-rs/foundry/issues/10552
412412
test_repro!(10552);
413+
414+
// https://github.com/foundry-rs/foundry/issues/10586
415+
test_repro!(10586);

testdata/default/cheats/ChainId.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity ^0.8.18;
44
import "ds-test/test.sol";
55
import "cheats/Vm.sol";
66

7-
contract DealTest is DSTest {
7+
contract ChainIdTest is DSTest {
88
Vm constant vm = Vm(HEVM_ADDRESS);
99

1010
function testChainId() public {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity ^0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "cheats/Vm.sol";
6+
7+
contract Target is DSTest {
8+
Vm constant vm = Vm(HEVM_ADDRESS);
9+
10+
function setChainId() public {
11+
vm.chainId(123);
12+
}
13+
}
14+
15+
contract Issue10586Test is DSTest {
16+
Vm constant vm = Vm(HEVM_ADDRESS);
17+
18+
Target public target;
19+
20+
function setUp() public {
21+
target = new Target();
22+
}
23+
24+
function runTest() internal {
25+
// By default, the chainId is 31337 during testing.
26+
assertEq(block.chainid, 31337);
27+
28+
// Call external function to set the chainId to 123.
29+
target.setChainId();
30+
31+
// The chainId is set to 123 in the block.
32+
assertEq(block.chainid, 123);
33+
34+
// Set the chainId to 100.
35+
vm.chainId(100);
36+
37+
// The chainId is set to 100 in the block.
38+
assertEq(block.chainid, 100);
39+
40+
// Call the external function again, which will set the chainId to 123.
41+
target.setChainId();
42+
43+
// The last call to chainId() will be the one that is set
44+
// in the block, so it should be 123.
45+
assertEq(block.chainid, 123);
46+
}
47+
48+
function testGetChainIdAfterSet() public {
49+
runTest();
50+
}
51+
52+
/// forge-config: default.isolate = true
53+
function testGetChainIdAfterSetIsolated() public {
54+
// Previous test failed with the following error:
55+
//
56+
// [FAIL: EvmError: Revert] testGetChainIdAfterSetIsolated() (gas: 30322)
57+
// Traces:
58+
// [208460] DefaultTestContract::setUp()
59+
// ├─ [170920] → new Target@0xCe71065D4017F316EC606Fe4422e11eB2c47c246
60+
// │ └─ ← [Return] 743 bytes of code
61+
// └─ ← [Stop]
62+
//
63+
// [30322] DefaultTestContract::testGetChainIdAfterSetIsolated()
64+
// ├─ [24037] Target::setChainId()
65+
// │ ├─ [0] VM::chainId(123)
66+
// │ │ └─ ← [Return]
67+
// │ └─ ← [Stop]
68+
// ├─ [0] VM::chainId(100)
69+
// │ └─ ← [Return]
70+
// ├─ [0] Target::setChainId()
71+
// │ └─ ← [Revert] EvmError: Revert
72+
// └─ ← [Revert] EvmError: Revert
73+
//
74+
// Suite result: FAILED. 1 passed; 1 failed; 0 skipped; finished in 2.89ms (1.79ms CPU time)
75+
76+
runTest();
77+
}
78+
}

0 commit comments

Comments
 (0)