Skip to content

Commit 94122ef

Browse files
authored
sort merged result (#1526)
* sort merged result * rebase
1 parent a51bd4a commit 94122ef

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

action/protocol/poll/nativestaking.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ func NewNativeStaking(cm protocol.ChainManager, getTipBlockTime GetTipBlockTime,
7878
}
7979

8080
// Votes returns the votes on height
81-
func (ns *NativeStaking) Votes() (*VoteTally, error) {
81+
func (ns *NativeStaking) Votes() (*VoteTally, time.Time, error) {
8282
if ns.contract == "" {
83-
return nil, ErrNoData
83+
return nil, time.Time{}, ErrNoData
8484
}
8585

8686
now, err := ns.getTipBlockTime()
8787
if err != nil {
88-
return nil, errors.Wrap(err, "failed to get current block time")
88+
return nil, time.Time{}, errors.Wrap(err, "failed to get current block time")
8989
}
9090
// read voter list from staking contract
9191
votes := VoteTally{
@@ -103,7 +103,7 @@ func (ns *NativeStaking) Votes() (*VoteTally, error) {
103103
break
104104
}
105105
if err != nil {
106-
return nil, err
106+
return nil, now, err
107107
}
108108
votes.tally(vote, now)
109109
if len(vote) < int(limit.Int64()) {
@@ -112,7 +112,7 @@ func (ns *NativeStaking) Votes() (*VoteTally, error) {
112112
}
113113
prevIndex.Add(prevIndex, limit)
114114
}
115-
return &votes, nil
115+
return &votes, now, nil
116116
}
117117

118118
func (ns *NativeStaking) readBuckets(prevIndx, limit *big.Int) ([]*types.Bucket, error) {

action/protocol/poll/staking_committee.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ package poll
88

99
import (
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

121124
func (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

chainservice/chainservice.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func New(
104104
committeeConfig.SelfStakingThreshold = cfg.Genesis.SelfStakingThreshold
105105

106106
if committeeConfig.GravityChainStartHeight != 0 {
107-
archive, err := committee.NewArchive(
107+
arch, err := committee.NewArchive(
108108
cfg.Chain.GravityChainDB.DbPath,
109109
cfg.Chain.GravityChainDB.NumRetries,
110110
committeeConfig.GravityChainStartHeight,
@@ -113,10 +113,7 @@ func New(
113113
if err != nil {
114114
return nil, err
115115
}
116-
if electionCommittee, err = committee.NewCommittee(
117-
archive,
118-
committeeConfig,
119-
); err != nil {
116+
if electionCommittee, err = committee.NewCommittee(arch, committeeConfig); err != nil {
120117
return nil, err
121118
}
122119
}

0 commit comments

Comments
 (0)