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