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