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