diff --git a/src/HuffConfig.sol b/src/HuffConfig.sol index 37b5081..7b1ae25 100644 --- a/src/HuffConfig.sol +++ b/src/HuffConfig.sol @@ -32,6 +32,9 @@ contract HuffConfig { /// @notice whether to broadcast the deployment tx bool public should_broadcast; + /// @notice supported evm versions + string public evm_version; + /// @notice constant overrides for the current compilation environment Constant[] public const_overrides; @@ -59,6 +62,12 @@ contract HuffConfig { return this; } + /// @notice sets the evm version to compile with + function with_evm_version(string memory _evm_version) public returns (HuffConfig) { + evm_version = _evm_version; + return this; + } + /// @notice sets a constant to a bytes memory value in the current compilation environment /// @dev The `value` string must contain a valid hex number that is <= 32 bytes /// i.e. "0x01", "0xa57b", "0x0de0b6b3a7640000", etc. @@ -124,6 +133,15 @@ contract HuffConfig { return string(str); } + /// @notice Get the evm version string | else return default ("shanghai") + function get_evm_version() public view returns (string memory) { + bytes32 _evm_version = bytes32(bytes(abi.encodePacked(evm_version))); + if (_evm_version == bytes32(0x0)) { + return "shanghai"; + } + return evm_version; + } + /// @notice Get the creation bytecode of a contract function creation_code(string memory file) public payable returns (bytes memory bytecode) { binary_check(); @@ -184,6 +202,8 @@ contract HuffConfig { cmds[0] = "huffc"; cmds[1] = string(string.concat("src/", tempFile, ".huff")); cmds[2] = "-b"; + cmds[3] = "-e"; + cmds[4] = get_evm_version(); /// @notice compile the Huff contract and return the bytecode bytecode = vm.ffi(cmds); diff --git a/src/test/HuffConfig.t.sol b/src/test/HuffConfig.t.sol index f41dc19..03fdb7b 100644 --- a/src/test/HuffConfig.t.sol +++ b/src/test/HuffConfig.t.sol @@ -47,4 +47,9 @@ contract HuffConfigTest is Test { bool b = config.should_broadcast(); assertEq(b, broadcast); } + + function testWithEvmVersion() public { + config.with_evm_version("paris"); + assertEq(config.evm_version(), "paris"); + } } diff --git a/src/test/HuffDeployer.t.sol b/src/test/HuffDeployer.t.sol index 4d3d8ee..d4b919b 100644 --- a/src/test/HuffDeployer.t.sol +++ b/src/test/HuffDeployer.t.sol @@ -229,4 +229,41 @@ contract HuffDeployerTest is Test { runTestConstructorCaller(address(0)); runTestConstructorCaller(address(uint160(0x1000))); } + + + function getBytecode(address contractAddress) view internal returns (bytes memory bytecode) { + uint256 codeSize; + assembly { + codeSize := extcodesize(contractAddress) + } + bytecode = new bytes(codeSize); + assembly { + extcodecopy(contractAddress, add(bytecode, 0x20), 0, codeSize) + } + } + + /// @dev test that compilation is different with new evm versions + function testSettingEVMVersion() public { + /// expected bytecode for EVM version "paris" + bytes memory expectedParis = hex"60028060093d393df36000"; + HuffConfig config = HuffDeployer.config().with_evm_version("paris"); + address withParis = config.deploy("test/contracts/EVMVersionCheck"); + + bytes memory parisBytecode = getBytecode(withParis); + assertEq(parisBytecode, expectedParis); + + /// expected bytecode for EVM version "shanghai" | default + bytes memory expectedShanghai = hex"60018060093d393df35f"; + HuffConfig shanghaiConfig = HuffDeployer.config().with_evm_version("shanghai"); + address withShanghai = config.deploy("test/contracts/EVMVersionCheck"); + bytes memory shanghaiBytecode = getBytecode(withShanghai); + assertEq(shanghaiBytecode, expectedShanghai); + + /// Default should be shanghai (latest) + HuffConfig defaultConfig = HuffDeployer.config().with_evm_version(""); + address withDefault = config.deploy("test/contracts/EVMVersionCheck"); + + bytes memory defaultBytecode = getBytecode(withDefault); + assertEq(defaultBytecode, expectedShanghai); + } } diff --git a/src/test/contracts/EVMVersionCheck.huff b/src/test/contracts/EVMVersionCheck.huff new file mode 100644 index 0000000..960917a --- /dev/null +++ b/src/test/contracts/EVMVersionCheck.huff @@ -0,0 +1,5 @@ + + +#define macro MAIN() = { + 0x00 +} \ No newline at end of file