github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/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 "io/ioutil" 24 "math/big" 25 "testing" 26 27 "github.com/zhiqiangxu/go-ethereum/common" 28 "github.com/zhiqiangxu/go-ethereum/crypto" 29 "github.com/zhiqiangxu/go-ethereum/params" 30 ) 31 32 type TwoOperandTestcase struct { 33 X string 34 Y string 35 Expected string 36 } 37 38 type twoOperandParams struct { 39 x string 40 y string 41 } 42 43 var commonParams []*twoOperandParams 44 var twoOpMethods map[string]executionFunc 45 46 func init() { 47 48 // Params is a list of common edgecases that should be used for some common tests 49 params := []string{ 50 "0000000000000000000000000000000000000000000000000000000000000000", // 0 51 "0000000000000000000000000000000000000000000000000000000000000001", // +1 52 "0000000000000000000000000000000000000000000000000000000000000005", // +5 53 "7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", // + max -1 54 "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // + max 55 "8000000000000000000000000000000000000000000000000000000000000000", // - max 56 "8000000000000000000000000000000000000000000000000000000000000001", // - max+1 57 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", // - 5 58 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // - 1 59 } 60 // Params are combined so each param is used on each 'side' 61 commonParams = make([]*twoOperandParams, len(params)*len(params)) 62 for i, x := range params { 63 for j, y := range params { 64 commonParams[i*len(params)+j] = &twoOperandParams{x, y} 65 } 66 } 67 twoOpMethods = map[string]executionFunc{ 68 "add": opAdd, 69 "sub": opSub, 70 "mul": opMul, 71 "div": opDiv, 72 "sdiv": opSdiv, 73 "mod": opMod, 74 "smod": opSmod, 75 "exp": opExp, 76 "signext": opSignExtend, 77 "lt": opLt, 78 "gt": opGt, 79 "slt": opSlt, 80 "sgt": opSgt, 81 "eq": opEq, 82 "and": opAnd, 83 "or": opOr, 84 "xor": opXor, 85 "byte": opByte, 86 "shl": opSHL, 87 "shr": opSHR, 88 "sar": opSAR, 89 } 90 } 91 92 func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFunc, name string) { 93 94 var ( 95 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 96 stack = newstack() 97 rstack = newReturnStack() 98 pc = uint64(0) 99 evmInterpreter = env.interpreter.(*EVMInterpreter) 100 ) 101 // Stuff a couple of nonzero bigints into pool, to ensure that ops do not rely on pooled integers to be zero 102 evmInterpreter.intPool = poolOfIntPools.get() 103 evmInterpreter.intPool.put(big.NewInt(-1337)) 104 evmInterpreter.intPool.put(big.NewInt(-1337)) 105 evmInterpreter.intPool.put(big.NewInt(-1337)) 106 107 for i, test := range tests { 108 x := new(big.Int).SetBytes(common.Hex2Bytes(test.X)) 109 y := new(big.Int).SetBytes(common.Hex2Bytes(test.Y)) 110 expected := new(big.Int).SetBytes(common.Hex2Bytes(test.Expected)) 111 stack.push(x) 112 stack.push(y) 113 opFn(&pc, evmInterpreter, &callCtx{nil, stack, rstack, nil}) 114 actual := stack.pop() 115 116 if actual.Cmp(expected) != 0 { 117 t.Errorf("Testcase %v %d, %v(%x, %x): expected %x, got %x", name, i, name, x, y, expected, actual) 118 } 119 // Check pool usage 120 // 1.pool is not allowed to contain anything on the stack 121 // 2.pool is not allowed to contain the same pointers twice 122 if evmInterpreter.intPool.pool.len() > 0 { 123 124 poolvals := make(map[*big.Int]struct{}) 125 poolvals[actual] = struct{}{} 126 127 for evmInterpreter.intPool.pool.len() > 0 { 128 key := evmInterpreter.intPool.get() 129 if _, exist := poolvals[key]; exist { 130 t.Errorf("Testcase %v %d, pool contains double-entry", name, i) 131 } 132 poolvals[key] = struct{}{} 133 } 134 } 135 } 136 poolOfIntPools.put(evmInterpreter.intPool) 137 } 138 139 func TestByteOp(t *testing.T) { 140 tests := []TwoOperandTestcase{ 141 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"}, 142 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"}, 143 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"}, 144 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"}, 145 {"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"}, 146 {"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"}, 147 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"}, 148 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"}, 149 } 150 testTwoOperandOp(t, tests, opByte, "byte") 151 } 152 153 func TestSHL(t *testing.T) { 154 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left 155 tests := []TwoOperandTestcase{ 156 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"}, 157 {"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 158 {"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 159 {"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 160 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 161 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 162 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 163 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 164 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 165 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 166 } 167 testTwoOperandOp(t, tests, opSHL, "shl") 168 } 169 170 func TestSHR(t *testing.T) { 171 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right 172 tests := []TwoOperandTestcase{ 173 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 174 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 175 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"}, 176 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 177 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 178 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 179 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 180 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 181 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 182 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 183 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 184 } 185 testTwoOperandOp(t, tests, opSHR, "shr") 186 } 187 188 func TestSAR(t *testing.T) { 189 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right 190 tests := []TwoOperandTestcase{ 191 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 192 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 193 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"}, 194 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 195 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 196 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 197 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 198 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 199 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 200 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 201 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 202 {"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 203 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"}, 204 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 205 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"}, 206 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 207 } 208 209 testTwoOperandOp(t, tests, opSAR, "sar") 210 } 211 212 // getResult is a convenience function to generate the expected values 213 func getResult(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase { 214 var ( 215 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 216 stack, rstack = newstack(), newReturnStack() 217 pc = uint64(0) 218 interpreter = env.interpreter.(*EVMInterpreter) 219 ) 220 interpreter.intPool = poolOfIntPools.get() 221 result := make([]TwoOperandTestcase, len(args)) 222 for i, param := range args { 223 x := new(big.Int).SetBytes(common.Hex2Bytes(param.x)) 224 y := new(big.Int).SetBytes(common.Hex2Bytes(param.y)) 225 stack.push(x) 226 stack.push(y) 227 opFn(&pc, interpreter, &callCtx{nil, stack, rstack, nil}) 228 actual := stack.pop() 229 result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)} 230 } 231 return result 232 } 233 234 // utility function to fill the json-file with testcases 235 // Enable this test to generate the 'testcases_xx.json' files 236 func TestWriteExpectedValues(t *testing.T) { 237 t.Skip("Enable this test to create json test cases.") 238 239 for name, method := range twoOpMethods { 240 data, err := json.Marshal(getResult(commonParams, method)) 241 if err != nil { 242 t.Fatal(err) 243 } 244 _ = ioutil.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) 245 if err != nil { 246 t.Fatal(err) 247 } 248 } 249 } 250 251 // TestJsonTestcases runs through all the testcases defined as json-files 252 func TestJsonTestcases(t *testing.T) { 253 for name := range twoOpMethods { 254 data, err := ioutil.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name)) 255 if err != nil { 256 t.Fatal("Failed to read file", err) 257 } 258 var testcases []TwoOperandTestcase 259 json.Unmarshal(data, &testcases) 260 testTwoOperandOp(t, testcases, twoOpMethods[name], name) 261 } 262 } 263 264 func opBenchmark(bench *testing.B, op executionFunc, args ...string) { 265 var ( 266 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 267 stack, rstack = newstack(), newReturnStack() 268 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 269 ) 270 271 env.interpreter = evmInterpreter 272 evmInterpreter.intPool = poolOfIntPools.get() 273 // convert args 274 byteArgs := make([][]byte, len(args)) 275 for i, arg := range args { 276 byteArgs[i] = common.Hex2Bytes(arg) 277 } 278 pc := uint64(0) 279 bench.ResetTimer() 280 for i := 0; i < bench.N; i++ { 281 for _, arg := range byteArgs { 282 a := new(big.Int).SetBytes(arg) 283 stack.push(a) 284 } 285 op(&pc, evmInterpreter, &callCtx{nil, stack, rstack, nil}) 286 stack.pop() 287 } 288 poolOfIntPools.put(evmInterpreter.intPool) 289 } 290 291 func BenchmarkOpAdd64(b *testing.B) { 292 x := "ffffffff" 293 y := "fd37f3e2bba2c4f" 294 295 opBenchmark(b, opAdd, x, y) 296 } 297 298 func BenchmarkOpAdd128(b *testing.B) { 299 x := "ffffffffffffffff" 300 y := "f5470b43c6549b016288e9a65629687" 301 302 opBenchmark(b, opAdd, x, y) 303 } 304 305 func BenchmarkOpAdd256(b *testing.B) { 306 x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9" 307 y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57" 308 309 opBenchmark(b, opAdd, x, y) 310 } 311 312 func BenchmarkOpSub64(b *testing.B) { 313 x := "51022b6317003a9d" 314 y := "a20456c62e00753a" 315 316 opBenchmark(b, opSub, x, y) 317 } 318 319 func BenchmarkOpSub128(b *testing.B) { 320 x := "4dde30faaacdc14d00327aac314e915d" 321 y := "9bbc61f5559b829a0064f558629d22ba" 322 323 opBenchmark(b, opSub, x, y) 324 } 325 326 func BenchmarkOpSub256(b *testing.B) { 327 x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37" 328 y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e" 329 330 opBenchmark(b, opSub, x, y) 331 } 332 333 func BenchmarkOpMul(b *testing.B) { 334 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 335 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 336 337 opBenchmark(b, opMul, x, y) 338 } 339 340 func BenchmarkOpDiv256(b *testing.B) { 341 x := "ff3f9014f20db29ae04af2c2d265de17" 342 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 343 opBenchmark(b, opDiv, x, y) 344 } 345 346 func BenchmarkOpDiv128(b *testing.B) { 347 x := "fdedc7f10142ff97" 348 y := "fbdfda0e2ce356173d1993d5f70a2b11" 349 opBenchmark(b, opDiv, x, y) 350 } 351 352 func BenchmarkOpDiv64(b *testing.B) { 353 x := "fcb34eb3" 354 y := "f97180878e839129" 355 opBenchmark(b, opDiv, x, y) 356 } 357 358 func BenchmarkOpSdiv(b *testing.B) { 359 x := "ff3f9014f20db29ae04af2c2d265de17" 360 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 361 362 opBenchmark(b, opSdiv, x, y) 363 } 364 365 func BenchmarkOpMod(b *testing.B) { 366 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 367 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 368 369 opBenchmark(b, opMod, x, y) 370 } 371 372 func BenchmarkOpSmod(b *testing.B) { 373 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 374 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 375 376 opBenchmark(b, opSmod, x, y) 377 } 378 379 func BenchmarkOpExp(b *testing.B) { 380 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 381 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 382 383 opBenchmark(b, opExp, x, y) 384 } 385 386 func BenchmarkOpSignExtend(b *testing.B) { 387 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 388 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 389 390 opBenchmark(b, opSignExtend, x, y) 391 } 392 393 func BenchmarkOpLt(b *testing.B) { 394 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 395 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 396 397 opBenchmark(b, opLt, x, y) 398 } 399 400 func BenchmarkOpGt(b *testing.B) { 401 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 402 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 403 404 opBenchmark(b, opGt, x, y) 405 } 406 407 func BenchmarkOpSlt(b *testing.B) { 408 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 409 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 410 411 opBenchmark(b, opSlt, x, y) 412 } 413 414 func BenchmarkOpSgt(b *testing.B) { 415 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 416 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 417 418 opBenchmark(b, opSgt, x, y) 419 } 420 421 func BenchmarkOpEq(b *testing.B) { 422 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 423 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 424 425 opBenchmark(b, opEq, x, y) 426 } 427 func BenchmarkOpEq2(b *testing.B) { 428 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 429 y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe" 430 opBenchmark(b, opEq, x, y) 431 } 432 func BenchmarkOpAnd(b *testing.B) { 433 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 434 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 435 436 opBenchmark(b, opAnd, x, y) 437 } 438 439 func BenchmarkOpOr(b *testing.B) { 440 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 441 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 442 443 opBenchmark(b, opOr, x, y) 444 } 445 446 func BenchmarkOpXor(b *testing.B) { 447 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 448 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 449 450 opBenchmark(b, opXor, x, y) 451 } 452 453 func BenchmarkOpByte(b *testing.B) { 454 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 455 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 456 457 opBenchmark(b, opByte, x, y) 458 } 459 460 func BenchmarkOpAddmod(b *testing.B) { 461 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 462 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 463 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 464 465 opBenchmark(b, opAddmod, x, y, z) 466 } 467 468 func BenchmarkOpMulmod(b *testing.B) { 469 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 470 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 471 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 472 473 opBenchmark(b, opMulmod, x, y, z) 474 } 475 476 func BenchmarkOpSHL(b *testing.B) { 477 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 478 y := "ff" 479 480 opBenchmark(b, opSHL, x, y) 481 } 482 func BenchmarkOpSHR(b *testing.B) { 483 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 484 y := "ff" 485 486 opBenchmark(b, opSHR, x, y) 487 } 488 func BenchmarkOpSAR(b *testing.B) { 489 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 490 y := "ff" 491 492 opBenchmark(b, opSAR, x, y) 493 } 494 func BenchmarkOpIsZero(b *testing.B) { 495 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 496 opBenchmark(b, opIszero, x) 497 } 498 499 func TestOpMstore(t *testing.T) { 500 var ( 501 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 502 stack, rstack = newstack(), newReturnStack() 503 mem = NewMemory() 504 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 505 ) 506 507 env.interpreter = evmInterpreter 508 evmInterpreter.intPool = poolOfIntPools.get() 509 mem.Resize(64) 510 pc := uint64(0) 511 v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" 512 stack.pushN(new(big.Int).SetBytes(common.Hex2Bytes(v)), big.NewInt(0)) 513 opMstore(&pc, evmInterpreter, &callCtx{mem, stack, rstack, nil}) 514 if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v { 515 t.Fatalf("Mstore fail, got %v, expected %v", got, v) 516 } 517 stack.pushN(big.NewInt(0x1), big.NewInt(0)) 518 opMstore(&pc, evmInterpreter, &callCtx{mem, stack, rstack, nil}) 519 if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" { 520 t.Fatalf("Mstore failed to overwrite previous value") 521 } 522 poolOfIntPools.put(evmInterpreter.intPool) 523 } 524 525 func BenchmarkOpMstore(bench *testing.B) { 526 var ( 527 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 528 stack, rstack = newstack(), newReturnStack() 529 mem = NewMemory() 530 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 531 ) 532 533 env.interpreter = evmInterpreter 534 evmInterpreter.intPool = poolOfIntPools.get() 535 mem.Resize(64) 536 pc := uint64(0) 537 memStart := big.NewInt(0) 538 value := big.NewInt(0x1337) 539 540 bench.ResetTimer() 541 for i := 0; i < bench.N; i++ { 542 stack.pushN(value, memStart) 543 opMstore(&pc, evmInterpreter, &callCtx{mem, stack, rstack, nil}) 544 } 545 poolOfIntPools.put(evmInterpreter.intPool) 546 } 547 548 func BenchmarkOpSHA3(bench *testing.B) { 549 var ( 550 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 551 stack, rstack = newstack(), newReturnStack() 552 mem = NewMemory() 553 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 554 ) 555 env.interpreter = evmInterpreter 556 evmInterpreter.intPool = poolOfIntPools.get() 557 mem.Resize(32) 558 pc := uint64(0) 559 start := big.NewInt(0) 560 561 bench.ResetTimer() 562 for i := 0; i < bench.N; i++ { 563 stack.pushN(big.NewInt(32), start) 564 opSha3(&pc, evmInterpreter, &callCtx{mem, stack, rstack, nil}) 565 } 566 poolOfIntPools.put(evmInterpreter.intPool) 567 } 568 569 func TestCreate2Addreses(t *testing.T) { 570 type testcase struct { 571 origin string 572 salt string 573 code string 574 expected string 575 } 576 577 for i, tt := range []testcase{ 578 { 579 origin: "0x0000000000000000000000000000000000000000", 580 salt: "0x0000000000000000000000000000000000000000", 581 code: "0x00", 582 expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38", 583 }, 584 { 585 origin: "0xdeadbeef00000000000000000000000000000000", 586 salt: "0x0000000000000000000000000000000000000000", 587 code: "0x00", 588 expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3", 589 }, 590 { 591 origin: "0xdeadbeef00000000000000000000000000000000", 592 salt: "0xfeed000000000000000000000000000000000000", 593 code: "0x00", 594 expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833", 595 }, 596 { 597 origin: "0x0000000000000000000000000000000000000000", 598 salt: "0x0000000000000000000000000000000000000000", 599 code: "0xdeadbeef", 600 expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e", 601 }, 602 { 603 origin: "0x00000000000000000000000000000000deadbeef", 604 salt: "0xcafebabe", 605 code: "0xdeadbeef", 606 expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7", 607 }, 608 { 609 origin: "0x00000000000000000000000000000000deadbeef", 610 salt: "0xcafebabe", 611 code: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", 612 expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C", 613 }, 614 { 615 origin: "0x0000000000000000000000000000000000000000", 616 salt: "0x0000000000000000000000000000000000000000", 617 code: "0x", 618 expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0", 619 }, 620 } { 621 622 origin := common.BytesToAddress(common.FromHex(tt.origin)) 623 salt := common.BytesToHash(common.FromHex(tt.salt)) 624 code := common.FromHex(tt.code) 625 codeHash := crypto.Keccak256(code) 626 address := crypto.CreateAddress2(origin, salt, codeHash) 627 /* 628 stack := newstack() 629 // salt, but we don't need that for this test 630 stack.push(big.NewInt(int64(len(code)))) //size 631 stack.push(big.NewInt(0)) // memstart 632 stack.push(big.NewInt(0)) // value 633 gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0) 634 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()) 635 */ 636 expected := common.BytesToAddress(common.FromHex(tt.expected)) 637 if !bytes.Equal(expected.Bytes(), address.Bytes()) { 638 t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String()) 639 } 640 641 } 642 }