prysm-pulse/validator/client/service.go
shayzluf fe247f6cc5 keystore and account support for multi key (#2054)
* keystore and account support for multi key

* fix service issues

* fix Potential file inclusion

* fix Potential file inclusion try2

* Update shared/keystore/keystore.go

remove security detection for file read

Co-Authored-By: shayzluf <thezluf@gmail.com>

* getkeys uses map to pt multiple copies of the same key

* use 12 char of public key to differentiate file names

* use map in test

* fix changes from 2069 into here

* add // #nosec G304
2019-03-29 17:26:41 -05:00

113 lines
3.0 KiB
Go

package client
import (
"context"
"errors"
"fmt"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
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
conn *grpc.ClientConn
endpoint string
withCert string
key *keystore.Key
keys map[string]*keystore.Key
}
// Config for the validator service.
type Config struct {
Endpoint string
CertFlag string
KeystorePath string
Password string
}
// NewValidatorService creates a new validator service for the service
// registry.
func NewValidatorService(ctx context.Context, cfg *Config) (*ValidatorService, error) {
ctx, cancel := context.WithCancel(ctx)
validatorFolder := cfg.KeystorePath
validatorPrefix := params.BeaconConfig().ValidatorPrivkeyFileName
ks := keystore.NewKeystore(cfg.KeystorePath)
keys, err := ks.GetKeys(validatorFolder, validatorPrefix, cfg.Password)
if err != nil {
return nil, fmt.Errorf("could not get private key: %v", err)
}
return &ValidatorService{
ctx: ctx,
cancel: cancel,
endpoint: cfg.Endpoint,
withCert: cfg.CertFlag,
keys: keys,
}, nil
}
// Start the validator service. Launches the main go routine for the validator
// client.
func (v *ValidatorService) Start() {
log.WithField("publicKey", fmt.Sprintf("%#x", v.key.PublicKey.Marshal())).Info("Initializing new validator service")
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, grpc.WithStatsHandler(&ocgrpc.ClientHandler{}))
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),
attesterClient: pb.NewAttesterServiceClient(v.conn),
proposerClient: pb.NewProposerServiceClient(v.conn),
key: v.key,
}
go run(v.ctx, v.validator)
}
// Stop the validator service.
func (v *ValidatorService) Stop() error {
v.cancel()
log.Info("Stopping service")
if v.conn != nil {
return v.conn.Close()
}
return nil
}
// Status ...
//
// WIP - not done.
func (v *ValidatorService) Status() error {
if v.conn == nil {
return errors.New("no connection to beacon RPC")
}
return nil
}