github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/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/vantum/vantum/common"
    21  	"github.com/vantum/vantum/common/math"
    22  	"github.com/vantum/vantum/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 common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) {
   128  		// 0 => non 0
   129  		return params.SstoreSetGas, nil
   130  	} else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) {
   131  		evm.StateDB.AddRefund(params.SstoreRefundGas)
   132  
   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 gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   245  	var overflow bool
   246  	gas, err := memoryGasCost(mem, memorySize)
   247  	if err != nil {
   248  		return 0, errGasUintOverflow
   249  	}
   250  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   251  		return 0, errGasUintOverflow
   252  	}
   253  	return gas, nil
   254  }
   255  
   256  func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   257  	var overflow bool
   258  	gas, err := memoryGasCost(mem, memorySize)
   259  	if err != nil {
   260  		return 0, errGasUintOverflow
   261  	}
   262  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   263  		return 0, errGasUintOverflow
   264  	}
   265  	return gas, nil
   266  }
   267  
   268  func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   269  	var overflow bool
   270  	gas, err := memoryGasCost(mem, memorySize)
   271  	if err != nil {
   272  		return 0, errGasUintOverflow
   273  	}
   274  	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
   275  		return 0, errGasUintOverflow
   276  	}
   277  	return gas, nil
   278  }
   279  
   280  func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   281  	var overflow bool
   282  	gas, err := memoryGasCost(mem, memorySize)
   283  	if err != nil {
   284  		return 0, err
   285  	}
   286  	if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow {
   287  		return 0, errGasUintOverflow
   288  	}
   289  	return gas, nil
   290  }
   291  
   292  func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   293  	return gt.Balance, nil
   294  }
   295  
   296  func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   297  	return gt.ExtcodeSize, nil
   298  }
   299  
   300  func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   301  	return gt.SLoad, nil
   302  }
   303  
   304  func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   305  	expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
   306  
   307  	var (
   308  		gas      = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas
   309  		overflow bool
   310  	)
   311  	if gas, overflow = math.SafeAdd(gas, GasSlowStep); overflow {
   312  		return 0, errGasUintOverflow
   313  	}
   314  	return gas, nil
   315  }
   316  
   317  func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   318  	var (
   319  		gas            = gt.Calls
   320  		transfersValue = stack.Back(2).Sign() != 0
   321  		address        = common.BigToAddress(stack.Back(1))
   322  		eip158         = evm.ChainConfig().IsEIP158(evm.BlockNumber)
   323  	)
   324  	if eip158 {
   325  		if transfersValue && evm.StateDB.Empty(address) {
   326  			gas += params.CallNewAccountGas
   327  		}
   328  	} else if !evm.StateDB.Exist(address) {
   329  		gas += params.CallNewAccountGas
   330  	}
   331  	if transfersValue {
   332  		gas += params.CallValueTransferGas
   333  	}
   334  	memoryGas, err := memoryGasCost(mem, memorySize)
   335  	if err != nil {
   336  		return 0, err
   337  	}
   338  	var overflow bool
   339  	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
   340  		return 0, errGasUintOverflow
   341  	}
   342  
   343  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   344  	if err != nil {
   345  		return 0, err
   346  	}
   347  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   348  		return 0, errGasUintOverflow
   349  	}
   350  	return gas, nil
   351  }
   352  
   353  func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   354  	gas := gt.Calls
   355  	if stack.Back(2).Sign() != 0 {
   356  		gas += params.CallValueTransferGas
   357  	}
   358  	memoryGas, err := memoryGasCost(mem, memorySize)
   359  	if err != nil {
   360  		return 0, err
   361  	}
   362  	var overflow bool
   363  	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
   364  		return 0, errGasUintOverflow
   365  	}
   366  
   367  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   368  	if err != nil {
   369  		return 0, err
   370  	}
   371  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   372  		return 0, errGasUintOverflow
   373  	}
   374  	return gas, nil
   375  }
   376  
   377  func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   378  	return memoryGasCost(mem, memorySize)
   379  }
   380  
   381  func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   382  	return memoryGasCost(mem, memorySize)
   383  }
   384  
   385  func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   386  	var gas uint64
   387  	// EIP150 homestead gas reprice fork:
   388  	if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
   389  		gas = gt.Suicide
   390  		var (
   391  			address = common.BigToAddress(stack.Back(0))
   392  			eip158  = evm.ChainConfig().IsEIP158(evm.BlockNumber)
   393  		)
   394  
   395  		if eip158 {
   396  			// if empty and transfers value
   397  			if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
   398  				gas += gt.CreateBySuicide
   399  			}
   400  		} else if !evm.StateDB.Exist(address) {
   401  			gas += gt.CreateBySuicide
   402  		}
   403  	}
   404  
   405  	if !evm.StateDB.HasSuicided(contract.Address()) {
   406  		evm.StateDB.AddRefund(params.SuicideRefundGas)
   407  	}
   408  	return gas, nil
   409  }
   410  
   411  func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   412  	gas, err := memoryGasCost(mem, memorySize)
   413  	if err != nil {
   414  		return 0, err
   415  	}
   416  	var overflow bool
   417  	if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
   418  		return 0, errGasUintOverflow
   419  	}
   420  
   421  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   422  	if err != nil {
   423  		return 0, err
   424  	}
   425  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   426  		return 0, errGasUintOverflow
   427  	}
   428  	return gas, nil
   429  }
   430  
   431  func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   432  	gas, err := memoryGasCost(mem, memorySize)
   433  	if err != nil {
   434  		return 0, err
   435  	}
   436  	var overflow bool
   437  	if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
   438  		return 0, errGasUintOverflow
   439  	}
   440  
   441  	evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
   442  	if err != nil {
   443  		return 0, err
   444  	}
   445  	if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
   446  		return 0, errGasUintOverflow
   447  	}
   448  	return gas, nil
   449  }
   450  
   451  func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   452  	return GasFastestStep, nil
   453  }
   454  
   455  func gasSwap(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   456  	return GasFastestStep, nil
   457  }
   458  
   459  func gasDup(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   460  	return GasFastestStep, nil
   461  }