Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EVM): Simplify ContractDeployer storage layout #1143

Open
wants to merge 1 commit into
base: evm-emulator/fixes
Choose a base branch
from
Open
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
34 changes: 13 additions & 21 deletions system-contracts/contracts/ContractDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,21 @@
contract ContractDeployer is IContractDeployer, SystemContractBase {
/// @dev Prefix for EVM contracts hashes storage slots.
uint256 private constant EVM_HASHES_PREFIX = 1 << 254;
/// @dev keccak256("ALLOWED_BYTECODE_TYPES_MODE_SLOT").
bytes32 private constant ALLOWED_BYTECODE_TYPES_MODE_SLOT =
0xd70708d0b933e26eab552567ce3a8ad69e6fbec9a2a68f16d51bd417a47d9d3b;

/// @notice Information about an account contract.
/// @dev For EOA and simple contracts (i.e. not accounts) this value is 0.
mapping(address => AccountInfo) internal accountInfo;

/// @notice What types of bytecode are allowed to be deployed on this chain.
AllowedBytecodeTypes public allowedBytecodeTypesToDeploy;

modifier onlySelf() {
if (msg.sender != address(this)) {
revert Unauthorized(msg.sender);
}
_;
}

/// @notice Returns what types of bytecode are allowed to be deployed on this chain.
function allowedBytecodeTypesToDeploy() external view returns (AllowedBytecodeTypes mode) {
mode = _getAllowedBytecodeTypesMode();
}

/// @notice Returns keccak of EVM bytecode at address if it is an EVM contract. Returns bytes32(0) if it isn't a EVM contract.
function evmCodeHash(address _address) external view returns (bytes32 _hash) {
_hash = _getEvmCodeHash(_address);
Expand Down Expand Up @@ -181,7 +176,7 @@
function createEVM(bytes calldata _initCode) external payable override onlySystemCall returns (uint256, address) {
uint256 senderNonce;
// If the account is an EOA, use the min nonce. If it's a contract, use deployment nonce
if (msg.sender == tx.origin) {

Check warning on line 179 in system-contracts/contracts/ContractDeployer.sol

View workflow job for this annotation

GitHub Actions / lint

Avoid to use tx.origin

Check warning on line 179 in system-contracts/contracts/ContractDeployer.sol

View workflow job for this annotation

GitHub Actions / lint

Avoid to use tx.origin

Check warning on line 179 in system-contracts/contracts/ContractDeployer.sol

View workflow job for this annotation

GitHub Actions / lint

Avoid to use tx.origin
// Subtract 1 for EOA since the nonce has already been incremented for this transaction
senderNonce = NONCE_HOLDER_SYSTEM_CONTRACT.getMinNonce(msg.sender) - 1;
} else {
Expand Down Expand Up @@ -224,7 +219,7 @@
bytes32 _salt,
bytes32 _evmBytecodeHash
) public onlySystemCallFromEvmEmulator returns (address newAddress) {
if (_getAllowedBytecodeTypesMode() != AllowedBytecodeTypes.EraVmAndEVM) {
if (allowedBytecodeTypesToDeploy != AllowedBytecodeTypes.EraVmAndEVM) {
revert EVMEmulationNotSupported();
}

Expand All @@ -250,7 +245,12 @@
address _newAddress,
bytes calldata _initCode
) external payable onlySystemCallFromEvmEmulator returns (uint256, address) {
uint256 constructorReturnEvmGas = _performDeployOnAddressEVM(msg.sender, _newAddress, AccountAbstractionVersion.None, _initCode);
uint256 constructorReturnEvmGas = _performDeployOnAddressEVM(
msg.sender,
_newAddress,
AccountAbstractionVersion.None,
_initCode
);
return (constructorReturnEvmGas, _newAddress);
}

Expand Down Expand Up @@ -376,10 +376,8 @@
revert InvalidAllowedBytecodeTypesMode();
}

if (uint256(_getAllowedBytecodeTypesMode()) != newAllowedBytecodeTypes) {
assembly {
sstore(ALLOWED_BYTECODE_TYPES_MODE_SLOT, newAllowedBytecodeTypes)
}
if (uint256(allowedBytecodeTypesToDeploy) != newAllowedBytecodeTypes) {
allowedBytecodeTypesToDeploy = AllowedBytecodeTypes(newAllowedBytecodeTypes);

emit AllowedBytecodeTypesModeUpdated(AllowedBytecodeTypes(newAllowedBytecodeTypes));
}
Expand Down Expand Up @@ -417,7 +415,7 @@
address _newAddress,
bytes calldata _initCode
) internal returns (uint256 constructorReturnEvmGas) {
if (_getAllowedBytecodeTypesMode() != AllowedBytecodeTypes.EraVmAndEVM) {
if (allowedBytecodeTypesToDeploy != AllowedBytecodeTypes.EraVmAndEVM) {
revert EVMEmulationNotSupported();
}

Expand Down Expand Up @@ -623,10 +621,4 @@
_hash := sload(or(EVM_HASHES_PREFIX, _address))
}
}

function _getAllowedBytecodeTypesMode() internal view returns (AllowedBytecodeTypes mode) {
assembly {
mode := sload(ALLOWED_BYTECODE_TYPES_MODE_SLOT)
}
}
}
Loading