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