Skip to content

Commit 5310926

Browse files
committed
Update GasManager
1 parent bb5a285 commit 5310926

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

system-contracts/contracts/EvmGasManager.sol

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ uint160 constant PRECOMPILES_END = 0xffff;
1111
// Denotes that passGas has been consumed
1212
uint256 constant INF_PASS_GAS = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
1313

14-
contract EvmGasManager {
15-
// We need strust to use `storage` pointers
16-
struct WarmAccountInfo {
17-
bool isWarm;
18-
}
14+
uint256 constant IS_ACCOUNT_EVM_PREFIX = 1 << 255;
15+
uint256 constant IS_ACCOUNT_WARM_PREFIX = 1 << 254;
16+
uint256 constant IS_SLOT_WARM_PREFIX = 1 << 253;
1917

20-
struct AccountInfo {
21-
bool isEVM;
22-
}
23-
18+
contract EvmGasManager {
2419
struct SlotInfo {
2520
bool warm;
2621
uint256 originalValue;
@@ -34,24 +29,25 @@ contract EvmGasManager {
3429

3530
// The following storage variables are not used anywhere explicitly and are just used to obtain the storage pointers
3631
// to use the transient storage with.
37-
mapping(address => WarmAccountInfo) private warmAccounts;
32+
mapping(address => bool) private warmAccounts;
3833
mapping(address => mapping(uint256 => SlotInfo)) private warmSlots;
3934
EVMStackFrameInfo[] private evmStackFrames;
40-
mapping(address => AccountInfo) private isAccountEVM;
4135

4236
modifier onlySystemEvm() {
4337
// cache use is safe since we do not support SELFDESTRUCT
44-
AccountInfo storage ptr = isAccountEVM[msg.sender];
38+
uint256 slot = IS_ACCOUNT_EVM_PREFIX;
39+
address _sender = msg.sender;
4540
bool isEVM;
4641
assembly {
47-
isEVM := tload(ptr.slot)
42+
slot := add(slot, _sender)
43+
isEVM := tload(slot)
4844
}
4945

5046
if (!isEVM) {
5147
isEVM = ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT.isAccountEVM(msg.sender);
5248
if (isEVM) {
5349
assembly {
54-
tstore(ptr.slot, isEVM)
50+
tstore(slot, isEVM)
5551
}
5652
}
5753
}
@@ -66,44 +62,54 @@ contract EvmGasManager {
6662
function warmAccount(address account) external payable onlySystemEvm returns (bool wasWarm) {
6763
if (uint160(account) < PRECOMPILES_END) return true;
6864

69-
WarmAccountInfo storage ptr = warmAccounts[account];
65+
uint256 slot = IS_ACCOUNT_WARM_PREFIX | uint256(uint160(account));
7066

7167
assembly {
72-
wasWarm := tload(ptr.slot)
68+
wasWarm := tload(slot)
7369
}
7470

7571
if (!wasWarm) {
7672
assembly {
77-
tstore(ptr.slot, 1)
73+
tstore(slot, 1)
7874
}
7975
}
8076
}
8177

8278
function isSlotWarm(uint256 _slot) external view returns (bool isWarm) {
83-
SlotInfo storage ptr = warmSlots[msg.sender][_slot];
79+
uint256 slot = IS_SLOT_WARM_PREFIX | uint256(uint160(msg.sender));
80+
assembly {
81+
mstore(0, slot)
82+
mstore(0x20, _slot)
83+
slot := keccak256(0, 64)
84+
}
8485

8586
assembly {
86-
isWarm := tload(ptr.slot)
87+
isWarm := tload(slot)
8788
}
8889
}
8990

9091
function warmSlot(uint256 _slot, uint256 _currentValue) external payable onlySystemEvm returns (bool isWarm, uint256 originalValue) {
91-
SlotInfo storage ptr = warmSlots[msg.sender][_slot];
92+
uint256 slot = IS_SLOT_WARM_PREFIX | uint256(uint160(msg.sender));
93+
assembly {
94+
mstore(0, slot)
95+
mstore(0x20, _slot)
96+
slot := keccak256(0, 64)
97+
}
9298

9399
assembly {
94-
isWarm := tload(ptr.slot)
100+
isWarm := tload(slot)
95101
}
96102

97103
if (isWarm) {
98104
assembly {
99-
originalValue := tload(add(ptr.slot, 1))
105+
originalValue := tload(add(slot, 1))
100106
}
101107
} else {
102108
originalValue = _currentValue;
103109

104110
assembly {
105-
tstore(ptr.slot, 1)
106-
tstore(add(ptr.slot, 1), originalValue)
111+
tstore(slot, 1)
112+
tstore(add(slot, 1), originalValue)
107113
}
108114
}
109115
}

0 commit comments

Comments
 (0)