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