Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,18 @@ interface Vm {
#[cheatcode(group = Evm, safety = Unsafe)]
function coolSlot(address target, bytes32 slot) external;

/// Returns the test or script execution evm version.
///
/// **Note:** The execution evm version is not the same as the compilation one.
#[cheatcode(group = Evm, safety = Safe)]
function getEvmVersion() external pure returns (string memory evm);

/// Set the test or script execution evm version.
///
/// **Note:** The execution evm version is not the same as the compilation one.
#[cheatcode(group = Evm, safety = Safe)]
function setEvmVersion(string calldata evm) external;

// -------- Call Manipulation --------
// --- Mocks ---

Expand Down
19 changes: 19 additions & 0 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use foundry_common::{
SlotInfo,
},
};
use foundry_compilers::artifacts::EvmVersion;
use foundry_evm_core::{
ContextExt,
backend::{DatabaseExt, RevertStateSnapshotAction},
Expand All @@ -45,6 +46,7 @@ use std::{

mod record_debug_step;
use foundry_common::fmt::format_token_raw;
use foundry_config::evm_spec_id;
use record_debug_step::{convert_call_trace_to_debug_step, flatten_call_trace};
use serde::Serialize;

Expand Down Expand Up @@ -1103,6 +1105,23 @@ impl Cheatcode for stopAndReturnDebugTraceRecordingCall {
}
}

impl Cheatcode for setEvmVersionCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { evm } = self;
ccx.ecx.cfg.spec = evm_spec_id(
EvmVersion::from_str(evm)
.map_err(|_| Error::from(format!("invalid evm version {evm}")))?,
);
Ok(Default::default())
}
}

impl Cheatcode for getEvmVersionCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
Ok(ccx.ecx.cfg.spec.to_string().to_lowercase().abi_encode())
}
}

pub(super) fn get_nonce(ccx: &mut CheatsCtxt, address: &Address) -> Result {
let account = ccx.ecx.journaled_state.load_account(*address)?;
Ok(account.info.nonce.abi_encode())
Expand Down
90 changes: 89 additions & 1 deletion crates/forge/tests/it/spec.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
//! Integration tests for EVM specifications.

use crate::{config::*, test_helpers::TEST_DATA_PARIS};
use foundry_test_utils::Filter;
use foundry_test_utils::{Filter, forgetest_init, rpc, str};
use revm::primitives::hardfork::SpecId;

#[tokio::test(flavor = "multi_thread")]
async fn test_shanghai_compat() {
let filter = Filter::new("", "ShanghaiCompat", ".*spec");
TestConfig::with_filter(TEST_DATA_PARIS.runner(), filter).spec_id(SpecId::SHANGHAI).run().await;
}

// Test evm version switch during tests / scripts.
// <https://github.com/foundry-rs/foundry/issues/9840>
// <https://github.com/foundry-rs/foundry/issues/6228>
forgetest_init!(test_set_evm_version, |prj, cmd| {
let endpoint = rpc::next_http_archive_rpc_url();
prj.add_test(
"TestEvmVersion.t.sol",
&r#"
import {Test} from "forge-std/Test.sol";

interface EvmVm {
function getEvmVersion() external pure returns (string memory evm);
function setEvmVersion(string calldata evm) external;
}

interface ICreate2Deployer {
function computeAddress(bytes32 salt, bytes32 codeHash) external view returns (address);
}

contract TestEvmVersion is Test {
function test_evm_version() public {
EvmVm evm = EvmVm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));
vm.createSelectFork("<rpc>");

evm.setEvmVersion("istanbul");
evm.getEvmVersion();

// revert with NotActivated for istanbul
vm.expectRevert();
compute();

evm.setEvmVersion("shanghai");
evm.getEvmVersion();
compute();

// switch to Paris, expect revert with NotActivated
evm.setEvmVersion("paris");
vm.expectRevert();
compute();
}

function compute() internal view {
ICreate2Deployer(0x35Da41c476fA5c6De066f20556069096A1F39364).computeAddress(bytes32(0), bytes32(0));
}
}
"#.replace("<rpc>", &endpoint),
);

cmd.args(["test", "--mc", "TestEvmVersion", "-vvvv"]).assert_success().stdout_eq(str![[r#"
[COMPILING_FILES] with [SOLC_VERSION]
[SOLC_VERSION] [ELAPSED]
Compiler run successful!

Ran 1 test for test/TestEvmVersion.t.sol:TestEvmVersion
[PASS] test_evm_version() ([GAS])
Traces:
[..] TestEvmVersion::test_evm_version()
├─ [0] VM::createSelectFork("<rpc url>")
│ └─ ← [Return] 0
├─ [0] VM::setEvmVersion("istanbul")
│ └─ ← [Return]
├─ [0] VM::getEvmVersion() [staticcall]
│ └─ ← [Return] "istanbul"
├─ [0] VM::expectRevert(custom error 0xf4844814)
│ └─ ← [Return]
├─ [..] 0x35Da41c476fA5c6De066f20556069096A1F39364::computeAddress(0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000) [staticcall]
│ └─ ← [NotActivated] EvmError: NotActivated
├─ [0] VM::setEvmVersion("shanghai")
│ └─ ← [Return]
├─ [0] VM::getEvmVersion() [staticcall]
│ └─ ← [Return] "shanghai"
├─ [..] 0x35Da41c476fA5c6De066f20556069096A1F39364::computeAddress(0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000) [staticcall]
│ └─ ← [Return] 0x0f40d7B7669e3a6683EaB25358318fd42a9F2342
├─ [0] VM::setEvmVersion("paris")
│ └─ ← [Return]
├─ [0] VM::expectRevert(custom error 0xf4844814)
│ └─ ← [Return]
├─ [..] 0x35Da41c476fA5c6De066f20556069096A1F39364::computeAddress(0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000) [staticcall]
│ └─ ← [NotActivated] EvmError: NotActivated
└─ ← [Stop]

Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]

Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)

"#]]);
});
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading