github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/instructions.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:36</date>
    10  //</624450082414006272>
    11  
    12  
    13  package vm
    14  
    15  import (
    16  	"errors"
    17  	"fmt"
    18  	"math/big"
    19  
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/common/math"
    22  	"github.com/ethereum/go-ethereum/core/types"
    23  	"github.com/ethereum/go-ethereum/params"
    24  	"golang.org/x/crypto/sha3"
    25  )
    26  
    27  var (
    28  	bigZero                  = new(big.Int)
    29  	tt255                    = math.BigPow(2, 255)
    30  	errWriteProtection       = errors.New("evm: write protection")
    31  	errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
    32  	errExecutionReverted     = errors.New("evm: execution reverted")
    33  	errMaxCodeSizeExceeded   = errors.New("evm: max code size exceeded")
    34  )
    35  
    36  func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    37  	x, y := stack.pop(), stack.peek()
    38  	math.U256(y.Add(x, y))
    39  
    40  	interpreter.intPool.put(x)
    41  	return nil, nil
    42  }
    43  
    44  func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    45  	x, y := stack.pop(), stack.peek()
    46  	math.U256(y.Sub(x, y))
    47  
    48  	interpreter.intPool.put(x)
    49  	return nil, nil
    50  }
    51  
    52  func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    53  	x, y := stack.pop(), stack.pop()
    54  	stack.push(math.U256(x.Mul(x, y)))
    55  
    56  	interpreter.intPool.put(y)
    57  
    58  	return nil, nil
    59  }
    60  
    61  func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    62  	x, y := stack.pop(), stack.peek()
    63  	if y.Sign() != 0 {
    64  		math.U256(y.Div(x, y))
    65  	} else {
    66  		y.SetUint64(0)
    67  	}
    68  	interpreter.intPool.put(x)
    69  	return nil, nil
    70  }
    71  
    72  func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    73  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
    74  	res := interpreter.intPool.getZero()
    75  
    76  	if y.Sign() == 0 || x.Sign() == 0 {
    77  		stack.push(res)
    78  	} else {
    79  		if x.Sign() != y.Sign() {
    80  			res.Div(x.Abs(x), y.Abs(y))
    81  			res.Neg(res)
    82  		} else {
    83  			res.Div(x.Abs(x), y.Abs(y))
    84  		}
    85  		stack.push(math.U256(res))
    86  	}
    87  	interpreter.intPool.put(x, y)
    88  	return nil, nil
    89  }
    90  
    91  func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    92  	x, y := stack.pop(), stack.pop()
    93  	if y.Sign() == 0 {
    94  		stack.push(x.SetUint64(0))
    95  	} else {
    96  		stack.push(math.U256(x.Mod(x, y)))
    97  	}
    98  	interpreter.intPool.put(y)
    99  	return nil, nil
   100  }
   101  
   102  func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   103  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
   104  	res := interpreter.intPool.getZero()
   105  
   106  	if y.Sign() == 0 {
   107  		stack.push(res)
   108  	} else {
   109  		if x.Sign() < 0 {
   110  			res.Mod(x.Abs(x), y.Abs(y))
   111  			res.Neg(res)
   112  		} else {
   113  			res.Mod(x.Abs(x), y.Abs(y))
   114  		}
   115  		stack.push(math.U256(res))
   116  	}
   117  	interpreter.intPool.put(x, y)
   118  	return nil, nil
   119  }
   120  
   121  func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   122  	base, exponent := stack.pop(), stack.pop()
   123  //某种捷径
   124  	cmpToOne := exponent.Cmp(big1)
   125  if cmpToOne < 0 { //指数为零
   126  //x^ 0==1
   127  		stack.push(base.SetUint64(1))
   128  	} else if base.Sign() == 0 {
   129  //0 ^ Y,如果Y!= 0,=0
   130  		stack.push(base.SetUint64(0))
   131  } else if cmpToOne == 0 { //指数是一
   132  //x^ 1==x
   133  		stack.push(base)
   134  	} else {
   135  		stack.push(math.Exp(base, exponent))
   136  		interpreter.intPool.put(base)
   137  	}
   138  	interpreter.intPool.put(exponent)
   139  	return nil, nil
   140  }
   141  
   142  func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   143  	back := stack.pop()
   144  	if back.Cmp(big.NewInt(31)) < 0 {
   145  		bit := uint(back.Uint64()*8 + 7)
   146  		num := stack.pop()
   147  		mask := back.Lsh(common.Big1, bit)
   148  		mask.Sub(mask, common.Big1)
   149  		if num.Bit(int(bit)) > 0 {
   150  			num.Or(num, mask.Not(mask))
   151  		} else {
   152  			num.And(num, mask)
   153  		}
   154  
   155  		stack.push(math.U256(num))
   156  	}
   157  
   158  	interpreter.intPool.put(back)
   159  	return nil, nil
   160  }
   161  
   162  func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   163  	x := stack.peek()
   164  	math.U256(x.Not(x))
   165  	return nil, nil
   166  }
   167  
   168  func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   169  	x, y := stack.pop(), stack.peek()
   170  	if x.Cmp(y) < 0 {
   171  		y.SetUint64(1)
   172  	} else {
   173  		y.SetUint64(0)
   174  	}
   175  	interpreter.intPool.put(x)
   176  	return nil, nil
   177  }
   178  
   179  func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   180  	x, y := stack.pop(), stack.peek()
   181  	if x.Cmp(y) > 0 {
   182  		y.SetUint64(1)
   183  	} else {
   184  		y.SetUint64(0)
   185  	}
   186  	interpreter.intPool.put(x)
   187  	return nil, nil
   188  }
   189  
   190  func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   191  	x, y := stack.pop(), stack.peek()
   192  
   193  	xSign := x.Cmp(tt255)
   194  	ySign := y.Cmp(tt255)
   195  
   196  	switch {
   197  	case xSign >= 0 && ySign < 0:
   198  		y.SetUint64(1)
   199  
   200  	case xSign < 0 && ySign >= 0:
   201  		y.SetUint64(0)
   202  
   203  	default:
   204  		if x.Cmp(y) < 0 {
   205  			y.SetUint64(1)
   206  		} else {
   207  			y.SetUint64(0)
   208  		}
   209  	}
   210  	interpreter.intPool.put(x)
   211  	return nil, nil
   212  }
   213  
   214  func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   215  	x, y := stack.pop(), stack.peek()
   216  
   217  	xSign := x.Cmp(tt255)
   218  	ySign := y.Cmp(tt255)
   219  
   220  	switch {
   221  	case xSign >= 0 && ySign < 0:
   222  		y.SetUint64(0)
   223  
   224  	case xSign < 0 && ySign >= 0:
   225  		y.SetUint64(1)
   226  
   227  	default:
   228  		if x.Cmp(y) > 0 {
   229  			y.SetUint64(1)
   230  		} else {
   231  			y.SetUint64(0)
   232  		}
   233  	}
   234  	interpreter.intPool.put(x)
   235  	return nil, nil
   236  }
   237  
   238  func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   239  	x, y := stack.pop(), stack.peek()
   240  	if x.Cmp(y) == 0 {
   241  		y.SetUint64(1)
   242  	} else {
   243  		y.SetUint64(0)
   244  	}
   245  	interpreter.intPool.put(x)
   246  	return nil, nil
   247  }
   248  
   249  func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   250  	x := stack.peek()
   251  	if x.Sign() > 0 {
   252  		x.SetUint64(0)
   253  	} else {
   254  		x.SetUint64(1)
   255  	}
   256  	return nil, nil
   257  }
   258  
   259  func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   260  	x, y := stack.pop(), stack.pop()
   261  	stack.push(x.And(x, y))
   262  
   263  	interpreter.intPool.put(y)
   264  	return nil, nil
   265  }
   266  
   267  func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   268  	x, y := stack.pop(), stack.peek()
   269  	y.Or(x, y)
   270  
   271  	interpreter.intPool.put(x)
   272  	return nil, nil
   273  }
   274  
   275  func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   276  	x, y := stack.pop(), stack.peek()
   277  	y.Xor(x, y)
   278  
   279  	interpreter.intPool.put(x)
   280  	return nil, nil
   281  }
   282  
   283  func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   284  	th, val := stack.pop(), stack.peek()
   285  	if th.Cmp(common.Big32) < 0 {
   286  		b := math.Byte(val, 32, int(th.Int64()))
   287  		val.SetUint64(uint64(b))
   288  	} else {
   289  		val.SetUint64(0)
   290  	}
   291  	interpreter.intPool.put(th)
   292  	return nil, nil
   293  }
   294  
   295  func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   296  	x, y, z := stack.pop(), stack.pop(), stack.pop()
   297  	if z.Cmp(bigZero) > 0 {
   298  		x.Add(x, y)
   299  		x.Mod(x, z)
   300  		stack.push(math.U256(x))
   301  	} else {
   302  		stack.push(x.SetUint64(0))
   303  	}
   304  	interpreter.intPool.put(y, z)
   305  	return nil, nil
   306  }
   307  
   308  func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   309  	x, y, z := stack.pop(), stack.pop(), stack.pop()
   310  	if z.Cmp(bigZero) > 0 {
   311  		x.Mul(x, y)
   312  		x.Mod(x, z)
   313  		stack.push(math.U256(x))
   314  	} else {
   315  		stack.push(x.SetUint64(0))
   316  	}
   317  	interpreter.intPool.put(y, z)
   318  	return nil, nil
   319  }
   320  
   321  //opshl执行左移
   322  //SHL指令(左移)从堆栈中弹出2个值,首先是arg1,然后是arg2,
   323  //并推动arg2堆栈,将arg1的位数移到左边。
   324  func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   325  //注意,第二个操作数留在堆栈中;将结果累积到堆栈中,之后无需再推它。
   326  	shift, value := math.U256(stack.pop()), math.U256(stack.peek())
   327  defer interpreter.intPool.put(shift) //第一个操作数返回池
   328  
   329  	if shift.Cmp(common.Big256) >= 0 {
   330  		value.SetUint64(0)
   331  		return nil, nil
   332  	}
   333  	n := uint(shift.Uint64())
   334  	math.U256(value.Lsh(value, n))
   335  
   336  	return nil, nil
   337  }
   338  
   339  //opshr实现逻辑右移
   340  //SHR指令(逻辑右移)从堆栈中弹出2个值,首先是arg1,然后是arg2,
   341  //并推动堆栈arg2向右移动arg1填充为零的位数。
   342  func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   343  //注意,第二个操作数留在堆栈中;将结果累积到堆栈中,之后无需再推它。
   344  	shift, value := math.U256(stack.pop()), math.U256(stack.peek())
   345  defer interpreter.intPool.put(shift) //第一个操作数返回池
   346  
   347  	if shift.Cmp(common.Big256) >= 0 {
   348  		value.SetUint64(0)
   349  		return nil, nil
   350  	}
   351  	n := uint(shift.Uint64())
   352  	math.U256(value.Rsh(value, n))
   353  
   354  	return nil, nil
   355  }
   356  
   357  //opsar实现算术右移
   358  //sar指令(算术右移)从堆栈中弹出2个值,首先是arg1,然后是arg2,
   359  //并推动堆栈arg2向右移动arg1个带符号扩展的位数。
   360  func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   361  //注意,s256返回(可能)一个新的bigint,所以我们弹出,而不是偷看这个bigint。
   362  	shift, value := math.U256(stack.pop()), math.S256(stack.pop())
   363  defer interpreter.intPool.put(shift) //第一个操作数返回池
   364  
   365  	if shift.Cmp(common.Big256) >= 0 {
   366  		if value.Sign() >= 0 {
   367  			value.SetUint64(0)
   368  		} else {
   369  			value.SetInt64(-1)
   370  		}
   371  		stack.push(math.U256(value))
   372  		return nil, nil
   373  	}
   374  	n := uint(shift.Uint64())
   375  	value.Rsh(value, n)
   376  	stack.push(math.U256(value))
   377  
   378  	return nil, nil
   379  }
   380  
   381  func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   382  	offset, size := stack.pop(), stack.pop()
   383  	data := memory.Get(offset.Int64(), size.Int64())
   384  
   385  	if interpreter.hasher == nil {
   386  		interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
   387  	} else {
   388  		interpreter.hasher.Reset()
   389  	}
   390  	interpreter.hasher.Write(data)
   391  	interpreter.hasher.Read(interpreter.hasherBuf[:])
   392  
   393  	evm := interpreter.evm
   394  	if evm.vmConfig.EnablePreimageRecording {
   395  		evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
   396  	}
   397  	stack.push(interpreter.intPool.get().SetBytes(interpreter.hasherBuf[:]))
   398  
   399  	interpreter.intPool.put(offset, size)
   400  	return nil, nil
   401  }
   402  
   403  func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   404  	stack.push(contract.Address().Big())
   405  	return nil, nil
   406  }
   407  
   408  func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   409  	slot := stack.peek()
   410  	slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
   411  	return nil, nil
   412  }
   413  
   414  func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   415  	stack.push(interpreter.evm.Origin.Big())
   416  	return nil, nil
   417  }
   418  
   419  func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   420  	stack.push(contract.Caller().Big())
   421  	return nil, nil
   422  }
   423  
   424  func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   425  	stack.push(interpreter.intPool.get().Set(contract.value))
   426  	return nil, nil
   427  }
   428  
   429  func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   430  	stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
   431  	return nil, nil
   432  }
   433  
   434  func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   435  	stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
   436  	return nil, nil
   437  }
   438  
   439  func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   440  	var (
   441  		memOffset  = stack.pop()
   442  		dataOffset = stack.pop()
   443  		length     = stack.pop()
   444  	)
   445  	memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
   446  
   447  	interpreter.intPool.put(memOffset, dataOffset, length)
   448  	return nil, nil
   449  }
   450  
   451  func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   452  	stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
   453  	return nil, nil
   454  }
   455  
   456  func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   457  	var (
   458  		memOffset  = stack.pop()
   459  		dataOffset = stack.pop()
   460  		length     = stack.pop()
   461  
   462  		end = interpreter.intPool.get().Add(dataOffset, length)
   463  	)
   464  	defer interpreter.intPool.put(memOffset, dataOffset, length, end)
   465  
   466  	if end.BitLen() > 64 || uint64(len(interpreter.returnData)) < end.Uint64() {
   467  		return nil, errReturnDataOutOfBounds
   468  	}
   469  	memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
   470  
   471  	return nil, nil
   472  }
   473  
   474  func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   475  	slot := stack.peek()
   476  	slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
   477  
   478  	return nil, nil
   479  }
   480  
   481  func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   482  	l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
   483  	stack.push(l)
   484  
   485  	return nil, nil
   486  }
   487  
   488  func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   489  	var (
   490  		memOffset  = stack.pop()
   491  		codeOffset = stack.pop()
   492  		length     = stack.pop()
   493  	)
   494  	codeCopy := getDataBig(contract.Code, codeOffset, length)
   495  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   496  
   497  	interpreter.intPool.put(memOffset, codeOffset, length)
   498  	return nil, nil
   499  }
   500  
   501  func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   502  	var (
   503  		addr       = common.BigToAddress(stack.pop())
   504  		memOffset  = stack.pop()
   505  		codeOffset = stack.pop()
   506  		length     = stack.pop()
   507  	)
   508  	codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
   509  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   510  
   511  	interpreter.intPool.put(memOffset, codeOffset, length)
   512  	return nil, nil
   513  }
   514  
   515  //opextcodehash返回指定帐户的代码哈希。
   516  //当函数被调用时有几种情况,而我们可以传递所有的
   517  //to`state.getcodehash`函数以确保正确性。
   518  //(1)调用方尝试获取普通合同帐户的代码哈希,状态
   519  //应返回相对代码哈希并将其设置为结果。
   520  //
   521  //(2)调用方试图获取不存在的帐户的代码哈希,状态应为
   522  //返回common。结果将设置哈希和零。
   523  //
   524  //(3)调用方试图获取没有合同代码的帐户的代码哈希,
   525  //状态应返回emptycodehash(0xc5d246…)作为结果。
   526  //
   527  //(4)调用方尝试获取预编译帐户的代码哈希,结果
   528  //应为零或EmptyCodeHash。
   529  //
   530  //值得注意的是,为了避免不必要的创造和清洁,
   531  //主网上所有预编译账户均已转账1卫,因此返回
   532  //这里应该是EmptyCodeHash。
   533  //如果预编译帐户未转移私人或
   534  //定制链,返回值为零。
   535  //
   536  //(5)调用方试图获取标记为自杀的帐户的代码哈希
   537  //在当前事务中,应返回此帐户的代码哈希。
   538  //
   539  //(6)调用方试图获取标记为已删除的帐户的代码哈希,
   540  //此帐户应视为不存在的帐户,应返回零。
   541  func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   542  	slot := stack.peek()
   543  	address := common.BigToAddress(slot)
   544  	if interpreter.evm.StateDB.Empty(address) {
   545  		slot.SetUint64(0)
   546  	} else {
   547  		slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
   548  	}
   549  	return nil, nil
   550  }
   551  
   552  func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   553  	stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
   554  	return nil, nil
   555  }
   556  
   557  func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   558  	num := stack.pop()
   559  
   560  	n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
   561  	if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
   562  		stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
   563  	} else {
   564  		stack.push(interpreter.intPool.getZero())
   565  	}
   566  	interpreter.intPool.put(num, n)
   567  	return nil, nil
   568  }
   569  
   570  func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   571  	stack.push(interpreter.evm.Coinbase.Big())
   572  	return nil, nil
   573  }
   574  
   575  func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   576  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
   577  	return nil, nil
   578  }
   579  
   580  func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   581  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
   582  	return nil, nil
   583  }
   584  
   585  func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   586  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
   587  	return nil, nil
   588  }
   589  
   590  func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   591  	stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
   592  	return nil, nil
   593  }
   594  
   595  func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   596  	interpreter.intPool.put(stack.pop())
   597  	return nil, nil
   598  }
   599  
   600  func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   601  	offset := stack.pop()
   602  	val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
   603  	stack.push(val)
   604  
   605  	interpreter.intPool.put(offset)
   606  	return nil, nil
   607  }
   608  
   609  func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   610  //堆栈的弹出值
   611  	mStart, val := stack.pop(), stack.pop()
   612  	memory.Set32(mStart.Uint64(), val)
   613  
   614  	interpreter.intPool.put(mStart, val)
   615  	return nil, nil
   616  }
   617  
   618  func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   619  	off, val := stack.pop().Int64(), stack.pop().Int64()
   620  	memory.store[off] = byte(val & 0xff)
   621  
   622  	return nil, nil
   623  }
   624  
   625  func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   626  	loc := stack.peek()
   627  	val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
   628  	loc.SetBytes(val.Bytes())
   629  	return nil, nil
   630  }
   631  
   632  func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   633  	loc := common.BigToHash(stack.pop())
   634  	val := stack.pop()
   635  	interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
   636  
   637  	interpreter.intPool.put(val)
   638  	return nil, nil
   639  }
   640  
   641  func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   642  	pos := stack.pop()
   643  	if !contract.validJumpdest(pos) {
   644  		nop := contract.GetOp(pos.Uint64())
   645  		return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
   646  	}
   647  	*pc = pos.Uint64()
   648  
   649  	interpreter.intPool.put(pos)
   650  	return nil, nil
   651  }
   652  
   653  func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   654  	pos, cond := stack.pop(), stack.pop()
   655  	if cond.Sign() != 0 {
   656  		if !contract.validJumpdest(pos) {
   657  			nop := contract.GetOp(pos.Uint64())
   658  			return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
   659  		}
   660  		*pc = pos.Uint64()
   661  	} else {
   662  		*pc++
   663  	}
   664  
   665  	interpreter.intPool.put(pos, cond)
   666  	return nil, nil
   667  }
   668  
   669  func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   670  	return nil, nil
   671  }
   672  
   673  func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   674  	stack.push(interpreter.intPool.get().SetUint64(*pc))
   675  	return nil, nil
   676  }
   677  
   678  func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   679  	stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
   680  	return nil, nil
   681  }
   682  
   683  func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   684  	stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
   685  	return nil, nil
   686  }
   687  
   688  func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   689  	var (
   690  		value        = stack.pop()
   691  		offset, size = stack.pop(), stack.pop()
   692  		input        = memory.Get(offset.Int64(), size.Int64())
   693  		gas          = contract.Gas
   694  	)
   695  	if interpreter.evm.ChainConfig().IsEIP150(interpreter.evm.BlockNumber) {
   696  		gas -= gas / 64
   697  	}
   698  
   699  	contract.UseGas(gas)
   700  	res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
   701  //根据返回的错误在堆栈上推送项。如果规则集是
   702  //宅基地我们必须检查codestoreoutofgaserror(仅限宅基地)
   703  //规则)并将其视为错误,如果规则集是前沿,我们必须
   704  //忽略此错误并假装操作成功。
   705  	if interpreter.evm.ChainConfig().IsHomestead(interpreter.evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
   706  		stack.push(interpreter.intPool.getZero())
   707  	} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
   708  		stack.push(interpreter.intPool.getZero())
   709  	} else {
   710  		stack.push(addr.Big())
   711  	}
   712  	contract.Gas += returnGas
   713  	interpreter.intPool.put(value, offset, size)
   714  
   715  	if suberr == errExecutionReverted {
   716  		return res, nil
   717  	}
   718  	return nil, nil
   719  }
   720  
   721  func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   722  	var (
   723  		endowment    = stack.pop()
   724  		offset, size = stack.pop(), stack.pop()
   725  		salt         = stack.pop()
   726  		input        = memory.Get(offset.Int64(), size.Int64())
   727  		gas          = contract.Gas
   728  	)
   729  
   730  //应用EIP150
   731  	gas -= gas / 64
   732  	contract.UseGas(gas)
   733  	res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
   734  //根据返回的错误在堆栈上推送项。
   735  	if suberr != nil {
   736  		stack.push(interpreter.intPool.getZero())
   737  	} else {
   738  		stack.push(addr.Big())
   739  	}
   740  	contract.Gas += returnGas
   741  	interpreter.intPool.put(endowment, offset, size, salt)
   742  
   743  	if suberr == errExecutionReverted {
   744  		return res, nil
   745  	}
   746  	return nil, nil
   747  }
   748  
   749  func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   750  //流行气体。explorer.evm.callgastemp中的实际气体。
   751  	interpreter.intPool.put(stack.pop())
   752  	gas := interpreter.evm.callGasTemp
   753  //弹出其他调用参数。
   754  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   755  	toAddr := common.BigToAddress(addr)
   756  	value = math.U256(value)
   757  //从内存中获取参数。
   758  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   759  
   760  	if value.Sign() != 0 {
   761  		gas += params.CallStipend
   762  	}
   763  	ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
   764  	if err != nil {
   765  		stack.push(interpreter.intPool.getZero())
   766  	} else {
   767  		stack.push(interpreter.intPool.get().SetUint64(1))
   768  	}
   769  	if err == nil || err == errExecutionReverted {
   770  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   771  	}
   772  	contract.Gas += returnGas
   773  
   774  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   775  	return ret, nil
   776  }
   777  
   778  func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   779  //流行气体。实际气体在explorer.evm.callgastemp中。
   780  	interpreter.intPool.put(stack.pop())
   781  	gas := interpreter.evm.callGasTemp
   782  //弹出其他调用参数。
   783  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   784  	toAddr := common.BigToAddress(addr)
   785  	value = math.U256(value)
   786  //从内存中获取参数。
   787  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   788  
   789  	if value.Sign() != 0 {
   790  		gas += params.CallStipend
   791  	}
   792  	ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
   793  	if err != nil {
   794  		stack.push(interpreter.intPool.getZero())
   795  	} else {
   796  		stack.push(interpreter.intPool.get().SetUint64(1))
   797  	}
   798  	if err == nil || err == errExecutionReverted {
   799  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   800  	}
   801  	contract.Gas += returnGas
   802  
   803  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   804  	return ret, nil
   805  }
   806  
   807  func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   808  //流行气体。实际气体在explorer.evm.callgastemp中。
   809  	interpreter.intPool.put(stack.pop())
   810  	gas := interpreter.evm.callGasTemp
   811  //弹出其他调用参数。
   812  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   813  	toAddr := common.BigToAddress(addr)
   814  //从内存中获取参数。
   815  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   816  
   817  	ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
   818  	if err != nil {
   819  		stack.push(interpreter.intPool.getZero())
   820  	} else {
   821  		stack.push(interpreter.intPool.get().SetUint64(1))
   822  	}
   823  	if err == nil || err == errExecutionReverted {
   824  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   825  	}
   826  	contract.Gas += returnGas
   827  
   828  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   829  	return ret, nil
   830  }
   831  
   832  func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   833  //流行气体。实际气体在explorer.evm.callgastemp中。
   834  	interpreter.intPool.put(stack.pop())
   835  	gas := interpreter.evm.callGasTemp
   836  //弹出其他调用参数。
   837  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   838  	toAddr := common.BigToAddress(addr)
   839  //从内存中获取参数。
   840  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   841  
   842  	ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
   843  	if err != nil {
   844  		stack.push(interpreter.intPool.getZero())
   845  	} else {
   846  		stack.push(interpreter.intPool.get().SetUint64(1))
   847  	}
   848  	if err == nil || err == errExecutionReverted {
   849  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   850  	}
   851  	contract.Gas += returnGas
   852  
   853  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   854  	return ret, nil
   855  }
   856  
   857  func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   858  	offset, size := stack.pop(), stack.pop()
   859  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   860  
   861  	interpreter.intPool.put(offset, size)
   862  	return ret, nil
   863  }
   864  
   865  func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   866  	offset, size := stack.pop(), stack.pop()
   867  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   868  
   869  	interpreter.intPool.put(offset, size)
   870  	return ret, nil
   871  }
   872  
   873  func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   874  	return nil, nil
   875  }
   876  
   877  func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   878  	balance := interpreter.evm.StateDB.GetBalance(contract.Address())
   879  	interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
   880  
   881  	interpreter.evm.StateDB.Suicide(contract.Address())
   882  	return nil, nil
   883  }
   884  
   885  //指令跳转表使用以下函数
   886  
   887  //生成日志指令功能
   888  func makeLog(size int) executionFunc {
   889  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   890  		topics := make([]common.Hash, size)
   891  		mStart, mSize := stack.pop(), stack.pop()
   892  		for i := 0; i < size; i++ {
   893  			topics[i] = common.BigToHash(stack.pop())
   894  		}
   895  
   896  		d := memory.Get(mStart.Int64(), mSize.Int64())
   897  		interpreter.evm.StateDB.AddLog(&types.Log{
   898  			Address: contract.Address(),
   899  			Topics:  topics,
   900  			Data:    d,
   901  //这是一个非共识领域,但分配给这里是因为
   902  //核心/状态不知道当前块号。
   903  			BlockNumber: interpreter.evm.BlockNumber.Uint64(),
   904  		})
   905  
   906  		interpreter.intPool.put(mStart, mSize)
   907  		return nil, nil
   908  	}
   909  }
   910  
   911  //生成推送指令功能
   912  func makePush(size uint64, pushByteSize int) executionFunc {
   913  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   914  		codeLen := len(contract.Code)
   915  
   916  		startMin := codeLen
   917  		if int(*pc+1) < startMin {
   918  			startMin = int(*pc + 1)
   919  		}
   920  
   921  		endMin := codeLen
   922  		if startMin+pushByteSize < endMin {
   923  			endMin = startMin + pushByteSize
   924  		}
   925  
   926  		integer := interpreter.intPool.get()
   927  		stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
   928  
   929  		*pc += size
   930  		return nil, nil
   931  	}
   932  }
   933  
   934  //生成dup指令功能
   935  func makeDup(size int64) executionFunc {
   936  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   937  		stack.dup(interpreter.intPool, int(size))
   938  		return nil, nil
   939  	}
   940  }
   941  
   942  //生成交换指令功能
   943  func makeSwap(size int64) executionFunc {
   944  //开关N+1,否则N将与N交换
   945  	size++
   946  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   947  		stack.swap(int(size))
   948  		return nil, nil
   949  	}
   950  }
   951