-
Notifications
You must be signed in to change notification settings - Fork 25
/
dpos.go
52 lines (44 loc) · 980 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
52
package dpos
import (
"io/ioutil"
"log"
"sort"
"strconv"
"strings"
)
// BPCount 区块生产者的数量
const BPCount = 5
// PickWinner 根据投票数量选择生成区块的节点
func PickWinner() (bp string) {
// 选择BlockProducer
f, err := ioutil.ReadFile(FileName)
if err != nil {
log.Fatal(err)
}
res := strings.Split(string(f), "\n")
voteList := make([]int, len(res))
voteMap := make(map[string]int)
for _, node := range res {
nodeSplit := strings.Split(node, ":")
if len(nodeSplit) > 1 {
vote, err := strconv.Atoi(nodeSplit[1])
if err != nil {
log.Fatal(err)
}
voteList = append(voteList, vote)
voteMap[nodeSplit[0]] = vote
}
}
sort.Slice(voteList, func(i, j int) bool {
return voteList[i] > voteList[j]
})
if len(voteList) > BPCount {
voteList = voteList[0:BPCount] // 选择前面的5个节点作为Block producer
}
for k, v := range voteMap {
if v > voteList[len(voteList)-1] {
bp = k
}
}
return
}