OSDN Git Service

d8a1bbf0f472ef8a1a33b30c27eb27e4ab289572
[bytom/vapor.git] / toolbar / apinode / block.go
1 package apinode
2
3 import (
4         "encoding/json"
5
6         "github.com/vapor/api"
7         "github.com/vapor/errors"
8         "github.com/vapor/protocol/bc/types"
9 )
10
11 func (n *Node) GetBlockByHash(hash string) (*types.Block, error) {
12         return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
13 }
14
15 func (n *Node) GetBlockByHeight(height uint64) (*types.Block, error) {
16         return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
17 }
18
19 type getRawBlockReq struct {
20         BlockHeight uint64 `json:"block_height"`
21         BlockHash   string `json:"block_hash"`
22 }
23
24 func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) {
25         url := "/get-raw-block"
26         payload, err := json.Marshal(req)
27         if err != nil {
28                 return nil, errors.Wrap(err, "json marshal")
29         }
30         resp := &api.GetRawBlockResp{}
31         return resp.RawBlock, n.request(url, payload, resp)
32 }
33
34 func (n *Node) GetVoteByHash(hash string) ([]api.VoteInfo, error) {
35         return n.getVoteResult(&getVoteResultReq{BlockHash: hash})
36 }
37
38 func (n *Node) GetVoteByHeight(height uint64) ([]api.VoteInfo, error) {
39         return n.getVoteResult(&getVoteResultReq{BlockHeight: height})
40 }
41
42 type getVoteResultReq struct {
43         BlockHeight uint64 `json:"block_height"`
44         BlockHash   string `json:"block_hash"`
45 }
46
47 func (n *Node) getVoteResult(req *getVoteResultReq) ([]api.VoteInfo, error) {
48         url := "/get-vote-result"
49         payload, err := json.Marshal(req)
50         if err != nil {
51                 return nil, errors.Wrap(err, "json marshal")
52         }
53         resp := []api.VoteInfo{}
54         return resp, n.request(url, payload, &resp)
55 }