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