OSDN Git Service

fix same vote num
authorshenao78 <shenao.78@163.com>
Tue, 4 Jun 2019 02:33:28 +0000 (10:33 +0800)
committershenao78 <shenao.78@163.com>
Tue, 4 Jun 2019 02:33:28 +0000 (10:33 +0800)
protocol/consensus_node_manager.go

index 282923c..81dd192 100644 (file)
@@ -26,8 +26,14 @@ type consensusNode struct {
 
 type consensusNodeSlice []*consensusNode
 
+func (c consensusNodeSlice) Less(i, j int) bool {
+       if c[i].voteNum == c[j].voteNum {
+               return c[i].pubkey < c[j].pubkey
+       }
+       return c[i].voteNum > c[j].voteNum 
+}
+
 func (c consensusNodeSlice) Len() int           { return len(c) }
-func (c consensusNodeSlice) Less(i, j int) bool { return c[i].voteNum > c[j].voteNum }
 func (c consensusNodeSlice) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
 
 type consensusNodeManager struct {
@@ -148,7 +154,6 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
        }
        // In principle, there is no need to sort all voting nodes.
        // if there is a performance problem, consider the optimization later.
-       // TODO not consider the same number of votes
        sort.Sort(consensusNodeSlice(nodes))
 
        result := make(map[string]*consensusNode)