github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/gas_table.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package vm 19 20 import ( 21 "errors" 22 23 "github.com/AigarNetwork/aigar/common" 24 "github.com/AigarNetwork/aigar/common/math" 25 "github.com/AigarNetwork/aigar/params" 26 ) 27 28 // memoryGasCost 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 if newMemSize == 0 { 32 return 0, nil 33 } 34 // The maximum that will fit in a uint64 is max_word_count - 1. Anything above 35 // that will result in an overflow. Additionally, a newMemSize which results in 36 // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to 37 // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used 38 // without overflowing the gas calculation. 39 if newMemSize > 0x1FFFFFFFE0 { 40 return 0, errGasUintOverflow 41 } 42 newMemSizeWords := toWordSize(newMemSize) 43 newMemSize = newMemSizeWords * 32 44 45 if newMemSize > uint64(mem.Len()) { 46 square := newMemSizeWords * newMemSizeWords 47 linCoef := newMemSizeWords * params.MemoryGas 48 quadCoef := square / params.QuadCoeffDiv 49 newTotalFee := linCoef + quadCoef 50 51 fee := newTotalFee - mem.lastGasCost 52 mem.lastGasCost = newTotalFee 53 54 return fee, nil 55 } 56 return 0, nil 57 } 58 59 // memoryCopierGas creates the gas functions for the following opcodes, and takes 60 // the stack position of the operand which determines the size of the data to copy 61 // as argument: 62 // CALLDATACOPY (stack position 2) 63 // CODECOPY (stack position 2) 64 // EXTCODECOPY (stack poition 3) 65 // RETURNDATACOPY (stack position 2) 66 func memoryCopierGas(stackpos int) gasFunc { 67 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 68 // Gas for expanding the memory 69 gas, err := memoryGasCost(mem, memorySize) 70 if err != nil { 71 return 0, err 72 } 73 // And gas for copying data, charged per word at param.CopyGas 74 words, overflow := bigUint64(stack.Back(stackpos)) 75 if overflow { 76 return 0, errGasUintOverflow 77 } 78 79 if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { 80 return 0, errGasUintOverflow 81 } 82 83 if gas, overflow = math.SafeAdd(gas, words); overflow { 84 return 0, errGasUintOverflow 85 } 86 return gas, nil 87 } 88 } 89 90 var ( 91 gasCallDataCopy = memoryCopierGas(2) 92 gasCodeCopy = memoryCopierGas(2) 93 gasExtCodeCopy = memoryCopierGas(3) 94 gasReturnDataCopy = memoryCopierGas(2) 95 ) 96 97 func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 98 var ( 99 y, x = stack.Back(1), stack.Back(0) 100 current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 101 ) 102 // The legacy gas metering only takes into consideration the current state 103 // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283) 104 // OR Constantinople is not active 105 if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople { 106 // This checks for 3 scenario's and calculates gas accordingly: 107 // 108 // 1. From a zero-value address to a non-zero value (NEW VALUE) 109 // 2. From a non-zero value address to a zero-value address (DELETE) 110 // 3. From a non-zero to a non-zero (CHANGE) 111 switch { 112 case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0 113 return params.SstoreSetGas, nil 114 case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0 115 evm.StateDB.AddRefund(params.SstoreRefundGas) 116 return params.SstoreClearGas, nil 117 default: // non 0 => non 0 (or 0 => 0) 118 return params.SstoreResetGas, nil 119 } 120 } 121 // The new gas metering is based on net gas costs (EIP-1283): 122 // 123 // 1. If current value equals new value (this is a no-op), 200 gas is deducted. 124 // 2. If current value does not equal new value 125 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context) 126 // 2.1.1. If original value is 0, 20000 gas is deducted. 127 // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter. 128 // 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. 129 // 2.2.1. If original value is not 0 130 // 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. 131 // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter. 132 // 2.2.2. If original value equals new value (this storage slot is reset) 133 // 2.2.2.1. If original value is 0, add 19800 gas to refund counter. 134 // 2.2.2.2. Otherwise, add 4800 gas to refund counter. 135 value := common.BigToHash(y) 136 if current == value { // noop (1) 137 return params.NetSstoreNoopGas, nil 138 } 139 original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x)) 140 if original == current { 141 if original == (common.Hash{}) { // create slot (2.1.1) 142 return params.NetSstoreInitGas, nil 143 } 144 if value == (common.Hash{}) { // delete slot (2.1.2b) 145 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 146 } 147 return params.NetSstoreCleanGas, nil // write existing slot (2.1.2) 148 } 149 if original != (common.Hash{}) { 150 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 151 evm.StateDB.SubRefund(params.NetSstoreClearRefund) 152 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 153 evm.StateDB.AddRefund(params.NetSstoreClearRefund) 154 } 155 } 156 if original == value { 157 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 158 evm.StateDB.AddRefund(params.NetSstoreResetClearRefund) 159 } else { // reset to original existing slot (2.2.2.2) 160 evm.StateDB.AddRefund(params.NetSstoreResetRefund) 161 } 162 } 163 return params.NetSstoreDirtyGas, nil 164 } 165 166 // 0. If *gasleft* is less than or equal to 2300, fail the current call. 167 // 1. If current value equals new value (this is a no-op), SSTORE_NOOP_GAS gas is deducted. 168 // 2. If current value does not equal new value: 169 // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): 170 // 2.1.1. If original value is 0, SSTORE_INIT_GAS gas is deducted. 171 // 2.1.2. Otherwise, SSTORE_CLEAN_GAS gas is deducted. If new value is 0, add SSTORE_CLEAR_REFUND to refund counter. 172 // 2.2. If original value does not equal current value (this storage slot is dirty), SSTORE_DIRTY_GAS gas is deducted. Apply both of the following clauses: 173 // 2.2.1. If original value is not 0: 174 // 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEAR_REFUND gas from refund counter. We can prove that refund counter will never go below 0. 175 // 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEAR_REFUND gas to refund counter. 176 // 2.2.2. If original value equals new value (this storage slot is reset): 177 // 2.2.2.1. If original value is 0, add SSTORE_INIT_REFUND to refund counter. 178 // 2.2.2.2. Otherwise, add SSTORE_CLEAN_REFUND gas to refund counter. 179 func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 180 // If we fail the minimum gas availability invariant, fail (0) 181 if contract.Gas <= params.SstoreSentryGasEIP2200 { 182 return 0, errors.New("not enough gas for reentrancy sentry") 183 } 184 // Gas sentry honoured, do the actual gas calculation based on the stored value 185 var ( 186 y, x = stack.Back(1), stack.Back(0) 187 current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) 188 ) 189 value := common.BigToHash(y) 190 191 if current == value { // noop (1) 192 return params.SstoreNoopGasEIP2200, nil 193 } 194 original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x)) 195 if original == current { 196 if original == (common.Hash{}) { // create slot (2.1.1) 197 return params.SstoreInitGasEIP2200, nil 198 } 199 if value == (common.Hash{}) { // delete slot (2.1.2b) 200 evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200) 201 } 202 return params.SstoreCleanGasEIP2200, nil // write existing slot (2.1.2) 203 } 204 if original != (common.Hash{}) { 205 if current == (common.Hash{}) { // recreate slot (2.2.1.1) 206 evm.StateDB.SubRefund(params.SstoreClearRefundEIP2200) 207 } else if value == (common.Hash{}) { // delete slot (2.2.1.2) 208 evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200) 209 } 210 } 211 if original == value { 212 if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) 213 evm.StateDB.AddRefund(params.SstoreInitRefundEIP2200) 214 } else { // reset to original existing slot (2.2.2.2) 215 evm.StateDB.AddRefund(params.SstoreCleanRefundEIP2200) 216 } 217 } 218 return params.SstoreDirtyGasEIP2200, nil // dirty update (2.2) 219 } 220 221 func makeGasLog(n uint64) gasFunc { 222 return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 223 requestedSize, overflow := bigUint64(stack.Back(1)) 224 if overflow { 225 return 0, errGasUintOverflow 226 } 227 228 gas, err := memoryGasCost(mem, memorySize) 229 if err != nil { 230 return 0, err 231 } 232 233 if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { 234 return 0, errGasUintOverflow 235 } 236 if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { 237 return 0, errGasUintOverflow 238 } 239 240 var memorySizeGas uint64 241 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { 242 return 0, errGasUintOverflow 243 } 244 if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { 245 return 0, errGasUintOverflow 246 } 247 return gas, nil 248 } 249 } 250 251 func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 252 gas, err := memoryGasCost(mem, memorySize) 253 if err != nil { 254 return 0, err 255 } 256 wordGas, overflow := bigUint64(stack.Back(1)) 257 if overflow { 258 return 0, errGasUintOverflow 259 } 260 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 261 return 0, errGasUintOverflow 262 } 263 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 264 return 0, errGasUintOverflow 265 } 266 return gas, nil 267 } 268 269 // pureMemoryGascost is used by several operations, which aside from their 270 // static cost have a dynamic cost which is solely based on the memory 271 // expansion 272 func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 273 return memoryGasCost(mem, memorySize) 274 } 275 276 var ( 277 gasReturn = pureMemoryGascost 278 gasRevert = pureMemoryGascost 279 gasMLoad = pureMemoryGascost 280 gasMStore8 = pureMemoryGascost 281 gasMStore = pureMemoryGascost 282 gasCreate = pureMemoryGascost 283 ) 284 285 func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 286 gas, err := memoryGasCost(mem, memorySize) 287 if err != nil { 288 return 0, err 289 } 290 wordGas, overflow := bigUint64(stack.Back(2)) 291 if overflow { 292 return 0, errGasUintOverflow 293 } 294 if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { 295 return 0, errGasUintOverflow 296 } 297 if gas, overflow = math.SafeAdd(gas, wordGas); overflow { 298 return 0, errGasUintOverflow 299 } 300 return gas, nil 301 } 302 303 func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 304 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 305 306 var ( 307 gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas 308 overflow bool 309 ) 310 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 311 return 0, errGasUintOverflow 312 } 313 return gas, nil 314 } 315 316 func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 317 expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) 318 319 var ( 320 gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas 321 overflow bool 322 ) 323 if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { 324 return 0, errGasUintOverflow 325 } 326 return gas, nil 327 } 328 329 func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 330 var ( 331 gas uint64 332 transfersValue = stack.Back(2).Sign() != 0 333 address = common.BigToAddress(stack.Back(1)) 334 ) 335 if evm.chainRules.IsEIP158 { 336 if transfersValue && evm.StateDB.Empty(address) { 337 gas += params.CallNewAccountGas 338 } 339 } else if !evm.StateDB.Exist(address) { 340 gas += params.CallNewAccountGas 341 } 342 if transfersValue { 343 gas += params.CallValueTransferGas 344 } 345 memoryGas, err := memoryGasCost(mem, memorySize) 346 if err != nil { 347 return 0, err 348 } 349 var overflow bool 350 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 351 return 0, errGasUintOverflow 352 } 353 354 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 355 if err != nil { 356 return 0, err 357 } 358 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 359 return 0, errGasUintOverflow 360 } 361 return gas, nil 362 } 363 364 func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 365 memoryGas, err := memoryGasCost(mem, memorySize) 366 if err != nil { 367 return 0, err 368 } 369 var ( 370 gas uint64 371 overflow bool 372 ) 373 if stack.Back(2).Sign() != 0 { 374 gas += params.CallValueTransferGas 375 } 376 if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { 377 return 0, errGasUintOverflow 378 } 379 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 380 if err != nil { 381 return 0, err 382 } 383 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 384 return 0, errGasUintOverflow 385 } 386 return gas, nil 387 } 388 389 func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 390 gas, err := memoryGasCost(mem, memorySize) 391 if err != nil { 392 return 0, err 393 } 394 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 395 if err != nil { 396 return 0, err 397 } 398 var overflow bool 399 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 400 return 0, errGasUintOverflow 401 } 402 return gas, nil 403 } 404 405 func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 406 gas, err := memoryGasCost(mem, memorySize) 407 if err != nil { 408 return 0, err 409 } 410 evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0)) 411 if err != nil { 412 return 0, err 413 } 414 var overflow bool 415 if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { 416 return 0, errGasUintOverflow 417 } 418 return gas, nil 419 } 420 421 func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { 422 var gas uint64 423 // EIP150 homestead gas reprice fork: 424 if evm.chainRules.IsEIP150 { 425 gas = params.SelfdestructGasEIP150 426 var address = common.BigToAddress(stack.Back(0)) 427 428 if evm.chainRules.IsEIP158 { 429 // if empty and transfers value 430 if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { 431 gas += params.CreateBySelfdestructGas 432 } 433 } else if !evm.StateDB.Exist(address) { 434 gas += params.CreateBySelfdestructGas 435 } 436 } 437 438 if !evm.StateDB.HasSuicided(contract.Address()) { 439 evm.StateDB.AddRefund(params.SelfdestructRefundGas) 440 } 441 return gas, nil 442 }