github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/toolbar/apinode/block.go (about) 1 package apinode 2 3 import ( 4 "encoding/json" 5 6 "github.com/bytom/bytom/api" 7 "github.com/bytom/bytom/errors" 8 "github.com/bytom/bytom/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 resp := &api.GetRawBlockResp{} 31 return resp.RawBlock, n.request(url, payload, resp) 32 } 33 34 // bytomChainStatusResp is the response of bytom chain status 35 type bytomChainStatusResp struct { 36 FinalizedHeight uint64 `json:"finalized_height"` 37 } 38 39 // GetFinalizedHeight return the finalized block height of connected node 40 func (n *Node) GetFinalizedHeight() (uint64, error) { 41 url := "/chain-status" 42 res := &bytomChainStatusResp{} 43 return res.FinalizedHeight, n.request(url, nil, res) 44 }