github.com/annchain/OG@v0.0.9/tests/crypto/crypto_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 crypto
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/arefactor/og_interface"
    19  	"github.com/annchain/OG/common/crypto"
    20  	"github.com/annchain/OG/og/types/archive"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestRawTx_Tx(t *testing.T) {
    26  	signer := crypto.NewSigner(crypto.CryptoTypeEd25519)
    27  	var num = 10000
    28  	var txs types.Txs
    29  	var rawtxs archive.RawTxs
    30  	for i := 0; i < num; i++ {
    31  		tx := archive.RandomTx()
    32  		pub, _ := signer.RandomKeyPair()
    33  		tx.PublicKey = pub.KeyBytes[:]
    34  		rawtxs = append(rawtxs, tx.RawTx())
    35  	}
    36  	start := time.Now()
    37  	for i := 0; i < num; i++ {
    38  		txs = append(txs, rawtxs[i].Tx())
    39  	}
    40  	fmt.Println("used time ", time.Now().Sub(start))
    41  
    42  }
    43  
    44  func TestRawTx_encode(t *testing.T) {
    45  	signer := crypto.NewSigner(crypto.CryptoTypeEd25519)
    46  	og_interface.Signer = signer
    47  	var num = 10000
    48  	var txs types.Txs
    49  	type bytes struct {
    50  		archive.RawData
    51  	}
    52  	var rawtxs []bytes
    53  	for i := 0; i < num; i++ {
    54  		tx := archive.RandomTx()
    55  		pub, _ := signer.RandomKeyPair()
    56  		tx.PublicKey = pub.KeyBytes[:]
    57  		data, _ := tx.MarshalMsg(nil)
    58  		rawtxs = append(rawtxs, bytes{data})
    59  	}
    60  	start := time.Now()
    61  	for i := 0; i < num; i++ {
    62  		var tx types.Tx
    63  		_, err := tx.UnmarshalMsg(rawtxs[i].RawData)
    64  		if err != nil {
    65  			panic(err)
    66  		}
    67  		txs = append(txs, &tx)
    68  	}
    69  	fmt.Println("used time ", time.Now().Sub(start))
    70  
    71  }