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 rawBlock, err := a.store.GetRawBlock(height)
32 type snapshotInfoResp struct {
33 Height uint64 `json:"height"`
34 Size uint64 `json:"size"`
35 BlockchainID bc.Hash `json:"blockchain_id"`
39 func (a *BlockchainReactor) getSnapshotInfoRPC(ctx context.Context) (resp snapshotInfoResp, err error) {
40 // TODO(jackson): cache latest snapshot and its height & size in-memory.
41 resp.Height, resp.Size, err = a.store.LatestSnapshotInfo(ctx)
42 resp.BlockchainID = *a.config.BlockchainId
46 // getSnapshotRPC returns the raw protobuf snapshot at the provided height.
47 // Non-generators can call this endpoint to get raw data
48 // that they can use to populate their own snapshot table.
50 // This handler doesn't use the httpjson.Handler format so that it can return
51 // raw protobuf bytes on the wire.
52 func (a *BlockchainReactor) getSnapshotRPC(rw http.ResponseWriter, req *http.Request) {
54 alwaysError(errUnconfigured).ServeHTTP(rw, req)
59 err := json.NewDecoder(req.Body).Decode(&height)
61 errorFormatter.Write(req.Context(), rw, httpjson.ErrBadRequest)
65 data, err := a.store.GetSnapshot(req.Context(), height)
67 errorFormatter.Write(req.Context(), rw, err)
70 rw.Header().Set("Content-Type", "application/x-protobuf")