github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/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/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 // 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 var ( 95 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 96 stack = newstack() 97 pc = uint64(0) 98 evmInterpreter = env.interpreter 99 ) 100 101 for i, test := range tests { 102 x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.X)) 103 y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Y)) 104 expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Expected)) 105 stack.push(x) 106 stack.push(y) 107 opFn(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) 108 if len(stack.data) != 1 { 109 t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.data)) 110 } 111 actual := stack.pop() 112 113 if actual.Cmp(expected) != 0 { 114 t.Errorf("Testcase %v %d, %v(%x, %x): expected %x, got %x", name, i, name, x, y, expected, actual) 115 } 116 } 117 } 118 119 func TestByteOp(t *testing.T) { 120 tests := []TwoOperandTestcase{ 121 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"}, 122 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"}, 123 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"}, 124 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"}, 125 {"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"}, 126 {"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"}, 127 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"}, 128 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"}, 129 } 130 testTwoOperandOp(t, tests, opByte, "byte") 131 } 132 133 func TestSHL(t *testing.T) { 134 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left 135 tests := []TwoOperandTestcase{ 136 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"}, 137 {"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 138 {"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 139 {"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 140 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 141 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 142 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 143 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 144 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 145 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 146 } 147 testTwoOperandOp(t, tests, opSHL, "shl") 148 } 149 150 func TestSHR(t *testing.T) { 151 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right 152 tests := []TwoOperandTestcase{ 153 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 154 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 155 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"}, 156 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 157 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 158 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 159 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 160 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 161 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 162 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 163 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 164 } 165 testTwoOperandOp(t, tests, opSHR, "shr") 166 } 167 168 func TestSAR(t *testing.T) { 169 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right 170 tests := []TwoOperandTestcase{ 171 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 172 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 173 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"}, 174 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 175 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 176 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 177 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 178 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 179 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 180 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 181 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 182 {"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 183 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"}, 184 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 185 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"}, 186 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 187 } 188 189 testTwoOperandOp(t, tests, opSAR, "sar") 190 } 191 192 func TestAddMod(t *testing.T) { 193 var ( 194 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 195 stack = newstack() 196 evmInterpreter = NewEVMInterpreter(env, env.Config) 197 pc = uint64(0) 198 ) 199 tests := []struct { 200 x string 201 y string 202 z string 203 expected string 204 }{ 205 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 206 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", 207 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 208 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", 209 }, 210 } 211 // x + y = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd 212 // in 256 bit repr, fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd 213 214 for i, test := range tests { 215 x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.x)) 216 y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.y)) 217 z := new(uint256.Int).SetBytes(common.Hex2Bytes(test.z)) 218 expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.expected)) 219 stack.push(z) 220 stack.push(y) 221 stack.push(x) 222 opAddmod(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) 223 actual := stack.pop() 224 if actual.Cmp(expected) != 0 { 225 t.Errorf("Testcase %d, expected %x, got %x", i, expected, actual) 226 } 227 } 228 } 229 230 // utility function to fill the json-file with testcases 231 // Enable this test to generate the 'testcases_xx.json' files 232 func TestWriteExpectedValues(t *testing.T) { 233 t.Skip("Enable this test to create json test cases.") 234 235 // getResult is a convenience function to generate the expected values 236 getResult := func(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase { 237 var ( 238 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 239 stack = newstack() 240 pc = uint64(0) 241 interpreter = env.interpreter 242 ) 243 result := make([]TwoOperandTestcase, len(args)) 244 for i, param := range args { 245 x := new(uint256.Int).SetBytes(common.Hex2Bytes(param.x)) 246 y := new(uint256.Int).SetBytes(common.Hex2Bytes(param.y)) 247 stack.push(x) 248 stack.push(y) 249 opFn(&pc, interpreter, &ScopeContext{nil, stack, nil}) 250 actual := stack.pop() 251 result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)} 252 } 253 return result 254 } 255 256 for name, method := range twoOpMethods { 257 data, err := json.Marshal(getResult(commonParams, method)) 258 if err != nil { 259 t.Fatal(err) 260 } 261 _ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) 262 if err != nil { 263 t.Fatal(err) 264 } 265 } 266 } 267 268 // TestJsonTestcases runs through all the testcases defined as json-files 269 func TestJsonTestcases(t *testing.T) { 270 for name := range twoOpMethods { 271 data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name)) 272 if err != nil { 273 t.Fatal("Failed to read file", err) 274 } 275 var testcases []TwoOperandTestcase 276 json.Unmarshal(data, &testcases) 277 testTwoOperandOp(t, testcases, twoOpMethods[name], name) 278 } 279 } 280 281 func opBenchmark(bench *testing.B, op executionFunc, args ...string) { 282 var ( 283 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 284 stack = newstack() 285 scope = &ScopeContext{nil, stack, nil} 286 evmInterpreter = NewEVMInterpreter(env, env.Config) 287 ) 288 289 env.interpreter = evmInterpreter 290 // convert args 291 intArgs := make([]*uint256.Int, len(args)) 292 for i, arg := range args { 293 intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) 294 } 295 pc := uint64(0) 296 bench.ResetTimer() 297 for i := 0; i < bench.N; i++ { 298 for _, arg := range intArgs { 299 stack.push(arg) 300 } 301 op(&pc, evmInterpreter, scope) 302 stack.pop() 303 } 304 bench.StopTimer() 305 306 for i, arg := range args { 307 want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) 308 if have := intArgs[i]; !want.Eq(have) { 309 bench.Fatalf("input #%d mutated, have %x want %x", i, have, want) 310 } 311 } 312 } 313 314 func BenchmarkOpAdd64(b *testing.B) { 315 x := "ffffffff" 316 y := "fd37f3e2bba2c4f" 317 318 opBenchmark(b, opAdd, x, y) 319 } 320 321 func BenchmarkOpAdd128(b *testing.B) { 322 x := "ffffffffffffffff" 323 y := "f5470b43c6549b016288e9a65629687" 324 325 opBenchmark(b, opAdd, x, y) 326 } 327 328 func BenchmarkOpAdd256(b *testing.B) { 329 x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9" 330 y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57" 331 332 opBenchmark(b, opAdd, x, y) 333 } 334 335 func BenchmarkOpSub64(b *testing.B) { 336 x := "51022b6317003a9d" 337 y := "a20456c62e00753a" 338 339 opBenchmark(b, opSub, x, y) 340 } 341 342 func BenchmarkOpSub128(b *testing.B) { 343 x := "4dde30faaacdc14d00327aac314e915d" 344 y := "9bbc61f5559b829a0064f558629d22ba" 345 346 opBenchmark(b, opSub, x, y) 347 } 348 349 func BenchmarkOpSub256(b *testing.B) { 350 x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37" 351 y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e" 352 353 opBenchmark(b, opSub, x, y) 354 } 355 356 func BenchmarkOpMul(b *testing.B) { 357 x := alphabetSoup 358 y := alphabetSoup 359 360 opBenchmark(b, opMul, x, y) 361 } 362 363 func BenchmarkOpDiv256(b *testing.B) { 364 x := "ff3f9014f20db29ae04af2c2d265de17" 365 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 366 opBenchmark(b, opDiv, x, y) 367 } 368 369 func BenchmarkOpDiv128(b *testing.B) { 370 x := "fdedc7f10142ff97" 371 y := "fbdfda0e2ce356173d1993d5f70a2b11" 372 opBenchmark(b, opDiv, x, y) 373 } 374 375 func BenchmarkOpDiv64(b *testing.B) { 376 x := "fcb34eb3" 377 y := "f97180878e839129" 378 opBenchmark(b, opDiv, x, y) 379 } 380 381 func BenchmarkOpSdiv(b *testing.B) { 382 x := "ff3f9014f20db29ae04af2c2d265de17" 383 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 384 385 opBenchmark(b, opSdiv, x, y) 386 } 387 388 func BenchmarkOpMod(b *testing.B) { 389 x := alphabetSoup 390 y := alphabetSoup 391 392 opBenchmark(b, opMod, x, y) 393 } 394 395 func BenchmarkOpSmod(b *testing.B) { 396 x := alphabetSoup 397 y := alphabetSoup 398 399 opBenchmark(b, opSmod, x, y) 400 } 401 402 func BenchmarkOpExp(b *testing.B) { 403 x := alphabetSoup 404 y := alphabetSoup 405 406 opBenchmark(b, opExp, x, y) 407 } 408 409 func BenchmarkOpSignExtend(b *testing.B) { 410 x := alphabetSoup 411 y := alphabetSoup 412 413 opBenchmark(b, opSignExtend, x, y) 414 } 415 416 func BenchmarkOpLt(b *testing.B) { 417 x := alphabetSoup 418 y := alphabetSoup 419 420 opBenchmark(b, opLt, x, y) 421 } 422 423 func BenchmarkOpGt(b *testing.B) { 424 x := alphabetSoup 425 y := alphabetSoup 426 427 opBenchmark(b, opGt, x, y) 428 } 429 430 func BenchmarkOpSlt(b *testing.B) { 431 x := alphabetSoup 432 y := alphabetSoup 433 434 opBenchmark(b, opSlt, x, y) 435 } 436 437 func BenchmarkOpSgt(b *testing.B) { 438 x := alphabetSoup 439 y := alphabetSoup 440 441 opBenchmark(b, opSgt, x, y) 442 } 443 444 func BenchmarkOpEq(b *testing.B) { 445 x := alphabetSoup 446 y := alphabetSoup 447 448 opBenchmark(b, opEq, x, y) 449 } 450 func BenchmarkOpEq2(b *testing.B) { 451 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 452 y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe" 453 opBenchmark(b, opEq, x, y) 454 } 455 func BenchmarkOpAnd(b *testing.B) { 456 x := alphabetSoup 457 y := alphabetSoup 458 459 opBenchmark(b, opAnd, x, y) 460 } 461 462 func BenchmarkOpOr(b *testing.B) { 463 x := alphabetSoup 464 y := alphabetSoup 465 466 opBenchmark(b, opOr, x, y) 467 } 468 469 func BenchmarkOpXor(b *testing.B) { 470 x := alphabetSoup 471 y := alphabetSoup 472 473 opBenchmark(b, opXor, x, y) 474 } 475 476 func BenchmarkOpByte(b *testing.B) { 477 x := alphabetSoup 478 y := alphabetSoup 479 480 opBenchmark(b, opByte, x, y) 481 } 482 483 func BenchmarkOpAddmod(b *testing.B) { 484 x := alphabetSoup 485 y := alphabetSoup 486 z := alphabetSoup 487 488 opBenchmark(b, opAddmod, x, y, z) 489 } 490 491 func BenchmarkOpMulmod(b *testing.B) { 492 x := alphabetSoup 493 y := alphabetSoup 494 z := alphabetSoup 495 496 opBenchmark(b, opMulmod, x, y, z) 497 } 498 499 func BenchmarkOpSHL(b *testing.B) { 500 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 501 y := "ff" 502 503 opBenchmark(b, opSHL, x, y) 504 } 505 func BenchmarkOpSHR(b *testing.B) { 506 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 507 y := "ff" 508 509 opBenchmark(b, opSHR, x, y) 510 } 511 func BenchmarkOpSAR(b *testing.B) { 512 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 513 y := "ff" 514 515 opBenchmark(b, opSAR, x, y) 516 } 517 func BenchmarkOpIsZero(b *testing.B) { 518 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 519 opBenchmark(b, opIszero, x) 520 } 521 522 func TestOpMstore(t *testing.T) { 523 var ( 524 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 525 stack = newstack() 526 mem = NewMemory() 527 evmInterpreter = NewEVMInterpreter(env, env.Config) 528 ) 529 530 env.interpreter = evmInterpreter 531 mem.Resize(64) 532 pc := uint64(0) 533 v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" 534 stack.push(new(uint256.Int).SetBytes(common.Hex2Bytes(v))) 535 stack.push(new(uint256.Int)) 536 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 537 if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v { 538 t.Fatalf("Mstore fail, got %v, expected %v", got, v) 539 } 540 stack.push(new(uint256.Int).SetUint64(0x1)) 541 stack.push(new(uint256.Int)) 542 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 543 if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" { 544 t.Fatalf("Mstore failed to overwrite previous value") 545 } 546 } 547 548 func BenchmarkOpMstore(bench *testing.B) { 549 var ( 550 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 551 stack = newstack() 552 mem = NewMemory() 553 evmInterpreter = NewEVMInterpreter(env, env.Config) 554 ) 555 556 env.interpreter = evmInterpreter 557 mem.Resize(64) 558 pc := uint64(0) 559 memStart := new(uint256.Int) 560 value := new(uint256.Int).SetUint64(0x1337) 561 562 bench.ResetTimer() 563 for i := 0; i < bench.N; i++ { 564 stack.push(value) 565 stack.push(memStart) 566 opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) 567 } 568 } 569 570 func BenchmarkOpKeccak256(bench *testing.B) { 571 var ( 572 env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) 573 stack = newstack() 574 mem = NewMemory() 575 evmInterpreter = NewEVMInterpreter(env, env.Config) 576 ) 577 env.interpreter = evmInterpreter 578 mem.Resize(32) 579 pc := uint64(0) 580 start := new(uint256.Int) 581 582 bench.ResetTimer() 583 for i := 0; i < bench.N; i++ { 584 stack.push(uint256.NewInt(32)) 585 stack.push(start) 586 opKeccak256(&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 origin := common.BytesToAddress(common.FromHex(tt.origin)) 643 salt := common.BytesToHash(common.FromHex(tt.salt)) 644 code := common.FromHex(tt.code) 645 codeHash := crypto.Keccak256(code) 646 address := crypto.CreateAddress2(origin, salt, codeHash) 647 /* 648 stack := newstack() 649 // salt, but we don't need that for this test 650 stack.push(big.NewInt(int64(len(code)))) //size 651 stack.push(big.NewInt(0)) // memstart 652 stack.push(big.NewInt(0)) // value 653 gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0) 654 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()) 655 */ 656 expected := common.BytesToAddress(common.FromHex(tt.expected)) 657 if !bytes.Equal(expected.Bytes(), address.Bytes()) { 658 t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String()) 659 } 660 } 661 } 662 663 func TestRandom(t *testing.T) { 664 type testcase struct { 665 name string 666 random common.Hash 667 } 668 669 for _, tt := range []testcase{ 670 {name: "empty hash", random: common.Hash{}}, 671 {name: "1", random: common.Hash{0}}, 672 {name: "emptyCodeHash", random: emptyCodeHash}, 673 {name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})}, 674 } { 675 var ( 676 env = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{}) 677 stack = newstack() 678 pc = uint64(0) 679 evmInterpreter = env.interpreter 680 ) 681 opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) 682 if len(stack.data) != 1 { 683 t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) 684 } 685 actual := stack.pop() 686 expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) 687 if overflow { 688 t.Errorf("Testcase %v: invalid overflow", tt.name) 689 } 690 if actual.Cmp(expected) != 0 { 691 t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual) 692 } 693 } 694 }