github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/instructions_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package vm 19 20 import ( 21 "bytes" 22 "encoding/json" 23 "fmt" 24 "io/ioutil" 25 "math/big" 26 "testing" 27 28 "github.com/AigarNetwork/aigar/common" 29 "github.com/AigarNetwork/aigar/crypto" 30 "github.com/AigarNetwork/aigar/params" 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 95 var ( 96 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 97 stack = newstack() 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, nil, nil, stack) 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 = newstack() 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, nil, nil, stack) 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 xTestWriteExpectedValues(t *testing.T) { 237 for name, method := range twoOpMethods { 238 data, err := json.Marshal(getResult(commonParams, method)) 239 if err != nil { 240 t.Fatal(err) 241 } 242 _ = ioutil.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) 243 if err != nil { 244 t.Fatal(err) 245 } 246 } 247 t.Fatal("This test should not be activated") 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 }