github.com/annchain/OG@v0.0.9/types/types_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package types
    15  
    16  import (
    17  	"crypto/sha256"
    18  	"fmt"
    19  	"github.com/annchain/OG/common/hexutil"
    20  	"github.com/annchain/OG/og/types"
    21  
    22  	"math/rand"
    23  	"sort"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestCuckooFilter_EncodeMsg(t *testing.T) {
    29  	m := &MessageSyncRequest{}
    30  	m.Filter = NewDefaultBloomFilter()
    31  	for i := 0; i < 25; i++ {
    32  		str := "abcdef" + fmt.Sprintf("%d%d%d", i, i+2, i) + "ef"
    33  		m.Filter.AddItem([]byte(str))
    34  		date, err := m.Filter.filter.Encode()
    35  		fmt.Println("len ", len(date), err)
    36  		out, _ := m.MarshalMsg(nil)
    37  		fmt.Println("i", i, "size ", m.Msgsize(), "len", len(out), err, len(m.Filter.Data))
    38  	}
    39  	for i := 0; i < 37; i++ {
    40  		str := "abcdef" + fmt.Sprintf("%d%d%d", i, i+2, i) + "ef"
    41  		ok, err := m.Filter.LookUpItem([]byte(str))
    42  		if i < 25 && !ok {
    43  			t.Fatal("should be true")
    44  		}
    45  		fmt.Println(i, str, ok, err)
    46  	}
    47  	fmt.Println(m.Filter.filter)
    48  }
    49  
    50  func TestRandomTx(t *testing.T) {
    51  	var txis types.Txis
    52  	for i := 0; i < 50; i++ {
    53  		if i%10 == 0 {
    54  			tx := RandomSequencer()
    55  			tx.Height = uint64(rand.Intn(4))
    56  			tx.Weight = uint64(rand.Intn(10))
    57  			txis = append(txis, types.Txi(tx))
    58  		} else {
    59  			tx := RandomTx()
    60  			tx.Height = uint64(rand.Intn(4))
    61  			tx.Weight = uint64(rand.Intn(10))
    62  			txis = append(txis, types.Txi(tx))
    63  		}
    64  	}
    65  	fmt.Println(len(txis), txis)
    66  	sort.Sort(txis)
    67  	fmt.Println(len(txis), txis)
    68  }
    69  
    70  func TestHash_Cmp(t *testing.T) {
    71  	m := MessageTxsResponse{}
    72  	var rawTxs TxisMarshaler
    73  	for i := 0; i < 10000; i++ {
    74  		tx := RandomTx()
    75  		rawTxMarshaler := &RawTxMarshaler{tx.RawTx()}
    76  		rawTxs = append(rawTxs, rawTxMarshaler)
    77  	}
    78  	m.RawTxs = &rawTxs
    79  	data1, _ := m.MarshalMsg(nil)
    80  	h := sha256.New()
    81  	start := time.Now()
    82  	h.Write(data1)
    83  	s1 := h.Sum(nil)
    84  	fmt.Println(time.Now().Sub(start), "used for txs ", "len", len(data1), hexutil.Encode(s1))
    85  	m2 := MessageNewTx{
    86  		RawTx: RandomTx().RawTx(),
    87  	}
    88  	data2, _ := m2.MarshalMsg(nil)
    89  	h = sha256.New()
    90  	start = time.Now()
    91  	h.Write(data2)
    92  	s2 := h.Sum(nil)
    93  	fmt.Println(time.Now().Sub(start), "used for tx", "len", len(data2), hexutil.Encode(s2))
    94  }
    95  
    96  func TestRawTxs_String(t *testing.T) {
    97  	var r RawSequencers
    98  	fmt.Println(r)
    99  	var s MessageSyncResponse
   100  	fmt.Println(s)
   101  }