github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/poll/candidateindexer_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package poll 7 8 import ( 9 "context" 10 "math/big" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/action/protocol/vote" 16 "github.com/iotexproject/iotex-core/db" 17 "github.com/iotexproject/iotex-core/state" 18 "github.com/iotexproject/iotex-core/test/identityset" 19 ) 20 21 func TestCandidateIndexer(t *testing.T) { 22 require := require.New(t) 23 indexer, err := NewCandidateIndexer(db.NewMemKVStore()) 24 require.NoError(err) 25 require.NoError(indexer.Start(context.Background())) 26 // PutCandidates and Candidates with height 1 27 candidates := state.CandidateList{ 28 { 29 Address: identityset.Address(1).String(), 30 Votes: big.NewInt(30), 31 RewardAddress: "rewardAddress1", 32 }, 33 { 34 Address: identityset.Address(2).String(), 35 Votes: big.NewInt(22), 36 RewardAddress: "rewardAddress2", 37 }, 38 { 39 Address: identityset.Address(3).String(), 40 Votes: big.NewInt(20), 41 RewardAddress: "rewardAddress3", 42 }, 43 { 44 Address: identityset.Address(4).String(), 45 Votes: big.NewInt(10), 46 RewardAddress: "rewardAddress4", 47 }, 48 } 49 require.NoError(indexer.PutCandidateList(uint64(1), &candidates)) 50 candidatesFromDB, err := indexer.CandidateList(uint64(1)) 51 require.NoError(err) 52 require.Equal(len(candidatesFromDB), len(candidates)) 53 for i, cand := range candidates { 54 require.True(cand.Equal(candidatesFromDB[i])) 55 } 56 57 // try to put again 58 require.NoError(indexer.PutCandidateList(uint64(1), &candidates)) 59 candidatesFromDB, err = indexer.CandidateList(uint64(1)) 60 require.NoError(err) 61 require.Equal(len(candidatesFromDB), len(candidates)) 62 for i, cand := range candidates { 63 require.True(cand.Equal(candidatesFromDB[i])) 64 } 65 66 // PutCandidates and Candidates with height 2 67 candidates2 := state.CandidateList{ 68 { 69 Address: identityset.Address(1).String(), 70 Votes: big.NewInt(30), 71 RewardAddress: "rewardAddress1", 72 }, 73 { 74 Address: identityset.Address(2).String(), 75 Votes: big.NewInt(22), 76 RewardAddress: "rewardAddress2", 77 }, 78 } 79 require.NoError(indexer.PutCandidateList(uint64(2), &candidates2)) 80 candidatesFromDB, err = indexer.CandidateList(uint64(2)) 81 require.NoError(err) 82 require.Equal(len(candidatesFromDB), len(candidates2)) 83 for i, cand := range candidates2 { 84 require.True(cand.Equal(candidatesFromDB[i])) 85 } 86 87 // PutProbationList and ProbationList with height 1 88 probationListMap := map[string]uint32{ 89 identityset.Address(1).String(): 1, 90 identityset.Address(2).String(): 1, 91 } 92 93 probationList := &vote.ProbationList{ 94 ProbationInfo: probationListMap, 95 IntensityRate: 50, 96 } 97 require.NoError(indexer.PutProbationList(uint64(1), probationList)) 98 probationList2, err := indexer.ProbationList(uint64(1)) 99 require.NoError(err) 100 101 require.Equal(probationList.IntensityRate, probationList2.IntensityRate) 102 require.Equal(len(probationList.ProbationInfo), len(probationList2.ProbationInfo)) 103 for str, count := range probationList.ProbationInfo { 104 require.Equal(probationList2.ProbationInfo[str], count) 105 } 106 }