github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/vm/analysis_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/neatlab/neatio/utilities/crypto"
     7  )
     8  
     9  func TestJumpDestAnalysis(t *testing.T) {
    10  	tests := []struct {
    11  		code  []byte
    12  		exp   byte
    13  		which int
    14  	}{
    15  		{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
    16  		{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
    17  		{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
    18  		{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
    19  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
    20  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    21  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
    22  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    23  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
    24  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
    25  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
    26  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
    27  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
    28  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
    29  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
    30  		{[]byte{byte(PUSH32)}, 0x7F, 0},
    31  		{[]byte{byte(PUSH32)}, 0xFF, 1},
    32  		{[]byte{byte(PUSH32)}, 0xFF, 2},
    33  	}
    34  	for _, test := range tests {
    35  		ret := codeBitmap(test.code)
    36  		if ret[test.which] != test.exp {
    37  			t.Fatalf("expected %x, got %02x", test.exp, ret[test.which])
    38  		}
    39  	}
    40  
    41  }
    42  
    43  func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
    44  
    45  	code := make([]byte, 1200000)
    46  	bench.ResetTimer()
    47  	for i := 0; i < bench.N; i++ {
    48  		codeBitmap(code)
    49  	}
    50  	bench.StopTimer()
    51  }
    52  func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
    53  
    54  	code := make([]byte, 1200000)
    55  	bench.ResetTimer()
    56  	for i := 0; i < bench.N; i++ {
    57  		crypto.Keccak256Hash(code)
    58  	}
    59  	bench.StopTimer()
    60  }