6 "github.com/bytom/vapor/api"
7 "github.com/bytom/vapor/errors"
8 "github.com/bytom/vapor/protocol/bc/types"
11 func (n *Node) GetBlockByHash(hash string) (*types.Block, error) {
12 return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
15 func (n *Node) GetBlockByHeight(height uint64) (*types.Block, error) {
16 return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
19 type getBlockCountResp struct {
20 BlockCount uint64 `json:"block_count"`
23 func (n *Node) GetBlockCount() (uint64, error) {
24 url := "/get-block-count"
25 res := &getBlockCountResp{}
26 return res.BlockCount, n.request(url, nil, res)
29 type getRawBlockReq struct {
30 BlockHeight uint64 `json:"block_height"`
31 BlockHash string `json:"block_hash"`
34 func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) {
35 url := "/get-raw-block"
36 payload, err := json.Marshal(req)
38 return nil, errors.Wrap(err, "json marshal")
40 resp := &api.GetRawBlockResp{}
41 return resp.RawBlock, n.request(url, payload, resp)
44 func (n *Node) GetVoteByHash(hash string) ([]*api.VoteInfo, error) {
45 return n.getVoteResult(&getVoteResultReq{BlockHash: hash})
48 func (n *Node) GetVoteByHeight(height uint64) ([]*api.VoteInfo, error) {
49 return n.getVoteResult(&getVoteResultReq{BlockHeight: height})
52 type getVoteResultReq struct {
53 BlockHeight uint64 `json:"block_height"`
54 BlockHash string `json:"block_hash"`
57 func (n *Node) getVoteResult(req *getVoteResultReq) ([]*api.VoteInfo, error) {
58 url := "/get-vote-result"
59 payload, err := json.Marshal(req)
61 return nil, errors.Wrap(err, "json marshal")
63 resp := []*api.VoteInfo{}
64 return resp, n.request(url, payload, &resp)