github.com/klaytn/klaytn@v1.10.2/blockchain/vm/gas_table.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/vm/gas_table.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package vm 22 23 import ( 24 "errors" 25 26 "github.com/klaytn/klaytn/common" 27 "github.com/klaytn/klaytn/common/math" 28 "github.com/klaytn/klaytn/params" 29 ) 30 31 // memoryGasCost calculates the quadratic gas for memory expansion. It does so 32 // only for the memory region that is expanded, not the total memory. 33 func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { 34 if newMemSize == 0 { 35 return 0, nil 36 } 37 // The maximum that will fit in a uint64 is max_word_count - 1. Anything above 38 // that will result in an overflow. Additionally, a newMemSize which results in 39 // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to 40 // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used 41 // without overflowing the gas calculation. 42 if newMemSize > 0x1FFFFFFFE0 { 43 return 0, errGasUintOverflow 44 } 45 newMemSizeWords := toWordSize(newMemSize) 46 newMemSize = newMemSizeWords * 32 47 48 if newMemSize > uint64(mem.Len()) { 49 square := newMemSizeWords * newMemSizeWords 50 linCoef := newMemSizeWords * params.MemoryGas 51 quadCoef := square / params.QuadCoeffDiv 52 newTotalFee := linCoef + quadCoef 53 54 fee := newTotalFee - mem.lastGasCost 55 mem.lastGasCost = newTotalFee 56 57 return fee, nil 58 } 59 return 0, nil 60 } 61 62 // memoryCopierGas creates the gas functions for the following opcodes, and takes 63 // the stack position of the operand which determines the size of the data to copy 64 // as argument: 65 // CALLDATACOPY (stack position 2) 66 // CODECOPY (stack position 2) 67 // EXTCODECOPY (stack poition 3) 68 // RETURNDATACOPY (stack position 2) 69 func memoryCopierGas(stackpos int) gasFunc { 70 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 71 // Gas for expanding the memory 72 gas, err := memoryGasCost(mem, memorySize) 73 if err != nil { 74 return 0, err 75 } 76 // And gas for copying data, charged per word at param.CopyGas 77 words, overflow := bigUint64(stack.Back(stackpos)) 78 if overflow { 79 return 0, errGasUintOverflow 80 } 81 82 if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { 83 return 0, errGasUintOverflow 84 } 85 86 if gas, overflow = math.SafeAdd(gas, words); overflow { 87 return 0, errGasUintOverflow 88 } 89 return gas, nil 90 } 91 } 92 93 var ( 94 gasCallDataCopy = memoryCopierGas(2) 95 gasCodeCopy = memoryCopierGas(2) 96 gasExtCodeCopy = memoryCopierGas(3) 97 gasReturnDataCopy = memoryCopierGas(2) 98 ) 99 100 func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 101 var ( 102 y, x = stack.Back(1), stack.Back(0) 103 current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 104 ) 105 // This checks for 3 scenario's and calculates gas accordingly 106 // 1. From a zero-value address to a non-zero value (NEW VALUE) 107 // 2. From a non-zero value address to a zero-value address (DELETE) 108 // 3. From a non-zero to a non-zero (CHANGE) 109 isOldEmpty := common.EmptyHash(current) 110 isNewEmpty := common.EmptyHash(common.BigToHash(y)) 111 if isOldEmpty && !isNewEmpty { 112 // 0 => non 0 113 return params.SstoreSetGas, nil 114 } else if !isOldEmpty && isNewEmpty { 115 // non 0 => 0 116 evm.StateDB.AddRefund(params.SstoreRefundGas) 117 return params.SstoreClearGas, nil 118 } else { 119 // non 0 => non 0 (or 0 => 0) 120 return params.SstoreResetGas, nil 121 } 122 } 123 124 // 0. If *gasleft* is less than or equal to 2300, fail the current call. 125 // 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted. 126 // 2. If current value does not equal new value: 127 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): 128 // 2.1.1. If original value is 0, SSTORE_SET_GAS (20K) gas is deducted. 129 // 2.1.2. Otherwise, SSTORE_RESET_GAS gas is deducted. If new value is 0, add SSTORE_CLEARS_SCHEDULE to refund counter. 130 // 2.2. If original value does not equal current value (this storage slot is dirty), SLOAD_GAS gas is deducted. Apply both of the following clauses: 131 // 2.2.1. If original value is not 0: 132 // 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEARS_SCHEDULE gas from refund counter. 133 // 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEARS_SCHEDULE gas to refund counter. 134 // 2.2.2. If original value equals new value (this storage slot is reset): 135 // 2.2.2.1. If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter. 136 // 2.2.2.2. Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter. 137 func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 138 // If we fail the minimum gas availability invariant, fail (0) 139 if contract.Gas <= params.SstoreSentryGasEIP2200 { 140 return 0, errors.New("not enough gas for reentrancy sentry") 141 } 142 // Gas sentry honoured, do the actual gas calculation based on the stored value 143 var ( 144 y, x = stack.Back(1), stack.Back(0) 145 current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 146 ) 147 value := common.BigToHash(y) 148 149 if current == value { // noop (1) 150 return params.SloadGasEIP2200, nil 151 } 152 original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x)) 153 if original == current { 154 if original == (common.Hash{}) { // create slot (2.1.1) 155 return params.SstoreSetGasEIP2200, nil 156 } 157 if value == (common.Hash{}) { // delete slot (2.1.2b) 158 evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) 159 } 160 return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) 161 } 162 if original != (common.Hash{}) { 163 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 164 evm.StateDB.SubRefund(params.SstoreClearsScheduleRefundEIP2200) 165 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 166 evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) 167 } 168 } 169 if original == value { 170 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 171 evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200) 172 } else { // reset to original existing slot (2.2.2.2) 173 evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) 174 } 175 } 176 return params.SloadGasEIP2200, nil // dirty update (2.2) 177 } 178 179 func makeGasLog(n uint64) gasFunc { 180 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 181 requestedSize, overflow := bigUint64(stack.Back(1)) 182 if overflow { 183 return 0, errGasUintOverflow 184 } 185 186 gas, err := memoryGasCost(mem, memorySize) 187 if err != nil { 188 return 0, err 189 } 190 191 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 192 return 0, errGasUintOverflow 193 } 194 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 195 return 0, errGasUintOverflow 196 } 197 198 var memorySizeGas uint64 199 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 200 return 0, errGasUintOverflow 201 } 202 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 203 return 0, errGasUintOverflow 204 } 205 return gas, nil 206 } 207 } 208 209 func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 210 gas, err := memoryGasCost(mem, memorySize) 211 if err != nil { 212 return 0, err 213 } 214 wordGas, overflow := bigUint64(stack.Back(1)) 215 if overflow { 216 return 0, errGasUintOverflow 217 } 218 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 219 return 0, errGasUintOverflow 220 } 221 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 222 return 0, errGasUintOverflow 223 } 224 return gas, nil 225 } 226 227 // pureMemoryGascost is used by several operations, which aside from their 228 // static cost have a dynamic cost which is solely based on the memory 229 // expansion 230 func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 231 return memoryGasCost(mem, memorySize) 232 } 233 234 var ( 235 gasReturn = pureMemoryGascost 236 gasRevert = pureMemoryGascost 237 gasMLoad = pureMemoryGascost 238 gasMStore8 = pureMemoryGascost 239 gasMStore = pureMemoryGascost 240 gasCreate = pureMemoryGascost 241 ) 242 243 func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 244 gas, err := memoryGasCost(mem, memorySize) 245 if err != nil { 246 return 0, err 247 } 248 wordGas, overflow := bigUint64(stack.Back(2)) 249 if overflow { 250 return 0, errGasUintOverflow 251 } 252 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 253 return 0, errGasUintOverflow 254 } 255 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 256 return 0, errGasUintOverflow 257 } 258 return gas, nil 259 } 260 261 // Geth Code contains gasExpFrontier and gasExpEip158 both 262 // Since eip158 is default in klaytn, both functions are integrated into gasExp functions. 263 func gasExp(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 264 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 265 266 var ( 267 gas = expByteLen * params.ExpByte // no overflow check required. Max is 256 * ExpByte gas 268 overflow bool 269 ) 270 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 271 return 0, errGasUintOverflow 272 } 273 return gas, nil 274 } 275 276 func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 277 var ( 278 gas uint64 279 transfersValue = stack.Back(2).Sign() != 0 280 address = common.BigToAddress(stack.Back(1)) 281 ) 282 if transfersValue { 283 if evm.StateDB.Empty(address) { 284 gas += params.CallNewAccountGas 285 } 286 gas += params.CallValueTransferGas 287 } 288 memoryGas, err := memoryGasCost(mem, memorySize) 289 if err != nil { 290 return 0, err 291 } 292 var overflow bool 293 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 294 return 0, errGasUintOverflow 295 } 296 297 evm.callGasTemp, err = callGas(contract.Gas, gas, stack.Back(0)) 298 if err != nil { 299 return 0, err 300 } 301 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 302 return 0, errGasUintOverflow 303 } 304 return gas, nil 305 } 306 307 func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 308 memoryGas, err := memoryGasCost(mem, memorySize) 309 if err != nil { 310 return 0, err 311 } 312 var ( 313 gas uint64 314 overflow bool 315 ) 316 if stack.Back(2).Sign() != 0 { 317 gas += params.CallValueTransferGas 318 } 319 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 320 return 0, errGasUintOverflow 321 } 322 evm.callGasTemp, err = callGas(contract.Gas, gas, stack.Back(0)) 323 if err != nil { 324 return 0, err 325 } 326 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 327 return 0, errGasUintOverflow 328 } 329 return gas, nil 330 } 331 332 func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 333 gas, err := memoryGasCost(mem, memorySize) 334 if err != nil { 335 return 0, err 336 } 337 evm.callGasTemp, err = callGas(contract.Gas, gas, stack.Back(0)) 338 if err != nil { 339 return 0, err 340 } 341 var overflow bool 342 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 343 return 0, errGasUintOverflow 344 } 345 return gas, nil 346 } 347 348 func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 349 gas, err := memoryGasCost(mem, memorySize) 350 if err != nil { 351 return 0, err 352 } 353 evm.callGasTemp, err = callGas(contract.Gas, gas, stack.Back(0)) 354 if err != nil { 355 return 0, err 356 } 357 var overflow bool 358 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 359 return 0, errGasUintOverflow 360 } 361 return gas, nil 362 } 363 364 func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 365 gas := params.SelfdestructGas 366 address := common.BigToAddress(stack.Back(0)) 367 368 // This is from eip158 369 // if empty and transfers value 370 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 371 gas += params.CreateBySelfdestructGas 372 } 373 374 if !evm.StateDB.HasSuicided(contract.Address()) { 375 evm.StateDB.AddRefund(params.SelfdestructRefundGas) 376 } 377 return gas, nil 378 }