github.com/codingfuture/orig-energi3@v0.8.4/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 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/common/math" 22 "github.com/ethereum/go-ethereum/params" 23 ) 24 25 // memoryGasCosts calculates the quadratic gas for memory expansion. It does so 26 // only for the memory region that is expanded, not the total memory. 27 func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { 28 29 if newMemSize == 0 { 30 return 0, nil 31 } 32 // The maximum that will fit in a uint64 is max_word_count - 1 33 // anything above that will result in an overflow. 34 // Additionally, a newMemSize which results in a 35 // newMemSizeWords larger than 0x7ffffffff will cause the square operation 36 // to overflow. 37 // The constant 0xffffffffe0 is the highest number that can be used without 38 // overflowing the gas calculation 39 if newMemSize > 0xffffffffe0 { 40 return 0, errGasUintOverflow 41 } 42 43 newMemSizeWords := toWordSize(newMemSize) 44 newMemSize = newMemSizeWords * 32 45 46 if newMemSize > uint64(mem.Len()) { 47 square := newMemSizeWords * newMemSizeWords 48 linCoef := newMemSizeWords * params.MemoryGas 49 quadCoef := square / params.QuadCoeffDiv 50 newTotalFee := linCoef + quadCoef 51 52 fee := newTotalFee - mem.lastGasCost 53 mem.lastGasCost = newTotalFee 54 55 return fee, nil 56 } 57 return 0, nil 58 } 59 60 func constGasFunc(gas uint64) gasFunc { 61 return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 62 return gas, nil 63 } 64 } 65 66 func gasCallDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 67 gas, err := memoryGasCost(mem, memorySize) 68 if err != nil { 69 return 0, err 70 } 71 72 var overflow bool 73 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 74 return 0, errGasUintOverflow 75 } 76 77 words, overflow := bigUint64(stack.Back(2)) 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 func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 93 gas, err := memoryGasCost(mem, memorySize) 94 if err != nil { 95 return 0, err 96 } 97 98 var overflow bool 99 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 100 return 0, errGasUintOverflow 101 } 102 103 words, overflow := bigUint64(stack.Back(2)) 104 if overflow { 105 return 0, errGasUintOverflow 106 } 107 108 if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { 109 return 0, errGasUintOverflow 110 } 111 112 if gas, overflow = math.SafeAdd(gas, words); overflow { 113 return 0, errGasUintOverflow 114 } 115 return gas, nil 116 } 117 118 func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 119 var ( 120 y, x = stack.Back(1), stack.Back(0) 121 current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 122 ) 123 // The legacy gas metering only takes into consideration the current state 124 // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283) 125 // OR Constantinople is not active 126 if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople { 127 // This checks for 3 scenario's and calculates gas accordingly: 128 // 129 // 1. From a zero-value address to a non-zero value (NEW VALUE) 130 // 2. From a non-zero value address to a zero-value address (DELETE) 131 // 3. From a non-zero to a non-zero (CHANGE) 132 switch { 133 case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0 134 return params.SstoreSetGas, nil 135 case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0 136 evm.StateDB.AddRefund(params.SstoreRefundGas) 137 return params.SstoreClearGas, nil 138 default: // non 0 => non 0 (or 0 => 0) 139 return params.SstoreResetGas, nil 140 } 141 } 142 // The new gas metering is based on net gas costs (EIP-1283): 143 // 144 // 1. If current value equals new value (this is a no-op), 200 gas is deducted. 145 // 2. If current value does not equal new value 146 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context) 147 // 2.1.1. If original value is 0, 20000 gas is deducted. 148 // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter. 149 // 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. 150 // 2.2.1. If original value is not 0 151 // 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. 152 // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter. 153 // 2.2.2. If original value equals new value (this storage slot is reset) 154 // 2.2.2.1. If original value is 0, add 19800 gas to refund counter. 155 // 2.2.2.2. Otherwise, add 4800 gas to refund counter. 156 value := common.BigToHash(y) 157 if current == value { // noop (1) 158 return params.NetSstoreNoopGas, nil 159 } 160 original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x)) 161 if original == current { 162 if original == (common.Hash{}) { // create slot (2.1.1) 163 return params.NetSstoreInitGas, nil 164 } 165 if value == (common.Hash{}) { // delete slot (2.1.2b) 166 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 167 } 168 return params.NetSstoreCleanGas, nil // write existing slot (2.1.2) 169 } 170 if original != (common.Hash{}) { 171 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 172 evm.StateDB.SubRefund(params.NetSstoreClearRefund) 173 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 174 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 175 } 176 } 177 if original == value { 178 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 179 evm.StateDB.AddRefund(params.NetSstoreResetClearRefund) 180 } else { // reset to original existing slot (2.2.2.2) 181 evm.StateDB.AddRefund(params.NetSstoreResetRefund) 182 } 183 } 184 return params.NetSstoreDirtyGas, nil 185 } 186 187 func makeGasLog(n uint64) gasFunc { 188 return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 189 requestedSize, overflow := bigUint64(stack.Back(1)) 190 if overflow { 191 return 0, errGasUintOverflow 192 } 193 194 gas, err := memoryGasCost(mem, memorySize) 195 if err != nil { 196 return 0, err 197 } 198 199 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 200 return 0, errGasUintOverflow 201 } 202 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 203 return 0, errGasUintOverflow 204 } 205 206 var memorySizeGas uint64 207 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 208 return 0, errGasUintOverflow 209 } 210 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 211 return 0, errGasUintOverflow 212 } 213 return gas, nil 214 } 215 } 216 217 func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 218 var overflow bool 219 gas, err := memoryGasCost(mem, memorySize) 220 if err != nil { 221 return 0, err 222 } 223 224 if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow { 225 return 0, errGasUintOverflow 226 } 227 228 wordGas, overflow := bigUint64(stack.Back(1)) 229 if overflow { 230 return 0, errGasUintOverflow 231 } 232 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 233 return 0, errGasUintOverflow 234 } 235 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 236 return 0, errGasUintOverflow 237 } 238 return gas, nil 239 } 240 241 func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 242 gas, err := memoryGasCost(mem, memorySize) 243 if err != nil { 244 return 0, err 245 } 246 247 var overflow bool 248 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 249 return 0, errGasUintOverflow 250 } 251 252 wordGas, overflow := bigUint64(stack.Back(2)) 253 if overflow { 254 return 0, errGasUintOverflow 255 } 256 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { 257 return 0, errGasUintOverflow 258 } 259 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 260 return 0, errGasUintOverflow 261 } 262 return gas, nil 263 } 264 265 func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 266 gas, err := memoryGasCost(mem, memorySize) 267 if err != nil { 268 return 0, err 269 } 270 271 var overflow bool 272 if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow { 273 return 0, errGasUintOverflow 274 } 275 276 wordGas, overflow := bigUint64(stack.Back(3)) 277 if overflow { 278 return 0, errGasUintOverflow 279 } 280 281 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { 282 return 0, errGasUintOverflow 283 } 284 285 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 286 return 0, errGasUintOverflow 287 } 288 return gas, nil 289 } 290 291 func gasExtCodeHash(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 292 return gt.ExtcodeHash, nil 293 } 294 295 func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 296 var overflow bool 297 gas, err := memoryGasCost(mem, memorySize) 298 if err != nil { 299 return 0, errGasUintOverflow 300 } 301 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 302 return 0, errGasUintOverflow 303 } 304 return gas, nil 305 } 306 307 func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 308 var overflow bool 309 gas, err := memoryGasCost(mem, memorySize) 310 if err != nil { 311 return 0, errGasUintOverflow 312 } 313 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 314 return 0, errGasUintOverflow 315 } 316 return gas, nil 317 } 318 319 func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 320 var overflow bool 321 gas, err := memoryGasCost(mem, memorySize) 322 if err != nil { 323 return 0, errGasUintOverflow 324 } 325 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 326 return 0, errGasUintOverflow 327 } 328 return gas, nil 329 } 330 331 func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 332 var overflow bool 333 gas, err := memoryGasCost(mem, memorySize) 334 if err != nil { 335 return 0, err 336 } 337 if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow { 338 return 0, errGasUintOverflow 339 } 340 return gas, nil 341 } 342 343 func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 344 var overflow bool 345 gas, err := memoryGasCost(mem, memorySize) 346 if err != nil { 347 return 0, err 348 } 349 if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow { 350 return 0, errGasUintOverflow 351 } 352 wordGas, overflow := bigUint64(stack.Back(2)) 353 if overflow { 354 return 0, errGasUintOverflow 355 } 356 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 357 return 0, errGasUintOverflow 358 } 359 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 360 return 0, errGasUintOverflow 361 } 362 363 return gas, nil 364 } 365 366 func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 367 return gt.Balance, nil 368 } 369 370 func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 371 return gt.ExtcodeSize, nil 372 } 373 374 func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 375 return gt.SLoad, nil 376 } 377 378 func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 379 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 380 381 var ( 382 gas = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas 383 overflow bool 384 ) 385 if gas, overflow = math.SafeAdd(gas, GasSlowStep); overflow { 386 return 0, errGasUintOverflow 387 } 388 return gas, nil 389 } 390 391 func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 392 var ( 393 gas = gt.Calls 394 transfersValue = stack.Back(2).Sign() != 0 395 address = common.BigToAddress(stack.Back(1)) 396 eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) 397 ) 398 if eip158 { 399 if transfersValue && evm.StateDB.Empty(address) { 400 gas += params.CallNewAccountGas 401 } 402 } else if !evm.StateDB.Exist(address) { 403 gas += params.CallNewAccountGas 404 } 405 if transfersValue { 406 gas += params.CallValueTransferGas 407 } 408 memoryGas, err := memoryGasCost(mem, memorySize) 409 if err != nil { 410 return 0, err 411 } 412 var overflow bool 413 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 414 return 0, errGasUintOverflow 415 } 416 417 evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) 418 if err != nil { 419 return 0, err 420 } 421 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 422 return 0, errGasUintOverflow 423 } 424 return gas, nil 425 } 426 427 func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 428 gas := gt.Calls 429 if stack.Back(2).Sign() != 0 { 430 gas += params.CallValueTransferGas 431 } 432 memoryGas, err := memoryGasCost(mem, memorySize) 433 if err != nil { 434 return 0, err 435 } 436 var overflow bool 437 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 438 return 0, errGasUintOverflow 439 } 440 441 evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) 442 if err != nil { 443 return 0, err 444 } 445 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 446 return 0, errGasUintOverflow 447 } 448 return gas, nil 449 } 450 451 func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 452 return memoryGasCost(mem, memorySize) 453 } 454 455 func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 456 return memoryGasCost(mem, memorySize) 457 } 458 459 func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 460 var gas uint64 461 // EIP150 homestead gas reprice fork: 462 if evm.ChainConfig().IsEIP150(evm.BlockNumber) { 463 gas = gt.Suicide 464 var ( 465 address = common.BigToAddress(stack.Back(0)) 466 eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) 467 ) 468 469 if eip158 { 470 // if empty and transfers value 471 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 472 gas += gt.CreateBySuicide 473 } 474 } else if !evm.StateDB.Exist(address) { 475 gas += gt.CreateBySuicide 476 } 477 } 478 479 if !evm.StateDB.HasSuicided(contract.Address()) { 480 evm.StateDB.AddRefund(params.SuicideRefundGas) 481 } 482 return gas, nil 483 } 484 485 func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 486 gas, err := memoryGasCost(mem, memorySize) 487 if err != nil { 488 return 0, err 489 } 490 var overflow bool 491 if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { 492 return 0, errGasUintOverflow 493 } 494 495 evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) 496 if err != nil { 497 return 0, err 498 } 499 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 500 return 0, errGasUintOverflow 501 } 502 return gas, nil 503 } 504 505 func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 506 gas, err := memoryGasCost(mem, memorySize) 507 if err != nil { 508 return 0, err 509 } 510 var overflow bool 511 if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { 512 return 0, errGasUintOverflow 513 } 514 515 evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) 516 if err != nil { 517 return 0, err 518 } 519 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 520 return 0, errGasUintOverflow 521 } 522 return gas, nil 523 } 524 525 func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 526 return GasFastestStep, nil 527 } 528 529 func gasSwap(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 530 return GasFastestStep, nil 531 } 532 533 func gasDup(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 534 return GasFastestStep, nil 535 }