@@ -8,7 +8,9 @@ package poll
88
99import (
1010 "context"
11+ "encoding/hex"
1112 "math/big"
13+ "time"
1214
1315 "github.com/pkg/errors"
1416 "go.uber.org/zap"
@@ -21,6 +23,7 @@ import (
2123 "github.com/iotexproject/iotex-core/state"
2224 "github.com/iotexproject/iotex-election/committee"
2325 "github.com/iotexproject/iotex-election/types"
26+ "github.com/iotexproject/iotex-election/util"
2427 "github.com/iotexproject/iotex-proto/golang/iotextypes"
2528)
2629
@@ -106,7 +109,7 @@ func (sc *stakingCommittee) DelegatesByHeight(height uint64) (state.CandidateLis
106109 if sc .nativeStaking == nil {
107110 return nil , errors .New ("native staking was not set after cook height" )
108111 }
109- nativeVotes , err := sc .nativeStaking .Votes ()
112+ nativeVotes , ts , err := sc .nativeStaking .Votes ()
110113 if err == ErrNoData {
111114 // no native staking data
112115 return cand , nil
@@ -115,28 +118,35 @@ func (sc *stakingCommittee) DelegatesByHeight(height uint64) (state.CandidateLis
115118 return nil , errors .Wrap (err , "failed to get native chain candidates" )
116119 }
117120 sc .currentNativeBuckets = nativeVotes .Buckets
118- return sc .mergeDelegates (cand , nativeVotes ), nil
121+ return sc .mergeDelegates (cand , nativeVotes , ts ), nil
119122}
120123
121124func (sc * stakingCommittee ) ReadState (ctx context.Context , sm protocol.StateManager , method []byte , args ... []byte ) ([]byte , error ) {
122125 return sc .governanceStaking .ReadState (ctx , sm , method , args ... )
123126}
124127
125- func (sc * stakingCommittee ) mergeDelegates (list state.CandidateList , votes * VoteTally ) state.CandidateList {
128+ func (sc * stakingCommittee ) mergeDelegates (list state.CandidateList , votes * VoteTally , ts time. Time ) state.CandidateList {
126129 // as of now, native staking does not have register contract, only voting/staking contract
127130 // it is assumed that all votes done on native staking target for delegates registered on Ethereum
128131 // votes cast to all outside address will not be counted and simply ignored
129- var merged state.CandidateList
132+ candidates := make (map [string ]* state.Candidate )
133+ candidateScores := make (map [string ]* big.Int )
130134 for _ , cand := range list {
131135 clone := cand .Clone ()
132136 name := to12Bytes (clone .CanName )
133137 if v , ok := votes .Candidates [name ]; ok {
134138 clone .Votes .Add (clone .Votes , v .Votes )
135139 }
136140 if clone .Votes .Cmp (sc .scoreThreshold ) >= 0 {
137- merged = append (merged , clone )
141+ candidates [hex .EncodeToString (name [:])] = clone
142+ candidateScores [hex .EncodeToString (name [:])] = clone .Votes
138143 }
139144 }
145+ sorted := util .Sort (candidateScores , uint64 (ts .Unix ()))
146+ var merged state.CandidateList
147+ for _ , name := range sorted {
148+ merged = append (merged , candidates [name ])
149+ }
140150 return merged
141151}
142152
0 commit comments