OSDN Git Service

Fix bug for hsm test
[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         block, err := a.chain.GetBlockByHeight(height)
25         if err != nil {
26                 return nil, err
27         }
28         rawBlock, err := block.MarshalText()
29         if err != nil {
30
31                 return nil, err
32         }
33
34         return rawBlock, nil
35 }
36
37 type snapshotInfoResp struct {
38         Height       uint64  `json:"height"`
39         Size         uint64  `json:"size"`
40         BlockchainID bc.Hash `json:"blockchain_id"`
41 }
42
43 /*
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
48         return resp, err
49 }
50
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.
54 //
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) {
58         if a.config == nil {
59                 alwaysError(errUnconfigured).ServeHTTP(rw, req)
60                 return
61         }
62
63         var height uint64
64         err := json.NewDecoder(req.Body).Decode(&height)
65         if err != nil {
66                 errorFormatter.Write(req.Context(), rw, httpjson.ErrBadRequest)
67                 return
68         }
69
70         data, err := a.store.GetSnapshot(req.Context(), height)
71         if err != nil {
72                 errorFormatter.Write(req.Context(), rw, err)
73                 return
74         }
75         rw.Header().Set("Content-Type", "application/x-protobuf")
76         rw.Write(data)
77 }*/