Skip to content

Commit 8e7efbc

Browse files
authored
fix(forge): set state changes journal in active fork (#11196)
1 parent f5932a6 commit 8e7efbc

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

crates/evm/core/src/backend/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,8 @@ impl DatabaseExt for Backend {
11331133
// selected. This ensures that there are no gaps in depth which would
11341134
// otherwise cause issues with the tracer
11351135
fork.journaled_state.depth = active_journaled_state.depth;
1136+
// Set proper journal of state changes into the fork.
1137+
fork.journaled_state.journal = active_journaled_state.journal.clone();
11361138

11371139
// another edge case where a fork is created and selected during setup with not
11381140
// necessarily the same caller as for the test, however we must always
@@ -1200,6 +1202,8 @@ impl DatabaseExt for Backend {
12001202
active.journaled_state = self.fork_init_journaled_state.clone();
12011203

12021204
active.journaled_state.depth = journaled_state.depth;
1205+
// Set proper journal of state changes into the fork.
1206+
active.journaled_state.journal = journaled_state.journal.clone();
12031207
for addr in persistent_addrs {
12041208
merge_journaled_state_data(addr, journaled_state, &mut active.journaled_state);
12051209
}

crates/forge/tests/cli/test_cmd.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,3 +3938,88 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
39383938
39393939
"#]]);
39403940
});
3941+
3942+
// tests proper reverts in fork mode for contracts with non-existent linked libraries.
3943+
// <https://github.com/foundry-rs/foundry/issues/11185>
3944+
forgetest_init!(can_fork_test_with_non_existent_linked_library, |prj, cmd| {
3945+
prj.update_config(|config| {
3946+
config.libraries =
3947+
vec!["src/Counter.sol:LibCounter:0x530008d2b058137d9c475b1b7d83984f1fcf1dd0".into()];
3948+
});
3949+
prj.add_source(
3950+
"Counter.sol",
3951+
r"
3952+
library LibCounter {
3953+
function dummy() external pure returns (uint) {
3954+
return 1;
3955+
}
3956+
}
3957+
3958+
contract Counter {
3959+
uint256 public number;
3960+
3961+
constructor() {
3962+
LibCounter.dummy();
3963+
}
3964+
3965+
function setNumber(uint256 newNumber) public {
3966+
number = newNumber;
3967+
}
3968+
3969+
function increment() public {
3970+
number++;
3971+
}
3972+
3973+
function dummy() external pure returns (uint) {
3974+
return LibCounter.dummy();
3975+
}
3976+
}
3977+
",
3978+
)
3979+
.unwrap();
3980+
3981+
let endpoint = rpc::next_http_archive_rpc_url();
3982+
3983+
prj.add_test(
3984+
"Counter.t.sol",
3985+
&r#"
3986+
import "forge-std/Test.sol";
3987+
import "src/Counter.sol";
3988+
3989+
contract CounterTest is Test {
3990+
function test_select_fork() public {
3991+
vm.createSelectFork("<url>");
3992+
new Counter();
3993+
}
3994+
3995+
function test_roll_fork() public {
3996+
vm.rollFork(block.number - 100);
3997+
new Counter();
3998+
}
3999+
}
4000+
"#
4001+
.replace("<url>", &endpoint),
4002+
)
4003+
.unwrap();
4004+
4005+
cmd.args(["test", "--fork-url", &endpoint]).assert_failure().stdout_eq(str![[r#"
4006+
[COMPILING_FILES] with [SOLC_VERSION]
4007+
[SOLC_VERSION] [ELAPSED]
4008+
Compiler run successful!
4009+
4010+
Ran 2 tests for test/Counter.t.sol:CounterTest
4011+
[FAIL: EvmError: Revert] test_roll_fork() ([GAS])
4012+
[FAIL: Contract 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f does not exist and is not marked as persistent, see `vm.makePersistent()`] test_select_fork() ([GAS])
4013+
Suite result: FAILED. 0 passed; 2 failed; 0 skipped; [ELAPSED]
4014+
4015+
Ran 1 test suite [ELAPSED]: 0 tests passed, 2 failed, 0 skipped (2 total tests)
4016+
4017+
Failing tests:
4018+
Encountered 2 failing tests in test/Counter.t.sol:CounterTest
4019+
[FAIL: EvmError: Revert] test_roll_fork() ([GAS])
4020+
[FAIL: Contract 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f does not exist and is not marked as persistent, see `vm.makePersistent()`] test_select_fork() ([GAS])
4021+
4022+
Encountered a total of 2 failing tests, 0 tests succeeded
4023+
4024+
"#]]);
4025+
});

0 commit comments

Comments
 (0)