6 "github.com/vapor/errors"
7 "github.com/vapor/federation/util"
8 "github.com/vapor/protocol/bc"
9 "github.com/vapor/protocol/bc/types"
12 // Node can invoke the api which provide by the full node server
17 // Node create a api client with target server
18 func NewNode(ip string) *Node {
22 func (n *Node) GetBlockByHash(hash string) (string, *bc.TransactionStatus, error) {
23 return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
26 func (n *Node) GetBlockByHeight(height uint64) (string, *bc.TransactionStatus, error) {
27 return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
30 type getBlockCountResp struct {
31 BlockCount uint64 `json:"block_count"`
34 func (n *Node) GetBlockCount() (uint64, error) {
35 url := "/get-block-count"
36 res := &getBlockCountResp{}
37 return res.BlockCount, n.request(url, nil, res)
40 type getRawBlockReq struct {
41 BlockHeight uint64 `json:"block_height"`
42 BlockHash string `json:"block_hash"`
45 type getRawBlockResp struct {
46 RawBlock string `json:"raw_block"`
47 // TransactionStatus has same marshalling rule for both bytom and vapor
48 TransactionStatus *bc.TransactionStatus `json:"transaction_status"`
51 func (n *Node) getRawBlock(req *getRawBlockReq) (string, *bc.TransactionStatus, error) {
52 url := "/get-raw-block"
53 payload, err := json.Marshal(req)
55 return "", nil, errors.Wrap(err, "json marshal")
58 res := &getRawBlockResp{}
59 return res.RawBlock, res.TransactionStatus, n.request(url, payload, res)
62 type submitTxReq struct {
63 Tx *types.Tx `json:"raw_transaction"`
66 type response struct {
67 Status string `json:"status"`
68 Data json.RawMessage `json:"data"`
69 ErrDetail string `json:"error_detail"`
72 func (n *Node) request(url string, payload []byte, respData interface{}) error {
74 if err := util.Post(n.ip+url, payload, resp); err != nil {
78 if resp.Status != "success" {
79 return errors.New(resp.ErrDetail)
82 return json.Unmarshal(resp.Data, respData)