| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | package vm | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	gmath "math" | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	"math/big" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common/math" | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/params" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | // memoryGasCosts calculates the quadratic gas for memory expansion. It does so | 
					
						
							|  |  |  | // only for the memory region that is expanded, not the total memory. | 
					
						
							|  |  |  | func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	// The maximum that will fit in a uint64 is max_word_count - 1 | 
					
						
							|  |  |  | 	// anything above that will result in an overflow. | 
					
						
							|  |  |  | 	if newMemSize > gmath.MaxUint64-32 { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if newMemSize == 0 { | 
					
						
							|  |  |  | 		return 0, nil | 
					
						
							| 
									
										
										
										
											2017-02-02 15:25:42 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	newMemSizeWords := toWordSize(newMemSize) | 
					
						
							|  |  |  | 	newMemSize = newMemSizeWords * 32 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if newMemSize > uint64(mem.Len()) { | 
					
						
							|  |  |  | 		square := newMemSizeWords * newMemSizeWords | 
					
						
							|  |  |  | 		linCoef := newMemSizeWords * params.MemoryGas | 
					
						
							|  |  |  | 		quadCoef := square / params.QuadCoeffDiv | 
					
						
							|  |  |  | 		newTotalFee := linCoef + quadCoef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fee := newTotalFee - mem.lastGasCost | 
					
						
							|  |  |  | 		mem.lastGasCost = newTotalFee | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return fee, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func constGasFunc(gas uint64) gasFunc { | 
					
						
							|  |  |  | 	return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 		return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasCalldataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	words, overflow := bigUint64(stack.Back(2)) | 
					
						
							|  |  |  | 	if overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, words); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		y, x = stack.Back(1), stack.Back(0) | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		val  = evm.StateDB.GetState(contract.Address(), common.BigToHash(x)) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	) | 
					
						
							|  |  |  | 	// This checks for 3 scenario's and calculates gas accordingly | 
					
						
							|  |  |  | 	// 1. From a zero-value address to a non-zero value         (NEW VALUE) | 
					
						
							|  |  |  | 	// 2. From a non-zero value address to a zero-value address (DELETE) | 
					
						
							|  |  |  | 	// 3. From a non-zero to a non-zero                         (CHANGE) | 
					
						
							|  |  |  | 	if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) { | 
					
						
							|  |  |  | 		// 0 => non 0 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		return params.SstoreSetGas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) { | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SstoreRefundGas)) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		return params.SstoreClearGas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		// non 0 => non 0 (or 0 => 0) | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		return params.SstoreResetGas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func makeGasLog(n uint64) gasFunc { | 
					
						
							|  |  |  | 	return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 		requestedSize, overflow := bigUint64(stack.Back(1)) | 
					
						
							|  |  |  | 		if overflow { | 
					
						
							|  |  |  | 			return 0, errGasUintOverflow | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return 0, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { | 
					
						
							|  |  |  | 			return 0, errGasUintOverflow | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { | 
					
						
							|  |  |  | 			return 0, errGasUintOverflow | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var memorySizeGas uint64 | 
					
						
							|  |  |  | 		if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { | 
					
						
							|  |  |  | 			return 0, errGasUintOverflow | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { | 
					
						
							|  |  |  | 			return 0, errGasUintOverflow | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	wordGas, overflow := bigUint64(stack.Back(1)) | 
					
						
							|  |  |  | 	if overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, wordGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-02 15:25:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	wordGas, overflow := bigUint64(stack.Back(2)) | 
					
						
							|  |  |  | 	if overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, wordGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wordGas, overflow := bigUint64(stack.Back(3)) | 
					
						
							|  |  |  | 	if overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, wordGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return gt.Balance, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return gt.ExtcodeSize, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return gt.SLoad, nil | 
					
						
							| 
									
										
										
										
											2017-02-02 15:25:42 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) | 
					
						
							| 
									
										
										
										
											2017-02-08 13:39:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 15:25:42 +01:00
										 |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		gas      = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas | 
					
						
							|  |  |  | 		overflow bool | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, GasSlowStep); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		gas            = gt.Calls | 
					
						
							|  |  |  | 		transfersValue = stack.Back(2).BitLen() > 0 | 
					
						
							|  |  |  | 		address        = common.BigToAddress(stack.Back(1)) | 
					
						
							|  |  |  | 		eip158         = evm.ChainConfig().IsEIP158(evm.BlockNumber) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	) | 
					
						
							|  |  |  | 	if eip158 { | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		if evm.StateDB.Empty(address) && transfersValue { | 
					
						
							|  |  |  | 			gas += params.CallNewAccountGas | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	} else if !evm.StateDB.Exist(address) { | 
					
						
							|  |  |  | 		gas += params.CallNewAccountGas | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if transfersValue { | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		gas += params.CallValueTransferGas | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memoryGas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	// Replace the stack item with the new gas calculation. This means that | 
					
						
							|  |  |  | 	// either the original item is left on the stack or the item is replaced by: | 
					
						
							|  |  |  | 	// (availableGas - gas) * 63 / 64 | 
					
						
							|  |  |  | 	// We replace the stack item so that it's available when the opCall instruction is | 
					
						
							|  |  |  | 	// called. This information is otherwise lost due to the dependency on *current* | 
					
						
							|  |  |  | 	// available gas. | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if gas, overflow = math.SafeAdd(gas, cg); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	gas := gt.Calls | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	if stack.Back(2).BitLen() > 0 { | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		gas += params.CallValueTransferGas | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memoryGas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	// Replace the stack item with the new gas calculation. This means that | 
					
						
							|  |  |  | 	// either the original item is left on the stack or the item is replaced by: | 
					
						
							|  |  |  | 	// (availableGas - gas) * 63 / 64 | 
					
						
							|  |  |  | 	// We replace the stack item so that it's available when the opCall instruction is | 
					
						
							|  |  |  | 	// called. This information is otherwise lost due to the dependency on *current* | 
					
						
							|  |  |  | 	// available gas. | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if gas, overflow = math.SafeAdd(gas, cg); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	return memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	var gas uint64 | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	// EIP150 homestead gas reprice fork: | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if evm.ChainConfig().IsEIP150(evm.BlockNumber) { | 
					
						
							|  |  |  | 		gas = gt.Suicide | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 		var ( | 
					
						
							|  |  |  | 			address = common.BigToAddress(stack.Back(0)) | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 			eip158  = evm.ChainConfig().IsEIP158(evm.BlockNumber) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 		) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if eip158 { | 
					
						
							|  |  |  | 			// if empty and transfers value | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 			if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).BitLen() > 0 { | 
					
						
							|  |  |  | 				gas += gt.CreateBySuicide | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 		} else if !evm.StateDB.Exist(address) { | 
					
						
							|  |  |  | 			gas += gt.CreateBySuicide | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if !evm.StateDB.HasSuicided(contract.Address()) { | 
					
						
							|  |  |  | 		evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SuicideRefundGas)) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	gas, err := memoryGasCost(mem, memorySize) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var overflow bool | 
					
						
							|  |  |  | 	if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	cg, err := callGas(gt, contract.Gas, gas, stack.Back(0)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 	// Replace the stack item with the new gas calculation. This means that | 
					
						
							|  |  |  | 	// either the original item is left on the stack or the item is replaced by: | 
					
						
							|  |  |  | 	// (availableGas - gas) * 63 / 64 | 
					
						
							|  |  |  | 	// We replace the stack item so that it's available when the opCall instruction is | 
					
						
							|  |  |  | 	// called. | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	stack.data[stack.len()-1] = new(big.Int).SetUint64(cg) | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | 	if gas, overflow = math.SafeAdd(gas, cg); overflow { | 
					
						
							|  |  |  | 		return 0, errGasUintOverflow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return gas, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return GasFastestStep, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasSwap(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return GasFastestStep, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 20:17:24 +01:00
										 |  |  | func gasDup(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { | 
					
						
							|  |  |  | 	return GasFastestStep, nil | 
					
						
							| 
									
										
										
										
											2017-01-05 11:52:10 +01:00
										 |  |  | } |