github.com/turingchain2020/turingchain@v1.1.21/common/address/address_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package address
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	lru "github.com/hashicorp/golang-lru"
    15  
    16  	"github.com/turingchain2020/turingchain/common/crypto"
    17  	_ "github.com/turingchain2020/turingchain/system/crypto/init"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func genkey() crypto.PrivKey {
    23  	c, err := crypto.New("secp256k1")
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	key, err := c.GenKey()
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	return key
    32  }
    33  func TestAddress(t *testing.T) {
    34  	key := genkey()
    35  	t.Logf("%X", key.Bytes())
    36  	addr := PubKeyToAddress(key.PubKey().Bytes())
    37  	t.Log(addr)
    38  }
    39  
    40  func TestMultiSignAddress(t *testing.T) {
    41  	key := genkey()
    42  	addr1 := MultiSignAddress(key.PubKey().Bytes())
    43  	addr := MultiSignAddress(key.PubKey().Bytes())
    44  	assert.Equal(t, addr1, addr)
    45  	err := CheckAddress(addr)
    46  	assert.Equal(t, ErrCheckVersion, err)
    47  	err = CheckMultiSignAddress(addr)
    48  	assert.Nil(t, err)
    49  	t.Log(addr)
    50  }
    51  
    52  func TestPubkeyToAddress(t *testing.T) {
    53  	pubkey := "024a17b0c6eb3143839482faa7e917c9b90a8cfe5008dff748789b8cea1a3d08d5"
    54  	b, err := hex.DecodeString(pubkey)
    55  	if err != nil {
    56  		t.Error(err)
    57  		return
    58  	}
    59  	t.Logf("%X", b)
    60  	addr := PubKeyToAddress(b)
    61  	t.Log(addr)
    62  }
    63  
    64  func TestCheckAddress(t *testing.T) {
    65  	c, err := crypto.New("secp256k1")
    66  	if err != nil {
    67  		t.Error(err)
    68  		return
    69  	}
    70  	key, err := c.GenKey()
    71  	if err != nil {
    72  		t.Error(err)
    73  		return
    74  	}
    75  	addr := PubKeyToAddress(key.PubKey().Bytes())
    76  	err = CheckAddress(addr.String())
    77  	require.NoError(t, err)
    78  
    79  	err = CheckAddress(addr.String() + addr.String())
    80  	require.Equal(t, err, ErrAddressChecksum)
    81  }
    82  
    83  func TestExecAddress(t *testing.T) {
    84  	assert.Equal(t, "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", ExecAddress("ticket"))
    85  	assert.Equal(t, "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", ExecAddress("ticket"))
    86  	addr, err := NewAddrFromString(ExecAddress("ticket"))
    87  	assert.Nil(t, err)
    88  	assert.Equal(t, addr.Version, NormalVer)
    89  }
    90  
    91  func BenchmarkExecAddress(b *testing.B) {
    92  	start := time.Now().UnixNano() / 1000000
    93  	fmt.Println(start)
    94  	for i := 0; i < b.N; i++ {
    95  		ExecAddress("ticket")
    96  	}
    97  	end := time.Now().UnixNano() / 1000000
    98  	fmt.Println(end)
    99  	duration := end - start
   100  	fmt.Println("duration with cache:", strconv.FormatInt(duration, 10))
   101  
   102  	start = time.Now().UnixNano() / 1000000
   103  	fmt.Println(start)
   104  	for i := 0; i < b.N; i++ {
   105  		GetExecAddress("ticket")
   106  	}
   107  	end = time.Now().UnixNano() / 1000000
   108  	fmt.Println(end)
   109  	duration = end - start
   110  	fmt.Println("duration without cache:", strconv.FormatInt(duration, 10))
   111  }
   112  
   113  func TestExecPubKey(t *testing.T) {
   114  	execPubKeyCache, _ = lru.New(10)
   115  	pub := ExecPubKey("test")
   116  	ExecPubKey("test2")
   117  	cachePub := ExecPubKey("test")
   118  	assert.True(t, len(pub) == 32)
   119  	assert.Equal(t, 2, execPubKeyCache.Len())
   120  	assert.Equal(t, pub, cachePub)
   121  }