github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/vm/election/electiondb_test.go (about)

     1  // Copyright 2019 The go-vnt Authors
     2  // This file is part of the go-vnt library.
     3  //
     4  // The go-vnt library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-vnt library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package election
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/vntchain/go-vnt/common"
    26  	"github.com/vntchain/go-vnt/core/state"
    27  	"github.com/vntchain/go-vnt/vntdb"
    28  )
    29  
    30  var (
    31  	voter = Voter{
    32  		Owner: common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"),
    33  		VoteCandidates: []common.Address{
    34  			common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"),
    35  			common.BytesToAddress([]byte{10}),
    36  		},
    37  		ProxyVoteCount: big.NewInt(102),
    38  		LastVoteCount:  big.NewInt(5),
    39  		IsProxy:        true,
    40  		TimeStamp:      big.NewInt(1531454152),
    41  	}
    42  	candidate = Candidate{
    43  		Owner:       common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"),
    44  		Binder:      binder,
    45  		Beneficiary: beneficiary,
    46  		Registered:  true,
    47  		Bind:        true,
    48  		VoteCount:   big.NewInt(0),
    49  		Url:         []byte("/ip4/192.168.9.102/tcp/5210/ipfs/1kHaMUmZgTpjGEhxcGATr1UVWy6iKkygFuknWEtW7LiLrev"),
    50  		Website:     []byte("www.testwebsite.net/test/witness/website"),
    51  		Name:        []byte("testNet"),
    52  	}
    53  	stake = Stake{
    54  		Owner:      common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"),
    55  		StakeCount: big.NewInt(230),
    56  		TimeStamp:  big.NewInt(1531454152),
    57  	}
    58  	allLock = AllLock{
    59  		Amount: big.NewInt(1e18),
    60  	}
    61  
    62  	bounty = Reward{
    63  		Rest: big.NewInt(1e18),
    64  	}
    65  )
    66  
    67  func print(key common.Hash, value common.Hash) {
    68  	fmt.Printf("%x:%x\n", key, value)
    69  }
    70  
    71  func sameVoter(voter *Voter, voter1 *Voter) (bool, error) {
    72  	if !bytes.Equal(voter.Owner[:], voter1.Owner[:]) {
    73  		return false, fmt.Errorf("Error, owner before %v and after %v is different", voter.Owner, voter1.Owner)
    74  	} else if voter.TimeStamp.Cmp(voter1.TimeStamp) != 0 {
    75  		return false, fmt.Errorf("Error, timestamp before %v and after %v is different", voter.TimeStamp, voter1.TimeStamp)
    76  	} else if voter.Proxy != voter1.Proxy {
    77  		return false, fmt.Errorf("Error, proxy before %v and after %v is different", voter.Proxy, voter1.Proxy)
    78  	} else if voter.IsProxy != voter1.IsProxy {
    79  		return false, fmt.Errorf("Error, isproxy before %v and after %v is different", voter.IsProxy, voter1.IsProxy)
    80  	} else if voter.ProxyVoteCount.Cmp(voter1.ProxyVoteCount) != 0 {
    81  		return false, fmt.Errorf("Error, ProxyVoteCount before %v and after %v is different", voter.ProxyVoteCount, voter1.ProxyVoteCount)
    82  	} else if voter.LastVoteCount.Cmp(voter1.LastVoteCount) != 0 {
    83  		return false, fmt.Errorf("Error, LastVoteCount before %v and after %v is different", voter.LastVoteCount, voter1.LastVoteCount)
    84  	} else {
    85  		if len(voter.VoteCandidates) != len(voter1.VoteCandidates) {
    86  			return false, fmt.Errorf("Error, the length of VoteCandidates before %v and after %v is different", len(voter.VoteCandidates), len(voter1.VoteCandidates))
    87  		} else {
    88  			for i, candi := range voter.VoteCandidates {
    89  				if !bytes.Equal(candi.Bytes(), voter1.VoteCandidates[i].Bytes()) {
    90  					return false, fmt.Errorf("Error,  VoteCandidates[%d] before %v and after %v is different", i, candi, voter1.VoteCandidates[i])
    91  				}
    92  			}
    93  		}
    94  	}
    95  	return true, nil
    96  }
    97  
    98  func sameCandidate(candidate *Candidate, candidate1 *Candidate) (bool, error) {
    99  	if !bytes.Equal(candidate.Owner[:], candidate1.Owner[:]) {
   100  		return false, fmt.Errorf("Error,owner before %v and after %v is different", candidate.Owner, candidate1.Owner)
   101  	} else if candidate.Registered != candidate1.Registered {
   102  		return false, fmt.Errorf("Error,registered before %v and after %v is different", candidate.Registered, candidate1.Registered)
   103  	} else if candidate.Binder != candidate1.Binder {
   104  		return false, fmt.Errorf("Error,binder before %v and after %v is different", candidate.Binder, candidate1.Binder)
   105  	} else if candidate.Bind != candidate1.Bind {
   106  		return false, fmt.Errorf("Error,bind before %v and after %v is different", candidate.Bind, candidate1.Bind)
   107  	} else if candidate.Beneficiary != candidate1.Beneficiary {
   108  		return false, fmt.Errorf("Error,beneficiary before %v and after %v is different", candidate.Beneficiary, candidate1.Beneficiary)
   109  	} else if candidate.VoteCount.Cmp(candidate1.VoteCount) != 0 {
   110  		return false, fmt.Errorf("Error,voteCount before %v and after %v is different", candidate.VoteCount, candidate1.VoteCount)
   111  	} else if !bytes.Equal(candidate.Url, candidate1.Url) {
   112  		return false, fmt.Errorf("Error, url before %x and after %x is different", candidate.Url, candidate1.Url)
   113  	} else if !bytes.Equal(candidate.Website, candidate1.Website) {
   114  		return false, fmt.Errorf("Error, Website before %v and after %v is different", candidate.Website, candidate1.Website)
   115  	} else if !bytes.Equal(candidate.Name, candidate1.Name) {
   116  		return false, fmt.Errorf("Error, Name before %v and after %v is different", candidate.Name, candidate1.Name)
   117  	}
   118  	return true, nil
   119  }
   120  
   121  func sameStake(stake *Stake, stake1 *Stake) (bool, error) {
   122  	if !bytes.Equal(stake.Owner[:], stake1.Owner[:]) {
   123  		return false, fmt.Errorf("Error, owner before %v and after %v is different", stake.Owner, stake1.Owner)
   124  	} else if stake.StakeCount.Cmp(stake1.StakeCount) != 0 {
   125  		return false, fmt.Errorf("Error, stakeCount before %v and after %v is different", stake.StakeCount, stake1.StakeCount)
   126  	} else if stake.TimeStamp.Cmp(stake1.TimeStamp) != 0 {
   127  		return false, fmt.Errorf("Error, timestamp before %v and after %v is different", stake.TimeStamp, stake1.TimeStamp)
   128  	}
   129  	return true, nil
   130  }
   131  
   132  func TestConvertToKV(t *testing.T) {
   133  	err := convertToKV(VOTERPREFIX, voter, print)
   134  	if err != nil {
   135  		t.Error(err)
   136  	}
   137  
   138  	err = convertToKV(CANDIDATEPREFIX, candidate, print)
   139  	if err != nil {
   140  		t.Error(err)
   141  	}
   142  
   143  	err = convertToKV(STAKEPREFIX, stake, print)
   144  	if err != nil {
   145  		t.Error(err)
   146  	}
   147  
   148  	err = convertToKV(REWARDPREFIX, bounty, print)
   149  	if err != nil {
   150  		t.Error(err)
   151  	}
   152  
   153  	err = convertToKV(ALLLOCKPREFIX, allLock, print)
   154  	if err != nil {
   155  		t.Error(err)
   156  	}
   157  }
   158  
   159  // 把上面单元测试打印的结果添加到下面,如果pass说明解析对了
   160  func TestConvertToStruct(t *testing.T) {
   161  	kvMap := make(map[common.Hash]common.Hash)
   162  	// voter
   163  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000000")] = common.HexToHash("0000000000000000000000949ee97d274eb4c215f23238fee1f103d9ea10a234") // Owner
   164  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000001")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001") // IsProxy
   165  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000002")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000066") // ProxyVoteCount
   166  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000003")] = common.HexToHash("0000000000000000000000940000000000000000000000000000000000000000") // Proxy
   167  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000004")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000005") // LastStakeCount
   168  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000005")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000005") // LastVoteCount
   169  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000006")] = common.HexToHash("000000000000000000000000000000000000000000000000000000845b4822c8") // TimeStamp
   170  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000100000007")] = common.HexToHash("0000000000000000000000949ee97d274eb4c215f23238fee1f103d9ea10a234") // VoteCandidates
   171  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000200000007")] = common.HexToHash("000000000000000000000094000000000000000000000000000000000000000a") // VoteCandidates
   172  	kvMap[common.HexToHash("000000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000007")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000002") // VoteCandidates
   173  	// candidate
   174  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000000")] = common.HexToHash("0000000000000000000000949ee97d274eb4c215f23238fee1f103d9ea10a234") // owner
   175  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000001")] = common.HexToHash("0000000000000000000000940000000000000000000000923839919383938289") // Binder
   176  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000002")] = common.HexToHash("0000000000000000000000940000000000000000000000923839919383938281") // Beneficiary
   177  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000003")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000080") // vote count
   178  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000004")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001") // register
   179  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000005")] = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001") // bind
   180  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000006")] = common.HexToHash("0000000000000000000000000000b8502f6970342f3139322e3136382e392e31") // url
   181  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000100000006")] = common.HexToHash("30322f7463702f353231302f697066732f316b48614d556d5a6754706a474568") // url
   182  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000200000006")] = common.HexToHash("786347415472315556577936694b6b796746756b6e57457457374c694c726576") // url
   183  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000100000007")] = common.HexToHash("776562736974652e6e65742f746573742f7769746e6573732f77656273697465") // Website
   184  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000007")] = common.HexToHash("0000000000000000000000000000000000000000000000a87777772e74657374") // Website
   185  	kvMap[common.HexToHash("010000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000008")] = common.HexToHash("00000000000000000000000000000000000000000000000087746573744e6574") // name
   186  	// stake
   187  	kvMap[common.HexToHash("020000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000000")] = common.HexToHash("0000000000000000000000949ee97d274eb4c215f23238fee1f103d9ea10a234") // Owner
   188  	kvMap[common.HexToHash("020000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000001")] = common.HexToHash("00000000000000000000000000000000000000000000000000000000000081e6") // StakeCount
   189  	kvMap[common.HexToHash("020000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000002")] = common.HexToHash("00000000000000000000000000000000000000000000000000000000000081e6") // Vnt
   190  	kvMap[common.HexToHash("020000009ee97d274eb4c215f23238fee1f103d9ea10a2340000000000000003")] = common.HexToHash("000000000000000000000000000000000000000000000000000000845b4822c8") // TimeStamp
   191  	// bounty
   192  	kvMap[common.HexToHash("0300000000000000000000000000000000000000000000090000000000000000")] = common.HexToHash("0000000000000000000000000000000000000000000000880de0b6b3a7640000") // RestTotalBounty
   193  	// Alllock
   194  	kvMap[common.HexToHash("0400000000000000000000000000000000000000000000090000000000000000")] = common.HexToHash("0000000000000000000000000000000000000000000000880de0b6b3a7640000") // RestTotalBounty
   195  
   196  	getFn := func(hash common.Hash) common.Hash {
   197  		return kvMap[hash]
   198  	}
   199  
   200  	voter1 := Voter{}
   201  	err := convertToStruct(VOTERPREFIX, common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"), &voter1, getFn)
   202  	if err != nil {
   203  		t.Errorf(err.Error())
   204  	}
   205  	if same, err := sameVoter(&voter, &voter1); !same {
   206  		t.Errorf(err.Error())
   207  	}
   208  
   209  	var candidate1 Candidate
   210  	err = convertToStruct(CANDIDATEPREFIX, common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"), &candidate1, getFn)
   211  	if err != nil {
   212  		t.Error(err)
   213  	}
   214  	if same, err := sameCandidate(&candidate, &candidate1); !same {
   215  		t.Errorf(err.Error())
   216  	}
   217  
   218  	var stake1 Stake
   219  	err = convertToStruct(STAKEPREFIX, common.HexToAddress("9ee97d274eb4c215f23238fee1f103d9ea10a234"), &stake1, getFn)
   220  	if err != nil {
   221  		t.Error(err)
   222  	}
   223  	if same, err := sameStake(&stake, &stake1); !same {
   224  		t.Errorf(err.Error())
   225  	}
   226  
   227  	var bounty1 Reward
   228  	err = convertToStruct(REWARDPREFIX, contractAddr, &bounty1, getFn)
   229  	if err != nil {
   230  		t.Error(err)
   231  	}
   232  	if bounty1.Rest.Cmp(bounty.Rest) != 0 {
   233  		t.Errorf("Error: the reset total Reward before is %v after is %v", bounty.Rest, bounty1.Rest)
   234  	}
   235  
   236  	var alllock1 AllLock
   237  	err = convertToStruct(ALLLOCKPREFIX, contractAddr, &alllock1, getFn)
   238  	if err != nil {
   239  		t.Error(err)
   240  	}
   241  	if alllock1.Amount.Cmp(allLock.Amount) != 0 {
   242  		t.Errorf("Error: the reset total Reward before is %v after is %v", allLock.Amount, alllock1.Amount)
   243  	}
   244  }
   245  
   246  func TestSetToDB(t *testing.T) {
   247  	db := vntdb.NewMemDatabase()
   248  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   249  
   250  	ctx := testContext{StateDB: stateDB}
   251  	c := newElectionContext(&ctx)
   252  
   253  	err := c.setVoter(voter)
   254  	if err != nil {
   255  		t.Error(err)
   256  	}
   257  
   258  	err = c.setCandidate(candidate)
   259  	if err != nil {
   260  		t.Error(err)
   261  	}
   262  
   263  	err = c.setStake(stake)
   264  	if err != nil {
   265  		t.Error(err)
   266  	}
   267  
   268  	err = setReward(stateDB, bounty)
   269  	if err != nil {
   270  		t.Error(err)
   271  	}
   272  
   273  	err = setLock(stateDB, allLock)
   274  	if err != nil {
   275  		t.Error(err)
   276  	}
   277  
   278  }
   279  
   280  func TestGetFromDB(t *testing.T) {
   281  	db := vntdb.NewMemDatabase()
   282  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   283  
   284  	ctx := testContext{StateDB: stateDB}
   285  	c := newElectionContext(&ctx)
   286  
   287  	err1 := c.setVoter(voter)
   288  	err2 := c.setCandidate(candidate)
   289  	err3 := c.setStake(stake)
   290  	err4 := setReward(stateDB, bounty)
   291  	err5 := setLock(stateDB, allLock)
   292  
   293  	if err1 != nil || err2 != nil || err3 != nil || err4 != nil || err5 != nil {
   294  		t.Fatal("SetToDB err", err1, err2, err3, err4)
   295  	}
   296  
   297  	voter1 := c.getVoter(voter.Owner)
   298  
   299  	if same, err := sameVoter(&voter, &voter1); !same {
   300  		t.Errorf(err.Error())
   301  	}
   302  
   303  	candidate1 := c.getCandidate(candidate.Owner)
   304  	if same, err := sameCandidate(&candidate, &candidate1); !same {
   305  		t.Errorf(err.Error())
   306  	}
   307  
   308  	stake1 := c.getStake(stake.Owner)
   309  	if same, err := sameStake(&stake, &stake1); !same {
   310  		t.Errorf(err.Error())
   311  	}
   312  
   313  	bounty1 := getReward(stateDB)
   314  	if bounty.Rest.Cmp(bounty1.Rest) != 0 {
   315  		t.Errorf("Error: the reset total Reward before is %v after is %v", bounty.Rest, bounty1.Rest)
   316  	}
   317  
   318  	allLock1, _:= getLock(stateDB)
   319  	if allLock1.Amount.Cmp(allLock.Amount) != 0 {
   320  		t.Errorf("Error: the reset total Reward before is %v after is %v", allLock.Amount, allLock1.Amount)
   321  	}
   322  }
   323  
   324  func TestGetAllCandidate(t *testing.T) {
   325  	db := vntdb.NewMemDatabase()
   326  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   327  
   328  	ctx := testContext{StateDB: stateDB}
   329  	c := newElectionContext(&ctx)
   330  
   331  	for i := 0; i < 255; i++ {
   332  		candidate1 := candidate
   333  		candidate1.Owner[0] = byte(i)
   334  		if err := c.setCandidate(candidate1); err != nil {
   335  			t.Errorf("candiates: %s, error: %s", candidate1.Owner, err)
   336  		}
   337  	}
   338  
   339  	candidates := getAllCandidate(stateDB)
   340  	if len(candidates) != 255 {
   341  		t.Error("the number of candidates is wrong!")
   342  	} else {
   343  		for _, candidate := range candidates {
   344  			if !candidate.Active() || candidate.VoteCount.Cmp(big.NewInt(0)) != 0 {
   345  				t.Fatalf("Error: %s", candidate.String())
   346  			}
   347  		}
   348  	}
   349  }
   350  
   351  func TestGetFirstXCandidates_1(t *testing.T) {
   352  	db := vntdb.NewMemDatabase()
   353  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   354  
   355  	ctx := testContext{StateDB: stateDB}
   356  	c := newElectionContext(&ctx)
   357  
   358  	type addrPair struct {
   359  		addrPre byte
   360  		votes   int64
   361  	}
   362  
   363  	witNum := 4
   364  	tests := []addrPair{
   365  		{byte(1), 200},
   366  		{byte(2), 100},
   367  		{byte(3), 50},
   368  		{byte(4), 10},
   369  		{byte(5), 100},
   370  		{byte(6), 5},
   371  	}
   372  	rets := []addrPair{
   373  		{byte(1), 200},
   374  		{byte(2), 100},
   375  		{byte(5), 100},
   376  		{byte(3), 50},
   377  		// {byte(5), 10},
   378  		// {byte(6), 5},
   379  	}
   380  
   381  	// set to db
   382  	for i := 0; i < len(tests); i++ {
   383  		candidate1 := candidate
   384  		candidate1.Owner[0] = byte(tests[i].addrPre)
   385  		candidate1.VoteCount = big.NewInt(tests[i].votes)
   386  		if err := c.setCandidate(candidate1); err != nil {
   387  			t.Errorf("candiates: %s, error: %s", candidate1.Owner, err)
   388  		}
   389  	}
   390  
   391  	witsAddr, _ := GetFirstNCandidates(stateDB, witNum)
   392  	if len(witsAddr) != len(rets) {
   393  		t.Errorf("lenght not match, want:%d, got:%d", witNum, len(witsAddr))
   394  	}
   395  	baseAddr := candidate.Owner
   396  	for i := 0; i < witNum; i++ {
   397  		can := baseAddr
   398  		can[0] = byte(rets[i].addrPre)
   399  		ret := bytes.Compare(can.Bytes(), witsAddr[i].Bytes())
   400  		if ret != 0 {
   401  			t.Errorf("candidates nots match at index:%d, ret:%d, want: %x, got:%x", i, ret, can, witsAddr[i])
   402  		}
   403  	}
   404  
   405  	// candidates := getAllCandidate(stateDB)
   406  	// for _, candi := range candidates {
   407  	// 	fmt.Printf("candidate owner: %x, active: %v, voteCount : %v\n", candi.Owner, candi.Active, candi.VoteCount)
   408  	// }
   409  
   410  }
   411  
   412  func TestGetFirstXCandidates_2(t *testing.T) {
   413  	db := vntdb.NewMemDatabase()
   414  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   415  
   416  	ctx := testContext{StateDB: stateDB}
   417  	c := newElectionContext(&ctx)
   418  
   419  	type addrPair struct {
   420  		addrPre byte
   421  		votes   int64
   422  	}
   423  
   424  	witNum := 4
   425  	tests := []addrPair{
   426  		{byte(1), 200},
   427  		{byte(2), 100},
   428  		{byte(3), 50},
   429  		{byte(4), 100},
   430  		{byte(5), 10},
   431  		{byte(6), 5},
   432  	}
   433  	rets := []addrPair{
   434  		{byte(1), 200},
   435  		{byte(2), 100},
   436  		{byte(4), 100},
   437  		{byte(3), 50},
   438  		// {byte(5), 10},
   439  		// {byte(6), 5},
   440  	}
   441  
   442  	// set to db
   443  	for i := 0; i < len(tests); i++ {
   444  		candidate1 := candidate
   445  		candidate1.Owner[0] = byte(tests[i].addrPre)
   446  		candidate1.VoteCount = big.NewInt(tests[i].votes)
   447  		if err := c.setCandidate(candidate1); err != nil {
   448  			t.Errorf("candiates: %s, error: %s", candidate1.Owner, err)
   449  		}
   450  	}
   451  
   452  	witsAddr, _ := GetFirstNCandidates(stateDB, witNum)
   453  	if len(witsAddr) != len(rets) {
   454  		t.Errorf("lenght not match, want:%d, got:%d", witNum, len(witsAddr))
   455  	}
   456  	baseAddr := candidate.Owner
   457  	for i := 0; i < witNum; i++ {
   458  		can := baseAddr
   459  		can[0] = byte(rets[i].addrPre)
   460  		ret := bytes.Compare(can.Bytes(), witsAddr[i].Bytes())
   461  		if ret != 0 {
   462  			t.Errorf("candidates nots match at index:%d, ret:%d, want: %x, got:%x", i, ret, can, witsAddr[i])
   463  		}
   464  	}
   465  
   466  	candidates := getAllCandidate(stateDB)
   467  	for _, candi := range candidates {
   468  		t.Logf("candidate owner: %x, active: %v, voteCount : %v\n", candi.Owner, candi.Active(), candi.VoteCount)
   469  	}
   470  }
   471  
   472  // 存在不active的节点
   473  func TestGetFirstXCandidates_3(t *testing.T) {
   474  	db := vntdb.NewMemDatabase()
   475  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   476  
   477  	ctx := testContext{StateDB: stateDB}
   478  	c := newElectionContext(&ctx)
   479  
   480  	type addrPair struct {
   481  		addrPre byte
   482  		votes   int64
   483  		bind    bool // register始终是true,bind是true则active为true
   484  	}
   485  
   486  	witNum := 4
   487  	tests := []addrPair{
   488  		{byte(1), 200, true},
   489  		{byte(2), 100, false},
   490  		{byte(3), 50, true},
   491  		{byte(4), 100, true},
   492  		{byte(5), 10, true},
   493  		{byte(6), 5, true},
   494  	}
   495  	rets := []addrPair{
   496  		{byte(1), 200, true},
   497  		{byte(4), 100, true},
   498  		{byte(3), 50, true},
   499  		{byte(5), 10, true},
   500  	}
   501  
   502  	// set to db
   503  	for i := 0; i < len(tests); i++ {
   504  		candidate1 := candidate
   505  		candidate1.Owner[0] = byte(tests[i].addrPre)
   506  		candidate1.VoteCount = big.NewInt(tests[i].votes)
   507  		candidate1.Registered = true
   508  		candidate1.Bind = tests[i].bind
   509  		if err := c.setCandidate(candidate1); err != nil {
   510  			t.Errorf("candiates: %s, error: %s", candidate1.Owner, err)
   511  		}
   512  	}
   513  
   514  	witsAddr, _ := GetFirstNCandidates(stateDB, witNum)
   515  	if len(witsAddr) != len(rets) {
   516  		t.Errorf("lenght not match, want:%d, got:%d", witNum, len(witsAddr))
   517  	}
   518  	baseAddr := candidate.Owner
   519  	for i := 0; i < witNum; i++ {
   520  		can := baseAddr
   521  		can[0] = byte(rets[i].addrPre)
   522  		ret := bytes.Compare(can.Bytes(), witsAddr[i].Bytes())
   523  		if ret != 0 {
   524  			t.Errorf("candidates nots match at index:%d, ret:%d, want: %x, got:%x", i, ret, can, witsAddr[i])
   525  		}
   526  	}
   527  
   528  	candidates := getAllCandidate(stateDB)
   529  	for _, candi := range candidates {
   530  		t.Logf("candidate owner: %x, active: %v, voteCount : %v\n", candi.Owner, candi.Active(), candi.VoteCount)
   531  	}
   532  }
   533  
   534  // 使用registerWitness注册见证人,每个人应当0票,但按地址排序
   535  func TestGetFirstXCandidates_4(t *testing.T) {
   536  	db := vntdb.NewMemDatabase()
   537  	stateDB, _ := state.New(common.Hash{}, state.NewDatabase(db))
   538  
   539  	ctx := testContext{StateDB: stateDB}
   540  	c := newElectionContext(&ctx)
   541  
   542  	type addrPair struct {
   543  		addrPre byte
   544  		votes   int64
   545  	}
   546  
   547  	// 忽略票数
   548  	witNum := 4
   549  	tests := []addrPair{
   550  		{byte(1), 0},
   551  		{byte(2), 0},
   552  		{byte(3), 0},
   553  		{byte(4), 0},
   554  		{byte(5), 0},
   555  		{byte(6), 0},
   556  	}
   557  	rets := []addrPair{
   558  		{byte(1), 0},
   559  		{byte(2), 0},
   560  		{byte(3), 0},
   561  		{byte(4), 0},
   562  	}
   563  
   564  	// 设置到数据库
   565  	baseAddr := candidate.Owner
   566  	for i := 0; i < len(tests); i++ {
   567  		candidate1 := candidate
   568  		candidate1.Owner[0] = byte(tests[i].addrPre)
   569  		candidate1.VoteCount = big.NewInt(tests[i].votes)
   570  		candidate1.Registered = true
   571  		candidate1.Bind = true
   572  		if err := c.setCandidate(candidate1); err != nil {
   573  			t.Errorf("candiates: %s, error: %s", candidate1.Owner, err)
   574  		}
   575  	}
   576  
   577  	witsAddr, _ := GetFirstNCandidates(stateDB, witNum)
   578  	if len(witsAddr) != len(rets) {
   579  		t.Errorf("lenght not match, want:%d, got:%d", witNum, len(witsAddr))
   580  		t.FailNow()
   581  	}
   582  	for i := 0; i < witNum; i++ {
   583  		can := baseAddr
   584  		can[0] = byte(rets[i].addrPre)
   585  		ret := bytes.Compare(can.Bytes(), witsAddr[i].Bytes())
   586  		if ret != 0 {
   587  			t.Errorf("candidates nots match at index:%d, ret:%d, want: %x, got:%x", i, ret, can, witsAddr[i])
   588  		}
   589  	}
   590  }