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