github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/analysis_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:35</date>
    10  //</624450081856163840>
    11  
    12  
    13  package vm
    14  
    15  import (
    16  	"testing"
    17  
    18  	"github.com/ethereum/go-ethereum/crypto"
    19  )
    20  
    21  func TestJumpDestAnalysis(t *testing.T) {
    22  	tests := []struct {
    23  		code  []byte
    24  		exp   byte
    25  		which int
    26  	}{
    27  		{[]byte{byte(PUSH1), 0x01, 0x01, 0x01}, 0x40, 0},
    28  		{[]byte{byte(PUSH1), byte(PUSH1), byte(PUSH1), byte(PUSH1)}, 0x50, 0},
    29  		{[]byte{byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), byte(PUSH8), 0x01, 0x01, 0x01}, 0x7F, 0},
    30  		{[]byte{byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 1},
    31  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), byte(PUSH2), byte(PUSH2), 0x01, 0x01, 0x01}, 0x03, 0},
    32  		{[]byte{0x01, 0x01, 0x01, 0x01, 0x01, byte(PUSH2), 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    33  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x74, 0},
    34  		{[]byte{byte(PUSH3), 0x01, 0x01, 0x01, byte(PUSH1), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x00, 1},
    35  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x3F, 0},
    36  		{[]byte{0x01, byte(PUSH8), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xC0, 1},
    37  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x7F, 0},
    38  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0xFF, 1},
    39  		{[]byte{byte(PUSH16), 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 0x80, 2},
    40  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0x7f, 0},
    41  		{[]byte{byte(PUSH8), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, byte(PUSH1), 0x01}, 0xA0, 1},
    42  		{[]byte{byte(PUSH32)}, 0x7F, 0},
    43  		{[]byte{byte(PUSH32)}, 0xFF, 1},
    44  		{[]byte{byte(PUSH32)}, 0xFF, 2},
    45  	}
    46  	for _, test := range tests {
    47  		ret := codeBitmap(test.code)
    48  		if ret[test.which] != test.exp {
    49  			t.Fatalf("expected %x, got %02x", test.exp, ret[test.which])
    50  		}
    51  	}
    52  }
    53  
    54  func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
    55  //1.4毫秒
    56  	code := make([]byte, 1200000)
    57  	bench.ResetTimer()
    58  	for i := 0; i < bench.N; i++ {
    59  		codeBitmap(code)
    60  	}
    61  	bench.StopTimer()
    62  }
    63  func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
    64  //4毫秒
    65  	code := make([]byte, 1200000)
    66  	bench.ResetTimer()
    67  	for i := 0; i < bench.N; i++ {
    68  		crypto.Keccak256Hash(code)
    69  	}
    70  	bench.StopTimer()
    71  }
    72