github.com/klaytn/klaytn@v1.12.1/blockchain/vm/analysis_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/analysis_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/klaytn/klaytn/crypto"
    27  )
    28  
    29  func TestJumpDestAnalysis(t *testing.T) {
    30  	tests := []struct {
    31  		code  []byte
    32  		exp   byte
    33  		which int
    34  	}{
    35  		{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
    36  		{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
    37  		{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
    38  		{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
    39  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
    40  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    41  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
    42  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    43  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
    44  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
    45  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
    46  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
    47  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
    48  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
    49  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
    50  		{[]byte{byte(PUSH32)}, 0x7F, 0},
    51  		{[]byte{byte(PUSH32)}, 0xFF, 1},
    52  		{[]byte{byte(PUSH32)}, 0xFF, 2},
    53  	}
    54  	for _, test := range tests {
    55  		ret := codeBitmap(test.code)
    56  		if ret[test.which] != test.exp {
    57  			t.Fatalf("expected %x, got %02x", test.exp, ret[test.which])
    58  		}
    59  	}
    60  }
    61  
    62  func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
    63  	// 1.4 ms
    64  	code := make([]byte, 1200000)
    65  	bench.ResetTimer()
    66  	for i := 0; i < bench.N; i++ {
    67  		codeBitmap(code)
    68  	}
    69  	bench.StopTimer()
    70  }
    71  
    72  func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
    73  	// 4 ms
    74  	code := make([]byte, 1200000)
    75  	bench.ResetTimer()
    76  	for i := 0; i < bench.N; i++ {
    77  		crypto.Keccak256Hash(code)
    78  	}
    79  	bench.StopTimer()
    80  }