-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode.go
63 lines (54 loc) · 1.45 KB
/
node.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
package rpcx
import (
"context"
"fmt"
"time"
"github.com/meshplus/bitxhub-model/pb"
)
const (
GetInfoTimeout = 2 * time.Second
)
func (cli *ChainClient) GetValidators() (*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_VALIDATORS})
if err != nil {
return nil, fmt.Errorf("%s, %w", err.Error(), ErrBrokenNetwork)
}
return response, nil
}
func (cli *ChainClient) GetNetworkMeta() (*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_NETWORK})
if err != nil {
return nil, fmt.Errorf("%s, %w", err.Error(), ErrBrokenNetwork)
}
return response, nil
}