github.com/kjezek/go-ethereum@v1.9.6/core/vm/instructions.go (about)

     1  // Copyright 2015 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  	"errors"
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/common/math"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/params"
    27  	"golang.org/x/crypto/sha3"
    28  )
    29  
    30  var (
    31  	bigZero                  = new(big.Int)
    32  	tt255                    = math.BigPow(2, 255)
    33  	errWriteProtection       = errors.New("evm: write protection")
    34  	errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
    35  	errExecutionReverted     = errors.New("evm: execution reverted")
    36  	errMaxCodeSizeExceeded   = errors.New("evm: max code size exceeded")
    37  	errInvalidJump           = errors.New("evm: invalid jump destination")
    38  )
    39  
    40  func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    41  	x, y := stack.pop(), stack.peek()
    42  	math.U256(y.Add(x, y))
    43  
    44  	interpreter.intPool.put(x)
    45  	return nil, nil
    46  }
    47  
    48  func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    49  	x, y := stack.pop(), stack.peek()
    50  	math.U256(y.Sub(x, y))
    51  
    52  	interpreter.intPool.put(x)
    53  	return nil, nil
    54  }
    55  
    56  func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    57  	x, y := stack.pop(), stack.pop()
    58  	stack.push(math.U256(x.Mul(x, y)))
    59  
    60  	interpreter.intPool.put(y)
    61  
    62  	return nil, nil
    63  }
    64  
    65  func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    66  	x, y := stack.pop(), stack.peek()
    67  	if y.Sign() != 0 {
    68  		math.U256(y.Div(x, y))
    69  	} else {
    70  		y.SetUint64(0)
    71  	}
    72  	interpreter.intPool.put(x)
    73  	return nil, nil
    74  }
    75  
    76  func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    77  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
    78  	res := interpreter.intPool.getZero()
    79  
    80  	if y.Sign() == 0 || x.Sign() == 0 {
    81  		stack.push(res)
    82  	} else {
    83  		if x.Sign() != y.Sign() {
    84  			res.Div(x.Abs(x), y.Abs(y))
    85  			res.Neg(res)
    86  		} else {
    87  			res.Div(x.Abs(x), y.Abs(y))
    88  		}
    89  		stack.push(math.U256(res))
    90  	}
    91  	interpreter.intPool.put(x, y)
    92  	return nil, nil
    93  }
    94  
    95  func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    96  	x, y := stack.pop(), stack.pop()
    97  	if y.Sign() == 0 {
    98  		stack.push(x.SetUint64(0))
    99  	} else {
   100  		stack.push(math.U256(x.Mod(x, y)))
   101  	}
   102  	interpreter.intPool.put(y)
   103  	return nil, nil
   104  }
   105  
   106  func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   107  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
   108  	res := interpreter.intPool.getZero()
   109  
   110  	if y.Sign() == 0 {
   111  		stack.push(res)
   112  	} else {
   113  		if x.Sign() < 0 {
   114  			res.Mod(x.Abs(x), y.Abs(y))
   115  			res.Neg(res)
   116  		} else {
   117  			res.Mod(x.Abs(x), y.Abs(y))
   118  		}
   119  		stack.push(math.U256(res))
   120  	}
   121  	interpreter.intPool.put(x, y)
   122  	return nil, nil
   123  }
   124  
   125  func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   126  	base, exponent := stack.pop(), stack.pop()
   127  	// some shortcuts
   128  	cmpToOne := exponent.Cmp(big1)
   129  	if cmpToOne < 0 { // Exponent is zero
   130  		// x ^ 0 == 1
   131  		stack.push(base.SetUint64(1))
   132  	} else if base.Sign() == 0 {
   133  		// 0 ^ y, if y != 0, == 0
   134  		stack.push(base.SetUint64(0))
   135  	} else if cmpToOne == 0 { // Exponent is one
   136  		// x ^ 1 == x
   137  		stack.push(base)
   138  	} else {
   139  		stack.push(math.Exp(base, exponent))
   140  		interpreter.intPool.put(base)
   141  	}
   142  	interpreter.intPool.put(exponent)
   143  	return nil, nil
   144  }
   145  
   146  func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   147  	back := stack.pop()
   148  	if back.Cmp(big.NewInt(31)) < 0 {
   149  		bit := uint(back.Uint64()*8 + 7)
   150  		num := stack.pop()
   151  		mask := back.Lsh(common.Big1, bit)
   152  		mask.Sub(mask, common.Big1)
   153  		if num.Bit(int(bit)) > 0 {
   154  			num.Or(num, mask.Not(mask))
   155  		} else {
   156  			num.And(num, mask)
   157  		}
   158  
   159  		stack.push(math.U256(num))
   160  	}
   161  
   162  	interpreter.intPool.put(back)
   163  	return nil, nil
   164  }
   165  
   166  func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   167  	x := stack.peek()
   168  	math.U256(x.Not(x))
   169  	return nil, nil
   170  }
   171  
   172  func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   173  	x, y := stack.pop(), stack.peek()
   174  	if x.Cmp(y) < 0 {
   175  		y.SetUint64(1)
   176  	} else {
   177  		y.SetUint64(0)
   178  	}
   179  	interpreter.intPool.put(x)
   180  	return nil, nil
   181  }
   182  
   183  func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   184  	x, y := stack.pop(), stack.peek()
   185  	if x.Cmp(y) > 0 {
   186  		y.SetUint64(1)
   187  	} else {
   188  		y.SetUint64(0)
   189  	}
   190  	interpreter.intPool.put(x)
   191  	return nil, nil
   192  }
   193  
   194  func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   195  	x, y := stack.pop(), stack.peek()
   196  
   197  	xSign := x.Cmp(tt255)
   198  	ySign := y.Cmp(tt255)
   199  
   200  	switch {
   201  	case xSign >= 0 && ySign < 0:
   202  		y.SetUint64(1)
   203  
   204  	case xSign < 0 && ySign >= 0:
   205  		y.SetUint64(0)
   206  
   207  	default:
   208  		if x.Cmp(y) < 0 {
   209  			y.SetUint64(1)
   210  		} else {
   211  			y.SetUint64(0)
   212  		}
   213  	}
   214  	interpreter.intPool.put(x)
   215  	return nil, nil
   216  }
   217  
   218  func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   219  	x, y := stack.pop(), stack.peek()
   220  
   221  	xSign := x.Cmp(tt255)
   222  	ySign := y.Cmp(tt255)
   223  
   224  	switch {
   225  	case xSign >= 0 && ySign < 0:
   226  		y.SetUint64(0)
   227  
   228  	case xSign < 0 && ySign >= 0:
   229  		y.SetUint64(1)
   230  
   231  	default:
   232  		if x.Cmp(y) > 0 {
   233  			y.SetUint64(1)
   234  		} else {
   235  			y.SetUint64(0)
   236  		}
   237  	}
   238  	interpreter.intPool.put(x)
   239  	return nil, nil
   240  }
   241  
   242  func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   243  	x, y := stack.pop(), stack.peek()
   244  	if x.Cmp(y) == 0 {
   245  		y.SetUint64(1)
   246  	} else {
   247  		y.SetUint64(0)
   248  	}
   249  	interpreter.intPool.put(x)
   250  	return nil, nil
   251  }
   252  
   253  func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   254  	x := stack.peek()
   255  	if x.Sign() > 0 {
   256  		x.SetUint64(0)
   257  	} else {
   258  		x.SetUint64(1)
   259  	}
   260  	return nil, nil
   261  }
   262  
   263  func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   264  	x, y := stack.pop(), stack.pop()
   265  	stack.push(x.And(x, y))
   266  
   267  	interpreter.intPool.put(y)
   268  	return nil, nil
   269  }
   270  
   271  func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   272  	x, y := stack.pop(), stack.peek()
   273  	y.Or(x, y)
   274  
   275  	interpreter.intPool.put(x)
   276  	return nil, nil
   277  }
   278  
   279  func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   280  	x, y := stack.pop(), stack.peek()
   281  	y.Xor(x, y)
   282  
   283  	interpreter.intPool.put(x)
   284  	return nil, nil
   285  }
   286  
   287  func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   288  	th, val := stack.pop(), stack.peek()
   289  	if th.Cmp(common.Big32) < 0 {
   290  		b := math.Byte(val, 32, int(th.Int64()))
   291  		val.SetUint64(uint64(b))
   292  	} else {
   293  		val.SetUint64(0)
   294  	}
   295  	interpreter.intPool.put(th)
   296  	return nil, nil
   297  }
   298  
   299  func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   300  	x, y, z := stack.pop(), stack.pop(), stack.pop()
   301  	if z.Cmp(bigZero) > 0 {
   302  		x.Add(x, y)
   303  		x.Mod(x, z)
   304  		stack.push(math.U256(x))
   305  	} else {
   306  		stack.push(x.SetUint64(0))
   307  	}
   308  	interpreter.intPool.put(y, z)
   309  	return nil, nil
   310  }
   311  
   312  func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   313  	x, y, z := stack.pop(), stack.pop(), stack.pop()
   314  	if z.Cmp(bigZero) > 0 {
   315  		x.Mul(x, y)
   316  		x.Mod(x, z)
   317  		stack.push(math.U256(x))
   318  	} else {
   319  		stack.push(x.SetUint64(0))
   320  	}
   321  	interpreter.intPool.put(y, z)
   322  	return nil, nil
   323  }
   324  
   325  // opSHL implements Shift Left
   326  // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
   327  // and pushes on the stack arg2 shifted to the left by arg1 number of bits.
   328  func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   329  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   330  	shift, value := math.U256(stack.pop()), math.U256(stack.peek())
   331  	defer interpreter.intPool.put(shift) // First operand back into the pool
   332  
   333  	if shift.Cmp(common.Big256) >= 0 {
   334  		value.SetUint64(0)
   335  		return nil, nil
   336  	}
   337  	n := uint(shift.Uint64())
   338  	math.U256(value.Lsh(value, n))
   339  
   340  	return nil, nil
   341  }
   342  
   343  // opSHR implements Logical Shift Right
   344  // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
   345  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
   346  func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   347  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   348  	shift, value := math.U256(stack.pop()), math.U256(stack.peek())
   349  	defer interpreter.intPool.put(shift) // First operand back into the pool
   350  
   351  	if shift.Cmp(common.Big256) >= 0 {
   352  		value.SetUint64(0)
   353  		return nil, nil
   354  	}
   355  	n := uint(shift.Uint64())
   356  	math.U256(value.Rsh(value, n))
   357  
   358  	return nil, nil
   359  }
   360  
   361  // opSAR implements Arithmetic Shift Right
   362  // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
   363  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
   364  func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   365  	// Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one
   366  	shift, value := math.U256(stack.pop()), math.S256(stack.pop())
   367  	defer interpreter.intPool.put(shift) // First operand back into the pool
   368  
   369  	if shift.Cmp(common.Big256) >= 0 {
   370  		if value.Sign() >= 0 {
   371  			value.SetUint64(0)
   372  		} else {
   373  			value.SetInt64(-1)
   374  		}
   375  		stack.push(math.U256(value))
   376  		return nil, nil
   377  	}
   378  	n := uint(shift.Uint64())
   379  	value.Rsh(value, n)
   380  	stack.push(math.U256(value))
   381  
   382  	return nil, nil
   383  }
   384  
   385  func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   386  	offset, size := stack.pop(), stack.pop()
   387  	data := memory.Get(offset.Int64(), size.Int64())
   388  
   389  	if interpreter.hasher == nil {
   390  		interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
   391  	} else {
   392  		interpreter.hasher.Reset()
   393  	}
   394  	interpreter.hasher.Write(data)
   395  	interpreter.hasher.Read(interpreter.hasherBuf[:])
   396  
   397  	evm := interpreter.evm
   398  	if evm.vmConfig.EnablePreimageRecording {
   399  		evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
   400  	}
   401  	stack.push(interpreter.intPool.get().SetBytes(interpreter.hasherBuf[:]))
   402  
   403  	interpreter.intPool.put(offset, size)
   404  	return nil, nil
   405  }
   406  
   407  func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   408  	stack.push(interpreter.intPool.get().SetBytes(contract.Address().Bytes()))
   409  	return nil, nil
   410  }
   411  
   412  func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   413  	slot := stack.peek()
   414  	slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
   415  	return nil, nil
   416  }
   417  
   418  func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   419  	stack.push(interpreter.intPool.get().SetBytes(interpreter.evm.Origin.Bytes()))
   420  	return nil, nil
   421  }
   422  
   423  func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   424  	stack.push(interpreter.intPool.get().SetBytes(contract.Caller().Bytes()))
   425  	return nil, nil
   426  }
   427  
   428  func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   429  	stack.push(interpreter.intPool.get().Set(contract.value))
   430  	return nil, nil
   431  }
   432  
   433  func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   434  	stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
   435  	return nil, nil
   436  }
   437  
   438  func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   439  	stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
   440  	return nil, nil
   441  }
   442  
   443  func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   444  	var (
   445  		memOffset  = stack.pop()
   446  		dataOffset = stack.pop()
   447  		length     = stack.pop()
   448  	)
   449  	memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
   450  
   451  	interpreter.intPool.put(memOffset, dataOffset, length)
   452  	return nil, nil
   453  }
   454  
   455  func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   456  	stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
   457  	return nil, nil
   458  }
   459  
   460  func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   461  	var (
   462  		memOffset  = stack.pop()
   463  		dataOffset = stack.pop()
   464  		length     = stack.pop()
   465  
   466  		end = interpreter.intPool.get().Add(dataOffset, length)
   467  	)
   468  	defer interpreter.intPool.put(memOffset, dataOffset, length, end)
   469  
   470  	if !end.IsUint64() || uint64(len(interpreter.returnData)) < end.Uint64() {
   471  		return nil, errReturnDataOutOfBounds
   472  	}
   473  	memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
   474  
   475  	return nil, nil
   476  }
   477  
   478  func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   479  	slot := stack.peek()
   480  	slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
   481  
   482  	return nil, nil
   483  }
   484  
   485  func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   486  	l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
   487  	stack.push(l)
   488  
   489  	return nil, nil
   490  }
   491  
   492  func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   493  	var (
   494  		memOffset  = stack.pop()
   495  		codeOffset = stack.pop()
   496  		length     = stack.pop()
   497  	)
   498  	codeCopy := getDataBig(contract.Code, codeOffset, length)
   499  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   500  
   501  	interpreter.intPool.put(memOffset, codeOffset, length)
   502  	return nil, nil
   503  }
   504  
   505  func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   506  	var (
   507  		addr       = common.BigToAddress(stack.pop())
   508  		memOffset  = stack.pop()
   509  		codeOffset = stack.pop()
   510  		length     = stack.pop()
   511  	)
   512  	codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
   513  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   514  
   515  	interpreter.intPool.put(memOffset, codeOffset, length)
   516  	return nil, nil
   517  }
   518  
   519  // opExtCodeHash returns the code hash of a specified account.
   520  // There are several cases when the function is called, while we can relay everything
   521  // to `state.GetCodeHash` function to ensure the correctness.
   522  //   (1) Caller tries to get the code hash of a normal contract account, state
   523  // should return the relative code hash and set it as the result.
   524  //
   525  //   (2) Caller tries to get the code hash of a non-existent account, state should
   526  // return common.Hash{} and zero will be set as the result.
   527  //
   528  //   (3) Caller tries to get the code hash for an account without contract code,
   529  // state should return emptyCodeHash(0xc5d246...) as the result.
   530  //
   531  //   (4) Caller tries to get the code hash of a precompiled account, the result
   532  // should be zero or emptyCodeHash.
   533  //
   534  // It is worth noting that in order to avoid unnecessary create and clean,
   535  // all precompile accounts on mainnet have been transferred 1 wei, so the return
   536  // here should be emptyCodeHash.
   537  // If the precompile account is not transferred any amount on a private or
   538  // customized chain, the return value will be zero.
   539  //
   540  //   (5) Caller tries to get the code hash for an account which is marked as suicided
   541  // in the current transaction, the code hash of this account should be returned.
   542  //
   543  //   (6) Caller tries to get the code hash for an account which is marked as deleted,
   544  // this account should be regarded as a non-existent account and zero should be returned.
   545  func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   546  	slot := stack.peek()
   547  	address := common.BigToAddress(slot)
   548  	if interpreter.evm.StateDB.Empty(address) {
   549  		slot.SetUint64(0)
   550  	} else {
   551  		slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
   552  	}
   553  	return nil, nil
   554  }
   555  
   556  func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   557  	stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
   558  	return nil, nil
   559  }
   560  
   561  func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   562  	num := stack.pop()
   563  
   564  	n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
   565  	if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
   566  		stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
   567  	} else {
   568  		stack.push(interpreter.intPool.getZero())
   569  	}
   570  	interpreter.intPool.put(num, n)
   571  	return nil, nil
   572  }
   573  
   574  func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   575  	stack.push(interpreter.intPool.get().SetBytes(interpreter.evm.Coinbase.Bytes()))
   576  	return nil, nil
   577  }
   578  
   579  func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   580  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
   581  	return nil, nil
   582  }
   583  
   584  func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   585  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
   586  	return nil, nil
   587  }
   588  
   589  func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   590  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
   591  	return nil, nil
   592  }
   593  
   594  func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   595  	stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
   596  	return nil, nil
   597  }
   598  
   599  func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   600  	interpreter.intPool.put(stack.pop())
   601  	return nil, nil
   602  }
   603  
   604  func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   605  	offset := stack.pop()
   606  	val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
   607  	stack.push(val)
   608  
   609  	interpreter.intPool.put(offset)
   610  	return nil, nil
   611  }
   612  
   613  func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   614  	// pop value of the stack
   615  	mStart, val := stack.pop(), stack.pop()
   616  	memory.Set32(mStart.Uint64(), val)
   617  
   618  	interpreter.intPool.put(mStart, val)
   619  	return nil, nil
   620  }
   621  
   622  func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   623  	off, val := stack.pop().Int64(), stack.pop().Int64()
   624  	memory.store[off] = byte(val & 0xff)
   625  
   626  	return nil, nil
   627  }
   628  
   629  func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   630  	loc := stack.peek()
   631  	val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
   632  	loc.SetBytes(val.Bytes())
   633  	return nil, nil
   634  }
   635  
   636  func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   637  	loc := common.BigToHash(stack.pop())
   638  	val := stack.pop()
   639  	interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
   640  
   641  	interpreter.intPool.put(val)
   642  	return nil, nil
   643  }
   644  
   645  func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   646  	pos := stack.pop()
   647  	if !contract.validJumpdest(pos) {
   648  		return nil, errInvalidJump
   649  	}
   650  	*pc = pos.Uint64()
   651  
   652  	interpreter.intPool.put(pos)
   653  	return nil, nil
   654  }
   655  
   656  func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   657  	pos, cond := stack.pop(), stack.pop()
   658  	if cond.Sign() != 0 {
   659  		if !contract.validJumpdest(pos) {
   660  			return nil, errInvalidJump
   661  		}
   662  		*pc = pos.Uint64()
   663  	} else {
   664  		*pc++
   665  	}
   666  
   667  	interpreter.intPool.put(pos, cond)
   668  	return nil, nil
   669  }
   670  
   671  func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   672  	return nil, nil
   673  }
   674  
   675  func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   676  	stack.push(interpreter.intPool.get().SetUint64(*pc))
   677  	return nil, nil
   678  }
   679  
   680  func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   681  	stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
   682  	return nil, nil
   683  }
   684  
   685  func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   686  	stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
   687  	return nil, nil
   688  }
   689  
   690  func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   691  	var (
   692  		value        = stack.pop()
   693  		offset, size = stack.pop(), stack.pop()
   694  		input        = memory.Get(offset.Int64(), size.Int64())
   695  		gas          = contract.Gas
   696  	)
   697  	if interpreter.evm.chainRules.IsEIP150 {
   698  		gas -= gas / 64
   699  	}
   700  
   701  	contract.UseGas(gas)
   702  	res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
   703  	// Push item on the stack based on the returned error. If the ruleset is
   704  	// homestead we must check for CodeStoreOutOfGasError (homestead only
   705  	// rule) and treat as an error, if the ruleset is frontier we must
   706  	// ignore this error and pretend the operation was successful.
   707  	if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
   708  		stack.push(interpreter.intPool.getZero())
   709  	} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
   710  		stack.push(interpreter.intPool.getZero())
   711  	} else {
   712  		stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
   713  	}
   714  	contract.Gas += returnGas
   715  	interpreter.intPool.put(value, offset, size)
   716  
   717  	if suberr == errExecutionReverted {
   718  		return res, nil
   719  	}
   720  	return nil, nil
   721  }
   722  
   723  func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   724  	var (
   725  		endowment    = stack.pop()
   726  		offset, size = stack.pop(), stack.pop()
   727  		salt         = stack.pop()
   728  		input        = memory.Get(offset.Int64(), size.Int64())
   729  		gas          = contract.Gas
   730  	)
   731  
   732  	// Apply EIP150
   733  	gas -= gas / 64
   734  	contract.UseGas(gas)
   735  	res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
   736  	// Push item on the stack based on the returned error.
   737  	if suberr != nil {
   738  		stack.push(interpreter.intPool.getZero())
   739  	} else {
   740  		stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
   741  	}
   742  	contract.Gas += returnGas
   743  	interpreter.intPool.put(endowment, offset, size, salt)
   744  
   745  	if suberr == errExecutionReverted {
   746  		return res, nil
   747  	}
   748  	return nil, nil
   749  }
   750  
   751  func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   752  	// Pop gas. The actual gas in interpreter.evm.callGasTemp.
   753  	interpreter.intPool.put(stack.pop())
   754  	gas := interpreter.evm.callGasTemp
   755  	// Pop other call parameters.
   756  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   757  	toAddr := common.BigToAddress(addr)
   758  	value = math.U256(value)
   759  	// Get the arguments from the memory.
   760  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   761  
   762  	if value.Sign() != 0 {
   763  		gas += params.CallStipend
   764  	}
   765  	ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
   766  	if err != nil {
   767  		stack.push(interpreter.intPool.getZero())
   768  	} else {
   769  		stack.push(interpreter.intPool.get().SetUint64(1))
   770  	}
   771  	if err == nil || err == errExecutionReverted {
   772  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   773  	}
   774  	contract.Gas += returnGas
   775  
   776  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   777  	return ret, nil
   778  }
   779  
   780  func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   781  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   782  	interpreter.intPool.put(stack.pop())
   783  	gas := interpreter.evm.callGasTemp
   784  	// Pop other call parameters.
   785  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   786  	toAddr := common.BigToAddress(addr)
   787  	value = math.U256(value)
   788  	// Get arguments from the memory.
   789  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   790  
   791  	if value.Sign() != 0 {
   792  		gas += params.CallStipend
   793  	}
   794  	ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
   795  	if err != nil {
   796  		stack.push(interpreter.intPool.getZero())
   797  	} else {
   798  		stack.push(interpreter.intPool.get().SetUint64(1))
   799  	}
   800  	if err == nil || err == errExecutionReverted {
   801  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   802  	}
   803  	contract.Gas += returnGas
   804  
   805  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   806  	return ret, nil
   807  }
   808  
   809  func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   810  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   811  	interpreter.intPool.put(stack.pop())
   812  	gas := interpreter.evm.callGasTemp
   813  	// Pop other call parameters.
   814  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   815  	toAddr := common.BigToAddress(addr)
   816  	// Get arguments from the memory.
   817  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   818  
   819  	ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
   820  	if err != nil {
   821  		stack.push(interpreter.intPool.getZero())
   822  	} else {
   823  		stack.push(interpreter.intPool.get().SetUint64(1))
   824  	}
   825  	if err == nil || err == errExecutionReverted {
   826  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   827  	}
   828  	contract.Gas += returnGas
   829  
   830  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   831  	return ret, nil
   832  }
   833  
   834  func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   835  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   836  	interpreter.intPool.put(stack.pop())
   837  	gas := interpreter.evm.callGasTemp
   838  	// Pop other call parameters.
   839  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   840  	toAddr := common.BigToAddress(addr)
   841  	// Get arguments from the memory.
   842  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   843  
   844  	ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
   845  	if err != nil {
   846  		stack.push(interpreter.intPool.getZero())
   847  	} else {
   848  		stack.push(interpreter.intPool.get().SetUint64(1))
   849  	}
   850  	if err == nil || err == errExecutionReverted {
   851  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   852  	}
   853  	contract.Gas += returnGas
   854  
   855  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   856  	return ret, nil
   857  }
   858  
   859  func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   860  	offset, size := stack.pop(), stack.pop()
   861  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   862  
   863  	interpreter.intPool.put(offset, size)
   864  	return ret, nil
   865  }
   866  
   867  func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   868  	offset, size := stack.pop(), stack.pop()
   869  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   870  
   871  	interpreter.intPool.put(offset, size)
   872  	return ret, nil
   873  }
   874  
   875  func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   876  	return nil, nil
   877  }
   878  
   879  func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   880  	balance := interpreter.evm.StateDB.GetBalance(contract.Address())
   881  	interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
   882  
   883  	interpreter.evm.StateDB.Suicide(contract.Address())
   884  	return nil, nil
   885  }
   886  
   887  // following functions are used by the instruction jump  table
   888  
   889  // make log instruction function
   890  func makeLog(size int) executionFunc {
   891  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   892  		topics := make([]common.Hash, size)
   893  		mStart, mSize := stack.pop(), stack.pop()
   894  		for i := 0; i < size; i++ {
   895  			topics[i] = common.BigToHash(stack.pop())
   896  		}
   897  
   898  		d := memory.Get(mStart.Int64(), mSize.Int64())
   899  		interpreter.evm.StateDB.AddLog(&types.Log{
   900  			Address: contract.Address(),
   901  			Topics:  topics,
   902  			Data:    d,
   903  			// This is a non-consensus field, but assigned here because
   904  			// core/state doesn't know the current block number.
   905  			BlockNumber: interpreter.evm.BlockNumber.Uint64(),
   906  		})
   907  
   908  		interpreter.intPool.put(mStart, mSize)
   909  		return nil, nil
   910  	}
   911  }
   912  
   913  // opPush1 is a specialized version of pushN
   914  func opPush1(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   915  	var (
   916  		codeLen = uint64(len(contract.Code))
   917  		integer = interpreter.intPool.get()
   918  	)
   919  	*pc += 1
   920  	if *pc < codeLen {
   921  		stack.push(integer.SetUint64(uint64(contract.Code[*pc])))
   922  	} else {
   923  		stack.push(integer.SetUint64(0))
   924  	}
   925  	return nil, nil
   926  }
   927  
   928  // make push instruction function
   929  func makePush(size uint64, pushByteSize int) executionFunc {
   930  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   931  		codeLen := len(contract.Code)
   932  
   933  		startMin := codeLen
   934  		if int(*pc+1) < startMin {
   935  			startMin = int(*pc + 1)
   936  		}
   937  
   938  		endMin := codeLen
   939  		if startMin+pushByteSize < endMin {
   940  			endMin = startMin + pushByteSize
   941  		}
   942  
   943  		integer := interpreter.intPool.get()
   944  		stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
   945  
   946  		*pc += size
   947  		return nil, nil
   948  	}
   949  }
   950  
   951  // make dup instruction function
   952  func makeDup(size int64) executionFunc {
   953  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   954  		stack.dup(interpreter.intPool, int(size))
   955  		return nil, nil
   956  	}
   957  }
   958  
   959  // make swap instruction function
   960  func makeSwap(size int64) executionFunc {
   961  	// switch n + 1 otherwise n would be swapped with n
   962  	size++
   963  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   964  		stack.swap(int(size))
   965  		return nil, nil
   966  	}
   967  }