github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/gas_table.go (about) 1 // (c) 2019-2020, 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 2017 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 // memoryGasCost calculates the quadratic gas for memory expansion. It does so 39 // only for the memory region that is expanded, not the total memory. 40 func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { 41 if newMemSize == 0 { 42 return 0, nil 43 } 44 // The maximum that will fit in a uint64 is max_word_count - 1. Anything above 45 // that will result in an overflow. Additionally, a newMemSize which results in 46 // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to 47 // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used 48 // without overflowing the gas calculation. 49 if newMemSize > 0x1FFFFFFFE0 { 50 return 0, vmerrs.ErrGasUintOverflow 51 } 52 newMemSizeWords := toWordSize(newMemSize) 53 newMemSize = newMemSizeWords * 32 54 55 if newMemSize > uint64(mem.Len()) { 56 square := newMemSizeWords * newMemSizeWords 57 linCoef := newMemSizeWords * params.MemoryGas 58 quadCoef := square / params.QuadCoeffDiv 59 newTotalFee := linCoef + quadCoef 60 61 fee := newTotalFee - mem.lastGasCost 62 mem.lastGasCost = newTotalFee 63 64 return fee, nil 65 } 66 return 0, nil 67 } 68 69 // memoryCopierGas creates the gas functions for the following opcodes, and takes 70 // the stack position of the operand which determines the size of the data to copy 71 // as argument: 72 // CALLDATACOPY (stack position 2) 73 // CODECOPY (stack position 2) 74 // EXTCODECOPY (stack position 3) 75 // RETURNDATACOPY (stack position 2) 76 func memoryCopierGas(stackpos int) gasFunc { 77 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 78 // Gas for expanding the memory 79 gas, err := memoryGasCost(mem, memorySize) 80 if err != nil { 81 return 0, err 82 } 83 // And gas for copying data, charged per word at param.CopyGas 84 words, overflow := stack.Back(stackpos).Uint64WithOverflow() 85 if overflow { 86 return 0, vmerrs.ErrGasUintOverflow 87 } 88 89 if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { 90 return 0, vmerrs.ErrGasUintOverflow 91 } 92 93 if gas, overflow = math.SafeAdd(gas, words); overflow { 94 return 0, vmerrs.ErrGasUintOverflow 95 } 96 return gas, nil 97 } 98 } 99 100 var ( 101 gasCallDataCopy = memoryCopierGas(2) 102 gasCodeCopy = memoryCopierGas(2) 103 gasExtCodeCopy = memoryCopierGas(3) 104 gasReturnDataCopy = memoryCopierGas(2) 105 ) 106 107 func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 108 var ( 109 y, x = stack.Back(1), stack.Back(0) 110 current = evm.StateDB.GetState(contract.Address(), x.Bytes32()) 111 ) 112 // The legacy gas metering only takes into consideration the current state 113 // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283) 114 // OR Constantinople is not active 115 if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople { 116 // This checks for 3 scenario's and calculates gas accordingly: 117 // 118 // 1. From a zero-value address to a non-zero value (NEW VALUE) 119 // 2. From a non-zero value address to a zero-value address (DELETE) 120 // 3. From a non-zero to a non-zero (CHANGE) 121 switch { 122 case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0 123 return params.SstoreSetGas, nil 124 case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0 125 evm.StateDB.AddRefund(params.SstoreRefundGas) 126 return params.SstoreClearGas, nil 127 default: // non 0 => non 0 (or 0 => 0) 128 return params.SstoreResetGas, nil 129 } 130 } 131 // The new gas metering is based on net gas costs (EIP-1283): 132 // 133 // 1. If current value equals new value (this is a no-op), 200 gas is deducted. 134 // 2. If current value does not equal new value 135 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context) 136 // 2.1.1. If original value is 0, 20000 gas is deducted. 137 // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter. 138 // 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses. 139 // 2.2.1. If original value is not 0 140 // 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0. 141 // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter. 142 // 2.2.2. If original value equals new value (this storage slot is reset) 143 // 2.2.2.1. If original value is 0, add 19800 gas to refund counter. 144 // 2.2.2.2. Otherwise, add 4800 gas to refund counter. 145 value := common.Hash(y.Bytes32()) 146 if current == value { // noop (1) 147 return params.NetSstoreNoopGas, nil 148 } 149 original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32()) 150 if original == current { 151 if original == (common.Hash{}) { // create slot (2.1.1) 152 return params.NetSstoreInitGas, nil 153 } 154 if value == (common.Hash{}) { // delete slot (2.1.2b) 155 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 156 } 157 return params.NetSstoreCleanGas, nil // write existing slot (2.1.2) 158 } 159 if original != (common.Hash{}) { 160 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 161 evm.StateDB.SubRefund(params.NetSstoreClearRefund) 162 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 163 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 164 } 165 } 166 if original == value { 167 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 168 evm.StateDB.AddRefund(params.NetSstoreResetClearRefund) 169 } else { // reset to original existing slot (2.2.2.2) 170 evm.StateDB.AddRefund(params.NetSstoreResetRefund) 171 } 172 } 173 return params.NetSstoreDirtyGas, nil 174 } 175 176 // 0. If *gasleft* is less than or equal to 2300, fail the current call. 177 // 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted. 178 // 2. If current value does not equal new value: 179 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): 180 // 2.1.1. If original value is 0, SSTORE_SET_GAS (20K) gas is deducted. 181 // 2.1.2. Otherwise, SSTORE_RESET_GAS gas is deducted. If new value is 0, add SSTORE_CLEARS_SCHEDULE to refund counter. 182 // 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: 183 // 2.2.1. If original value is not 0: 184 // 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. 185 // 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. 186 // 2.2.2. If original value equals new value (this storage slot is reset): 187 // 2.2.2.1. If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter. 188 // 2.2.2.2. Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter. 189 func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 190 // If we fail the minimum gas availability invariant, fail (0) 191 if contract.Gas <= params.SstoreSentryGasEIP2200 { 192 return 0, errors.New("not enough gas for reentrancy sentry") 193 } 194 // Gas sentry honoured, do the actual gas calculation based on the stored value 195 var ( 196 y, x = stack.Back(1), stack.Back(0) 197 current = evm.StateDB.GetState(contract.Address(), x.Bytes32()) 198 ) 199 value := common.Hash(y.Bytes32()) 200 201 if current == value { // noop (1) 202 return params.SloadGasEIP2200, nil 203 } 204 original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32()) 205 if original == current { 206 if original == (common.Hash{}) { // create slot (2.1.1) 207 return params.SstoreSetGasEIP2200, nil 208 } 209 if value == (common.Hash{}) { // delete slot (2.1.2b) 210 evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) 211 } 212 return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) 213 } 214 if original != (common.Hash{}) { 215 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 216 evm.StateDB.SubRefund(params.SstoreClearsScheduleRefundEIP2200) 217 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 218 evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) 219 } 220 } 221 if original == value { 222 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 223 evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200) 224 } else { // reset to original existing slot (2.2.2.2) 225 evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) 226 } 227 } 228 return params.SloadGasEIP2200, nil // dirty update (2.2) 229 } 230 231 func makeGasLog(n uint64) gasFunc { 232 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 233 requestedSize, overflow := stack.Back(1).Uint64WithOverflow() 234 if overflow { 235 return 0, vmerrs.ErrGasUintOverflow 236 } 237 238 gas, err := memoryGasCost(mem, memorySize) 239 if err != nil { 240 return 0, err 241 } 242 243 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 244 return 0, vmerrs.ErrGasUintOverflow 245 } 246 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 247 return 0, vmerrs.ErrGasUintOverflow 248 } 249 250 var memorySizeGas uint64 251 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 252 return 0, vmerrs.ErrGasUintOverflow 253 } 254 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 255 return 0, vmerrs.ErrGasUintOverflow 256 } 257 return gas, nil 258 } 259 } 260 261 func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 262 gas, err := memoryGasCost(mem, memorySize) 263 if err != nil { 264 return 0, err 265 } 266 wordGas, overflow := stack.Back(1).Uint64WithOverflow() 267 if overflow { 268 return 0, vmerrs.ErrGasUintOverflow 269 } 270 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { 271 return 0, vmerrs.ErrGasUintOverflow 272 } 273 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 274 return 0, vmerrs.ErrGasUintOverflow 275 } 276 return gas, nil 277 } 278 279 // pureMemoryGascost is used by several operations, which aside from their 280 // static cost have a dynamic cost which is solely based on the memory 281 // expansion 282 func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 283 return memoryGasCost(mem, memorySize) 284 } 285 286 var ( 287 gasReturn = pureMemoryGascost 288 gasRevert = pureMemoryGascost 289 gasMLoad = pureMemoryGascost 290 gasMStore8 = pureMemoryGascost 291 gasMStore = pureMemoryGascost 292 gasCreate = pureMemoryGascost 293 ) 294 295 func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 296 gas, err := memoryGasCost(mem, memorySize) 297 if err != nil { 298 return 0, err 299 } 300 wordGas, overflow := stack.Back(2).Uint64WithOverflow() 301 if overflow { 302 return 0, vmerrs.ErrGasUintOverflow 303 } 304 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { 305 return 0, vmerrs.ErrGasUintOverflow 306 } 307 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 308 return 0, vmerrs.ErrGasUintOverflow 309 } 310 return gas, nil 311 } 312 313 func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 314 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 315 316 var ( 317 gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas 318 overflow bool 319 ) 320 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 321 return 0, vmerrs.ErrGasUintOverflow 322 } 323 return gas, nil 324 } 325 326 func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 327 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 328 329 var ( 330 gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas 331 overflow bool 332 ) 333 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 334 return 0, vmerrs.ErrGasUintOverflow 335 } 336 return gas, nil 337 } 338 339 func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 340 var ( 341 gas uint64 342 transfersValue = !stack.Back(2).IsZero() 343 address = common.Address(stack.Back(1).Bytes20()) 344 ) 345 if evm.chainRules.IsEIP158 { 346 if transfersValue && evm.StateDB.Empty(address) { 347 gas += params.CallNewAccountGas 348 } 349 } else if !evm.StateDB.Exist(address) { 350 gas += params.CallNewAccountGas 351 } 352 if transfersValue { 353 gas += params.CallValueTransferGas 354 } 355 memoryGas, err := memoryGasCost(mem, memorySize) 356 if err != nil { 357 return 0, err 358 } 359 var overflow bool 360 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 361 return 0, vmerrs.ErrGasUintOverflow 362 } 363 364 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 365 if err != nil { 366 return 0, err 367 } 368 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 369 return 0, vmerrs.ErrGasUintOverflow 370 } 371 return gas, nil 372 } 373 374 func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 375 memoryGas, err := memoryGasCost(mem, memorySize) 376 if err != nil { 377 return 0, err 378 } 379 var ( 380 gas uint64 381 overflow bool 382 ) 383 if stack.Back(2).Sign() != 0 { 384 gas += params.CallValueTransferGas 385 } 386 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 387 return 0, vmerrs.ErrGasUintOverflow 388 } 389 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 390 if err != nil { 391 return 0, err 392 } 393 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 394 return 0, vmerrs.ErrGasUintOverflow 395 } 396 return gas, nil 397 } 398 399 func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 400 gas, err := memoryGasCost(mem, memorySize) 401 if err != nil { 402 return 0, err 403 } 404 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 405 if err != nil { 406 return 0, err 407 } 408 var overflow bool 409 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 410 return 0, vmerrs.ErrGasUintOverflow 411 } 412 return gas, nil 413 } 414 415 func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 416 gas, err := memoryGasCost(mem, memorySize) 417 if err != nil { 418 return 0, err 419 } 420 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 421 if err != nil { 422 return 0, err 423 } 424 var overflow bool 425 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 426 return 0, vmerrs.ErrGasUintOverflow 427 } 428 return gas, nil 429 } 430 431 func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 432 var gas uint64 433 // EIP150 homestead gas reprice fork: 434 if evm.chainRules.IsEIP150 { 435 gas = params.SelfdestructGasEIP150 436 var address = common.Address(stack.Back(0).Bytes20()) 437 438 if evm.chainRules.IsEIP158 { 439 // if empty and transfers value 440 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 441 gas += params.CreateBySelfdestructGas 442 } 443 } else if !evm.StateDB.Exist(address) { 444 gas += params.CreateBySelfdestructGas 445 } 446 } 447 448 if !evm.StateDB.HasSuicided(contract.Address()) { 449 evm.StateDB.AddRefund(params.SelfdestructRefundGas) 450 } 451 return gas, nil 452 }