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