github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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 "io/ioutil" 24 "math/big" 25 "testing" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/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 _ = ioutil.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 := ioutil.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 evmInterpreter = NewEVMInterpreter(env, env.Config) 288 ) 289 290 env.interpreter = evmInterpreter 291 // convert args 292 byteArgs := make([][]byte, len(args)) 293 for i, arg := range args { 294 byteArgs[i] = common.Hex2Bytes(arg) 295 } 296 pc := uint64(0) 297 bench.ResetTimer() 298 for i := 0; i < bench.N; i++ { 299 for _, arg := range byteArgs { 300 a := new(uint256.Int) 301 a.SetBytes(arg) 302 stack.push(a) 303 } 304 op(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) 305 stack.pop() 306 } 307 } 308 309 func BenchmarkOpAdd64(b *testing.B) { 310 x := "ffffffff" 311 y := "fd37f3e2bba2c4f" 312 313 opBenchmark(b, opAdd, x, y) 314 } 315 316 func BenchmarkOpAdd128(b *testing.B) { 317 x := "ffffffffffffffff" 318 y := "f5470b43c6549b016288e9a65629687" 319 320 opBenchmark(b, opAdd, x, y) 321 } 322 323 func BenchmarkOpAdd256(b *testing.B) { 324 x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9" 325 y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57" 326 327 opBenchmark(b, opAdd, x, y) 328 } 329 330 func BenchmarkOpSub64(b *testing.B) { 331 x := "51022b6317003a9d" 332 y := "a20456c62e00753a" 333 334 opBenchmark(b, opSub, x, y) 335 } 336 337 func BenchmarkOpSub128(b *testing.B) { 338 x := "4dde30faaacdc14d00327aac314e915d" 339 y := "9bbc61f5559b829a0064f558629d22ba" 340 341 opBenchmark(b, opSub, x, y) 342 } 343 344 func BenchmarkOpSub256(b *testing.B) { 345 x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37" 346 y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e" 347 348 opBenchmark(b, opSub, x, y) 349 } 350 351 func BenchmarkOpMul(b *testing.B) { 352 x := alphabetSoup 353 y := alphabetSoup 354 355 opBenchmark(b, opMul, x, y) 356 } 357 358 func BenchmarkOpDiv256(b *testing.B) { 359 x := "ff3f9014f20db29ae04af2c2d265de17" 360 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 361 opBenchmark(b, opDiv, x, y) 362 } 363 364 func BenchmarkOpDiv128(b *testing.B) { 365 x := "fdedc7f10142ff97" 366 y := "fbdfda0e2ce356173d1993d5f70a2b11" 367 opBenchmark(b, opDiv, x, y) 368 } 369 370 func BenchmarkOpDiv64(b *testing.B) { 371 x := "fcb34eb3" 372 y := "f97180878e839129" 373 opBenchmark(b, opDiv, x, y) 374 } 375 376 func BenchmarkOpSdiv(b *testing.B) { 377 x := "ff3f9014f20db29ae04af2c2d265de17" 378 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 379 380 opBenchmark(b, opSdiv, x, y) 381 } 382 383 func BenchmarkOpMod(b *testing.B) { 384 x := alphabetSoup 385 y := alphabetSoup 386 387 opBenchmark(b, opMod, x, y) 388 } 389 390 func BenchmarkOpSmod(b *testing.B) { 391 x := alphabetSoup 392 y := alphabetSoup 393 394 opBenchmark(b, opSmod, x, y) 395 } 396 397 func BenchmarkOpExp(b *testing.B) { 398 x := alphabetSoup 399 y := alphabetSoup 400 401 opBenchmark(b, opExp, x, y) 402 } 403 404 func BenchmarkOpSignExtend(b *testing.B) { 405 x := alphabetSoup 406 y := alphabetSoup 407 408 opBenchmark(b, opSignExtend, x, y) 409 } 410 411 func BenchmarkOpLt(b *testing.B) { 412 x := alphabetSoup 413 y := alphabetSoup 414 415 opBenchmark(b, opLt, x, y) 416 } 417 418 func BenchmarkOpGt(b *testing.B) { 419 x := alphabetSoup 420 y := alphabetSoup 421 422 opBenchmark(b, opGt, x, y) 423 } 424 425 func BenchmarkOpSlt(b *testing.B) { 426 x := alphabetSoup 427 y := alphabetSoup 428 429 opBenchmark(b, opSlt, x, y) 430 } 431 432 func BenchmarkOpSgt(b *testing.B) { 433 x := alphabetSoup 434 y := alphabetSoup 435 436 opBenchmark(b, opSgt, x, y) 437 } 438 439 func BenchmarkOpEq(b *testing.B) { 440 x := alphabetSoup 441 y := alphabetSoup 442 443 opBenchmark(b, opEq, x, y) 444 } 445 func BenchmarkOpEq2(b *testing.B) { 446 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 447 y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe" 448 opBenchmark(b, opEq, x, y) 449 } 450 func BenchmarkOpAnd(b *testing.B) { 451 x := alphabetSoup 452 y := alphabetSoup 453 454 opBenchmark(b, opAnd, x, y) 455 } 456 457 func BenchmarkOpOr(b *testing.B) { 458 x := alphabetSoup 459 y := alphabetSoup 460 461 opBenchmark(b, opOr, x, y) 462 } 463 464 func BenchmarkOpXor(b *testing.B) { 465 x := alphabetSoup 466 y := alphabetSoup 467 468 opBenchmark(b, opXor, x, y) 469 } 470 471 func BenchmarkOpByte(b *testing.B) { 472 x := alphabetSoup 473 y := alphabetSoup 474 475 opBenchmark(b, opByte, x, y) 476 } 477 478 func BenchmarkOpAddmod(b *testing.B) { 479 x := alphabetSoup 480 y := alphabetSoup 481 z := alphabetSoup 482 483 opBenchmark(b, opAddmod, x, y, z) 484 } 485 486 func BenchmarkOpMulmod(b *testing.B) { 487 x := alphabetSoup 488 y := alphabetSoup 489 z := alphabetSoup 490 491 opBenchmark(b, opMulmod, x, y, z) 492 } 493 494 func BenchmarkOpSHL(b *testing.B) { 495 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 496 y := "ff" 497 498 opBenchmark(b, opSHL, x, y) 499 } 500 func BenchmarkOpSHR(b *testing.B) { 501 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 502 y := "ff" 503 504 opBenchmark(b, opSHR, x, y) 505 } 506 func BenchmarkOpSAR(b *testing.B) { 507 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 508 y := "ff" 509 510 opBenchmark(b, opSAR, x, y) 511 } 512 func BenchmarkOpIsZero(b *testing.B) { 513 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 514 opBenchmark(b, opIszero, x) 515 } 516 517 func TestOpMstore(t *testing.T) { 518 var ( 519 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 520 stack = newstack() 521 mem = NewMemory() 522 evmInterpreter = NewEVMInterpreter(env, env.Config) 523 ) 524 525 env.interpreter = evmInterpreter 526 mem.Resize(64) 527 pc := uint64(0) 528 v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" 529 stack.push(new(uint256.Int).SetBytes(common.Hex2Bytes(v))) 530 stack.push(new(uint256.Int)) 531 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 532 if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v { 533 t.Fatalf("Mstore fail, got %v, expected %v", got, v) 534 } 535 stack.push(new(uint256.Int).SetUint64(0x1)) 536 stack.push(new(uint256.Int)) 537 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 538 if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" { 539 t.Fatalf("Mstore failed to overwrite previous value") 540 } 541 } 542 543 func BenchmarkOpMstore(bench *testing.B) { 544 var ( 545 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 546 stack = newstack() 547 mem = NewMemory() 548 evmInterpreter = NewEVMInterpreter(env, env.Config) 549 ) 550 551 env.interpreter = evmInterpreter 552 mem.Resize(64) 553 pc := uint64(0) 554 memStart := new(uint256.Int) 555 value := new(uint256.Int).SetUint64(0x1337) 556 557 bench.ResetTimer() 558 for i := 0; i < bench.N; i++ { 559 stack.push(value) 560 stack.push(memStart) 561 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 562 } 563 } 564 565 func BenchmarkOpKeccak256(bench *testing.B) { 566 var ( 567 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 568 stack = newstack() 569 mem = NewMemory() 570 evmInterpreter = NewEVMInterpreter(env, env.Config) 571 ) 572 env.interpreter = evmInterpreter 573 mem.Resize(32) 574 pc := uint64(0) 575 start := new(uint256.Int) 576 577 bench.ResetTimer() 578 for i := 0; i < bench.N; i++ { 579 stack.push(uint256.NewInt(32)) 580 stack.push(start) 581 opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 582 } 583 } 584 585 func TestCreate2Addreses(t *testing.T) { 586 type testcase struct { 587 origin string 588 salt string 589 code string 590 expected string 591 } 592 593 for i, tt := range []testcase{ 594 { 595 origin: "0x0000000000000000000000000000000000000000", 596 salt: "0x0000000000000000000000000000000000000000", 597 code: "0x00", 598 expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38", 599 }, 600 { 601 origin: "0xdeadbeef00000000000000000000000000000000", 602 salt: "0x0000000000000000000000000000000000000000", 603 code: "0x00", 604 expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3", 605 }, 606 { 607 origin: "0xdeadbeef00000000000000000000000000000000", 608 salt: "0xfeed000000000000000000000000000000000000", 609 code: "0x00", 610 expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833", 611 }, 612 { 613 origin: "0x0000000000000000000000000000000000000000", 614 salt: "0x0000000000000000000000000000000000000000", 615 code: "0xdeadbeef", 616 expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e", 617 }, 618 { 619 origin: "0x00000000000000000000000000000000deadbeef", 620 salt: "0xcafebabe", 621 code: "0xdeadbeef", 622 expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7", 623 }, 624 { 625 origin: "0x00000000000000000000000000000000deadbeef", 626 salt: "0xcafebabe", 627 code: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", 628 expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C", 629 }, 630 { 631 origin: "0x0000000000000000000000000000000000000000", 632 salt: "0x0000000000000000000000000000000000000000", 633 code: "0x", 634 expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0", 635 }, 636 } { 637 638 origin := common.BytesToAddress(common.FromHex(tt.origin)) 639 salt := common.BytesToHash(common.FromHex(tt.salt)) 640 code := common.FromHex(tt.code) 641 codeHash := crypto.Keccak256(code) 642 address := crypto.CreateAddress2(origin, salt, codeHash) 643 /* 644 stack := newstack() 645 // salt, but we don't need that for this test 646 stack.push(big.NewInt(int64(len(code)))) //size 647 stack.push(big.NewInt(0)) // memstart 648 stack.push(big.NewInt(0)) // value 649 gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0) 650 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()) 651 */ 652 expected := common.BytesToAddress(common.FromHex(tt.expected)) 653 if !bytes.Equal(expected.Bytes(), address.Bytes()) { 654 t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String()) 655 } 656 } 657 } 658 659 func TestRandom(t *testing.T) { 660 type testcase struct { 661 name string 662 random common.Hash 663 } 664 665 for _, tt := range []testcase{ 666 {name: "empty hash", random: common.Hash{}}, 667 {name: "1", random: common.Hash{0}}, 668 {name: "emptyCodeHash", random: emptyCodeHash}, 669 {name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})}, 670 } { 671 var ( 672 env = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{}) 673 stack = newstack() 674 pc = uint64(0) 675 evmInterpreter = env.interpreter 676 ) 677 opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) 678 if len(stack.data) != 1 { 679 t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) 680 } 681 actual := stack.pop() 682 expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) 683 if overflow { 684 t.Errorf("Testcase %v: invalid overflow", tt.name) 685 } 686 if actual.Cmp(expected) != 0 { 687 t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual) 688 } 689 } 690 }