github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/params/consensus_params_test.go (about)

     1  package params
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"github.com/SmartMeshFoundation/Spectrum/accounts/abi"
     7  	"github.com/SmartMeshFoundation/Spectrum/common"
     8  	"math/big"
     9  	"sort"
    10  	"strings"
    11  	"sync"
    12  	"testing"
    13  )
    14  
    15  var data = ChiefInfoList{
    16  	newChiefInfo(TestnetChainConfig.Chief002Block, "0.0.2", TestnetChainConfig.Chief002Address, TribeChief_0_0_2ABI),
    17  	newChiefInfo(TestnetChainConfig.Chief003Block, "0.0.3", TestnetChainConfig.Chief003Address, TribeChief_0_0_3ABI),
    18  	newChiefInfo(TestnetChainConfig.Chief004Block, "0.0.4", TestnetChainConfig.Chief004Address, TribeChief_0_0_4ABI),
    19  	newChiefInfo(TestnetChainConfig.Chief005Block, "0.0.5", TestnetChainConfig.Chief005Address, TribeChief_0_0_5ABI),
    20  }
    21  
    22  func TestChiefInfoList(t *testing.T) {
    23  	t.Log(data)
    24  	sort.Sort(data)
    25  	t.Log(data)
    26  }
    27  
    28  func TestGetChiefAddress(t *testing.T) {
    29  	sort.Sort(data)
    30  	t.Log(getChiefInfo(data, big.NewInt(5)))
    31  	t.Log(getChiefInfo(data, big.NewInt(50010)))
    32  	t.Log(getChiefInfo(data, big.NewInt(20010)))
    33  	t.Log(getChiefInfo(data, big.NewInt(30000)))
    34  	t.Log(getChiefInfo(data, big.NewInt(40010)))
    35  	t.Log(getChiefInfo(data, big.NewInt(80000)))
    36  }
    37  
    38  func TestIsChiefAddress(t *testing.T) {
    39  	t.Log(isChiefAddress(data, common.HexToAddress("0x04")))
    40  	t.Log(isChiefAddress(data, common.HexToAddress("0x0f")))
    41  }
    42  
    43  type ChiefStatus1 struct {
    44  	NumberList []*big.Int
    45  	BlackList  []*big.Int
    46  }
    47  
    48  type ChiefStatus2 struct {
    49  	NumberList []*big.Int
    50  	BlackList  []*big.Int
    51  }
    52  
    53  func TestFooBar(t *testing.T) {
    54  	a := ChiefStatus1{[]*big.Int{big.NewInt(1)}, []*big.Int{big.NewInt(2)}}
    55  	t.Log(a)
    56  	b := ChiefStatus2(a)
    57  	t.Log(b)
    58  	var x []common.Address
    59  	x = nil
    60  	t.Log(x == nil)
    61  }
    62  
    63  func TestIsChiefUpdate(t *testing.T) {
    64  	data := []byte{28, 27, 135, 114, 0, 0}
    65  	t.Log(IsChiefUpdate(data))
    66  	t.Log(IsChiefUpdate(data))
    67  	t.Log(IsChiefUpdate(data))
    68  	data = []byte{28, 27, 135, 115, 0, 0}
    69  	t.Log(IsChiefUpdate(data))
    70  
    71  }
    72  
    73  func TestAddr(t *testing.T) {
    74  	add1 := common.HexToAddress("0xAd4c80164065a3c33dD2014908c7563eFf88aB49")
    75  	add2 := common.HexToAddress("0xAd4c80164065a3c33dD2014908c7563eFf88Ab49")
    76  	t.Log(add1 == add2)
    77  }
    78  
    79  func TestRegisterContract(t *testing.T) {
    80  	t.Log(TribeChief_0_0_6ABI)
    81  	hexdata := "1c1b87720000000000000000000000000000000000000000000000000000000000000000"
    82  	data, err := hex.DecodeString(hexdata)
    83  	t.Log("1 err=", err, data)
    84  	_abi, err := abi.JSON(strings.NewReader(TribeChief_0_0_2ABI))
    85  	t.Log("2 err=", err)
    86  	method := _abi.Methods["update"]
    87  	id := new(common.Address)
    88  	r := []interface{}{id}
    89  	t.Log(len(data[4:]), len(data), data[:4])
    90  	err = method.Inputs.Unpack(id, data[4:])
    91  	t.Log("4 err=", err, r, id.Hex())
    92  
    93  	t.Log(bytes.Equal(data[:4], []byte{28, 27, 135, 114}), data[:4])
    94  
    95  	rrr, _ := _abi.Pack("update", common.HexToAddress("0xAd4c80164065a3c33dD2014908c7563eFf88Ab49"))
    96  	t.Log(rrr[4:])
    97  	aaa := common.Bytes2Hex(rrr[4:])
    98  	t.Log(common.HexToAddress(aaa) == common.HexToAddress("0xAd4c80164065a3c33dD2014908c7563eFf88Ab49"))
    99  	bbb := common.Bytes2Hex([]byte{0, 0, 0, 0, 0, 0, 0})
   100  	t.Log(common.HexToAddress(bbb) == common.HexToAddress(""))
   101  }
   102  
   103  func TestError(t *testing.T) {
   104  	ch := make(chan int)
   105  	sm := new(sync.Map)
   106  	sm.Store("foo", "bar")
   107  	sm.Store("hello", "world")
   108  
   109  	sm.Range(func(k, v interface{}) bool {
   110  		defer func() {
   111  			if err := recover(); err != nil {
   112  				t.Log(k, v, "err:", err)
   113  			}
   114  		}()
   115  		defer close(ch)
   116  		t.Log(k, v)
   117  		return true
   118  	})
   119  
   120  }