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