/
beaconGrpcClient.go
107 lines (96 loc) · 3.12 KB
/
beaconGrpcClient.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
102
103
104
105
106
107
package beaconapi
import (
"context"
"encoding/hex"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
grpcutil "github.com/prysmaticlabs/prysm/v5/api/grpc"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
log "github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"time"
)
// default grpc port is 4000
func GetValidators(grpcEndpoint string) ([]string, error) {
client := NewGrpcBeaconChainClient(grpcEndpoint)
req := ðpb.ListValidatorsRequest{}
vals, err := client.ListValidators(context.Background(), req)
if err != nil {
return nil, err
}
var pubkeys = make([]string, 0, len(vals.ValidatorList))
for _, val := range vals.ValidatorList {
pubkeys = append(pubkeys, hex.EncodeToString(val.Validator.PublicKey))
}
return pubkeys, nil
}
// ConstructDialOptions constructs a list of grpc dial options
func ConstructDialOptions(
maxCallRecvMsgSize int,
withCert string,
grpcRetries uint,
grpcRetryDelay time.Duration,
extraOpts ...grpc.DialOption,
) []grpc.DialOption {
var transportSecurity grpc.DialOption
if withCert != "" {
creds, err := credentials.NewClientTLSFromFile(withCert, "")
if err != nil {
log.WithError(err).Error("Could not get valid credentials")
return nil
}
transportSecurity = grpc.WithTransportCredentials(creds)
} else {
transportSecurity = grpc.WithInsecure()
log.Warn("You are using an insecure gRPC connection. If you are running your beacon node and " +
"validator on the same machines, you can ignore this message. If you want to know " +
"how to enable secure connections, see: https://docs.prylabs.network/docs/prysm-usage/secure-grpc")
}
if maxCallRecvMsgSize == 0 {
maxCallRecvMsgSize = 10 * 5 << 20 // Default 50Mb
}
dialOpts := []grpc.DialOption{
transportSecurity,
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
grpcretry.WithMax(grpcRetries),
grpcretry.WithBackoff(grpcretry.BackoffLinear(grpcRetryDelay)),
),
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
grpc.WithUnaryInterceptor(middleware.ChainUnaryClient(
grpcopentracing.UnaryClientInterceptor(),
grpcprometheus.UnaryClientInterceptor,
grpcretry.UnaryClientInterceptor(),
grpcutil.LogRequests,
)),
grpc.WithChainStreamInterceptor(
grpcutil.LogStream,
grpcopentracing.StreamClientInterceptor(),
grpcprometheus.StreamClientInterceptor,
grpcretry.StreamClientInterceptor(),
),
grpc.WithResolvers(&multipleEndpointsGrpcResolverBuilder{}),
}
dialOpts = append(dialOpts, extraOpts...)
return dialOpts
}
func NewGrpcBeaconChainClient(endpoint string) ethpb.BeaconChainClient {
dialOpts := ConstructDialOptions(
1024*1024,
"",
5,
time.Second,
)
if dialOpts == nil {
return nil
}
grpcConn, err := grpc.DialContext(context.Background(), endpoint, dialOpts...)
if err != nil {
return nil
}
return ethpb.NewBeaconChainClient(grpcConn)
}