github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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 gmath "math" 21 "math/big" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/math" 25 "github.com/ethereum/go-ethereum/params" 26 ) 27 28 // memoryGasCosts calculates the quadratic gas for memory expansion. It does so 29 // only for the memory region that is expanded, not the total memory. 30 func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { 31 // The maximum that will fit in a uint64 is max_word_count - 1 32 // anything above that will result in an overflow. 33 if newMemSize > gmath.MaxUint64-32 { 34 return 0, errGasUintOverflow 35 } 36 37 if newMemSize == 0 { 38 return 0, nil 39 } 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 func constGasFunc(gas uint64) gasFunc { 59 return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 60 return gas, nil 61 } 62 } 63 64 func gasCalldataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 65 gas, err := memoryGasCost(mem, memorySize) 66 if err != nil { 67 return 0, err 68 } 69 70 var overflow bool 71 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 72 return 0, errGasUintOverflow 73 } 74 75 words, overflow := bigUint64(stack.Back(2)) 76 if overflow { 77 return 0, errGasUintOverflow 78 } 79 80 if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { 81 return 0, errGasUintOverflow 82 } 83 84 if gas, overflow = math.SafeAdd(gas, words); overflow { 85 return 0, errGasUintOverflow 86 } 87 return gas, nil 88 } 89 90 func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 91 var ( 92 y, x = stack.Back(1), stack.Back(0) 93 val = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 94 ) 95 // This checks for 3 scenario's and calculates gas accordingly 96 // 1. From a zero-value address to a non-zero value (NEW VALUE) 97 // 2. From a non-zero value address to a zero-value address (DELETE) 98 // 3. From a non-zero to a non-zero (CHANGE) 99 if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) { 100 // 0 => non 0 101 return params.SstoreSetGas, nil 102 } else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) { 103 evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SstoreRefundGas)) 104 105 return params.SstoreClearGas, nil 106 } else { 107 // non 0 => non 0 (or 0 => 0) 108 return params.SstoreResetGas, nil 109 } 110 } 111 112 func makeGasLog(n uint64) gasFunc { 113 return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 114 requestedSize, overflow := bigUint64(stack.Back(1)) 115 if overflow { 116 return 0, errGasUintOverflow 117 } 118 119 gas, err := memoryGasCost(mem, memorySize) 120 if err != nil { 121 return 0, err 122 } 123 124 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 125 return 0, errGasUintOverflow 126 } 127 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 128 return 0, errGasUintOverflow 129 } 130 131 var memorySizeGas uint64 132 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 133 return 0, errGasUintOverflow 134 } 135 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 136 return 0, errGasUintOverflow 137 } 138 return gas, nil 139 } 140 } 141 142 func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 143 var overflow bool 144 gas, err := memoryGasCost(mem, memorySize) 145 if err != nil { 146 return 0, err 147 } 148 149 if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow { 150 return 0, errGasUintOverflow 151 } 152 153 wordGas, overflow := bigUint64(stack.Back(1)) 154 if overflow { 155 return 0, errGasUintOverflow 156 } 157 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 158 return 0, errGasUintOverflow 159 } 160 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 161 return 0, errGasUintOverflow 162 } 163 return gas, nil 164 } 165 166 func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 167 gas, err := memoryGasCost(mem, memorySize) 168 if err != nil { 169 return 0, err 170 } 171 172 var overflow bool 173 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 174 return 0, errGasUintOverflow 175 } 176 177 wordGas, overflow := bigUint64(stack.Back(2)) 178 if overflow { 179 return 0, errGasUintOverflow 180 } 181 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { 182 return 0, errGasUintOverflow 183 } 184 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 185 return 0, errGasUintOverflow 186 } 187 return gas, nil 188 } 189 190 func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 191 gas, err := memoryGasCost(mem, memorySize) 192 if err != nil { 193 return 0, err 194 } 195 196 var overflow bool 197 if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow { 198 return 0, errGasUintOverflow 199 } 200 201 wordGas, overflow := bigUint64(stack.Back(3)) 202 if overflow { 203 return 0, errGasUintOverflow 204 } 205 206 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { 207 return 0, errGasUintOverflow 208 } 209 210 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 211 return 0, errGasUintOverflow 212 } 213 return gas, nil 214 } 215 216 func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 217 var overflow bool 218 gas, err := memoryGasCost(mem, memorySize) 219 if err != nil { 220 return 0, errGasUintOverflow 221 } 222 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 223 return 0, errGasUintOverflow 224 } 225 return gas, nil 226 } 227 228 func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 229 var overflow bool 230 gas, err := memoryGasCost(mem, memorySize) 231 if err != nil { 232 return 0, errGasUintOverflow 233 } 234 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 235 return 0, errGasUintOverflow 236 } 237 return gas, nil 238 } 239 240 func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 241 var overflow bool 242 gas, err := memoryGasCost(mem, memorySize) 243 if err != nil { 244 return 0, errGasUintOverflow 245 } 246 if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { 247 return 0, errGasUintOverflow 248 } 249 return gas, nil 250 } 251 252 func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 253 var overflow bool 254 gas, err := memoryGasCost(mem, memorySize) 255 if err != nil { 256 return 0, err 257 } 258 if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow { 259 return 0, errGasUintOverflow 260 } 261 return gas, nil 262 } 263 264 func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 265 return gt.Balance, nil 266 } 267 268 func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 269 return gt.ExtcodeSize, nil 270 } 271 272 func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 273 return gt.SLoad, nil 274 } 275 276 func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 277 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 278 279 var ( 280 gas = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas 281 overflow bool 282 ) 283 if gas, overflow = math.SafeAdd(gas, GasSlowStep); overflow { 284 return 0, errGasUintOverflow 285 } 286 return gas, nil 287 } 288 289 func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 290 var ( 291 gas = gt.Calls 292 transfersValue = stack.Back(2).Sign() != 0 293 address = common.BigToAddress(stack.Back(1)) 294 eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) 295 ) 296 if eip158 { 297 if evm.StateDB.Empty(address) && transfersValue { 298 gas += params.CallNewAccountGas 299 } 300 } else if !evm.StateDB.Exist(address) { 301 gas += params.CallNewAccountGas 302 } 303 if transfersValue { 304 gas += params.CallValueTransferGas 305 } 306 memoryGas, err := memoryGasCost(mem, memorySize) 307 if err != nil { 308 return 0, err 309 } 310 var overflow bool 311 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 312 return 0, errGasUintOverflow 313 } 314 315 cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) 316 if err != nil { 317 return 0, err 318 } 319 // Replace the stack item with the new gas calculation. This means that 320 // either the original item is left on the stack or the item is replaced by: 321 // (availableGas - gas) * 63 / 64 322 // We replace the stack item so that it's available when the opCall instruction is 323 // called. This information is otherwise lost due to the dependency on *current* 324 // available gas. 325 stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) 326 327 if gas, overflow = math.SafeAdd(gas, cg); overflow { 328 return 0, errGasUintOverflow 329 } 330 return gas, nil 331 } 332 333 func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 334 gas := gt.Calls 335 if stack.Back(2).Sign() != 0 { 336 gas += params.CallValueTransferGas 337 } 338 memoryGas, err := memoryGasCost(mem, memorySize) 339 if err != nil { 340 return 0, err 341 } 342 var overflow bool 343 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 344 return 0, errGasUintOverflow 345 } 346 347 cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) 348 if err != nil { 349 return 0, err 350 } 351 // Replace the stack item with the new gas calculation. This means that 352 // either the original item is left on the stack or the item is replaced by: 353 // (availableGas - gas) * 63 / 64 354 // We replace the stack item so that it's available when the opCall instruction is 355 // called. This information is otherwise lost due to the dependency on *current* 356 // available gas. 357 stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) 358 359 if gas, overflow = math.SafeAdd(gas, cg); overflow { 360 return 0, errGasUintOverflow 361 } 362 return gas, nil 363 } 364 365 func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 366 return memoryGasCost(mem, memorySize) 367 } 368 369 func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 370 var gas uint64 371 // EIP150 homestead gas reprice fork: 372 if evm.ChainConfig().IsEIP150(evm.BlockNumber) { 373 gas = gt.Suicide 374 var ( 375 address = common.BigToAddress(stack.Back(0)) 376 eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) 377 ) 378 379 if eip158 { 380 // if empty and transfers value 381 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 382 gas += gt.CreateBySuicide 383 } 384 } else if !evm.StateDB.Exist(address) { 385 gas += gt.CreateBySuicide 386 } 387 } 388 389 if !evm.StateDB.HasSuicided(contract.Address()) { 390 evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SuicideRefundGas)) 391 } 392 return gas, nil 393 } 394 395 func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 396 gas, err := memoryGasCost(mem, memorySize) 397 if err != nil { 398 return 0, err 399 } 400 var overflow bool 401 if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { 402 return 0, errGasUintOverflow 403 } 404 405 cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) 406 if err != nil { 407 return 0, err 408 } 409 // Replace the stack item with the new gas calculation. This means that 410 // either the original item is left on the stack or the item is replaced by: 411 // (availableGas - gas) * 63 / 64 412 // We replace the stack item so that it's available when the opCall instruction is 413 // called. 414 stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) 415 416 if gas, overflow = math.SafeAdd(gas, cg); overflow { 417 return 0, errGasUintOverflow 418 } 419 return gas, nil 420 } 421 422 func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 423 return GasFastestStep, nil 424 } 425 426 func gasSwap(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 427 return GasFastestStep, nil 428 } 429 430 func gasDup(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 431 return GasFastestStep, nil 432 }