github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/core/vm/operations_acl.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "errors" 21 22 "github.com/ethereum-optimism/optimism/l2geth/common" 23 "github.com/ethereum-optimism/optimism/l2geth/common/math" 24 "github.com/ethereum-optimism/optimism/l2geth/params" 25 ) 26 27 func makeGasSStoreFunc(clearingRefund uint64) gasFunc { 28 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 29 // If we fail the minimum gas availability invariant, fail (0) 30 if contract.Gas <= params.SstoreSentryGasEIP2200 { 31 return 0, errors.New("not enough gas for reentrancy sentry") 32 } 33 // Gas sentry honoured, do the actual gas calculation based on the stored value 34 var ( 35 y, x = stack.Back(1), stack.peek() 36 slot = common.BigToHash(x) 37 current = evm.StateDB.GetState(contract.Address(), slot) 38 cost = uint64(0) 39 ) 40 // Check slot presence in the access list 41 if addrPresent, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent { 42 cost = params.ColdSloadCostEIP2929 43 // If the caller cannot afford the cost, this change will be rolled back 44 evm.StateDB.AddSlotToAccessList(contract.Address(), slot) 45 if !addrPresent { 46 // Once we're done with YOLOv2 and schedule this for mainnet, might 47 // be good to remove this panic here, which is just really a 48 // canary to have during testing 49 panic("impossible case: address was not present in access list during sstore op") 50 } 51 } 52 value := common.BigToHash(y) 53 54 if current == value { // noop (1) 55 // EIP 2200 original clause: 56 // return params.SloadGasEIP2200, nil 57 return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS 58 } 59 original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x)) 60 if original == current { 61 if original == (common.Hash{}) { // create slot (2.1.1) 62 return cost + params.SstoreSetGasEIP2200, nil 63 } 64 if value == (common.Hash{}) { // delete slot (2.1.2b) 65 evm.StateDB.AddRefund(clearingRefund) 66 } 67 // EIP-2200 original clause: 68 // return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) 69 return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929), nil // write existing slot (2.1.2) 70 } 71 if original != (common.Hash{}) { 72 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 73 evm.StateDB.SubRefund(clearingRefund) 74 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 75 evm.StateDB.AddRefund(clearingRefund) 76 } 77 } 78 if original == value { 79 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 80 // EIP 2200 Original clause: 81 //evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200) 82 evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929) 83 } else { // reset to original existing slot (2.2.2.2) 84 // EIP 2200 Original clause: 85 // evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) 86 // - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST) 87 // - SLOAD_GAS redefined as WARM_STORAGE_READ_COST 88 // Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST 89 evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929) 90 } 91 } 92 // EIP-2200 original clause: 93 //return params.SloadGasEIP2200, nil // dirty update (2.2) 94 return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2) 95 } 96 } 97 98 // gasSLoadEIP2929 calculates dynamic gas for SLOAD according to EIP-2929 99 // For SLOAD, if the (address, storage_key) pair (where address is the address of the contract 100 // whose storage is being read) is not yet in accessed_storage_keys, 101 // charge 2100 gas and add the pair to accessed_storage_keys. 102 // If the pair is already in accessed_storage_keys, charge 100 gas. 103 func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 104 loc := stack.peek() 105 slot := common.BigToHash(loc) 106 // Check slot presence in the access list 107 if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent { 108 // When it fails, this returns false 109 // When it succeeds, this returns true 110 111 // If the caller cannot afford the cost, this change will be rolled back 112 // If he does afford it, we can skip checking the same thing later on, during execution 113 evm.StateDB.AddSlotToAccessList(contract.Address(), slot) 114 115 // This is what happens during actual execution 116 return params.ColdSloadCostEIP2929, nil 117 } 118 119 // Every other time, during gas estimation, we hit the bottom code path 120 // Which causes the gas estimation to be too small, and the tx runs out 121 // of gas 122 return params.WarmStorageReadCostEIP2929, nil 123 } 124 125 // gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929 126 // EIP spec: 127 // > If the target is not in accessed_addresses, 128 // > charge COLD_ACCOUNT_ACCESS_COST gas, and add the address to accessed_addresses. 129 // > Otherwise, charge WARM_STORAGE_READ_COST gas. 130 func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 131 // memory expansion first (dynamic part of pre-2929 implementation) 132 gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize) 133 if err != nil { 134 return 0, err 135 } 136 addr := common.BigToAddress(stack.peek()) 137 // Check slot presence in the access list 138 if !evm.StateDB.AddressInAccessList(addr) { 139 evm.StateDB.AddAddressToAccessList(addr) 140 var overflow bool 141 // We charge (cold-warm), since 'warm' is already charged as constantGas 142 if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow { 143 return 0, ErrGasUintOverflow 144 } 145 return gas, nil 146 } 147 return gas, nil 148 } 149 150 // gasEip2929AccountCheck checks whether the first stack item (as address) is present in the access list. 151 // If it is, this method returns '0', otherwise 'cold-warm' gas, presuming that the opcode using it 152 // is also using 'warm' as constant factor. 153 // This method is used by: 154 // - extcodehash, 155 // - extcodesize, 156 // - (ext) balance 157 func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 158 addr := common.BigToAddress(stack.peek()) 159 // Check slot presence in the access list 160 if !evm.StateDB.AddressInAccessList(addr) { 161 // If the caller cannot afford the cost, this change will be rolled back 162 evm.StateDB.AddAddressToAccessList(addr) 163 // The warm storage read cost is already charged as constantGas 164 return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929, nil 165 } 166 return 0, nil 167 } 168 169 func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc { 170 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 171 addr := common.BigToAddress(stack.Back(1)) 172 // Check slot presence in the access list 173 warmAccess := evm.StateDB.AddressInAccessList(addr) 174 // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so 175 // the cost to charge for cold access, if any, is Cold - Warm 176 coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929 177 if !warmAccess { 178 evm.StateDB.AddAddressToAccessList(addr) 179 // Charge the remaining difference here already, to correctly calculate available 180 // gas for call 181 if !contract.UseGas(coldCost) { 182 return 0, ErrOutOfGas 183 } 184 } 185 // Now call the old calculator, which takes into account 186 // - create new account 187 // - transfer value 188 // - memory expansion 189 // - 63/64ths rule 190 gas, err := oldCalculator(evm, contract, stack, mem, memorySize) 191 if warmAccess || err != nil { 192 return gas, err 193 } 194 // In case of a cold access, we temporarily add the cold charge back, and also 195 // add it to the returned gas. By adding it to the return, it will be charged 196 // outside of this function, as part of the dynamic gas, and that will make it 197 // also become correctly reported to tracers. 198 contract.Gas += coldCost 199 return gas + coldCost, nil 200 } 201 } 202 203 var ( 204 gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall) 205 gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall) 206 gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall) 207 gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode) 208 gasSelfdestructEIP2929 = makeSelfdestructGasFn(true) 209 // gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds) 210 gasSelfdestructEIP3529 = makeSelfdestructGasFn(false) 211 212 // gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929 213 // 214 // When calling SSTORE, check if the (address, storage_key) pair is in accessed_storage_keys. 215 // If it is not, charge an additional COLD_SLOAD_COST gas, and add the pair to accessed_storage_keys. 216 // Additionally, modify the parameters defined in EIP 2200 as follows: 217 // 218 // Parameter Old value New value 219 // SLOAD_GAS 800 = WARM_STORAGE_READ_COST 220 // SSTORE_RESET_GAS 5000 5000 - COLD_SLOAD_COST 221 // 222 //The other parameters defined in EIP 2200 are unchanged. 223 // see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified 224 gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200) 225 226 // gasSStoreEIP2539 implements gas cost for SSTORE according to EPI-2539 227 // Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800) 228 gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529) 229 ) 230 231 // makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-2539 232 func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { 233 gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 234 var ( 235 gas uint64 236 address = common.BigToAddress(stack.peek()) 237 ) 238 if !evm.StateDB.AddressInAccessList(address) { 239 // If the caller cannot afford the cost, this change will be rolled back 240 evm.StateDB.AddAddressToAccessList(address) 241 gas = params.ColdAccountAccessCostEIP2929 242 } 243 // if empty and transfers value 244 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 245 gas += params.CreateBySelfdestructGas 246 } 247 if refundsEnabled && !evm.StateDB.HasSuicided(contract.Address()) { 248 evm.StateDB.AddRefund(params.SelfdestructRefundGas) 249 } 250 return gas, nil 251 } 252 return gasFunc 253 }