2019-01-23 02:52:39 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-30 12:28:53 +00:00
|
|
|
"errors"
|
2019-02-13 23:49:06 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/keystore"
|
2019-01-23 02:52:39 +00:00
|
|
|
|
2019-01-30 12:28:53 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2019-01-23 02:52:39 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "validator")
|
|
|
|
|
|
|
|
// ValidatorService represents a service to manage the validator client
|
|
|
|
// routine.
|
|
|
|
type ValidatorService struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
validator Validator
|
2019-01-30 12:28:53 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
endpoint string
|
|
|
|
withCert string
|
2019-02-13 23:49:06 +00:00
|
|
|
key *keystore.Key
|
2019-01-30 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config for the validator service.
|
|
|
|
type Config struct {
|
2019-02-13 23:49:06 +00:00
|
|
|
Endpoint string
|
|
|
|
CertFlag string
|
|
|
|
KeystorePath string
|
|
|
|
Password string
|
2019-01-23 02:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidatorService creates a new validator service for the service
|
|
|
|
// registry.
|
2019-02-13 23:49:06 +00:00
|
|
|
func NewValidatorService(ctx context.Context, cfg *Config) (*ValidatorService, error) {
|
2019-01-23 02:52:39 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-02-13 23:49:06 +00:00
|
|
|
validatorKeyFile := cfg.KeystorePath + params.BeaconConfig().ValidatorPrivkeyFileName
|
|
|
|
ks := keystore.NewKeystore(cfg.KeystorePath)
|
|
|
|
key, err := ks.GetKey(validatorKeyFile, cfg.Password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get private key: %v", err)
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
return &ValidatorService{
|
2019-01-30 12:28:53 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
endpoint: cfg.Endpoint,
|
|
|
|
withCert: cfg.CertFlag,
|
2019-02-13 23:49:06 +00:00
|
|
|
key: key,
|
|
|
|
}, nil
|
2019-01-23 02:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the validator service. Launches the main go routine for the validator
|
|
|
|
// client.
|
|
|
|
func (v *ValidatorService) Start() {
|
2019-01-30 12:28:53 +00:00
|
|
|
var dialOpt grpc.DialOption
|
|
|
|
if v.withCert != "" {
|
|
|
|
creds, err := credentials.NewClientTLSFromFile(v.withCert, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not get valid credentials: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dialOpt = grpc.WithTransportCredentials(creds)
|
|
|
|
} else {
|
|
|
|
dialOpt = grpc.WithInsecure()
|
|
|
|
log.Warn("You are using an insecure gRPC connection! Please provide a certificate and key to use a secure connection.")
|
|
|
|
}
|
|
|
|
conn, err := grpc.DialContext(v.ctx, v.endpoint, dialOpt)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not dial endpoint: %s, %v", v.endpoint, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Successfully started gRPC connection")
|
|
|
|
v.conn = conn
|
|
|
|
v.validator = &validator{
|
|
|
|
beaconClient: pb.NewBeaconServiceClient(v.conn),
|
|
|
|
validatorClient: pb.NewValidatorServiceClient(v.conn),
|
2019-02-06 16:20:38 +00:00
|
|
|
attesterClient: pb.NewAttesterServiceClient(v.conn),
|
2019-02-13 23:49:06 +00:00
|
|
|
proposerClient: pb.NewProposerServiceClient(v.conn),
|
|
|
|
key: v.key,
|
2019-01-30 12:28:53 +00:00
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
go run(v.ctx, v.validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the validator service.
|
|
|
|
func (v *ValidatorService) Stop() error {
|
|
|
|
v.cancel()
|
2019-01-30 12:28:53 +00:00
|
|
|
log.Info("Stopping service")
|
|
|
|
if v.conn != nil {
|
|
|
|
return v.conn.Close()
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status ...
|
|
|
|
//
|
|
|
|
// WIP - not done.
|
|
|
|
func (v *ValidatorService) Status() error {
|
2019-01-30 12:28:53 +00:00
|
|
|
if v.conn == nil {
|
|
|
|
return errors.New("no connection to beacon RPC")
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
return nil
|
|
|
|
}
|