/
bbft.go
48 lines (39 loc) · 1.17 KB
/
bbft.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
package api
import (
"sort"
chainjson "github.com/bytom/vapor/encoding/json"
)
type VoteInfo struct {
Vote string `json:"vote"`
VoteNum uint64 `json:"vote_number"`
}
type voteInfoSlice []*VoteInfo
func (v voteInfoSlice) Len() int { return len(v) }
func (v voteInfoSlice) Less(i, j int) bool { return v[i].VoteNum > v[j].VoteNum }
func (v voteInfoSlice) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (a *API) getVoteResult(req struct {
BlockHash chainjson.HexBytes `json:"block_hash"`
BlockHeight uint64 `json:"block_height"`
}) Response {
blockHash := hexBytesToHash(req.BlockHash)
if len(req.BlockHash) != 32 {
blockHeader, err := a.chain.GetHeaderByHeight(req.BlockHeight)
if err != nil {
return NewErrorResponse(err)
}
blockHash = blockHeader.Hash()
}
consensusResult, err := a.chain.GetConsensusResultByHash(&blockHash)
if err != nil {
return NewErrorResponse(err)
}
voteInfos := []*VoteInfo{}
for pubKey, voteNum := range consensusResult.NumOfVote {
voteInfos = append(voteInfos, &VoteInfo{
Vote: pubKey,
VoteNum: voteNum,
})
}
sort.Sort(voteInfoSlice(voteInfos))
return NewSuccessResponse(voteInfos)
}