Skip to content

Commit

Permalink
feat: add ability to set evm version
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed May 29, 2023
1 parent de82d5f commit f027ce6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/HuffConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/test/HuffConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
37 changes: 37 additions & 0 deletions src/test/HuffDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/test/contracts/EVMVersionCheck.huff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


#define macro MAIN() = {
0x00
}

0 comments on commit f027ce6

Please # to comment.