github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/core/vm/instructions_test.go (about)

     1  // Copyright 2017 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  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"math/big"
    24  	"os"
    25  	"testing"
    26  
    27  	"github.com/holiman/uint256"
    28  	"github.com/tacshi/go-ethereum/common"
    29  	"github.com/tacshi/go-ethereum/core/rawdb"
    30  	"github.com/tacshi/go-ethereum/core/state"
    31  	"github.com/tacshi/go-ethereum/crypto"
    32  	"github.com/tacshi/go-ethereum/params"
    33  )
    34  
    35  type TwoOperandTestcase struct {
    36  	X        string
    37  	Y        string
    38  	Expected string
    39  }
    40  
    41  type twoOperandParams struct {
    42  	x string
    43  	y string
    44  }
    45  
    46  var alphabetSoup = "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
    47  var commonParams []*twoOperandParams
    48  var twoOpMethods map[string]executionFunc
    49  
    50  type contractRef struct {
    51  	addr common.Address
    52  }
    53  
    54  func (c contractRef) Address() common.Address {
    55  	return c.addr
    56  }
    57  
    58  func init() {
    59  	// Params is a list of common edgecases that should be used for some common tests
    60  	params := []string{
    61  		"0000000000000000000000000000000000000000000000000000000000000000", // 0
    62  		"0000000000000000000000000000000000000000000000000000000000000001", // +1
    63  		"0000000000000000000000000000000000000000000000000000000000000005", // +5
    64  		"7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", // + max -1
    65  		"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // + max
    66  		"8000000000000000000000000000000000000000000000000000000000000000", // - max
    67  		"8000000000000000000000000000000000000000000000000000000000000001", // - max+1
    68  		"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", // - 5
    69  		"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // - 1
    70  	}
    71  	// Params are combined so each param is used on each 'side'
    72  	commonParams = make([]*twoOperandParams, len(params)*len(params))
    73  	for i, x := range params {
    74  		for j, y := range params {
    75  			commonParams[i*len(params)+j] = &twoOperandParams{x, y}
    76  		}
    77  	}
    78  	twoOpMethods = map[string]executionFunc{
    79  		"add":     opAdd,
    80  		"sub":     opSub,
    81  		"mul":     opMul,
    82  		"div":     opDiv,
    83  		"sdiv":    opSdiv,
    84  		"mod":     opMod,
    85  		"smod":    opSmod,
    86  		"exp":     opExp,
    87  		"signext": opSignExtend,
    88  		"lt":      opLt,
    89  		"gt":      opGt,
    90  		"slt":     opSlt,
    91  		"sgt":     opSgt,
    92  		"eq":      opEq,
    93  		"and":     opAnd,
    94  		"or":      opOr,
    95  		"xor":     opXor,
    96  		"byte":    opByte,
    97  		"shl":     opSHL,
    98  		"shr":     opSHR,
    99  		"sar":     opSAR,
   100  	}
   101  }
   102  
   103  func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFunc, name string) {
   104  	var (
   105  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   106  		stack          = newstack()
   107  		pc             = uint64(0)
   108  		evmInterpreter = env.interpreter
   109  	)
   110  
   111  	for i, test := range tests {
   112  		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.X))
   113  		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Y))
   114  		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Expected))
   115  		stack.push(x)
   116  		stack.push(y)
   117  		opFn(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   118  		if len(stack.data) != 1 {
   119  			t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.data))
   120  		}
   121  		actual := stack.pop()
   122  
   123  		if actual.Cmp(expected) != 0 {
   124  			t.Errorf("Testcase %v %d, %v(%x, %x): expected  %x, got %x", name, i, name, x, y, expected, actual)
   125  		}
   126  	}
   127  }
   128  
   129  func TestByteOp(t *testing.T) {
   130  	tests := []TwoOperandTestcase{
   131  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"},
   132  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"},
   133  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"},
   134  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"},
   135  		{"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"},
   136  		{"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"},
   137  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"},
   138  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"},
   139  	}
   140  	testTwoOperandOp(t, tests, opByte, "byte")
   141  }
   142  
   143  func TestSHL(t *testing.T) {
   144  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
   145  	tests := []TwoOperandTestcase{
   146  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"},
   147  		{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   148  		{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   149  		{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   150  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   151  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   152  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   153  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   154  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   155  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   156  	}
   157  	testTwoOperandOp(t, tests, opSHL, "shl")
   158  }
   159  
   160  func TestSHR(t *testing.T) {
   161  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
   162  	tests := []TwoOperandTestcase{
   163  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   164  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   165  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"},
   166  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   167  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   168  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   169  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   170  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   171  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   172  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   173  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   174  	}
   175  	testTwoOperandOp(t, tests, opSHR, "shr")
   176  }
   177  
   178  func TestSAR(t *testing.T) {
   179  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
   180  	tests := []TwoOperandTestcase{
   181  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   182  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   183  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"},
   184  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   185  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   186  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   187  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   188  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   189  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   190  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   191  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   192  		{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   193  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"},
   194  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   195  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"},
   196  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   197  	}
   198  
   199  	testTwoOperandOp(t, tests, opSAR, "sar")
   200  }
   201  
   202  func TestAddMod(t *testing.T) {
   203  	var (
   204  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   205  		stack          = newstack()
   206  		evmInterpreter = NewEVMInterpreter(env)
   207  		pc             = uint64(0)
   208  	)
   209  	tests := []struct {
   210  		x        string
   211  		y        string
   212  		z        string
   213  		expected string
   214  	}{
   215  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   216  			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
   217  			"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   218  			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
   219  		},
   220  	}
   221  	// x + y = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
   222  	// in 256 bit repr, fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
   223  
   224  	for i, test := range tests {
   225  		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.x))
   226  		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.y))
   227  		z := new(uint256.Int).SetBytes(common.Hex2Bytes(test.z))
   228  		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.expected))
   229  		stack.push(z)
   230  		stack.push(y)
   231  		stack.push(x)
   232  		opAddmod(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   233  		actual := stack.pop()
   234  		if actual.Cmp(expected) != 0 {
   235  			t.Errorf("Testcase %d, expected  %x, got %x", i, expected, actual)
   236  		}
   237  	}
   238  }
   239  
   240  // utility function to fill the json-file with testcases
   241  // Enable this test to generate the 'testcases_xx.json' files
   242  func TestWriteExpectedValues(t *testing.T) {
   243  	t.Skip("Enable this test to create json test cases.")
   244  
   245  	// getResult is a convenience function to generate the expected values
   246  	getResult := func(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase {
   247  		var (
   248  			env         = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   249  			stack       = newstack()
   250  			pc          = uint64(0)
   251  			interpreter = env.interpreter
   252  		)
   253  		result := make([]TwoOperandTestcase, len(args))
   254  		for i, param := range args {
   255  			x := new(uint256.Int).SetBytes(common.Hex2Bytes(param.x))
   256  			y := new(uint256.Int).SetBytes(common.Hex2Bytes(param.y))
   257  			stack.push(x)
   258  			stack.push(y)
   259  			opFn(&pc, interpreter, &ScopeContext{nil, stack, nil})
   260  			actual := stack.pop()
   261  			result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)}
   262  		}
   263  		return result
   264  	}
   265  
   266  	for name, method := range twoOpMethods {
   267  		data, err := json.Marshal(getResult(commonParams, method))
   268  		if err != nil {
   269  			t.Fatal(err)
   270  		}
   271  		_ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644)
   272  		if err != nil {
   273  			t.Fatal(err)
   274  		}
   275  	}
   276  }
   277  
   278  // TestJsonTestcases runs through all the testcases defined as json-files
   279  func TestJsonTestcases(t *testing.T) {
   280  	for name := range twoOpMethods {
   281  		data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name))
   282  		if err != nil {
   283  			t.Fatal("Failed to read file", err)
   284  		}
   285  		var testcases []TwoOperandTestcase
   286  		json.Unmarshal(data, &testcases)
   287  		testTwoOperandOp(t, testcases, twoOpMethods[name], name)
   288  	}
   289  }
   290  
   291  func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
   292  	var (
   293  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   294  		stack          = newstack()
   295  		scope          = &ScopeContext{nil, stack, nil}
   296  		evmInterpreter = NewEVMInterpreter(env)
   297  	)
   298  
   299  	env.interpreter = evmInterpreter
   300  	// convert args
   301  	intArgs := make([]*uint256.Int, len(args))
   302  	for i, arg := range args {
   303  		intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
   304  	}
   305  	pc := uint64(0)
   306  	bench.ResetTimer()
   307  	for i := 0; i < bench.N; i++ {
   308  		for _, arg := range intArgs {
   309  			stack.push(arg)
   310  		}
   311  		op(&pc, evmInterpreter, scope)
   312  		stack.pop()
   313  	}
   314  	bench.StopTimer()
   315  
   316  	for i, arg := range args {
   317  		want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
   318  		if have := intArgs[i]; !want.Eq(have) {
   319  			bench.Fatalf("input #%d mutated, have %x want %x", i, have, want)
   320  		}
   321  	}
   322  }
   323  
   324  func BenchmarkOpAdd64(b *testing.B) {
   325  	x := "ffffffff"
   326  	y := "fd37f3e2bba2c4f"
   327  
   328  	opBenchmark(b, opAdd, x, y)
   329  }
   330  
   331  func BenchmarkOpAdd128(b *testing.B) {
   332  	x := "ffffffffffffffff"
   333  	y := "f5470b43c6549b016288e9a65629687"
   334  
   335  	opBenchmark(b, opAdd, x, y)
   336  }
   337  
   338  func BenchmarkOpAdd256(b *testing.B) {
   339  	x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
   340  	y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
   341  
   342  	opBenchmark(b, opAdd, x, y)
   343  }
   344  
   345  func BenchmarkOpSub64(b *testing.B) {
   346  	x := "51022b6317003a9d"
   347  	y := "a20456c62e00753a"
   348  
   349  	opBenchmark(b, opSub, x, y)
   350  }
   351  
   352  func BenchmarkOpSub128(b *testing.B) {
   353  	x := "4dde30faaacdc14d00327aac314e915d"
   354  	y := "9bbc61f5559b829a0064f558629d22ba"
   355  
   356  	opBenchmark(b, opSub, x, y)
   357  }
   358  
   359  func BenchmarkOpSub256(b *testing.B) {
   360  	x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
   361  	y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
   362  
   363  	opBenchmark(b, opSub, x, y)
   364  }
   365  
   366  func BenchmarkOpMul(b *testing.B) {
   367  	x := alphabetSoup
   368  	y := alphabetSoup
   369  
   370  	opBenchmark(b, opMul, x, y)
   371  }
   372  
   373  func BenchmarkOpDiv256(b *testing.B) {
   374  	x := "ff3f9014f20db29ae04af2c2d265de17"
   375  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   376  	opBenchmark(b, opDiv, x, y)
   377  }
   378  
   379  func BenchmarkOpDiv128(b *testing.B) {
   380  	x := "fdedc7f10142ff97"
   381  	y := "fbdfda0e2ce356173d1993d5f70a2b11"
   382  	opBenchmark(b, opDiv, x, y)
   383  }
   384  
   385  func BenchmarkOpDiv64(b *testing.B) {
   386  	x := "fcb34eb3"
   387  	y := "f97180878e839129"
   388  	opBenchmark(b, opDiv, x, y)
   389  }
   390  
   391  func BenchmarkOpSdiv(b *testing.B) {
   392  	x := "ff3f9014f20db29ae04af2c2d265de17"
   393  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   394  
   395  	opBenchmark(b, opSdiv, x, y)
   396  }
   397  
   398  func BenchmarkOpMod(b *testing.B) {
   399  	x := alphabetSoup
   400  	y := alphabetSoup
   401  
   402  	opBenchmark(b, opMod, x, y)
   403  }
   404  
   405  func BenchmarkOpSmod(b *testing.B) {
   406  	x := alphabetSoup
   407  	y := alphabetSoup
   408  
   409  	opBenchmark(b, opSmod, x, y)
   410  }
   411  
   412  func BenchmarkOpExp(b *testing.B) {
   413  	x := alphabetSoup
   414  	y := alphabetSoup
   415  
   416  	opBenchmark(b, opExp, x, y)
   417  }
   418  
   419  func BenchmarkOpSignExtend(b *testing.B) {
   420  	x := alphabetSoup
   421  	y := alphabetSoup
   422  
   423  	opBenchmark(b, opSignExtend, x, y)
   424  }
   425  
   426  func BenchmarkOpLt(b *testing.B) {
   427  	x := alphabetSoup
   428  	y := alphabetSoup
   429  
   430  	opBenchmark(b, opLt, x, y)
   431  }
   432  
   433  func BenchmarkOpGt(b *testing.B) {
   434  	x := alphabetSoup
   435  	y := alphabetSoup
   436  
   437  	opBenchmark(b, opGt, x, y)
   438  }
   439  
   440  func BenchmarkOpSlt(b *testing.B) {
   441  	x := alphabetSoup
   442  	y := alphabetSoup
   443  
   444  	opBenchmark(b, opSlt, x, y)
   445  }
   446  
   447  func BenchmarkOpSgt(b *testing.B) {
   448  	x := alphabetSoup
   449  	y := alphabetSoup
   450  
   451  	opBenchmark(b, opSgt, x, y)
   452  }
   453  
   454  func BenchmarkOpEq(b *testing.B) {
   455  	x := alphabetSoup
   456  	y := alphabetSoup
   457  
   458  	opBenchmark(b, opEq, x, y)
   459  }
   460  func BenchmarkOpEq2(b *testing.B) {
   461  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   462  	y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe"
   463  	opBenchmark(b, opEq, x, y)
   464  }
   465  func BenchmarkOpAnd(b *testing.B) {
   466  	x := alphabetSoup
   467  	y := alphabetSoup
   468  
   469  	opBenchmark(b, opAnd, x, y)
   470  }
   471  
   472  func BenchmarkOpOr(b *testing.B) {
   473  	x := alphabetSoup
   474  	y := alphabetSoup
   475  
   476  	opBenchmark(b, opOr, x, y)
   477  }
   478  
   479  func BenchmarkOpXor(b *testing.B) {
   480  	x := alphabetSoup
   481  	y := alphabetSoup
   482  
   483  	opBenchmark(b, opXor, x, y)
   484  }
   485  
   486  func BenchmarkOpByte(b *testing.B) {
   487  	x := alphabetSoup
   488  	y := alphabetSoup
   489  
   490  	opBenchmark(b, opByte, x, y)
   491  }
   492  
   493  func BenchmarkOpAddmod(b *testing.B) {
   494  	x := alphabetSoup
   495  	y := alphabetSoup
   496  	z := alphabetSoup
   497  
   498  	opBenchmark(b, opAddmod, x, y, z)
   499  }
   500  
   501  func BenchmarkOpMulmod(b *testing.B) {
   502  	x := alphabetSoup
   503  	y := alphabetSoup
   504  	z := alphabetSoup
   505  
   506  	opBenchmark(b, opMulmod, x, y, z)
   507  }
   508  
   509  func BenchmarkOpSHL(b *testing.B) {
   510  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   511  	y := "ff"
   512  
   513  	opBenchmark(b, opSHL, x, y)
   514  }
   515  func BenchmarkOpSHR(b *testing.B) {
   516  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   517  	y := "ff"
   518  
   519  	opBenchmark(b, opSHR, x, y)
   520  }
   521  func BenchmarkOpSAR(b *testing.B) {
   522  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   523  	y := "ff"
   524  
   525  	opBenchmark(b, opSAR, x, y)
   526  }
   527  func BenchmarkOpIsZero(b *testing.B) {
   528  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   529  	opBenchmark(b, opIszero, x)
   530  }
   531  
   532  func TestOpMstore(t *testing.T) {
   533  	var (
   534  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   535  		stack          = newstack()
   536  		mem            = NewMemory()
   537  		evmInterpreter = NewEVMInterpreter(env)
   538  	)
   539  
   540  	env.interpreter = evmInterpreter
   541  	mem.Resize(64)
   542  	pc := uint64(0)
   543  	v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
   544  	stack.push(new(uint256.Int).SetBytes(common.Hex2Bytes(v)))
   545  	stack.push(new(uint256.Int))
   546  	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   547  	if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v {
   548  		t.Fatalf("Mstore fail, got %v, expected %v", got, v)
   549  	}
   550  	stack.push(new(uint256.Int).SetUint64(0x1))
   551  	stack.push(new(uint256.Int))
   552  	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   553  	if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
   554  		t.Fatalf("Mstore failed to overwrite previous value")
   555  	}
   556  }
   557  
   558  func BenchmarkOpMstore(bench *testing.B) {
   559  	var (
   560  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   561  		stack          = newstack()
   562  		mem            = NewMemory()
   563  		evmInterpreter = NewEVMInterpreter(env)
   564  	)
   565  
   566  	env.interpreter = evmInterpreter
   567  	mem.Resize(64)
   568  	pc := uint64(0)
   569  	memStart := new(uint256.Int)
   570  	value := new(uint256.Int).SetUint64(0x1337)
   571  
   572  	bench.ResetTimer()
   573  	for i := 0; i < bench.N; i++ {
   574  		stack.push(value)
   575  		stack.push(memStart)
   576  		opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   577  	}
   578  }
   579  
   580  func TestOpTstore(t *testing.T) {
   581  	var (
   582  		statedb, _     = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   583  		env            = NewEVM(BlockContext{}, TxContext{}, statedb, params.TestChainConfig, Config{})
   584  		stack          = newstack()
   585  		mem            = NewMemory()
   586  		evmInterpreter = NewEVMInterpreter(env)
   587  		caller         = common.Address{}
   588  		to             = common.Address{1}
   589  		contractRef    = contractRef{caller}
   590  		contract       = NewContract(contractRef, AccountRef(to), new(big.Int), 0)
   591  		scopeContext   = ScopeContext{mem, stack, contract}
   592  		value          = common.Hex2Bytes("abcdef00000000000000abba000000000deaf000000c0de00100000000133700")
   593  	)
   594  
   595  	// Add a stateObject for the caller and the contract being called
   596  	statedb.CreateAccount(caller)
   597  	statedb.CreateAccount(to)
   598  
   599  	env.interpreter = evmInterpreter
   600  	pc := uint64(0)
   601  	// push the value to the stack
   602  	stack.push(new(uint256.Int).SetBytes(value))
   603  	// push the location to the stack
   604  	stack.push(new(uint256.Int))
   605  	opTstore(&pc, evmInterpreter, &scopeContext)
   606  	// there should be no elements on the stack after TSTORE
   607  	if stack.len() != 0 {
   608  		t.Fatal("stack wrong size")
   609  	}
   610  	// push the location to the stack
   611  	stack.push(new(uint256.Int))
   612  	opTload(&pc, evmInterpreter, &scopeContext)
   613  	// there should be one element on the stack after TLOAD
   614  	if stack.len() != 1 {
   615  		t.Fatal("stack wrong size")
   616  	}
   617  	val := stack.peek()
   618  	if !bytes.Equal(val.Bytes(), value) {
   619  		t.Fatal("incorrect element read from transient storage")
   620  	}
   621  }
   622  
   623  func BenchmarkOpKeccak256(bench *testing.B) {
   624  	var (
   625  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   626  		stack          = newstack()
   627  		mem            = NewMemory()
   628  		evmInterpreter = NewEVMInterpreter(env)
   629  	)
   630  	env.interpreter = evmInterpreter
   631  	mem.Resize(32)
   632  	pc := uint64(0)
   633  	start := new(uint256.Int)
   634  
   635  	bench.ResetTimer()
   636  	for i := 0; i < bench.N; i++ {
   637  		stack.push(uint256.NewInt(32))
   638  		stack.push(start)
   639  		opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   640  	}
   641  }
   642  
   643  func TestCreate2Addreses(t *testing.T) {
   644  	type testcase struct {
   645  		origin   string
   646  		salt     string
   647  		code     string
   648  		expected string
   649  	}
   650  
   651  	for i, tt := range []testcase{
   652  		{
   653  			origin:   "0x0000000000000000000000000000000000000000",
   654  			salt:     "0x0000000000000000000000000000000000000000",
   655  			code:     "0x00",
   656  			expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38",
   657  		},
   658  		{
   659  			origin:   "0xdeadbeef00000000000000000000000000000000",
   660  			salt:     "0x0000000000000000000000000000000000000000",
   661  			code:     "0x00",
   662  			expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3",
   663  		},
   664  		{
   665  			origin:   "0xdeadbeef00000000000000000000000000000000",
   666  			salt:     "0xfeed000000000000000000000000000000000000",
   667  			code:     "0x00",
   668  			expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833",
   669  		},
   670  		{
   671  			origin:   "0x0000000000000000000000000000000000000000",
   672  			salt:     "0x0000000000000000000000000000000000000000",
   673  			code:     "0xdeadbeef",
   674  			expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e",
   675  		},
   676  		{
   677  			origin:   "0x00000000000000000000000000000000deadbeef",
   678  			salt:     "0xcafebabe",
   679  			code:     "0xdeadbeef",
   680  			expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7",
   681  		},
   682  		{
   683  			origin:   "0x00000000000000000000000000000000deadbeef",
   684  			salt:     "0xcafebabe",
   685  			code:     "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
   686  			expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C",
   687  		},
   688  		{
   689  			origin:   "0x0000000000000000000000000000000000000000",
   690  			salt:     "0x0000000000000000000000000000000000000000",
   691  			code:     "0x",
   692  			expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
   693  		},
   694  	} {
   695  		origin := common.BytesToAddress(common.FromHex(tt.origin))
   696  		salt := common.BytesToHash(common.FromHex(tt.salt))
   697  		code := common.FromHex(tt.code)
   698  		codeHash := crypto.Keccak256(code)
   699  		address := crypto.CreateAddress2(origin, salt, codeHash)
   700  		/*
   701  			stack          := newstack()
   702  			// salt, but we don't need that for this test
   703  			stack.push(big.NewInt(int64(len(code)))) //size
   704  			stack.push(big.NewInt(0)) // memstart
   705  			stack.push(big.NewInt(0)) // value
   706  			gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0)
   707  			fmt.Printf("Example %d\n* address `0x%x`\n* salt `0x%x`\n* init_code `0x%x`\n* gas (assuming no mem expansion): `%v`\n* result: `%s`\n\n", i,origin, salt, code, gas, address.String())
   708  		*/
   709  		expected := common.BytesToAddress(common.FromHex(tt.expected))
   710  		if !bytes.Equal(expected.Bytes(), address.Bytes()) {
   711  			t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String())
   712  		}
   713  	}
   714  }
   715  
   716  func TestRandom(t *testing.T) {
   717  	type testcase struct {
   718  		name   string
   719  		random common.Hash
   720  	}
   721  
   722  	for _, tt := range []testcase{
   723  		{name: "empty hash", random: common.Hash{}},
   724  		{name: "1", random: common.Hash{0}},
   725  		{name: "emptyCodeHash", random: emptyCodeHash},
   726  		{name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})},
   727  	} {
   728  		var (
   729  			env            = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{})
   730  			stack          = newstack()
   731  			pc             = uint64(0)
   732  			evmInterpreter = env.interpreter
   733  		)
   734  		opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   735  		if len(stack.data) != 1 {
   736  			t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data))
   737  		}
   738  		actual := stack.pop()
   739  		expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes()))
   740  		if overflow {
   741  			t.Errorf("Testcase %v: invalid overflow", tt.name)
   742  		}
   743  		if actual.Cmp(expected) != 0 {
   744  			t.Errorf("Testcase %v: expected  %x, got %x", tt.name, expected, actual)
   745  		}
   746  	}
   747  }