OSDN Git Service

Merge branch 'demo' into demo
[bytom/bytom.git] / blockchain / rpc.go
1 package blockchain
2
3 import (
4         "context"
5 //      "encoding/json"
6 //      "net/http"
7
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"
12 )
13
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)
20         if err != nil {
21                 return nil, errors.Wrapf(err, "waiting for block at height %d", height)
22         }
23
24         rawBlock, err := a.store.GetRawBlock(height)
25         if err != nil {
26                 return nil, err
27         }
28
29         return rawBlock, nil
30 }
31
32 type snapshotInfoResp struct {
33         Height       uint64  `json:"height"`
34         Size         uint64  `json:"size"`
35         BlockchainID bc.Hash `json:"blockchain_id"`
36 }
37
38 /*
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
43         return resp, err
44 }
45
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.
49 //
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) {
53         if a.config == nil {
54                 alwaysError(errUnconfigured).ServeHTTP(rw, req)
55                 return
56         }
57
58         var height uint64
59         err := json.NewDecoder(req.Body).Decode(&height)
60         if err != nil {
61                 errorFormatter.Write(req.Context(), rw, httpjson.ErrBadRequest)
62                 return
63         }
64
65         data, err := a.store.GetSnapshot(req.Context(), height)
66         if err != nil {
67                 errorFormatter.Write(req.Context(), rw, err)
68                 return
69         }
70         rw.Header().Set("Content-Type", "application/x-protobuf")
71         rw.Write(data)
72 }*/