github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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  	"github.com/intfoundation/intchain/crypto"
    21  	"testing"
    22  )
    23  
    24  func TestJumpDestAnalysis(t *testing.T) {
    25  	tests := []struct {
    26  		code  []byte
    27  		exp   byte
    28  		which int
    29  	}{
    30  		{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
    31  		{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
    32  		{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
    33  		{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
    34  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
    35  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    36  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
    37  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    38  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
    39  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
    40  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
    41  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
    42  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
    43  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
    44  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
    45  		{[]byte{byte(PUSH32)}, 0x7F, 0},
    46  		{[]byte{byte(PUSH32)}, 0xFF, 1},
    47  		{[]byte{byte(PUSH32)}, 0xFF, 2},
    48  	}
    49  	for _, test := range tests {
    50  		ret := codeBitmap(test.code)
    51  		if ret[test.which] != test.exp {
    52  			t.Fatalf("expected %x, got %02x", test.exp, ret[test.which])
    53  		}
    54  	}
    55  
    56  }
    57  
    58  func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
    59  	// 1.4 ms
    60  	code := make([]byte, 1200000)
    61  	bench.ResetTimer()
    62  	for i := 0; i < bench.N; i++ {
    63  		codeBitmap(code)
    64  	}
    65  	bench.StopTimer()
    66  }
    67  func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
    68  	// 4 ms
    69  	code := make([]byte, 1200000)
    70  	bench.ResetTimer()
    71  	for i := 0; i < bench.N; i++ {
    72  		crypto.Keccak256Hash(code)
    73  	}
    74  	bench.StopTimer()
    75  }