OSDN Git Service

Reward util (#342)
[bytom/vapor.git] / toolbar / api_node / block.go
diff --git a/toolbar/api_node/block.go b/toolbar/api_node/block.go
new file mode 100644 (file)
index 0000000..a9789a2
--- /dev/null
@@ -0,0 +1,33 @@
+package apinode
+
+import (
+       "encoding/json"
+
+       "github.com/vapor/api"
+       "github.com/vapor/errors"
+       "github.com/vapor/protocol/bc/types"
+)
+
+func (n *Node) GetBlockByHash(hash string) (*types.Block, error) {
+       return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
+}
+
+func (n *Node) GetBlockByHeight(height uint64) (*types.Block, error) {
+       return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
+}
+
+type getRawBlockReq struct {
+       BlockHeight uint64 `json:"block_height"`
+       BlockHash   string `json:"block_hash"`
+}
+
+func (n *Node) getRawBlock(req *getRawBlockReq) (*types.Block, error) {
+       url := "/get-raw-block"
+       payload, err := json.Marshal(req)
+       if err != nil {
+               return nil, errors.Wrap(err, "json marshal")
+       }
+
+       resp := &api.GetRawBlockResp{}
+       return resp.RawBlock, n.request(url, payload, resp)
+}