github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/common"
    21  	"github.com/luckypickle/go-ethereum-vet/common/math"
    22  	"github.com/luckypickle/go-ethereum-vet/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  		val  = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
   122  	)
   123  	// This checks for 3 scenario's and calculates gas accordingly
   124  	// 1. From a zero-value address to a non-zero value         (NEW VALUE)
   125  	// 2. From a non-zero value address to a zero-value address (DELETE)
   126  	// 3. From a non-zero to a non-zero                         (CHANGE)
   127  	if val == (common.Hash{}) && y.Sign() != 0 {
   128  		// 0 => non 0
   129  		return params.SstoreSetGas, nil
   130  	} else if val != (common.Hash{}) && y.Sign() == 0 {
   131  		// non 0 => 0
   132  		evm.StateDB.AddRefund(params.SstoreRefundGas)
   133  		return params.SstoreClearGas, nil
   134  	} else {
   135  		// non 0 => non 0 (or 0 => 0)
   136  		return params.SstoreResetGas, nil
   137  	}
   138  }
   139  
   140  func makeGasLog(n uint64) gasFunc {
   141  	return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   142  		requestedSize, overflow := bigUint64(stack.Back(1))
   143  		if overflow {
   144  			return 0, errGasUintOverflow
   145  		}
   146  
   147  		gas, err := memoryGasCost(mem, memorySize)
   148  		if err != nil {
   149  			return 0, err
   150  		}
   151  
   152  		if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
   153  			return 0, errGasUintOverflow
   154  		}
   155  		if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
   156  			return 0, errGasUintOverflow
   157  		}
   158  
   159  		var memorySizeGas uint64
   160  		if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
   161  			return 0, errGasUintOverflow
   162  		}
   163  		if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
   164  			return 0, errGasUintOverflow
   165  		}
   166  		return gas, nil
   167  	}
   168  }
   169  
   170  func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   171  	var overflow bool
   172  	gas, err := memoryGasCost(mem, memorySize)
   173  	if err != nil {
   174  		return 0, err
   175  	}
   176  
   177  	if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow {
   178  		return 0, errGasUintOverflow
   179  	}
   180  
   181  	wordGas, overflow := bigUint64(stack.Back(1))
   182  	if overflow {
   183  		return 0, errGasUintOverflow
   184  	}
   185  	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
   186  		return 0, errGasUintOverflow
   187  	}
   188  	if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
   189  		return 0, errGasUintOverflow
   190  	}
   191  	return gas, nil
   192  }
   193  
   194  func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   195  	gas, err := memoryGasCost(mem, memorySize)
   196  	if err != nil {
   197  		return 0, err
   198  	}
   199  
   200  	var overflow bool
   201  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   202  		return 0, errGasUintOverflow
   203  	}
   204  
   205  	wordGas, overflow := bigUint64(stack.Back(2))
   206  	if overflow {
   207  		return 0, errGasUintOverflow
   208  	}
   209  	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow {
   210  		return 0, errGasUintOverflow
   211  	}
   212  	if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
   213  		return 0, errGasUintOverflow
   214  	}
   215  	return gas, nil
   216  }
   217  
   218  func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   219  	gas, err := memoryGasCost(mem, memorySize)
   220  	if err != nil {
   221  		return 0, err
   222  	}
   223  
   224  	var overflow bool
   225  	if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow {
   226  		return 0, errGasUintOverflow
   227  	}
   228  
   229  	wordGas, overflow := bigUint64(stack.Back(3))
   230  	if overflow {
   231  		return 0, errGasUintOverflow
   232  	}
   233  
   234  	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow {
   235  		return 0, errGasUintOverflow
   236  	}
   237  
   238  	if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
   239  		return 0, errGasUintOverflow
   240  	}
   241  	return gas, nil
   242  }
   243  
   244  func gasExtCodeHash(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   245  	return gt.ExtcodeHash, nil
   246  }
   247  
   248  func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   249  	var overflow bool
   250  	gas, err := memoryGasCost(mem, memorySize)
   251  	if err != nil {
   252  		return 0, errGasUintOverflow
   253  	}
   254  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   255  		return 0, errGasUintOverflow
   256  	}
   257  	return gas, nil
   258  }
   259  
   260  func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   261  	var overflow bool
   262  	gas, err := memoryGasCost(mem, memorySize)
   263  	if err != nil {
   264  		return 0, errGasUintOverflow
   265  	}
   266  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   267  		return 0, errGasUintOverflow
   268  	}
   269  	return gas, nil
   270  }
   271  
   272  func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   273  	var overflow bool
   274  	gas, err := memoryGasCost(mem, memorySize)
   275  	if err != nil {
   276  		return 0, errGasUintOverflow
   277  	}
   278  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   279  		return 0, errGasUintOverflow
   280  	}
   281  	return gas, nil
   282  }
   283  
   284  func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   285  	var overflow bool
   286  	gas, err := memoryGasCost(mem, memorySize)
   287  	if err != nil {
   288  		return 0, err
   289  	}
   290  	if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow {
   291  		return 0, errGasUintOverflow
   292  	}
   293  	return gas, nil
   294  }
   295  
   296  func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   297  	var overflow bool
   298  	gas, err := memoryGasCost(mem, memorySize)
   299  	if err != nil {
   300  		return 0, err
   301  	}
   302  	if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
   303  		return 0, errGasUintOverflow
   304  	}
   305  	return gas, nil
   306  }
   307  
   308  func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   309  	return gt.Balance, nil
   310  }
   311  
   312  func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   313  	return gt.ExtcodeSize, nil
   314  }
   315  
   316  func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   317  	return gt.SLoad, nil
   318  }
   319  
   320  func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   321  	expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
   322  
   323  	var (
   324  		gas      = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas
   325  		overflow bool
   326  	)
   327  	if gas, overflow = math.SafeAdd(gas, GasSlowStep); overflow {
   328  		return 0, errGasUintOverflow
   329  	}
   330  	return gas, nil
   331  }
   332  
   333  func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   334  	var (
   335  		gas            = gt.Calls
   336  		transfersValue = stack.Back(2).Sign() != 0
   337  		address        = common.BigToAddress(stack.Back(1))
   338  		eip158         = evm.ChainConfig().IsEIP158(evm.BlockNumber)
   339  	)
   340  	if eip158 {
   341  		if transfersValue && evm.StateDB.Empty(address) {
   342  			gas += params.CallNewAccountGas
   343  		}
   344  	} else if !evm.StateDB.Exist(address) {
   345  		gas += params.CallNewAccountGas
   346  	}
   347  	if transfersValue {
   348  		gas += params.CallValueTransferGas
   349  	}
   350  	memoryGas, err := memoryGasCost(mem, memorySize)
   351  	if err != nil {
   352  		return 0, err
   353  	}
   354  	var overflow bool
   355  	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
   356  		return 0, errGasUintOverflow
   357  	}
   358  
   359  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   360  	if err != nil {
   361  		return 0, err
   362  	}
   363  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   364  		return 0, errGasUintOverflow
   365  	}
   366  	return gas, nil
   367  }
   368  
   369  func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   370  	gas := gt.Calls
   371  	if stack.Back(2).Sign() != 0 {
   372  		gas += params.CallValueTransferGas
   373  	}
   374  	memoryGas, err := memoryGasCost(mem, memorySize)
   375  	if err != nil {
   376  		return 0, err
   377  	}
   378  	var overflow bool
   379  	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
   380  		return 0, errGasUintOverflow
   381  	}
   382  
   383  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   384  	if err != nil {
   385  		return 0, err
   386  	}
   387  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   388  		return 0, errGasUintOverflow
   389  	}
   390  	return gas, nil
   391  }
   392  
   393  func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   394  	return memoryGasCost(mem, memorySize)
   395  }
   396  
   397  func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   398  	return memoryGasCost(mem, memorySize)
   399  }
   400  
   401  func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   402  	var gas uint64
   403  	// EIP150 homestead gas reprice fork:
   404  	if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
   405  		gas = gt.Suicide
   406  		var (
   407  			address = common.BigToAddress(stack.Back(0))
   408  			eip158  = evm.ChainConfig().IsEIP158(evm.BlockNumber)
   409  		)
   410  
   411  		if eip158 {
   412  			// if empty and transfers value
   413  			if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
   414  				gas += gt.CreateBySuicide
   415  			}
   416  		} else if !evm.StateDB.Exist(address) {
   417  			gas += gt.CreateBySuicide
   418  		}
   419  	}
   420  
   421  	if !evm.StateDB.HasSuicided(contract.Address()) {
   422  		evm.StateDB.AddRefund(params.SuicideRefundGas)
   423  	}
   424  	return gas, nil
   425  }
   426  
   427  func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   428  	gas, err := memoryGasCost(mem, memorySize)
   429  	if err != nil {
   430  		return 0, err
   431  	}
   432  	var overflow bool
   433  	if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
   434  		return 0, errGasUintOverflow
   435  	}
   436  
   437  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   438  	if err != nil {
   439  		return 0, err
   440  	}
   441  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   442  		return 0, errGasUintOverflow
   443  	}
   444  	return gas, nil
   445  }
   446  
   447  func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   448  	gas, err := memoryGasCost(mem, memorySize)
   449  	if err != nil {
   450  		return 0, err
   451  	}
   452  	var overflow bool
   453  	if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
   454  		return 0, errGasUintOverflow
   455  	}
   456  
   457  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   458  	if err != nil {
   459  		return 0, err
   460  	}
   461  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   462  		return 0, errGasUintOverflow
   463  	}
   464  	return gas, nil
   465  }
   466  
   467  func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   468  	return GasFastestStep, nil
   469  }
   470  
   471  func gasSwap(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   472  	return GasFastestStep, nil
   473  }
   474  
   475  func gasDup(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   476  	return GasFastestStep, nil
   477  }