From 944b6c920897d68c5348d499157684a125b2227c Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Wed, 28 Aug 2024 16:09:53 +0200 Subject: [PATCH] Implement evmCodeHash without mapping (no need to keccak key) --- .../contracts/ContractDeployer.sol | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/system-contracts/contracts/ContractDeployer.sol b/system-contracts/contracts/ContractDeployer.sol index ba7a733de..f32e95367 100644 --- a/system-contracts/contracts/ContractDeployer.sol +++ b/system-contracts/contracts/ContractDeployer.sol @@ -33,7 +33,7 @@ contract ContractDeployer is IContractDeployer, ISystemContract { Deployed } - mapping(address => bytes32) public evmCodeHash; + uint256 constant EVM_HASHES_PREFIX = 1 << 254; uint256 public constructorReturnGas; @@ -43,7 +43,7 @@ contract ContractDeployer is IContractDeployer, ISystemContract { uint256 bytecodeLen = uint256(bytes32(paddedNewDeployedCode[:32])); bytes memory trueBytecode = paddedNewDeployedCode[32:32 + bytecodeLen]; - evmCodeHash[msg.sender] = keccak256(trueBytecode); + _setEvmCodeHash(msg.sender, keccak256(trueBytecode)); constructorReturnGas = constructorGasLeft; // ToDO: use efficient call @@ -58,6 +58,10 @@ contract ContractDeployer is IContractDeployer, ISystemContract { _; } + function evmCodeHash(address _address) external view returns (bytes32 _hash) { + _hash = _getEvmCodeHash(_address); + } + /// @notice Returns information about a certain account. function getAccountInfo(address _address) external view returns (AccountInfo memory info) { return accountInfo[_address]; @@ -505,6 +509,20 @@ contract ContractDeployer is IContractDeployer, ISystemContract { } } - require(evmCodeHash[_newAddress] != 0x0, "The code hash must be set after the constructor call"); + require(_getEvmCodeHash(_newAddress) != 0x0, "The code hash must be set after the constructor call"); + } + + function _setEvmCodeHash(address _address, bytes32 _hash) internal { + assembly { + let slot := or(EVM_HASHES_PREFIX, _address) + sstore(slot, _hash) + } + } + + function _getEvmCodeHash(address _address) internal view returns (bytes32 _hash) { + assembly { + let slot := or(EVM_HASHES_PREFIX, _address) + _hash := sload(slot) + } } }