2020-04-29 21:32:39 +00:00
|
|
|
// Package node is the main process which handles the lifecycle of
|
|
|
|
// the runtime services in a slasher process, gracefully shutting
|
|
|
|
// everything down upon close.
|
2020-02-14 07:09:54 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-08 06:35:37 +00:00
|
|
|
"fmt"
|
2020-02-14 07:09:54 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2020-04-02 03:08:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-14 07:09:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/debug"
|
2020-02-14 18:11:15 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2020-04-29 23:27:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2020-07-20 20:23:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-10-02 06:45:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/prereq"
|
2020-03-09 05:35:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/prometheus"
|
2020-02-14 07:09:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/tracing"
|
2020-06-05 12:04:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
2020-02-14 07:09:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/beaconclient"
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db/kv"
|
2020-02-14 19:03:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/detection"
|
2020-02-14 07:09:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/flags"
|
2020-03-24 18:30:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/rpc"
|
2020-02-14 07:09:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-31 06:44:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-14 07:09:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "node")
|
|
|
|
|
|
|
|
const slasherDBName = "slasherdata"
|
|
|
|
|
|
|
|
// SlasherNode defines a struct that handles the services running a slashing detector
|
|
|
|
// for eth2. It handles the lifecycle of the entire system and registers
|
|
|
|
// services to a service registry.
|
|
|
|
type SlasherNode struct {
|
2020-04-29 23:27:01 +00:00
|
|
|
cliCtx *cli.Context
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2020-02-14 18:11:15 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
services *shared.ServiceRegistry
|
|
|
|
proposerSlashingsFeed *event.Feed
|
|
|
|
attesterSlashingsFeed *event.Feed
|
|
|
|
stop chan struct{} // Channel to wait for termination notifications.
|
|
|
|
db db.Database
|
2020-02-14 07:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSlasherNode creates a new node instance, sets up configuration options,
|
|
|
|
// and registers every required service.
|
2020-04-29 23:27:01 +00:00
|
|
|
func NewSlasherNode(cliCtx *cli.Context) (*SlasherNode, error) {
|
2020-02-14 07:09:54 +00:00
|
|
|
if err := tracing.Setup(
|
|
|
|
"slasher", // Service name.
|
2020-04-29 23:27:01 +00:00
|
|
|
cliCtx.String(cmd.TracingProcessNameFlag.Name),
|
|
|
|
cliCtx.String(cmd.TracingEndpointFlag.Name),
|
|
|
|
cliCtx.Float64(cmd.TraceSampleFractionFlag.Name),
|
|
|
|
cliCtx.Bool(cmd.EnableTracingFlag.Name),
|
2020-02-14 07:09:54 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-22 15:53:09 +00:00
|
|
|
|
2020-10-02 06:45:34 +00:00
|
|
|
// Warn if user's platform is not supported
|
|
|
|
prereq.WarnIfNotSupported(cliCtx.Context)
|
|
|
|
|
2020-08-02 22:09:10 +00:00
|
|
|
if cliCtx.Bool(flags.EnableHistoricalDetectionFlag.Name) {
|
2020-07-20 20:23:48 +00:00
|
|
|
// Set the max RPC size to 4096 as configured by --historical-slasher-node for optimal historical detection.
|
|
|
|
cmdConfig := cmd.Get()
|
|
|
|
cmdConfig.MaxRPCPageSize = int(params.BeaconConfig().SlotsPerEpoch * params.BeaconConfig().MaxAttestations)
|
|
|
|
cmd.Init(cmdConfig)
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
featureconfig.ConfigureSlasher(cliCtx)
|
2020-07-30 13:45:00 +00:00
|
|
|
cmd.ConfigureSlasher(cliCtx)
|
2020-02-14 07:09:54 +00:00
|
|
|
registry := shared.NewServiceRegistry()
|
|
|
|
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx, cancel := context.WithCancel(cliCtx.Context)
|
2020-02-14 07:09:54 +00:00
|
|
|
slasher := &SlasherNode{
|
2020-04-29 23:27:01 +00:00
|
|
|
cliCtx: cliCtx,
|
2020-02-14 18:11:15 +00:00
|
|
|
ctx: ctx,
|
2020-04-29 23:27:01 +00:00
|
|
|
cancel: cancel,
|
2020-02-14 18:11:15 +00:00
|
|
|
proposerSlashingsFeed: new(event.Feed),
|
|
|
|
attesterSlashingsFeed: new(event.Feed),
|
|
|
|
services: registry,
|
|
|
|
stop: make(chan struct{}),
|
2020-02-14 07:09:54 +00:00
|
|
|
}
|
2020-09-30 20:55:56 +00:00
|
|
|
|
|
|
|
if !cliCtx.Bool(cmd.DisableMonitoringFlag.Name) {
|
|
|
|
if err := slasher.registerPrometheusService(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-08 06:35:37 +00:00
|
|
|
}
|
2020-02-14 07:09:54 +00:00
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
if err := slasher.startDB(); err != nil {
|
2020-02-14 07:09:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
if err := slasher.registerBeaconClientService(); err != nil {
|
2020-02-14 07:09:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:03:25 +00:00
|
|
|
if err := slasher.registerDetectionService(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
if err := slasher.registerRPCService(); err != nil {
|
2020-03-24 18:30:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-14 07:09:54 +00:00
|
|
|
return slasher, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the slasher and kick off every registered service.
|
|
|
|
func (s *SlasherNode) Start() {
|
|
|
|
s.lock.Lock()
|
|
|
|
s.services.StartAll()
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
2020-06-05 12:04:12 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"version": version.GetVersion(),
|
|
|
|
}).Info("Starting slasher client")
|
|
|
|
|
2020-02-14 07:09:54 +00:00
|
|
|
stop := s.stop
|
|
|
|
go func() {
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(sigc)
|
|
|
|
<-sigc
|
|
|
|
log.Info("Got interrupt, shutting down...")
|
2020-04-29 23:27:01 +00:00
|
|
|
debug.Exit(s.cliCtx) // Ensure trace and CPU profile data are flushed.
|
2020-02-14 07:09:54 +00:00
|
|
|
go s.Close()
|
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
<-sigc
|
|
|
|
if i > 1 {
|
2020-04-29 23:27:01 +00:00
|
|
|
log.WithField("times", i-1).Info("Already shutting down, interrupt more to panic")
|
2020-02-14 07:09:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("Panic closing the beacon node")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for stop channel to be closed.
|
|
|
|
<-stop
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close handles graceful shutdown of the system.
|
|
|
|
func (s *SlasherNode) Close() {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
log.Info("Stopping hash slinging slasher")
|
2020-04-29 23:27:01 +00:00
|
|
|
s.cancel()
|
2020-02-14 07:09:54 +00:00
|
|
|
s.services.StopAll()
|
|
|
|
if err := s.db.Close(); err != nil {
|
|
|
|
log.Errorf("Failed to close database: %v", err)
|
|
|
|
}
|
|
|
|
close(s.stop)
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
func (s *SlasherNode) registerPrometheusService() error {
|
2020-09-23 08:59:49 +00:00
|
|
|
service := prometheus.NewService(
|
2020-07-08 08:21:06 +00:00
|
|
|
fmt.Sprintf("%s:%d", s.cliCtx.String(cmd.MonitoringHostFlag.Name), s.cliCtx.Int(flags.MonitoringPortFlag.Name)),
|
2020-03-08 06:35:37 +00:00
|
|
|
s.services,
|
|
|
|
)
|
|
|
|
logrus.AddHook(prometheus.NewLogrusCollector())
|
|
|
|
return s.services.RegisterService(service)
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
func (s *SlasherNode) startDB() error {
|
|
|
|
baseDir := s.cliCtx.String(cmd.DataDirFlag.Name)
|
|
|
|
clearDB := s.cliCtx.Bool(cmd.ClearDB.Name)
|
|
|
|
forceClearDB := s.cliCtx.Bool(cmd.ForceClearDB.Name)
|
2020-02-14 07:09:54 +00:00
|
|
|
dbPath := path.Join(baseDir, slasherDBName)
|
2020-08-18 11:18:33 +00:00
|
|
|
spanCacheSize := s.cliCtx.Int(flags.SpanCacheSize.Name)
|
2020-10-26 12:15:42 +00:00
|
|
|
highestAttCacheSize := s.cliCtx.Int(flags.HighestAttCacheSize.Name)
|
|
|
|
cfg := &kv.Config{SpanCacheSize: spanCacheSize, HighestAttestationCacheSize: highestAttCacheSize}
|
2020-08-18 11:18:33 +00:00
|
|
|
log.Infof("Span cache size has been set to: %d", spanCacheSize)
|
2020-02-14 07:09:54 +00:00
|
|
|
d, err := db.NewDB(dbPath, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clearDBConfirmed := false
|
|
|
|
if clearDB && !forceClearDB {
|
|
|
|
actionText := "This will delete your slasher database stored in your data directory. " +
|
|
|
|
"Your database backups will not be removed - do you want to proceed? (Y/N)"
|
|
|
|
deniedText := "Database will not be deleted. No changes have been made."
|
|
|
|
clearDBConfirmed, err = cmd.ConfirmAction(actionText, deniedText)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if clearDBConfirmed || forceClearDB {
|
|
|
|
log.Warning("Removing database")
|
2020-10-09 09:28:35 +00:00
|
|
|
if err := d.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not close db prior to clearing")
|
|
|
|
}
|
2020-02-14 07:09:54 +00:00
|
|
|
if err := d.ClearDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d, err = db.NewDB(dbPath, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.WithField("database-path", baseDir).Info("Checking DB")
|
|
|
|
s.db = d
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
func (s *SlasherNode) registerBeaconClientService() error {
|
|
|
|
beaconCert := s.cliCtx.String(flags.BeaconCertFlag.Name)
|
|
|
|
beaconProvider := s.cliCtx.String(flags.BeaconRPCProviderFlag.Name)
|
2020-02-14 07:09:54 +00:00
|
|
|
if beaconProvider == "" {
|
|
|
|
beaconProvider = flags.BeaconRPCProviderFlag.Value
|
|
|
|
}
|
2020-02-14 19:03:25 +00:00
|
|
|
|
2020-09-23 08:59:49 +00:00
|
|
|
bs, err := beaconclient.NewService(s.ctx, &beaconclient.Config{
|
2020-02-14 18:11:15 +00:00
|
|
|
BeaconCert: beaconCert,
|
2020-02-27 17:22:39 +00:00
|
|
|
SlasherDB: s.db,
|
2020-02-14 18:11:15 +00:00
|
|
|
BeaconProvider: beaconProvider,
|
|
|
|
AttesterSlashingsFeed: s.attesterSlashingsFeed,
|
|
|
|
ProposerSlashingsFeed: s.proposerSlashingsFeed,
|
2020-02-14 07:09:54 +00:00
|
|
|
})
|
2020-04-02 03:08:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to initialize beacon client")
|
|
|
|
}
|
2020-02-14 07:09:54 +00:00
|
|
|
return s.services.RegisterService(bs)
|
|
|
|
}
|
2020-02-14 19:03:25 +00:00
|
|
|
|
|
|
|
func (s *SlasherNode) registerDetectionService() error {
|
|
|
|
var bs *beaconclient.Service
|
|
|
|
if err := s.services.FetchService(&bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-09-23 08:59:49 +00:00
|
|
|
ds := detection.NewService(s.ctx, &detection.Config{
|
2020-02-14 19:03:25 +00:00
|
|
|
Notifier: bs,
|
2020-02-19 22:26:14 +00:00
|
|
|
SlasherDB: s.db,
|
|
|
|
BeaconClient: bs,
|
|
|
|
ChainFetcher: bs,
|
2020-02-14 19:03:25 +00:00
|
|
|
AttesterSlashingsFeed: s.attesterSlashingsFeed,
|
|
|
|
ProposerSlashingsFeed: s.proposerSlashingsFeed,
|
2020-08-02 22:09:10 +00:00
|
|
|
HistoricalDetection: s.cliCtx.Bool(flags.EnableHistoricalDetectionFlag.Name),
|
2020-02-14 19:03:25 +00:00
|
|
|
})
|
|
|
|
return s.services.RegisterService(ds)
|
|
|
|
}
|
2020-03-24 18:30:21 +00:00
|
|
|
|
2020-04-29 23:27:01 +00:00
|
|
|
func (s *SlasherNode) registerRPCService() error {
|
2020-03-24 18:30:21 +00:00
|
|
|
var detectionService *detection.Service
|
|
|
|
if err := s.services.FetchService(&detectionService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-20 15:23:22 +00:00
|
|
|
var bs *beaconclient.Service
|
|
|
|
if err := s.services.FetchService(&bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
host := s.cliCtx.String(flags.RPCHost.Name)
|
2020-04-29 23:27:01 +00:00
|
|
|
port := s.cliCtx.String(flags.RPCPort.Name)
|
|
|
|
cert := s.cliCtx.String(flags.CertFlag.Name)
|
|
|
|
key := s.cliCtx.String(flags.KeyFlag.Name)
|
|
|
|
rpcService := rpc.NewService(s.ctx, &rpc.Config{
|
2020-05-20 15:23:22 +00:00
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
CertFlag: cert,
|
|
|
|
KeyFlag: key,
|
|
|
|
Detector: detectionService,
|
|
|
|
SlasherDB: s.db,
|
|
|
|
BeaconClient: bs,
|
2020-03-24 18:30:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return s.services.RegisterService(rpcService)
|
|
|
|
}
|