OSDN Git Service

a9789a28017768b34871980d31f3863e392d08de
[bytom/vapor.git] / toolbar / api_node / 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
31         resp := &api.GetRawBlockResp{}
32         return resp.RawBlock, n.request(url, payload, resp)
33 }