6 btmTypes "github.com/bytom/protocol/bc/types"
8 "github.com/vapor/errors"
9 "github.com/vapor/federation/util"
10 "github.com/vapor/protocol/bc"
11 vaporTypes "github.com/vapor/protocol/bc/types"
14 // Node can invoke the api which provide by the full node server
19 // Node create a api client with target server
20 func NewNode(ip string) *Node {
24 func (n *Node) GetBlockByHash(hash string) (string, *bc.TransactionStatus, error) {
25 return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
28 func (n *Node) GetBlockByHeight(height uint64) (string, *bc.TransactionStatus, error) {
29 return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
32 type getBlockCountResp struct {
33 BlockCount uint64 `json:"block_count"`
36 func (n *Node) GetBlockCount() (uint64, error) {
37 url := "/get-block-count"
38 res := &getBlockCountResp{}
39 return res.BlockCount, n.request(url, nil, res)
42 type getRawBlockReq struct {
43 BlockHeight uint64 `json:"block_height"`
44 BlockHash string `json:"block_hash"`
47 type getRawBlockResp struct {
48 RawBlock string `json:"raw_block"`
49 // TransactionStatus has same marshalling rule for both bytom and vapor
50 TransactionStatus *bc.TransactionStatus `json:"transaction_status"`
53 func (n *Node) getRawBlock(req *getRawBlockReq) (string, *bc.TransactionStatus, error) {
54 url := "/get-raw-block"
55 payload, err := json.Marshal(req)
57 return "", nil, errors.Wrap(err, "json marshal")
60 res := &getRawBlockResp{}
61 return res.RawBlock, res.TransactionStatus, n.request(url, payload, res)
64 type submitMainchainTxReq struct {
65 Tx *btmTypes.Tx `json:"raw_transaction"`
68 type submitSidechainTxReq struct {
69 Tx *vaporTypes.Tx `json:"raw_transaction"`
72 type submitTxResp struct {
73 TxID string `json:"tx_id"`
76 func (n *Node) SubmitTx(tx interface{} /*, isMainchain bool*/) (string, error) {
77 url := "/submit-transaction"
81 switch tx := tx.(type) {
83 payload, err = json.Marshal(submitMainchainTxReq{Tx: tx})
85 return "", errors.Wrap(err, "json marshal")
89 payload, err = json.Marshal(submitSidechainTxReq{Tx: tx})
91 return "", errors.Wrap(err, "json marshal")
95 return "", errors.New("unknown tx type")
98 res := &submitTxResp{}
99 return res.TxID, n.request(url, payload, res)
102 type response struct {
103 Status string `json:"status"`
104 Data json.RawMessage `json:"data"`
105 ErrDetail string `json:"error_detail"`
108 func (n *Node) request(url string, payload []byte, respData interface{}) error {
110 if err := util.Post(n.ip+url, payload, resp); err != nil {
114 if resp.Status != "success" {
115 return errors.New(resp.ErrDetail)
118 return json.Unmarshal(resp.Data, respData)