github.com/dim4egster/coreth@v0.10.2/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/dim4egster/coreth/params" 33 "github.com/dim4egster/coreth/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 // gasSStoreAP1 simplifies the dynamic gas cost of SSTORE by removing all refund logic 232 // 233 // 0. If *gasleft* is less than or equal to 2300, fail the current call. 234 // 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted. 235 // 2. If current value does not equal new value: 236 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): 237 // 2.1.1. If original value is 0, SSTORE_SET_GAS (20K) gas is deducted. 238 // 2.1.2. Otherwise, SSTORE_RESET_GAS gas is deducted. If new value is 0, add SSTORE_CLEARS_SCHEDULE to refund counter. 239 // 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: 240 func gasSStoreAP1(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 241 // If we fail the minimum gas availability invariant, fail (0) 242 if contract.Gas <= params.SstoreSentryGasEIP2200 { 243 return 0, errors.New("not enough gas for reentrancy sentry") 244 } 245 // Gas sentry honoured, do the actual gas calculation based on the stored value 246 var ( 247 y, x = stack.Back(1), stack.Back(0) 248 current = evm.StateDB.GetState(contract.Address(), x.Bytes32()) 249 ) 250 value := common.Hash(y.Bytes32()) 251 252 if current == value { // noop (1) 253 return params.SloadGasEIP2200, nil 254 } 255 original := evm.StateDB.GetCommittedStateAP1(contract.Address(), x.Bytes32()) 256 if original == current { 257 if original == (common.Hash{}) { // create slot (2.1.1) 258 return params.SstoreSetGasEIP2200, nil 259 } 260 return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) 261 } 262 263 return params.SloadGasEIP2200, nil // dirty update (2.2) 264 } 265 266 func makeGasLog(n uint64) gasFunc { 267 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 268 requestedSize, overflow := stack.Back(1).Uint64WithOverflow() 269 if overflow { 270 return 0, vmerrs.ErrGasUintOverflow 271 } 272 273 gas, err := memoryGasCost(mem, memorySize) 274 if err != nil { 275 return 0, err 276 } 277 278 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 279 return 0, vmerrs.ErrGasUintOverflow 280 } 281 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 282 return 0, vmerrs.ErrGasUintOverflow 283 } 284 285 var memorySizeGas uint64 286 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 287 return 0, vmerrs.ErrGasUintOverflow 288 } 289 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 290 return 0, vmerrs.ErrGasUintOverflow 291 } 292 return gas, nil 293 } 294 } 295 296 func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 297 gas, err := memoryGasCost(mem, memorySize) 298 if err != nil { 299 return 0, err 300 } 301 wordGas, overflow := stack.Back(1).Uint64WithOverflow() 302 if overflow { 303 return 0, vmerrs.ErrGasUintOverflow 304 } 305 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { 306 return 0, vmerrs.ErrGasUintOverflow 307 } 308 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 309 return 0, vmerrs.ErrGasUintOverflow 310 } 311 return gas, nil 312 } 313 314 // pureMemoryGascost is used by several operations, which aside from their 315 // static cost have a dynamic cost which is solely based on the memory 316 // expansion 317 func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 318 return memoryGasCost(mem, memorySize) 319 } 320 321 var ( 322 gasReturn = pureMemoryGascost 323 gasRevert = pureMemoryGascost 324 gasMLoad = pureMemoryGascost 325 gasMStore8 = pureMemoryGascost 326 gasMStore = pureMemoryGascost 327 gasCreate = pureMemoryGascost 328 ) 329 330 func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 331 gas, err := memoryGasCost(mem, memorySize) 332 if err != nil { 333 return 0, err 334 } 335 wordGas, overflow := stack.Back(2).Uint64WithOverflow() 336 if overflow { 337 return 0, vmerrs.ErrGasUintOverflow 338 } 339 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { 340 return 0, vmerrs.ErrGasUintOverflow 341 } 342 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 343 return 0, vmerrs.ErrGasUintOverflow 344 } 345 return gas, nil 346 } 347 348 func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 349 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 350 351 var ( 352 gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas 353 overflow bool 354 ) 355 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 356 return 0, vmerrs.ErrGasUintOverflow 357 } 358 return gas, nil 359 } 360 361 func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 362 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 363 364 var ( 365 gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas 366 overflow bool 367 ) 368 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 369 return 0, vmerrs.ErrGasUintOverflow 370 } 371 return gas, nil 372 } 373 374 func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 375 var ( 376 gas uint64 377 transfersValue = !stack.Back(2).IsZero() 378 address = common.Address(stack.Back(1).Bytes20()) 379 ) 380 if evm.chainRules.IsEIP158 { 381 if transfersValue && evm.StateDB.Empty(address) { 382 gas += params.CallNewAccountGas 383 } 384 } else if !evm.StateDB.Exist(address) { 385 gas += params.CallNewAccountGas 386 } 387 if transfersValue { 388 gas += params.CallValueTransferGas 389 } 390 memoryGas, err := memoryGasCost(mem, memorySize) 391 if err != nil { 392 return 0, err 393 } 394 var overflow bool 395 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 396 return 0, vmerrs.ErrGasUintOverflow 397 } 398 399 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 400 if err != nil { 401 return 0, err 402 } 403 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 404 return 0, vmerrs.ErrGasUintOverflow 405 } 406 return gas, nil 407 } 408 409 func gasCallExpertAP1(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 410 var ( 411 gas uint64 412 transfersValue = !stack.Back(2).IsZero() 413 multiCoinTransfersValue = !stack.Back(4).IsZero() 414 address = common.Address(stack.Back(1).Bytes20()) 415 ) 416 if evm.chainRules.IsEIP158 { 417 if (transfersValue || multiCoinTransfersValue) && evm.StateDB.Empty(address) { 418 gas += params.CallNewAccountGas 419 } 420 } else if !evm.StateDB.Exist(address) { 421 gas += params.CallNewAccountGas 422 } 423 if transfersValue { 424 gas += params.CallValueTransferGas 425 } 426 if multiCoinTransfersValue { 427 gas += params.CallValueTransferGas 428 } 429 memoryGas, err := memoryGasCost(mem, memorySize) 430 if err != nil { 431 return 0, err 432 } 433 var overflow bool 434 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 435 return 0, vmerrs.ErrGasUintOverflow 436 } 437 438 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 439 if err != nil { 440 return 0, err 441 } 442 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 443 return 0, vmerrs.ErrGasUintOverflow 444 } 445 return gas, nil 446 } 447 448 func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 449 memoryGas, err := memoryGasCost(mem, memorySize) 450 if err != nil { 451 return 0, err 452 } 453 var ( 454 gas uint64 455 overflow bool 456 ) 457 if stack.Back(2).Sign() != 0 { 458 gas += params.CallValueTransferGas 459 } 460 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 461 return 0, vmerrs.ErrGasUintOverflow 462 } 463 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 464 if err != nil { 465 return 0, err 466 } 467 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 468 return 0, vmerrs.ErrGasUintOverflow 469 } 470 return gas, nil 471 } 472 473 func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 474 gas, err := memoryGasCost(mem, memorySize) 475 if err != nil { 476 return 0, err 477 } 478 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 479 if err != nil { 480 return 0, err 481 } 482 var overflow bool 483 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 484 return 0, vmerrs.ErrGasUintOverflow 485 } 486 return gas, nil 487 } 488 489 func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 490 gas, err := memoryGasCost(mem, memorySize) 491 if err != nil { 492 return 0, err 493 } 494 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 495 if err != nil { 496 return 0, err 497 } 498 var overflow bool 499 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 500 return 0, vmerrs.ErrGasUintOverflow 501 } 502 return gas, nil 503 } 504 505 func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 506 var gas uint64 507 // EIP150 homestead gas reprice fork: 508 if evm.chainRules.IsEIP150 { 509 gas = params.SelfdestructGasEIP150 510 var address = common.Address(stack.Back(0).Bytes20()) 511 512 if evm.chainRules.IsEIP158 { 513 // if empty and transfers value 514 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 515 gas += params.CreateBySelfdestructGas 516 } 517 } else if !evm.StateDB.Exist(address) { 518 gas += params.CreateBySelfdestructGas 519 } 520 } 521 522 if !evm.StateDB.HasSuicided(contract.Address()) { 523 evm.StateDB.AddRefund(params.SelfdestructRefundGas) 524 } 525 return gas, nil 526 } 527 528 func gasSelfdestructAP1(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 529 var gas uint64 530 // EIP150 homestead gas reprice fork: 531 if evm.chainRules.IsEIP150 { 532 gas = params.SelfdestructGasEIP150 533 var address = common.Address(stack.Back(0).Bytes20()) 534 535 if evm.chainRules.IsEIP158 { 536 // if empty and transfers value 537 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 538 gas += params.CreateBySelfdestructGas 539 } 540 } else if !evm.StateDB.Exist(address) { 541 gas += params.CreateBySelfdestructGas 542 } 543 } 544 545 return gas, nil 546 }