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

feat: Optimize EvmGasManager further #35

Closed
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
3 changes: 1 addition & 2 deletions system-contracts/contracts/AccountCodeStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ contract AccountCodeStorage is IAccountCodeStorage {
// so set `keccak256("")` as a code hash. The EVM has the same behavior.
else if (Utils.isContractConstructing(codeHash)) {
codeHash = EMPTY_STRING_KECCAK;
}
else if (Utils.isCodeHashEVM(codeHash)) {
} else if (Utils.isCodeHashEVM(codeHash)) {
codeHash = DEPLOYER_SYSTEM_CONTRACT.evmCodeHash(account);
}

Expand Down
124 changes: 66 additions & 58 deletions system-contracts/contracts/EvmGasManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,67 @@

pragma solidity ^0.8.0;

import "./libraries/Utils.sol";
import {Utils} from "./libraries/Utils.sol";

import {ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT} from "./Constants.sol";
import {SystemContractHelper} from "./libraries/SystemContractHelper.sol";
import {SystemCallFlagRequired, CallerMustBeEvmContract} from "./SystemContractErrors.sol";

// We consider all the contracts (including system ones) as warm.
uint160 constant PRECOMPILES_END = 0xffff;

uint256 constant INF_PASS_GAS = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;

// Transient storage prefixes
uint256 constant IS_ACCOUNT_EVM_PREFIX = 1 << 255;
uint256 constant IS_ACCOUNT_WARM_PREFIX = 1 << 254;
uint256 constant IS_SLOT_WARM_PREFIX = 1 << 253;
uint256 constant EVM_STACK_SLOT = 2;

uint256 constant EVM_GAS_SLOT = 4;
uint256 constant EVM_AUX_DATA_SLOT = 5;
uint256 constant EVM_ACTIVE_FRAME_FLAG = 1 << 1;

contract EvmGasManager {
modifier onlySystemEvm() {
if (!SystemContractHelper.isSystemCall()) {
revert SystemCallFlagRequired();
}

// cache use is safe since we do not support SELFDESTRUCT
uint256 transient_slot = IS_ACCOUNT_EVM_PREFIX | uint256(uint160(msg.sender));
uint256 _sender = uint256(uint160(msg.sender));
uint256 transient_slot = IS_ACCOUNT_EVM_PREFIX | _sender;
bool isEVM;
assembly {
isEVM := tload(transient_slot)
}

if (!isEVM) {
bytes32 bytecodeHash = ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT.getRawCodeHash(msg.sender);
isEVM = Utils.isCodeHashEVM(bytecodeHash);
address to = address(ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT);
bytes32 rawCodeHash;
assembly {
// function getRawCodeHash(address _address) public view returns (bytes32 codeHash)
mstore(0, 0x4DE2E46800000000000000000000000000000000000000000000000000000000)
mstore(4, _sender)

let success := staticcall(gas(), to, 0, 36, 0, 32)

if iszero(success) {
// This error should never happen
revert(0, 0)
}

rawCodeHash := mload(0)
}

isEVM = Utils.isCodeHashEVM(rawCodeHash);
if (isEVM) {
if (!Utils.isContractConstructing(bytecodeHash)) {
if (!Utils.isContractConstructing(rawCodeHash)) {
assembly {
tstore(transient_slot, isEVM)
}
}
} else {
revert CallerMustBeEvmContract();
}
}

require(isEVM, "only system evm");
require(SystemContractHelper.isSystemCall(), "This method requires system call flag");
_;
}

Expand All @@ -51,17 +73,17 @@ contract EvmGasManager {
*/
function warmAccount(address account) external payable onlySystemEvm returns (bool wasWarm) {
if (uint160(account) < PRECOMPILES_END) return true;

uint256 transient_slot = IS_ACCOUNT_WARM_PREFIX | uint256(uint160(account));

assembly {
wasWarm := tload(transient_slot)
}

if (!wasWarm) {
assembly {
if iszero(wasWarm) {
tstore(transient_slot, 1)
}

mstore(0x0, wasWarm)
return(0x0, 0x20)
}
}

Expand All @@ -72,10 +94,10 @@ contract EvmGasManager {
mstore(0, prefix)
mstore(0x20, _slot)
transient_slot := keccak256(0, 64)
}

assembly {
isWarm := tload(transient_slot)

mstore(0x0, isWarm)
return(0x0, 0x20)
}
}

Expand All @@ -89,69 +111,55 @@ contract EvmGasManager {
mstore(0, prefix)
mstore(0x20, _slot)
transient_slot := keccak256(0, 64)
}

assembly {
isWarm := tload(transient_slot)
}

if (isWarm) {
assembly {
originalValue := tload(add(transient_slot, 1))
}
} else {
originalValue = _currentValue;

assembly {
switch isWarm
case 0 {
originalValue := _currentValue
tstore(transient_slot, 1)
tstore(add(transient_slot, 1), originalValue)
}
default {
originalValue := tload(add(transient_slot, 1))
}

mstore(0x0, isWarm)
mstore(0x20, originalValue)
return(0x0, 0x40)
}
}

/*

The flow is the following:

When conducting call:
1. caller calls to an EVM contract pushEVMFrame with the corresponding gas
2. callee calls consumeEvmFrame to get the gas and determine if a call is static
3. calleer calls popEVMFrame to remove the frame
2. callee calls consumeEvmFrame to get the gas and determine if a call is static, frame is marked as used
*/

function pushEVMFrame(uint256 passGas, bool isStatic) external onlySystemEvm {
function pushEVMFrame(uint256 passGas, bool isStatic) external payable onlySystemEvm {
assembly {
let stackDepth := add(tload(EVM_STACK_SLOT), 1)
tstore(EVM_STACK_SLOT, stackDepth)
let stackPointer := add(EVM_STACK_SLOT, mul(2, stackDepth))
tstore(stackPointer, passGas)
tstore(add(stackPointer, 1), isStatic)
tstore(EVM_GAS_SLOT, passGas)
tstore(EVM_AUX_DATA_SLOT, or(isStatic, EVM_ACTIVE_FRAME_FLAG))
}
}

function consumeEvmFrame() external onlySystemEvm returns (uint256 passGas, bool isStatic) {
uint256 stackDepth;
function consumeEvmFrame() external payable onlySystemEvm returns (uint256 passGas, uint256 auxDataRes) {
assembly {
stackDepth := tload(EVM_STACK_SLOT)
}
if (stackDepth == 0) return (INF_PASS_GAS, false);
let auxData := tload(EVM_AUX_DATA_SLOT)
let isFrameActive := and(auxData, EVM_ACTIVE_FRAME_FLAG)

assembly {
let stackPointer := add(EVM_STACK_SLOT, mul(2, stackDepth))
passGas := tload(stackPointer)
isStatic := tload(add(stackPointer, 1))
tstore(stackPointer, INF_PASS_GAS) // mark as consumed
}
}
if isFrameActive {
passGas := tload(EVM_GAS_SLOT)
auxDataRes := auxData

function popEVMFrame() external onlySystemEvm {
uint256 stackDepth;
assembly {
stackDepth := tload(EVM_STACK_SLOT)
}
require(stackDepth != 0);
assembly {
tstore(EVM_STACK_SLOT, sub(stackDepth, 1))
tstore(EVM_AUX_DATA_SLOT, 0) // mark as consumed
}

mstore(0x0, passGas)
mstore(0x20, auxDataRes)
return(0x0, 0x40)
}
}
}
Loading