From faa1881b5d4ad5e6f8409b6c4aaecf6aa7f8a946 Mon Sep 17 00:00:00 2001 From: iczc Date: Wed, 24 Jul 2019 09:58:32 +0800 Subject: [PATCH] add get vote result (#345) * add get vote result * fix return type * fix Unmarshal error * use existing voteinfo struct --- api/bbft.go | 8 ++++---- toolbar/api_node/block.go | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/api/bbft.go b/api/bbft.go index 38c6dd6d..0ab6fe6a 100644 --- a/api/bbft.go +++ b/api/bbft.go @@ -6,12 +6,12 @@ import ( chainjson "github.com/vapor/encoding/json" ) -type voteInfo struct { +type VoteInfo struct { 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 } @@ -36,9 +36,9 @@ func (a *API) getVoteResult(req struct { return NewErrorResponse(err) } - voteInfos := []*voteInfo{} + voteInfos := []*VoteInfo{} for pubKey, voteNum := range consensusResult.NumOfVote { - voteInfos = append(voteInfos, &voteInfo{ + voteInfos = append(voteInfos, &VoteInfo{ Vote: pubKey, VoteNum: voteNum, }) diff --git a/toolbar/api_node/block.go b/toolbar/api_node/block.go index a9789a28..d8a1bbf0 100644 --- a/toolbar/api_node/block.go +++ b/toolbar/api_node/block.go @@ -27,7 +27,29 @@ func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) { if err != nil { return nil, errors.Wrap(err, "json marshal") } - 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) +} -- 2.11.0