OSDN Git Service

add get vote result (#345)
authoriczc <iczcalan@gmail.com>
Wed, 24 Jul 2019 01:58:32 +0000 (09:58 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 24 Jul 2019 01:58:32 +0000 (09:58 +0800)
* add get vote result

* fix return type

* fix Unmarshal error

* use existing voteinfo struct

api/bbft.go
toolbar/api_node/block.go

index 38c6dd6..0ab6fe6 100644 (file)
@@ -6,12 +6,12 @@ import (
        chainjson "github.com/vapor/encoding/json"
 )
 
        chainjson "github.com/vapor/encoding/json"
 )
 
-type voteInfo struct {
+type VoteInfo struct {
        Vote    string `json:"vote"`
        VoteNum uint64 `json:"vote_number"`
 }
 
        Vote    string `json:"vote"`
        VoteNum uint64 `json:"vote_number"`
 }
 
-type voteInfoSlice []*voteInfo
+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) Len() int           { return len(v) }
 func (v voteInfoSlice) Less(i, j int) bool { return v[i].VoteNum > v[j].VoteNum }
@@ -36,9 +36,9 @@ func (a *API) getVoteResult(req struct {
                return NewErrorResponse(err)
        }
 
                return NewErrorResponse(err)
        }
 
-       voteInfos := []*voteInfo{}
+       voteInfos := []*VoteInfo{}
        for pubKey, voteNum := range consensusResult.NumOfVote {
        for pubKey, voteNum := range consensusResult.NumOfVote {
-               voteInfos = append(voteInfos, &voteInfo{
+               voteInfos = append(voteInfos, &VoteInfo{
                        Vote:    pubKey,
                        VoteNum: voteNum,
                })
                        Vote:    pubKey,
                        VoteNum: voteNum,
                })
index a9789a2..d8a1bbf 100644 (file)
@@ -27,7 +27,29 @@ func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) {
        if err != nil {
                return nil, errors.Wrap(err, "json marshal")
        }
        if err != nil {
                return nil, errors.Wrap(err, "json marshal")
        }
-
        resp := &api.GetRawBlockResp{}
        return resp.RawBlock, n.request(url, payload, resp)
 }
        resp := &api.GetRawBlockResp{}
        return resp.RawBlock, n.request(url, payload, resp)
 }
+
+func (n *Node) GetVoteByHash(hash string) ([]api.VoteInfo, error) {
+       return n.getVoteResult(&getVoteResultReq{BlockHash: hash})
+}
+
+func (n *Node) GetVoteByHeight(height uint64) ([]api.VoteInfo, error) {
+       return n.getVoteResult(&getVoteResultReq{BlockHeight: height})
+}
+
+type getVoteResultReq struct {
+       BlockHeight uint64 `json:"block_height"`
+       BlockHash   string `json:"block_hash"`
+}
+
+func (n *Node) getVoteResult(req *getVoteResultReq) ([]api.VoteInfo, error) {
+       url := "/get-vote-result"
+       payload, err := json.Marshal(req)
+       if err != nil {
+               return nil, errors.Wrap(err, "json marshal")
+       }
+       resp := []api.VoteInfo{}
+       return resp, n.request(url, payload, &resp)
+}