-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpos.go
51 lines (39 loc) · 927 Bytes
/
dpos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package blockchain
import (
"errors"
"sort"
)
var nodeVoteMap map[string]int
func InitVoteMap() {
nodeVoteMap = make(map[string]int)
}
func NodeVoteMap() map[string]int {
return nodeVoteMap
}
func Vote(id string, ticket int) {
nodeVoteMap[id] = ticket
}
// PickWinner 根据投票数量选择生成区块的节点
func PickWinnerWithDpos() (bp string, err error) {
if len(nodeVoteMap) < 1 {
err = errors.New("Error: failed to pick winner, node vote map is empty")
return
}
// 选择BlockProducer
ticketList := make([]int, len(nodeVoteMap))
for _, ticket := range nodeVoteMap {
ticketList = append(ticketList, ticket)
}
sort.Slice(ticketList, func(i, j int) bool {
return ticketList[i] > ticketList[j]
})
// 前一半作为producer
ticketList = ticketList[0 : len(ticketList)/2]
for k, v := range nodeVoteMap {
if v > ticketList[len(ticketList)-1] {
bp = k
err = nil
}
}
return
}