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