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