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