github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/epoch/epoch_test.go (about)

     1  package epoch
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/intfoundation/intchain/common"
     6  	tmTypes "github.com/intfoundation/intchain/consensus/ipbft/types"
     7  	"math/big"
     8  	"sort"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestEstimateEpoch(t *testing.T) {
    14  	timeA := time.Now()
    15  	timeB := timeA.Unix()
    16  	timeStr := timeA.String()
    17  	t.Logf("\ntimeA now: %v\n, timeA unix: %v\n, timeA String: %v\n, timeA second %v\n", timeA, timeB, timeStr, timeA.Second())
    18  
    19  	formatTimeStr := "2020-06-23 09:51:38.397502 +0800 CST m=+10323.270024761"
    20  	parse, e := time.Parse("", formatTimeStr)
    21  	if e == nil {
    22  		t.Logf("time: %v", parse)
    23  	} else {
    24  		t.Errorf("parse error: %v", e)
    25  	}
    26  
    27  	timeC := time.Now().UnixNano()
    28  	t.Logf("time c %v", timeC)
    29  	t.Logf("time c %v", timeC)
    30  
    31  	var timeD time.Time
    32  	t.Logf("t %v", timeD) // 0001-01-01 00:00:00 +0000 UTC
    33  
    34  	fee := big.NewInt(30000000)
    35  	halfFee := big.NewInt(0).Div(fee, big.NewInt(2))
    36  	t.Logf("half fee %v, fee %v\n", halfFee, fee)
    37  }
    38  
    39  func TestVoteSetCompare(t *testing.T) {
    40  	var voteArr []*EpochValidatorVote
    41  	voteArr = []*EpochValidatorVote{
    42  		{
    43  			Address: common.StringToAddress("INT3CFVNpTwr3QrykhPWiLP8n9wsyCVa"),
    44  			Amount:  big.NewInt(1),
    45  		},
    46  		{
    47  			Address: common.StringToAddress("INT39iewq2jAyREvwqAZX4Wig5GVmSsc"),
    48  			Amount:  big.NewInt(1),
    49  		},
    50  		{
    51  			Address: common.StringToAddress("INT3JqvEfW7eTymfA6mfruwipcc1dAEi"),
    52  			Amount:  big.NewInt(1),
    53  		},
    54  		{
    55  			Address: common.StringToAddress("INT3D4sNnoM4NcLJeosDKUjxgwhofDdi"),
    56  			Amount:  big.NewInt(1),
    57  		},
    58  		{
    59  			Address: common.StringToAddress("INT3ETpxfNquuFa2czSHuFJTyhuepgXa"),
    60  			Amount:  big.NewInt(1),
    61  		},
    62  		{
    63  			Address: common.StringToAddress("INT3MjFkyK3bZ6oSCK8i38HVxbbsiRTY"),
    64  			Amount:  big.NewInt(1),
    65  		},
    66  	}
    67  
    68  	sort.Slice(voteArr, func(i, j int) bool {
    69  		if voteArr[i].Amount.Cmp(voteArr[j].Amount) == 0 {
    70  			return compareAddress(voteArr[i].Address[:], voteArr[j].Address[:])
    71  		}
    72  
    73  		return voteArr[i].Amount.Cmp(voteArr[j].Amount) == 1
    74  	})
    75  	for i := range voteArr {
    76  		fmt.Printf("address:%v, amount: %v\n", voteArr[i].Address, voteArr[i].Amount)
    77  	}
    78  }
    79  
    80  func TestVoteSetRemove(t *testing.T) {
    81  	var validatorsArr []*tmTypes.Validator
    82  	validatorsArr = []*tmTypes.Validator{
    83  		{
    84  			Address:     common.HexToAddress("0x2b14a6b2649a28b5fc90c42bf90f5242ea82f66a").Bytes(),
    85  			VotingPower: big.NewInt(1),
    86  		},
    87  		{
    88  			Address:     common.HexToAddress("0x6784d4990a5b042f17f149a387bac8e2f6f74064").Bytes(),
    89  			VotingPower: big.NewInt(1),
    90  		},
    91  		{
    92  			Address:     common.HexToAddress("0xeae4528f182e96ce021a8b803ce94755d65c1779").Bytes(),
    93  			VotingPower: big.NewInt(1),
    94  		},
    95  		{
    96  			Address:     common.HexToAddress("0xc906ae8ac16b80c2b591bde248283b66974756ea").Bytes(),
    97  			VotingPower: big.NewInt(1),
    98  		},
    99  		{
   100  			Address:     common.HexToAddress("0xc6540804d9994f6642d4786468c2eef0c66f69aa").Bytes(),
   101  			VotingPower: big.NewInt(1),
   102  		},
   103  		{
   104  			Address:     common.HexToAddress("0x11668e7a9ef9aa00fffaa4394c848226a588c860").Bytes(),
   105  			VotingPower: big.NewInt(1),
   106  		},
   107  		{
   108  			Address:     common.HexToAddress("0x17d28a143d48f325e5375fe54e427178e0ae5945").Bytes(),
   109  			VotingPower: big.NewInt(1),
   110  		},
   111  		{
   112  			Address:     common.HexToAddress("0xd5b9a960badecc32b67c89e04a7174a6880f7199").Bytes(),
   113  			VotingPower: big.NewInt(1),
   114  		},
   115  		{
   116  			Address:     common.HexToAddress("0x9ddc9c979f8f8d3d98601b05c4f71b504663640a").Bytes(),
   117  			VotingPower: big.NewInt(1),
   118  		},
   119  		{
   120  			Address:     common.HexToAddress("0x4fb47b878d1ebaa9e838295d305c928783c3442f").Bytes(),
   121  			VotingPower: big.NewInt(1),
   122  		},
   123  		{
   124  			Address:     common.HexToAddress("0x881f9437d5488a0162e2afacbc124e45fb24a527").Bytes(),
   125  			VotingPower: big.NewInt(1),
   126  		},
   127  		{
   128  			Address:     common.HexToAddress("0xe51116ef1cc8917bb9ef1b36c73c0fa79062746e").Bytes(),
   129  			VotingPower: big.NewInt(1),
   130  		},
   131  		{
   132  			Address:     common.HexToAddress("0x56db076b7d71d2b3a4fcabbe9df2d3a06e5611ec").Bytes(),
   133  			VotingPower: big.NewInt(1),
   134  		},
   135  	}
   136  
   137  	validatorSet := tmTypes.NewValidatorSet(validatorsArr)
   138  	fmt.Printf("validator set: %v\n", validatorSet)
   139  	//for _, val := range validatorSet.Validators {
   140  	//	addr := common.BytesToAddress(val.Address)
   141  	//	t.Logf("address: %X, %v", addr, len(validatorSet.Validators))
   142  	//	//t.Logf("len 1: %v, %v", len(validatorSet.Validators), cap(validatorSet.Validators))
   143  	//	if addr == common.HexToAddress("0x17d28a143d48f325e5375fe54e427178e0ae5945") ||
   144  	//		addr == common.HexToAddress("0x56db076b7d71d2b3a4fcabbe9df2d3a06e5611ec"){
   145  	//		validatorSet.Remove(val.Address)
   146  	//		//fmt.Printf("validator set: %v\n", validatorSet)
   147  	//	}
   148  	//	//t.Logf("len 2: %v, %v", len(validatorSet.Validators), cap(validatorSet.Validators))
   149  	//}
   150  	fmt.Printf("\n\n\n")
   151  	for i := 0; i < len(validatorSet.Validators); i++ {
   152  		fmt.Printf("validator set: %v\n", validatorSet)
   153  		addr := common.BytesToAddress(validatorSet.Validators[i].Address)
   154  		t.Logf("address: %X, %v", addr, len(validatorSet.Validators))
   155  		//t.Logf("len 1: %v, %v", len(validatorSet.Validators), cap(validatorSet.Validators))
   156  		if addr == common.HexToAddress("0x17d28a143d48f325e5375fe54e427178e0ae5945") ||
   157  			addr == common.HexToAddress("0x56db076b7d71d2b3a4fcabbe9df2d3a06e5611ec") {
   158  			validatorSet.Remove(validatorSet.Validators[i].Address)
   159  			//fmt.Printf("validator set: %v\n", validatorSet)
   160  			i--
   161  		}
   162  
   163  		fmt.Print(fmt.Errorf("error"))
   164  	}
   165  }