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