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