github.com/theQRL/go-zond@v0.1.1/core/vm/analysis_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 "math/bits" 21 "testing" 22 23 "github.com/theQRL/go-zond/crypto" 24 ) 25 26 func TestJumpDestAnalysis(t *testing.T) { 27 tests := []struct { 28 code []byte 29 exp byte 30 which int 31 }{ 32 {[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0b0000_0010, 0}, 33 {[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0b0000_1010, 0}, 34 {[]byte{0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1), 0x00, byte(PUSH1)}, 0b0101_0100, 0}, 35 {[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, bits.Reverse8(0x7F), 0}, 36 {[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 1}, 37 {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0b1100_0000, 0}, 38 {[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1}, 39 {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0010_1110, 0}, 40 {[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0000, 1}, 41 {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1100, 0}, 42 {[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0011, 1}, 43 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1110, 0}, 44 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b1111_1111, 1}, 45 {[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0b0000_0001, 2}, 46 {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b1111_1110, 0}, 47 {[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0b0000_0101, 1}, 48 {[]byte{byte(PUSH32)}, 0b1111_1110, 0}, 49 {[]byte{byte(PUSH32)}, 0b1111_1111, 1}, 50 {[]byte{byte(PUSH32)}, 0b1111_1111, 2}, 51 {[]byte{byte(PUSH32)}, 0b1111_1111, 3}, 52 {[]byte{byte(PUSH32)}, 0b0000_0001, 4}, 53 } 54 for i, test := range tests { 55 ret := codeBitmap(test.code) 56 if ret[test.which] != test.exp { 57 t.Fatalf("test %d: expected %x, got %02x", i, test.exp, ret[test.which]) 58 } 59 } 60 } 61 62 const analysisCodeSize = 1200 * 1024 63 64 func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) { 65 // 1.4 ms 66 code := make([]byte, analysisCodeSize) 67 bench.SetBytes(analysisCodeSize) 68 bench.ResetTimer() 69 for i := 0; i < bench.N; i++ { 70 codeBitmap(code) 71 } 72 bench.StopTimer() 73 } 74 func BenchmarkJumpdestHashing_1200k(bench *testing.B) { 75 // 4 ms 76 code := make([]byte, analysisCodeSize) 77 bench.SetBytes(analysisCodeSize) 78 bench.ResetTimer() 79 for i := 0; i < bench.N; i++ { 80 crypto.Keccak256Hash(code) 81 } 82 bench.StopTimer() 83 } 84 85 func BenchmarkJumpdestOpAnalysis(bench *testing.B) { 86 var op OpCode 87 bencher := func(b *testing.B) { 88 code := make([]byte, analysisCodeSize) 89 b.SetBytes(analysisCodeSize) 90 for i := range code { 91 code[i] = byte(op) 92 } 93 bits := make(bitvec, len(code)/8+1+4) 94 b.ResetTimer() 95 for i := 0; i < b.N; i++ { 96 for j := range bits { 97 bits[j] = 0 98 } 99 codeBitmapInternal(code, bits) 100 } 101 } 102 for op = PUSH1; op <= PUSH32; op++ { 103 bench.Run(op.String(), bencher) 104 } 105 op = JUMPDEST 106 bench.Run(op.String(), bencher) 107 op = STOP 108 bench.Run(op.String(), bencher) 109 }