github.com/codingfuture/orig-energi3@v0.8.4/core/vm/instructions.go (about)

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