-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblock.go
101 lines (88 loc) · 2.4 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package rpcx
import (
"context"
"fmt"
"time"
"github.com/meshplus/bitxhub-model/pb"
)
var _ Client = (*ChainClient)(nil)
const (
GetBlocksTimeout = 10 * time.Second
GetBlockTimeout = 10 * time.Second
)
func (cli *ChainClient) GetBlocks(start uint64, end uint64, fullTx bool) (*pb.GetBlocksResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), GetBlocksTimeout)
defer cancel()
ctx, err := cli.SetCtxMetadata(ctx)
if err != nil {
return nil, fmt.Errorf("set ctx metadata err: %v", err)
}
grpcClient, err := cli.pool.getClient()
if err != nil {
return nil, err
}
defer func() {
if err := grpcClient.conn.Close(); err != nil {
cli.logger.Errorf("close conn err: %s", err)
}
}()
request := &pb.GetBlocksRequest{
Start: start,
End: end,
FullTx: fullTx,
}
response, err := grpcClient.broker.GetBlocks(ctx, request)
if err != nil {
return nil, fmt.Errorf("%s, %w", err.Error(), ErrBrokenNetwork)
}
return response, nil
}
func (cli *ChainClient) GetBlock(value string, blockType pb.GetBlockRequest_Type, fullTx bool) (*pb.Block, error) {
ctx, cancel := context.WithTimeout(context.Background(), GetBlockTimeout)
defer cancel()
ctx, err := cli.SetCtxMetadata(ctx)
if err != nil {
return nil, fmt.Errorf("set ctx metadata err: %v", err)
}
grpcClient, err := cli.pool.getClient()
if err != nil {
return nil, err
}
defer func() {
if err := grpcClient.conn.Close(); err != nil {
cli.logger.Errorf("close conn err: %s", err)
}
}()
request := &pb.GetBlockRequest{
Type: blockType,
Value: value,
FullTx: fullTx,
}
response, err := grpcClient.broker.GetBlock(ctx, request)
if err != nil {
return nil, fmt.Errorf("%s, %w", err.Error(), ErrBrokenNetwork)
}
return response, nil
}
func (cli *ChainClient) GetChainStatus() (*pb.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), GetInfoTimeout)
defer cancel()
ctx, err := cli.SetCtxMetadata(ctx)
if err != nil {
return nil, fmt.Errorf("set ctx metadata err: %v", err)
}
grpcClient, err := cli.pool.getClient()
if err != nil {
return nil, err
}
defer func() {
if err := grpcClient.conn.Close(); err != nil {
cli.logger.Errorf("close conn err: %s", err)
}
}()
response, err := grpcClient.broker.GetInfo(ctx, &pb.Request{Type: pb.Request_CHAIN_STATUS})
if err != nil {
return nil, fmt.Errorf("%s, %w", err.Error(), ErrBrokenNetwork)
}
return response, nil
}