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