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