/
microservice.go
100 lines (80 loc) · 2.48 KB
/
microservice.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
package microservice
import (
"net"
"github.com/bloxapp/eth2-key-manager/core"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"google.golang.org/grpc"
"github.com/begmaroman/beaconspot/beaconchain"
"github.com/begmaroman/beaconspot/beaconchain/combined"
"github.com/begmaroman/beaconspot/beaconchain/lighthouse"
"github.com/begmaroman/beaconspot/beaconchain/prysm"
"github.com/begmaroman/beaconspot/grpcapi"
beaconspotproto "github.com/begmaroman/beaconspot/proto/beaconspot"
"github.com/begmaroman/beaconspot/utils/grpcex"
)
var opts Options
// MicroService is the micro-service.
type MicroService struct {
grpcServer *grpc.Server
lis net.Listener
}
// Init initializes the service.
func Init(clientOpts *ClientOptions) (*MicroService, error) {
if err := opts.Load(); err != nil {
return nil, err
}
// Validate options.
if err := opts.Validate(); err != nil {
return nil, errors.Wrap(err, "options validation failed")
}
if opts.IsTest {
clientOpts.Log.Info("Running in test mode!")
}
lis, err := net.Listen("tcp", ":5000")
if err != nil {
return nil, err
}
grpcServer := grpc.NewServer()
// Create beacon chain client
var clients []beaconchain.BeaconChain
// Populate Prysm clients
for _, addr := range opts.PrysmAddrs {
conn, err := grpcex.Dial(clientOpts.Log, addr)
if err != nil {
return nil, errors.Wrapf(err, "failed to create gRPC connection with beacon chain server '%s'", addr)
}
beaconNodeClient := ethpb.NewBeaconNodeValidatorClient(conn)
nodeClient := ethpb.NewNodeClient(conn)
prysmClient := prysm.New(clientOpts.Log, beaconNodeClient, nodeClient)
clients = append(clients, prysmClient)
}
// Populate Lighthouse clients
for _, addr := range opts.LighthouseAddrs {
if addr == "" {
continue
}
// TODO: Pass network through evars
lightHouseClient := lighthouse.New(clientOpts.Log, core.PyrmontNetwork, addr)
clients = append(clients, lightHouseClient)
}
if len(clients) == 0 {
return nil, errors.New("no clients provided")
}
beaconChainClient := combined.New(clients...)
// Create RPC handler.
handler := grpcapi.New(grpcapi.Options{
BeaconChainClient: beaconChainClient,
Log: clientOpts.Log,
})
// Register the service.
beaconspotproto.RegisterBeaconSpotServiceServer(grpcServer, handler)
return &MicroService{
lis: lis,
grpcServer: grpcServer,
}, nil
}
// Run runs the service.
func (s *MicroService) Run() error {
return s.grpcServer.Serve(s.lis)
}