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