github.com/ethereum/go-ethereum@v1.16.1/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"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/core/tracing"
    24  	"github.com/ethereum/go-ethereum/core/types"
    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  	z.AddMod(&x, &y, z)
   176  	return nil, nil
   177  }
   178  
   179  func opMulmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   180  	x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek()
   181  	z.MulMod(&x, &y, z)
   182  	return nil, nil
   183  }
   184  
   185  // opSHL implements Shift Left
   186  // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
   187  // and pushes on the stack arg2 shifted to the left by arg1 number of bits.
   188  func opSHL(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   189  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   190  	shift, value := scope.Stack.pop(), scope.Stack.peek()
   191  	if shift.LtUint64(256) {
   192  		value.Lsh(value, uint(shift.Uint64()))
   193  	} else {
   194  		value.Clear()
   195  	}
   196  	return nil, nil
   197  }
   198  
   199  // opSHR implements Logical Shift Right
   200  // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
   201  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
   202  func opSHR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   203  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   204  	shift, value := scope.Stack.pop(), scope.Stack.peek()
   205  	if shift.LtUint64(256) {
   206  		value.Rsh(value, uint(shift.Uint64()))
   207  	} else {
   208  		value.Clear()
   209  	}
   210  	return nil, nil
   211  }
   212  
   213  // opSAR implements Arithmetic Shift Right
   214  // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
   215  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
   216  func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   217  	shift, value := scope.Stack.pop(), scope.Stack.peek()
   218  	if shift.GtUint64(256) {
   219  		if value.Sign() >= 0 {
   220  			value.Clear()
   221  		} else {
   222  			// Max negative shift: all bits set
   223  			value.SetAllOne()
   224  		}
   225  		return nil, nil
   226  	}
   227  	n := uint(shift.Uint64())
   228  	value.SRsh(value, n)
   229  	return nil, nil
   230  }
   231  
   232  func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   233  	offset, size := scope.Stack.pop(), scope.Stack.peek()
   234  	data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
   235  
   236  	interpreter.hasher.Reset()
   237  	interpreter.hasher.Write(data)
   238  	interpreter.hasher.Read(interpreter.hasherBuf[:])
   239  
   240  	evm := interpreter.evm
   241  	if evm.Config.EnablePreimageRecording {
   242  		evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
   243  	}
   244  	size.SetBytes(interpreter.hasherBuf[:])
   245  	return nil, nil
   246  }
   247  
   248  func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   249  	scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes()))
   250  	return nil, nil
   251  }
   252  
   253  func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   254  	slot := scope.Stack.peek()
   255  	address := common.Address(slot.Bytes20())
   256  	slot.Set(interpreter.evm.StateDB.GetBalance(address))
   257  	return nil, nil
   258  }
   259  
   260  func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   261  	scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
   262  	return nil, nil
   263  }
   264  
   265  func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   266  	scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes()))
   267  	return nil, nil
   268  }
   269  
   270  func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   271  	scope.Stack.push(scope.Contract.value)
   272  	return nil, nil
   273  }
   274  
   275  func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   276  	x := scope.Stack.peek()
   277  	if offset, overflow := x.Uint64WithOverflow(); !overflow {
   278  		data := getData(scope.Contract.Input, offset, 32)
   279  		x.SetBytes(data)
   280  	} else {
   281  		x.Clear()
   282  	}
   283  	return nil, nil
   284  }
   285  
   286  func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   287  	scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input))))
   288  	return nil, nil
   289  }
   290  
   291  func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   292  	var (
   293  		memOffset  = scope.Stack.pop()
   294  		dataOffset = scope.Stack.pop()
   295  		length     = scope.Stack.pop()
   296  	)
   297  	dataOffset64, overflow := dataOffset.Uint64WithOverflow()
   298  	if overflow {
   299  		dataOffset64 = math.MaxUint64
   300  	}
   301  	// These values are checked for overflow during gas cost calculation
   302  	memOffset64 := memOffset.Uint64()
   303  	length64 := length.Uint64()
   304  	scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64))
   305  
   306  	return nil, nil
   307  }
   308  
   309  func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   310  	scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
   311  	return nil, nil
   312  }
   313  
   314  func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   315  	var (
   316  		memOffset  = scope.Stack.pop()
   317  		dataOffset = scope.Stack.pop()
   318  		length     = scope.Stack.pop()
   319  	)
   320  
   321  	offset64, overflow := dataOffset.Uint64WithOverflow()
   322  	if overflow {
   323  		return nil, ErrReturnDataOutOfBounds
   324  	}
   325  	// we can reuse dataOffset now (aliasing it for clarity)
   326  	var end = dataOffset
   327  	end.Add(&dataOffset, &length)
   328  	end64, overflow := end.Uint64WithOverflow()
   329  	if overflow || uint64(len(interpreter.returnData)) < end64 {
   330  		return nil, ErrReturnDataOutOfBounds
   331  	}
   332  	scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
   333  	return nil, nil
   334  }
   335  
   336  func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   337  	slot := scope.Stack.peek()
   338  	slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())))
   339  	return nil, nil
   340  }
   341  
   342  func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   343  	scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Code))))
   344  	return nil, nil
   345  }
   346  
   347  func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   348  	var (
   349  		memOffset  = scope.Stack.pop()
   350  		codeOffset = scope.Stack.pop()
   351  		length     = scope.Stack.pop()
   352  	)
   353  	uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
   354  	if overflow {
   355  		uint64CodeOffset = math.MaxUint64
   356  	}
   357  
   358  	codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64())
   359  	scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   360  	return nil, nil
   361  }
   362  
   363  func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   364  	var (
   365  		stack      = scope.Stack
   366  		a          = stack.pop()
   367  		memOffset  = stack.pop()
   368  		codeOffset = stack.pop()
   369  		length     = stack.pop()
   370  	)
   371  	uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
   372  	if overflow {
   373  		uint64CodeOffset = math.MaxUint64
   374  	}
   375  	addr := common.Address(a.Bytes20())
   376  	code := interpreter.evm.StateDB.GetCode(addr)
   377  	codeCopy := getData(code, uint64CodeOffset, length.Uint64())
   378  	scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   379  
   380  	return nil, nil
   381  }
   382  
   383  // opExtCodeHash returns the code hash of a specified account.
   384  // There are several cases when the function is called, while we can relay everything
   385  // to `state.GetCodeHash` function to ensure the correctness.
   386  //
   387  //  1. Caller tries to get the code hash of a normal contract account, state
   388  //     should return the relative code hash and set it as the result.
   389  //
   390  //  2. Caller tries to get the code hash of a non-existent account, state should
   391  //     return common.Hash{} and zero will be set as the result.
   392  //
   393  //  3. Caller tries to get the code hash for an account without contract code, state
   394  //     should return emptyCodeHash(0xc5d246...) as the result.
   395  //
   396  //  4. Caller tries to get the code hash of a precompiled account, the result should be
   397  //     zero or emptyCodeHash.
   398  //
   399  // It is worth noting that in order to avoid unnecessary create and clean, all precompile
   400  // accounts on mainnet have been transferred 1 wei, so the return here should be
   401  // emptyCodeHash. If the precompile account is not transferred any amount on a private or
   402  // customized chain, the return value will be zero.
   403  //
   404  //  5. Caller tries to get the code hash for an account which is marked as self-destructed
   405  //     in the current transaction, the code hash of this account should be returned.
   406  //
   407  //  6. Caller tries to get the code hash for an account which is marked as deleted, this
   408  //     account should be regarded as a non-existent account and zero should be returned.
   409  func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   410  	slot := scope.Stack.peek()
   411  	address := common.Address(slot.Bytes20())
   412  	if interpreter.evm.StateDB.Empty(address) {
   413  		slot.Clear()
   414  	} else {
   415  		slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
   416  	}
   417  	return nil, nil
   418  }
   419  
   420  func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   421  	v, _ := uint256.FromBig(interpreter.evm.GasPrice)
   422  	scope.Stack.push(v)
   423  	return nil, nil
   424  }
   425  
   426  func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   427  	num := scope.Stack.peek()
   428  	num64, overflow := num.Uint64WithOverflow()
   429  	if overflow {
   430  		num.Clear()
   431  		return nil, nil
   432  	}
   433  
   434  	var upper, lower uint64
   435  	upper = interpreter.evm.Context.BlockNumber.Uint64()
   436  	if upper < 257 {
   437  		lower = 0
   438  	} else {
   439  		lower = upper - 256
   440  	}
   441  	if num64 >= lower && num64 < upper {
   442  		res := interpreter.evm.Context.GetHash(num64)
   443  		if witness := interpreter.evm.StateDB.Witness(); witness != nil {
   444  			witness.AddBlockHash(num64)
   445  		}
   446  		if tracer := interpreter.evm.Config.Tracer; tracer != nil && tracer.OnBlockHashRead != nil {
   447  			tracer.OnBlockHashRead(num64, res)
   448  		}
   449  		num.SetBytes(res[:])
   450  	} else {
   451  		num.Clear()
   452  	}
   453  	return nil, nil
   454  }
   455  
   456  func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   457  	scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes()))
   458  	return nil, nil
   459  }
   460  
   461  func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   462  	scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.Time))
   463  	return nil, nil
   464  }
   465  
   466  func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   467  	v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber)
   468  	scope.Stack.push(v)
   469  	return nil, nil
   470  }
   471  
   472  func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   473  	v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty)
   474  	scope.Stack.push(v)
   475  	return nil, nil
   476  }
   477  
   478  func opRandom(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   479  	v := new(uint256.Int).SetBytes(interpreter.evm.Context.Random.Bytes())
   480  	scope.Stack.push(v)
   481  	return nil, nil
   482  }
   483  
   484  func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   485  	scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit))
   486  	return nil, nil
   487  }
   488  
   489  func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   490  	scope.Stack.pop()
   491  	return nil, nil
   492  }
   493  
   494  func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   495  	v := scope.Stack.peek()
   496  	offset := v.Uint64()
   497  	v.SetBytes(scope.Memory.GetPtr(offset, 32))
   498  	return nil, nil
   499  }
   500  
   501  func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   502  	mStart, val := scope.Stack.pop(), scope.Stack.pop()
   503  	scope.Memory.Set32(mStart.Uint64(), &val)
   504  	return nil, nil
   505  }
   506  
   507  func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   508  	off, val := scope.Stack.pop(), scope.Stack.pop()
   509  	scope.Memory.store[off.Uint64()] = byte(val.Uint64())
   510  	return nil, nil
   511  }
   512  
   513  func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   514  	loc := scope.Stack.peek()
   515  	hash := common.Hash(loc.Bytes32())
   516  	val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash)
   517  	loc.SetBytes(val.Bytes())
   518  	return nil, nil
   519  }
   520  
   521  func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   522  	if interpreter.readOnly {
   523  		return nil, ErrWriteProtection
   524  	}
   525  	loc := scope.Stack.pop()
   526  	val := scope.Stack.pop()
   527  	interpreter.evm.StateDB.SetState(scope.Contract.Address(), loc.Bytes32(), val.Bytes32())
   528  	return nil, nil
   529  }
   530  
   531  func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   532  	if interpreter.evm.abort.Load() {
   533  		return nil, errStopToken
   534  	}
   535  	pos := scope.Stack.pop()
   536  	if !scope.Contract.validJumpdest(&pos) {
   537  		return nil, ErrInvalidJump
   538  	}
   539  	*pc = pos.Uint64() - 1 // pc will be increased by the interpreter loop
   540  	return nil, nil
   541  }
   542  
   543  func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   544  	if interpreter.evm.abort.Load() {
   545  		return nil, errStopToken
   546  	}
   547  	pos, cond := scope.Stack.pop(), scope.Stack.pop()
   548  	if !cond.IsZero() {
   549  		if !scope.Contract.validJumpdest(&pos) {
   550  			return nil, ErrInvalidJump
   551  		}
   552  		*pc = pos.Uint64() - 1 // pc will be increased by the interpreter loop
   553  	}
   554  	return nil, nil
   555  }
   556  
   557  func opJumpdest(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   558  	return nil, nil
   559  }
   560  
   561  func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   562  	scope.Stack.push(new(uint256.Int).SetUint64(*pc))
   563  	return nil, nil
   564  }
   565  
   566  func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   567  	scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len())))
   568  	return nil, nil
   569  }
   570  
   571  func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   572  	scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas))
   573  	return nil, nil
   574  }
   575  
   576  func opSwap1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   577  	scope.Stack.swap1()
   578  	return nil, nil
   579  }
   580  
   581  func opSwap2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   582  	scope.Stack.swap2()
   583  	return nil, nil
   584  }
   585  
   586  func opSwap3(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   587  	scope.Stack.swap3()
   588  	return nil, nil
   589  }
   590  
   591  func opSwap4(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   592  	scope.Stack.swap4()
   593  	return nil, nil
   594  }
   595  
   596  func opSwap5(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   597  	scope.Stack.swap5()
   598  	return nil, nil
   599  }
   600  
   601  func opSwap6(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   602  	scope.Stack.swap6()
   603  	return nil, nil
   604  }
   605  
   606  func opSwap7(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   607  	scope.Stack.swap7()
   608  	return nil, nil
   609  }
   610  
   611  func opSwap8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   612  	scope.Stack.swap8()
   613  	return nil, nil
   614  }
   615  
   616  func opSwap9(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   617  	scope.Stack.swap9()
   618  	return nil, nil
   619  }
   620  
   621  func opSwap10(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   622  	scope.Stack.swap10()
   623  	return nil, nil
   624  }
   625  
   626  func opSwap11(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   627  	scope.Stack.swap11()
   628  	return nil, nil
   629  }
   630  
   631  func opSwap12(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   632  	scope.Stack.swap12()
   633  	return nil, nil
   634  }
   635  
   636  func opSwap13(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   637  	scope.Stack.swap13()
   638  	return nil, nil
   639  }
   640  
   641  func opSwap14(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   642  	scope.Stack.swap14()
   643  	return nil, nil
   644  }
   645  
   646  func opSwap15(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   647  	scope.Stack.swap15()
   648  	return nil, nil
   649  }
   650  
   651  func opSwap16(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   652  	scope.Stack.swap16()
   653  	return nil, nil
   654  }
   655  
   656  func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   657  	if interpreter.readOnly {
   658  		return nil, ErrWriteProtection
   659  	}
   660  	var (
   661  		value        = scope.Stack.pop()
   662  		offset, size = scope.Stack.pop(), scope.Stack.pop()
   663  		input        = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
   664  		gas          = scope.Contract.Gas
   665  	)
   666  	if interpreter.evm.chainRules.IsEIP150 {
   667  		gas -= gas / 64
   668  	}
   669  
   670  	// reuse size int for stackvalue
   671  	stackvalue := size
   672  
   673  	scope.Contract.UseGas(gas, interpreter.evm.Config.Tracer, tracing.GasChangeCallContractCreation)
   674  
   675  	res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract.Address(), input, gas, &value)
   676  	// Push item on the stack based on the returned error. If the ruleset is
   677  	// homestead we must check for CodeStoreOutOfGasError (homestead only
   678  	// rule) and treat as an error, if the ruleset is frontier we must
   679  	// ignore this error and pretend the operation was successful.
   680  	if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
   681  		stackvalue.Clear()
   682  	} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
   683  		stackvalue.Clear()
   684  	} else {
   685  		stackvalue.SetBytes(addr.Bytes())
   686  	}
   687  	scope.Stack.push(&stackvalue)
   688  
   689  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   690  
   691  	if suberr == ErrExecutionReverted {
   692  		interpreter.returnData = res // set REVERT data to return data buffer
   693  		return res, nil
   694  	}
   695  	interpreter.returnData = nil // clear dirty return data buffer
   696  	return nil, nil
   697  }
   698  
   699  func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   700  	if interpreter.readOnly {
   701  		return nil, ErrWriteProtection
   702  	}
   703  	var (
   704  		endowment    = scope.Stack.pop()
   705  		offset, size = scope.Stack.pop(), scope.Stack.pop()
   706  		salt         = scope.Stack.pop()
   707  		input        = scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
   708  		gas          = scope.Contract.Gas
   709  	)
   710  
   711  	// Apply EIP150
   712  	gas -= gas / 64
   713  	scope.Contract.UseGas(gas, interpreter.evm.Config.Tracer, tracing.GasChangeCallContractCreation2)
   714  	// reuse size int for stackvalue
   715  	stackvalue := size
   716  	res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract.Address(), input, gas,
   717  		&endowment, &salt)
   718  	// Push item on the stack based on the returned error.
   719  	if suberr != nil {
   720  		stackvalue.Clear()
   721  	} else {
   722  		stackvalue.SetBytes(addr.Bytes())
   723  	}
   724  	scope.Stack.push(&stackvalue)
   725  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   726  
   727  	if suberr == ErrExecutionReverted {
   728  		interpreter.returnData = res // set REVERT data to return data buffer
   729  		return res, nil
   730  	}
   731  	interpreter.returnData = nil // clear dirty return data buffer
   732  	return nil, nil
   733  }
   734  
   735  func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   736  	stack := scope.Stack
   737  	// Pop gas. The actual gas in interpreter.evm.callGasTemp.
   738  	// We can use this as a temporary value
   739  	temp := stack.pop()
   740  	gas := interpreter.evm.callGasTemp
   741  	// Pop other call parameters.
   742  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   743  	toAddr := common.Address(addr.Bytes20())
   744  	// Get the arguments from the memory.
   745  	args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
   746  
   747  	if interpreter.readOnly && !value.IsZero() {
   748  		return nil, ErrWriteProtection
   749  	}
   750  	if !value.IsZero() {
   751  		gas += params.CallStipend
   752  	}
   753  	ret, returnGas, err := interpreter.evm.Call(scope.Contract.Address(), toAddr, args, gas, &value)
   754  
   755  	if err != nil {
   756  		temp.Clear()
   757  	} else {
   758  		temp.SetOne()
   759  	}
   760  	stack.push(&temp)
   761  	if err == nil || err == ErrExecutionReverted {
   762  		scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   763  	}
   764  
   765  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   766  
   767  	interpreter.returnData = ret
   768  	return ret, nil
   769  }
   770  
   771  func opCallCode(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, value, inOffset, inSize, retOffset, retSize := stack.pop(), 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(inOffset.Uint64(), inSize.Uint64())
   782  
   783  	if !value.IsZero() {
   784  		gas += params.CallStipend
   785  	}
   786  
   787  	ret, returnGas, err := interpreter.evm.CallCode(scope.Contract.Address(), toAddr, args, gas, &value)
   788  	if err != nil {
   789  		temp.Clear()
   790  	} else {
   791  		temp.SetOne()
   792  	}
   793  	stack.push(&temp)
   794  	if err == nil || err == ErrExecutionReverted {
   795  		scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   796  	}
   797  
   798  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   799  
   800  	interpreter.returnData = ret
   801  	return ret, nil
   802  }
   803  
   804  func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   805  	stack := scope.Stack
   806  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   807  	// We use it as a temporary value
   808  	temp := stack.pop()
   809  	gas := interpreter.evm.callGasTemp
   810  	// Pop other call parameters.
   811  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   812  	toAddr := common.Address(addr.Bytes20())
   813  	// Get arguments from the memory.
   814  	args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
   815  
   816  	ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract.Caller(), scope.Contract.Address(), toAddr, args, gas, scope.Contract.value)
   817  	if err != nil {
   818  		temp.Clear()
   819  	} else {
   820  		temp.SetOne()
   821  	}
   822  	stack.push(&temp)
   823  	if err == nil || err == ErrExecutionReverted {
   824  		scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   825  	}
   826  
   827  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   828  
   829  	interpreter.returnData = ret
   830  	return ret, nil
   831  }
   832  
   833  func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   834  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   835  	stack := scope.Stack
   836  	// We use it as a temporary value
   837  	temp := stack.pop()
   838  	gas := interpreter.evm.callGasTemp
   839  	// Pop other call parameters.
   840  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   841  	toAddr := common.Address(addr.Bytes20())
   842  	// Get arguments from the memory.
   843  	args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
   844  
   845  	ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract.Address(), toAddr, args, gas)
   846  	if err != nil {
   847  		temp.Clear()
   848  	} else {
   849  		temp.SetOne()
   850  	}
   851  	stack.push(&temp)
   852  	if err == nil || err == ErrExecutionReverted {
   853  		scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   854  	}
   855  
   856  	scope.Contract.RefundGas(returnGas, interpreter.evm.Config.Tracer, tracing.GasChangeCallLeftOverRefunded)
   857  
   858  	interpreter.returnData = ret
   859  	return ret, nil
   860  }
   861  
   862  func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   863  	offset, size := scope.Stack.pop(), scope.Stack.pop()
   864  	ret := scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
   865  
   866  	return ret, errStopToken
   867  }
   868  
   869  func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   870  	offset, size := scope.Stack.pop(), scope.Stack.pop()
   871  	ret := scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
   872  
   873  	interpreter.returnData = ret
   874  	return ret, ErrExecutionReverted
   875  }
   876  
   877  func opUndefined(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   878  	return nil, &ErrInvalidOpCode{opcode: OpCode(scope.Contract.Code[*pc])}
   879  }
   880  
   881  func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   882  	return nil, errStopToken
   883  }
   884  
   885  func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   886  	if interpreter.readOnly {
   887  		return nil, ErrWriteProtection
   888  	}
   889  	beneficiary := scope.Stack.pop()
   890  	balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
   891  	interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct)
   892  	interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address())
   893  	if tracer := interpreter.evm.Config.Tracer; tracer != nil {
   894  		if tracer.OnEnter != nil {
   895  			tracer.OnEnter(interpreter.evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance.ToBig())
   896  		}
   897  		if tracer.OnExit != nil {
   898  			tracer.OnExit(interpreter.evm.depth, []byte{}, 0, nil, false)
   899  		}
   900  	}
   901  	return nil, errStopToken
   902  }
   903  
   904  func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   905  	if interpreter.readOnly {
   906  		return nil, ErrWriteProtection
   907  	}
   908  	beneficiary := scope.Stack.pop()
   909  	balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
   910  	interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct)
   911  	interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct)
   912  	interpreter.evm.StateDB.SelfDestruct6780(scope.Contract.Address())
   913  	if tracer := interpreter.evm.Config.Tracer; tracer != nil {
   914  		if tracer.OnEnter != nil {
   915  			tracer.OnEnter(interpreter.evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance.ToBig())
   916  		}
   917  		if tracer.OnExit != nil {
   918  			tracer.OnExit(interpreter.evm.depth, []byte{}, 0, nil, false)
   919  		}
   920  	}
   921  	return nil, errStopToken
   922  }
   923  
   924  // following functions are used by the instruction jump  table
   925  
   926  // make log instruction function
   927  func makeLog(size int) executionFunc {
   928  	return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   929  		if interpreter.readOnly {
   930  			return nil, ErrWriteProtection
   931  		}
   932  		topics := make([]common.Hash, size)
   933  		stack := scope.Stack
   934  		mStart, mSize := stack.pop(), stack.pop()
   935  		for i := 0; i < size; i++ {
   936  			addr := stack.pop()
   937  			topics[i] = addr.Bytes32()
   938  		}
   939  
   940  		d := scope.Memory.GetCopy(mStart.Uint64(), mSize.Uint64())
   941  		interpreter.evm.StateDB.AddLog(&types.Log{
   942  			Address: scope.Contract.Address(),
   943  			Topics:  topics,
   944  			Data:    d,
   945  			// This is a non-consensus field, but assigned here because
   946  			// core/state doesn't know the current block number.
   947  			BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(),
   948  		})
   949  
   950  		return nil, nil
   951  	}
   952  }
   953  
   954  // opPush1 is a specialized version of pushN
   955  func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   956  	var (
   957  		codeLen = uint64(len(scope.Contract.Code))
   958  		integer = new(uint256.Int)
   959  	)
   960  	*pc += 1
   961  	if *pc < codeLen {
   962  		scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc])))
   963  	} else {
   964  		scope.Stack.push(integer.Clear())
   965  	}
   966  	return nil, nil
   967  }
   968  
   969  // opPush2 is a specialized version of pushN
   970  func opPush2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   971  	var (
   972  		codeLen = uint64(len(scope.Contract.Code))
   973  		integer = new(uint256.Int)
   974  	)
   975  	if *pc+2 < codeLen {
   976  		scope.Stack.push(integer.SetBytes2(scope.Contract.Code[*pc+1 : *pc+3]))
   977  	} else if *pc+1 < codeLen {
   978  		scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8))
   979  	} else {
   980  		scope.Stack.push(integer.Clear())
   981  	}
   982  	*pc += 2
   983  	return nil, nil
   984  }
   985  
   986  // make push instruction function
   987  func makePush(size uint64, pushByteSize int) executionFunc {
   988  	return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   989  		var (
   990  			codeLen = len(scope.Contract.Code)
   991  			start   = min(codeLen, int(*pc+1))
   992  			end     = min(codeLen, start+pushByteSize)
   993  		)
   994  		a := new(uint256.Int).SetBytes(scope.Contract.Code[start:end])
   995  
   996  		// Missing bytes: pushByteSize - len(pushData)
   997  		if missing := pushByteSize - (end - start); missing > 0 {
   998  			a.Lsh(a, uint(8*missing))
   999  		}
  1000  		scope.Stack.push(a)
  1001  		*pc += size
  1002  		return nil, nil
  1003  	}
  1004  }
  1005  
  1006  // make dup instruction function
  1007  func makeDup(size int64) executionFunc {
  1008  	return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  1009  		scope.Stack.dup(int(size))
  1010  		return nil, nil
  1011  	}
  1012  }