github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/vm/instructions.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2015 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package vm
    26  
    27  import (
    28  	"errors"
    29  	"fmt"
    30  	"math/big"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/common/math"
    34  	"github.com/ethereum/go-ethereum/core/types"
    35  	"github.com/ethereum/go-ethereum/crypto"
    36  	"github.com/ethereum/go-ethereum/params"
    37  )
    38  
    39  var (
    40  	bigZero                  = new(big.Int)
    41  	tt255                    = math.BigPow(2, 255)
    42  	errWriteProtection       = errors.New("evm: write protection")
    43  	errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
    44  	errExecutionReverted     = errors.New("evm: execution reverted")
    45  	errMaxCodeSizeExceeded   = errors.New("evm: max code size exceeded")
    46  )
    47  
    48  func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    49  	x, y := stack.pop(), stack.peek()
    50  	math.U256(y.Add(x, y))
    51  
    52  	interpreter.intPool.put(x)
    53  	return nil, nil
    54  }
    55  
    56  func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    57  	x, y := stack.pop(), stack.peek()
    58  	math.U256(y.Sub(x, y))
    59  
    60  	interpreter.intPool.put(x)
    61  	return nil, nil
    62  }
    63  
    64  func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    65  	x, y := stack.pop(), stack.pop()
    66  	stack.push(math.U256(x.Mul(x, y)))
    67  
    68  	interpreter.intPool.put(y)
    69  
    70  	return nil, nil
    71  }
    72  
    73  func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    74  	x, y := stack.pop(), stack.peek()
    75  	if y.Sign() != 0 {
    76  		math.U256(y.Div(x, y))
    77  	} else {
    78  		y.SetUint64(0)
    79  	}
    80  	interpreter.intPool.put(x)
    81  	return nil, nil
    82  }
    83  
    84  func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    85  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
    86  	res := interpreter.intPool.getZero()
    87  
    88  	if y.Sign() == 0 || x.Sign() == 0 {
    89  		stack.push(res)
    90  	} else {
    91  		if x.Sign() != y.Sign() {
    92  			res.Div(x.Abs(x), y.Abs(y))
    93  			res.Neg(res)
    94  		} else {
    95  			res.Div(x.Abs(x), y.Abs(y))
    96  		}
    97  		stack.push(math.U256(res))
    98  	}
    99  	interpreter.intPool.put(x, y)
   100  	return nil, nil
   101  }
   102  
   103  func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   104  	x, y := stack.pop(), stack.pop()
   105  	if y.Sign() == 0 {
   106  		stack.push(x.SetUint64(0))
   107  	} else {
   108  		stack.push(math.U256(x.Mod(x, y)))
   109  	}
   110  	interpreter.intPool.put(y)
   111  	return nil, nil
   112  }
   113  
   114  func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   115  	x, y := math.S256(stack.pop()), math.S256(stack.pop())
   116  	res := interpreter.intPool.getZero()
   117  
   118  	if y.Sign() == 0 {
   119  		stack.push(res)
   120  	} else {
   121  		if x.Sign() < 0 {
   122  			res.Mod(x.Abs(x), y.Abs(y))
   123  			res.Neg(res)
   124  		} else {
   125  			res.Mod(x.Abs(x), y.Abs(y))
   126  		}
   127  		stack.push(math.U256(res))
   128  	}
   129  	interpreter.intPool.put(x, y)
   130  	return nil, nil
   131  }
   132  
   133  func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   134  	base, exponent := stack.pop(), stack.pop()
   135  	stack.push(math.Exp(base, exponent))
   136  
   137  	interpreter.intPool.put(base, exponent)
   138  
   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  	hash := crypto.Keccak256(data)
   385  	evm := interpreter.evm
   386  
   387  	if evm.vmConfig.EnablePreimageRecording {
   388  		evm.StateDB.AddPreimage(common.BytesToHash(hash), data)
   389  	}
   390  	stack.push(interpreter.intPool.get().SetBytes(hash))
   391  
   392  	interpreter.intPool.put(offset, size)
   393  	return nil, nil
   394  }
   395  
   396  func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   397  	stack.push(contract.Address().Big())
   398  	return nil, nil
   399  }
   400  
   401  func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   402  	slot := stack.peek()
   403  	slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
   404  	return nil, nil
   405  }
   406  
   407  func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   408  	stack.push(interpreter.evm.Origin.Big())
   409  	return nil, nil
   410  }
   411  
   412  func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   413  	stack.push(contract.Caller().Big())
   414  	return nil, nil
   415  }
   416  
   417  func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   418  	stack.push(interpreter.intPool.get().Set(contract.value))
   419  	return nil, nil
   420  }
   421  
   422  func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   423  	stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
   424  	return nil, nil
   425  }
   426  
   427  func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   428  	stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
   429  	return nil, nil
   430  }
   431  
   432  func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   433  	var (
   434  		memOffset  = stack.pop()
   435  		dataOffset = stack.pop()
   436  		length     = stack.pop()
   437  	)
   438  	memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
   439  
   440  	interpreter.intPool.put(memOffset, dataOffset, length)
   441  	return nil, nil
   442  }
   443  
   444  func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   445  	stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
   446  	return nil, nil
   447  }
   448  
   449  func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   450  	var (
   451  		memOffset  = stack.pop()
   452  		dataOffset = stack.pop()
   453  		length     = stack.pop()
   454  
   455  		end = interpreter.intPool.get().Add(dataOffset, length)
   456  	)
   457  	defer interpreter.intPool.put(memOffset, dataOffset, length, end)
   458  
   459  	if end.BitLen() > 64 || uint64(len(interpreter.returnData)) < end.Uint64() {
   460  		return nil, errReturnDataOutOfBounds
   461  	}
   462  	memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
   463  
   464  	return nil, nil
   465  }
   466  
   467  func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   468  	slot := stack.peek()
   469  	slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
   470  
   471  	return nil, nil
   472  }
   473  
   474  func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   475  	l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
   476  	stack.push(l)
   477  
   478  	return nil, nil
   479  }
   480  
   481  func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   482  	var (
   483  		memOffset  = stack.pop()
   484  		codeOffset = stack.pop()
   485  		length     = stack.pop()
   486  	)
   487  	codeCopy := getDataBig(contract.Code, codeOffset, length)
   488  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   489  
   490  	interpreter.intPool.put(memOffset, codeOffset, length)
   491  	return nil, nil
   492  }
   493  
   494  func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   495  	var (
   496  		addr       = common.BigToAddress(stack.pop())
   497  		memOffset  = stack.pop()
   498  		codeOffset = stack.pop()
   499  		length     = stack.pop()
   500  	)
   501  	codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
   502  	memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   503  
   504  	interpreter.intPool.put(memOffset, codeOffset, length)
   505  	return nil, nil
   506  }
   507  
   508  //opextcodehash返回指定帐户的代码哈希。
   509  //当函数被调用时有几种情况,而我们可以传递所有的
   510  //to`state.getcodehash`函数以确保正确性。
   511  //(1)调用方尝试获取普通合同帐户的代码哈希,状态
   512  //应返回相对代码哈希并将其设置为结果。
   513  //
   514  //(2)调用方试图获取不存在的帐户的代码哈希,状态应为
   515  //返回common。结果将设置哈希和零。
   516  //
   517  //(3)调用方试图获取没有合同代码的帐户的代码哈希,
   518  //状态应返回emptycodehash(0xc5d246…)作为结果。
   519  //
   520  //(4)调用方尝试获取预编译帐户的代码哈希,结果
   521  //应为零或EmptyCodeHash。
   522  //
   523  //值得注意的是,为了避免不必要的创造和清洁,
   524  //主网上所有预编译账户均已转账1卫,因此返回
   525  //这里应该是EmptyCodeHash。
   526  //如果预编译帐户未转移私人或
   527  //定制链,返回值为零。
   528  //
   529  //(5)调用方试图获取标记为自杀的帐户的代码哈希
   530  //在当前事务中,应返回此帐户的代码哈希。
   531  //
   532  //(6)调用方试图获取标记为已删除的帐户的代码哈希,
   533  //此帐户应视为不存在的帐户,应返回零。
   534  func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   535  	slot := stack.peek()
   536  	slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(common.BigToAddress(slot)).Bytes())
   537  	return nil, nil
   538  }
   539  
   540  func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   541  	stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
   542  	return nil, nil
   543  }
   544  
   545  func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   546  	num := stack.pop()
   547  
   548  	n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
   549  	if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
   550  		stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
   551  	} else {
   552  		stack.push(interpreter.intPool.getZero())
   553  	}
   554  	interpreter.intPool.put(num, n)
   555  	return nil, nil
   556  }
   557  
   558  func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   559  	stack.push(interpreter.evm.Coinbase.Big())
   560  	return nil, nil
   561  }
   562  
   563  func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   564  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
   565  	return nil, nil
   566  }
   567  
   568  func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   569  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
   570  	return nil, nil
   571  }
   572  
   573  func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   574  	stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
   575  	return nil, nil
   576  }
   577  
   578  func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   579  	stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
   580  	return nil, nil
   581  }
   582  
   583  func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   584  	interpreter.intPool.put(stack.pop())
   585  	return nil, nil
   586  }
   587  
   588  func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   589  	offset := stack.pop()
   590  	val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
   591  	stack.push(val)
   592  
   593  	interpreter.intPool.put(offset)
   594  	return nil, nil
   595  }
   596  
   597  func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   598  //堆栈的弹出值
   599  	mStart, val := stack.pop(), stack.pop()
   600  	memory.Set32(mStart.Uint64(), val)
   601  
   602  	interpreter.intPool.put(mStart, val)
   603  	return nil, nil
   604  }
   605  
   606  func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   607  	off, val := stack.pop().Int64(), stack.pop().Int64()
   608  	memory.store[off] = byte(val & 0xff)
   609  
   610  	return nil, nil
   611  }
   612  
   613  func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   614  	loc := stack.peek()
   615  	val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
   616  	loc.SetBytes(val.Bytes())
   617  	return nil, nil
   618  }
   619  
   620  func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   621  	loc := common.BigToHash(stack.pop())
   622  	val := stack.pop()
   623  	interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
   624  
   625  	interpreter.intPool.put(val)
   626  	return nil, nil
   627  }
   628  
   629  func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   630  	pos := stack.pop()
   631  	if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
   632  		nop := contract.GetOp(pos.Uint64())
   633  		return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
   634  	}
   635  	*pc = pos.Uint64()
   636  
   637  	interpreter.intPool.put(pos)
   638  	return nil, nil
   639  }
   640  
   641  func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   642  	pos, cond := stack.pop(), stack.pop()
   643  	if cond.Sign() != 0 {
   644  		if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
   645  			nop := contract.GetOp(pos.Uint64())
   646  			return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
   647  		}
   648  		*pc = pos.Uint64()
   649  	} else {
   650  		*pc++
   651  	}
   652  
   653  	interpreter.intPool.put(pos, cond)
   654  	return nil, nil
   655  }
   656  
   657  func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   658  	return nil, nil
   659  }
   660  
   661  func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   662  	stack.push(interpreter.intPool.get().SetUint64(*pc))
   663  	return nil, nil
   664  }
   665  
   666  func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   667  	stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
   668  	return nil, nil
   669  }
   670  
   671  func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   672  	stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
   673  	return nil, nil
   674  }
   675  
   676  func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   677  	var (
   678  		value        = stack.pop()
   679  		offset, size = stack.pop(), stack.pop()
   680  		input        = memory.Get(offset.Int64(), size.Int64())
   681  		gas          = contract.Gas
   682  	)
   683  	if interpreter.evm.ChainConfig().IsEIP150(interpreter.evm.BlockNumber) {
   684  		gas -= gas / 64
   685  	}
   686  
   687  	contract.UseGas(gas)
   688  	res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
   689  //根据返回的错误在堆栈上推送项。如果规则集是
   690  //宅基地我们必须检查codestoreoutofgaserror(仅限宅基地)
   691  //规则)并将其视为错误,如果规则集是前沿,我们必须
   692  //忽略此错误并假装操作成功。
   693  	if interpreter.evm.ChainConfig().IsHomestead(interpreter.evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
   694  		stack.push(interpreter.intPool.getZero())
   695  	} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
   696  		stack.push(interpreter.intPool.getZero())
   697  	} else {
   698  		stack.push(addr.Big())
   699  	}
   700  	contract.Gas += returnGas
   701  	interpreter.intPool.put(value, offset, size)
   702  
   703  	if suberr == errExecutionReverted {
   704  		return res, nil
   705  	}
   706  	return nil, nil
   707  }
   708  
   709  func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   710  	var (
   711  		endowment    = stack.pop()
   712  		offset, size = stack.pop(), stack.pop()
   713  		salt         = stack.pop()
   714  		input        = memory.Get(offset.Int64(), size.Int64())
   715  		gas          = contract.Gas
   716  	)
   717  
   718  //应用EIP150
   719  	gas -= gas / 64
   720  	contract.UseGas(gas)
   721  	res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
   722  //根据返回的错误在堆栈上推送项。
   723  	if suberr != nil {
   724  		stack.push(interpreter.intPool.getZero())
   725  	} else {
   726  		stack.push(addr.Big())
   727  	}
   728  	contract.Gas += returnGas
   729  	interpreter.intPool.put(endowment, offset, size, salt)
   730  
   731  	if suberr == errExecutionReverted {
   732  		return res, nil
   733  	}
   734  	return nil, nil
   735  }
   736  
   737  func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   738  //流行气体。explorer.evm.callgastemp中的实际气体。
   739  	interpreter.intPool.put(stack.pop())
   740  	gas := interpreter.evm.callGasTemp
   741  //弹出其他调用参数。
   742  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   743  	toAddr := common.BigToAddress(addr)
   744  	value = math.U256(value)
   745  //从内存中获取参数。
   746  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   747  
   748  	if value.Sign() != 0 {
   749  		gas += params.CallStipend
   750  	}
   751  	ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
   752  	if err != nil {
   753  		stack.push(interpreter.intPool.getZero())
   754  	} else {
   755  		stack.push(interpreter.intPool.get().SetUint64(1))
   756  	}
   757  	if err == nil || err == errExecutionReverted {
   758  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   759  	}
   760  	contract.Gas += returnGas
   761  
   762  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   763  	return ret, nil
   764  }
   765  
   766  func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   767  //流行气体。实际气体在explorer.evm.callgastemp中。
   768  	interpreter.intPool.put(stack.pop())
   769  	gas := interpreter.evm.callGasTemp
   770  //弹出其他调用参数。
   771  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   772  	toAddr := common.BigToAddress(addr)
   773  	value = math.U256(value)
   774  //从内存中获取参数。
   775  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   776  
   777  	if value.Sign() != 0 {
   778  		gas += params.CallStipend
   779  	}
   780  	ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
   781  	if err != nil {
   782  		stack.push(interpreter.intPool.getZero())
   783  	} else {
   784  		stack.push(interpreter.intPool.get().SetUint64(1))
   785  	}
   786  	if err == nil || err == errExecutionReverted {
   787  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   788  	}
   789  	contract.Gas += returnGas
   790  
   791  	interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
   792  	return ret, nil
   793  }
   794  
   795  func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   796  //流行气体。实际气体在explorer.evm.callgastemp中。
   797  	interpreter.intPool.put(stack.pop())
   798  	gas := interpreter.evm.callGasTemp
   799  //弹出其他调用参数。
   800  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   801  	toAddr := common.BigToAddress(addr)
   802  //从内存中获取参数。
   803  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   804  
   805  	ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
   806  	if err != nil {
   807  		stack.push(interpreter.intPool.getZero())
   808  	} else {
   809  		stack.push(interpreter.intPool.get().SetUint64(1))
   810  	}
   811  	if err == nil || err == errExecutionReverted {
   812  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   813  	}
   814  	contract.Gas += returnGas
   815  
   816  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   817  	return ret, nil
   818  }
   819  
   820  func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   821  //流行气体。实际气体在explorer.evm.callgastemp中。
   822  	interpreter.intPool.put(stack.pop())
   823  	gas := interpreter.evm.callGasTemp
   824  //弹出其他调用参数。
   825  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   826  	toAddr := common.BigToAddress(addr)
   827  //从内存中获取参数。
   828  	args := memory.Get(inOffset.Int64(), inSize.Int64())
   829  
   830  	ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
   831  	if err != nil {
   832  		stack.push(interpreter.intPool.getZero())
   833  	} else {
   834  		stack.push(interpreter.intPool.get().SetUint64(1))
   835  	}
   836  	if err == nil || err == errExecutionReverted {
   837  		memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   838  	}
   839  	contract.Gas += returnGas
   840  
   841  	interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
   842  	return ret, nil
   843  }
   844  
   845  func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   846  	offset, size := stack.pop(), stack.pop()
   847  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   848  
   849  	interpreter.intPool.put(offset, size)
   850  	return ret, nil
   851  }
   852  
   853  func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   854  	offset, size := stack.pop(), stack.pop()
   855  	ret := memory.GetPtr(offset.Int64(), size.Int64())
   856  
   857  	interpreter.intPool.put(offset, size)
   858  	return ret, nil
   859  }
   860  
   861  func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   862  	return nil, nil
   863  }
   864  
   865  func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   866  	balance := interpreter.evm.StateDB.GetBalance(contract.Address())
   867  	interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
   868  
   869  	interpreter.evm.StateDB.Suicide(contract.Address())
   870  	return nil, nil
   871  }
   872  
   873  //指令跳转表使用以下函数
   874  
   875  //生成日志指令功能
   876  func makeLog(size int) executionFunc {
   877  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   878  		topics := make([]common.Hash, size)
   879  		mStart, mSize := stack.pop(), stack.pop()
   880  		for i := 0; i < size; i++ {
   881  			topics[i] = common.BigToHash(stack.pop())
   882  		}
   883  
   884  		d := memory.Get(mStart.Int64(), mSize.Int64())
   885  		interpreter.evm.StateDB.AddLog(&types.Log{
   886  			Address: contract.Address(),
   887  			Topics:  topics,
   888  			Data:    d,
   889  //这是一个非共识领域,但分配给这里是因为
   890  //核心/状态不知道当前块号。
   891  			BlockNumber: interpreter.evm.BlockNumber.Uint64(),
   892  		})
   893  
   894  		interpreter.intPool.put(mStart, mSize)
   895  		return nil, nil
   896  	}
   897  }
   898  
   899  //生成推送指令功能
   900  func makePush(size uint64, pushByteSize int) executionFunc {
   901  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   902  		codeLen := len(contract.Code)
   903  
   904  		startMin := codeLen
   905  		if int(*pc+1) < startMin {
   906  			startMin = int(*pc + 1)
   907  		}
   908  
   909  		endMin := codeLen
   910  		if startMin+pushByteSize < endMin {
   911  			endMin = startMin + pushByteSize
   912  		}
   913  
   914  		integer := interpreter.intPool.get()
   915  		stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
   916  
   917  		*pc += size
   918  		return nil, nil
   919  	}
   920  }
   921  
   922  //生成dup指令功能
   923  func makeDup(size int64) executionFunc {
   924  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   925  		stack.dup(interpreter.intPool, int(size))
   926  		return nil, nil
   927  	}
   928  }
   929  
   930  //生成交换指令功能
   931  func makeSwap(size int64) executionFunc {
   932  //开关N+1,否则N将与N交换
   933  	size++
   934  	return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   935  		stack.swap(int(size))
   936  		return nil, nil
   937  	}
   938  }