8 chainjson "github.com/bytom/encoding/json"
9 "github.com/bytom/errors"
10 // "github.com/bytom/net/http/httpjson"
11 "github.com/bytom/protocol/bc"
14 // getBlockRPC returns the block at the requested height.
15 // If successful, it always returns at least one block,
16 // waiting if necessary until one is created.
17 // It is an error to request blocks very far in the future.
18 func (a *BlockchainReactor) getBlockRPC(ctx context.Context, height uint64) (chainjson.HexBytes, error) {
19 err := <-a.chain.BlockSoonWaiter(ctx, height)
21 return nil, errors.Wrapf(err, "waiting for block at height %d", height)
24 block, err := a.chain.GetBlockByHeight(height)
28 rawBlock, err := block.MarshalText()
37 type snapshotInfoResp struct {
38 Height uint64 `json:"height"`
39 Size uint64 `json:"size"`
40 BlockchainID bc.Hash `json:"blockchain_id"`
44 func (a *BlockchainReactor) getSnapshotInfoRPC(ctx context.Context) (resp snapshotInfoResp, err error) {
45 // TODO(jackson): cache latest snapshot and its height & size in-memory.
46 resp.Height, resp.Size, err = a.store.LatestSnapshotInfo(ctx)
47 resp.BlockchainID = *a.config.BlockchainId
51 // getSnapshotRPC returns the raw protobuf snapshot at the provided height.
52 // Non-generators can call this endpoint to get raw data
53 // that they can use to populate their own snapshot table.
55 // This handler doesn't use the httpjson.Handler format so that it can return
56 // raw protobuf bytes on the wire.
57 func (a *BlockchainReactor) getSnapshotRPC(rw http.ResponseWriter, req *http.Request) {
59 alwaysError(errUnconfigured).ServeHTTP(rw, req)
64 err := json.NewDecoder(req.Body).Decode(&height)
66 errorFormatter.Write(req.Context(), rw, httpjson.ErrBadRequest)
70 data, err := a.store.GetSnapshot(req.Context(), height)
72 errorFormatter.Write(req.Context(), rw, err)
75 rw.Header().Set("Content-Type", "application/x-protobuf")