Skip to content

Commit

Permalink
Unify usage of context in slasher client (#5684)
Browse files Browse the repository at this point in the history
* Unify usage of context in slasher client

* Fix build

* Add error handlers so all processes stop

* Remove unneeded checks
  • Loading branch information
0xKiwi authored Apr 29, 2020
1 parent 546196a commit 2f88174
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion slasher/beaconclient/chain_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (bs *Service) querySyncStatus(ctx context.Context) {
if err != nil {
log.WithError(err).Error("Could not fetch sync status")
}
if !status.Syncing {
if status != nil && !status.Syncing {
log.Info("Beacon node is fully synced, starting slashing detection")
return
}
Expand Down
7 changes: 3 additions & 4 deletions slasher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ import (

var log = logrus.WithField("prefix", "main")

func startSlasher(ctx *cli.Context) error {
featureconfig.ConfigureSlasher(ctx)
verbosity := ctx.String(cmd.VerbosityFlag.Name)
func startSlasher(cliCtx *cli.Context) error {
verbosity := cliCtx.String(cmd.VerbosityFlag.Name)
level, err := logrus.ParseLevel(verbosity)
if err != nil {
return err
}
logrus.SetLevel(level)
slasher, err := node.NewSlasherNode(ctx)
slasher, err := node.NewSlasherNode(cliCtx)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions slasher/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"//shared/cmd:go_default_library",
"//shared/debug:go_default_library",
"//shared/event:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/prometheus:go_default_library",
"//shared/tracing:go_default_library",
"//slasher/beaconclient:go_default_library",
Expand Down
64 changes: 36 additions & 28 deletions slasher/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/debug"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/prometheus"
"github.com/prysmaticlabs/prysm/shared/tracing"
"github.com/prysmaticlabs/prysm/slasher/beaconclient"
Expand All @@ -37,7 +38,9 @@ const slasherDBName = "slasherdata"
// for eth2. It handles the lifecycle of the entire system and registers
// services to a service registry.
type SlasherNode struct {
ctx *cli.Context
cliCtx *cli.Context
ctx context.Context
cancel context.CancelFunc
lock sync.RWMutex
services *shared.ServiceRegistry
proposerSlashingsFeed *event.Feed
Expand All @@ -48,43 +51,47 @@ type SlasherNode struct {

// NewSlasherNode creates a new node instance, sets up configuration options,
// and registers every required service.
func NewSlasherNode(ctx *cli.Context) (*SlasherNode, error) {
func NewSlasherNode(cliCtx *cli.Context) (*SlasherNode, error) {
if err := tracing.Setup(
"slasher", // Service name.
ctx.String(cmd.TracingProcessNameFlag.Name),
ctx.String(cmd.TracingEndpointFlag.Name),
ctx.Float64(cmd.TraceSampleFractionFlag.Name),
ctx.Bool(cmd.EnableTracingFlag.Name),
cliCtx.String(cmd.TracingProcessNameFlag.Name),
cliCtx.String(cmd.TracingEndpointFlag.Name),
cliCtx.Float64(cmd.TraceSampleFractionFlag.Name),
cliCtx.Bool(cmd.EnableTracingFlag.Name),
); err != nil {
return nil, err
}

featureconfig.ConfigureSlasher(cliCtx)
registry := shared.NewServiceRegistry()

ctx, cancel := context.WithCancel(cliCtx)
slasher := &SlasherNode{
cliCtx: cliCtx,
ctx: ctx,
cancel: cancel,
proposerSlashingsFeed: new(event.Feed),
attesterSlashingsFeed: new(event.Feed),
services: registry,
stop: make(chan struct{}),
}
if err := slasher.registerPrometheusService(ctx); err != nil {
if err := slasher.registerPrometheusService(); err != nil {
return nil, err
}

if err := slasher.startDB(ctx); err != nil {
if err := slasher.startDB(); err != nil {
return nil, err
}

if err := slasher.registerBeaconClientService(ctx); err != nil {
if err := slasher.registerBeaconClientService(); err != nil {
return nil, err
}

if err := slasher.registerDetectionService(); err != nil {
return nil, err
}

if err := slasher.registerRPCService(ctx); err != nil {
if err := slasher.registerRPCService(); err != nil {
return nil, err
}

Expand All @@ -104,12 +111,12 @@ func (s *SlasherNode) Start() {
defer signal.Stop(sigc)
<-sigc
log.Info("Got interrupt, shutting down...")
debug.Exit(s.ctx) // Ensure trace and CPU profile data are flushed.
debug.Exit(s.cliCtx) // Ensure trace and CPU profile data are flushed.
go s.Close()
for i := 10; i > 0; i-- {
<-sigc
if i > 1 {
log.Info("Already shutting down, interrupt more to panic", "times", i-1)
log.WithField("times", i-1).Info("Already shutting down, interrupt more to panic")
}
}
panic("Panic closing the beacon node")
Expand All @@ -125,26 +132,27 @@ func (s *SlasherNode) Close() {
defer s.lock.Unlock()

log.Info("Stopping hash slinging slasher")
s.cancel()
s.services.StopAll()
if err := s.db.Close(); err != nil {
log.Errorf("Failed to close database: %v", err)
}
close(s.stop)
}

func (s *SlasherNode) registerPrometheusService(ctx *cli.Context) error {
func (s *SlasherNode) registerPrometheusService() error {
service := prometheus.NewPrometheusService(
fmt.Sprintf(":%d", ctx.Int64(flags.MonitoringPortFlag.Name)),
fmt.Sprintf(":%d", s.cliCtx.Int64(flags.MonitoringPortFlag.Name)),
s.services,
)
logrus.AddHook(prometheus.NewLogrusCollector())
return s.services.RegisterService(service)
}

func (s *SlasherNode) startDB(ctx *cli.Context) error {
baseDir := ctx.String(cmd.DataDirFlag.Name)
clearDB := ctx.Bool(cmd.ClearDB.Name)
forceClearDB := ctx.Bool(cmd.ForceClearDB.Name)
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)
dbPath := path.Join(baseDir, slasherDBName)
cfg := &kv.Config{}
d, err := db.NewDB(dbPath, cfg)
Expand Down Expand Up @@ -176,14 +184,14 @@ func (s *SlasherNode) startDB(ctx *cli.Context) error {
return nil
}

func (s *SlasherNode) registerBeaconClientService(ctx *cli.Context) error {
beaconCert := ctx.String(flags.BeaconCertFlag.Name)
beaconProvider := ctx.String(flags.BeaconRPCProviderFlag.Name)
func (s *SlasherNode) registerBeaconClientService() error {
beaconCert := s.cliCtx.String(flags.BeaconCertFlag.Name)
beaconProvider := s.cliCtx.String(flags.BeaconRPCProviderFlag.Name)
if beaconProvider == "" {
beaconProvider = flags.BeaconRPCProviderFlag.Value
}

bs, err := beaconclient.NewBeaconClientService(context.Background(), &beaconclient.Config{
bs, err := beaconclient.NewBeaconClientService(s.ctx, &beaconclient.Config{
BeaconCert: beaconCert,
SlasherDB: s.db,
BeaconProvider: beaconProvider,
Expand All @@ -201,7 +209,7 @@ func (s *SlasherNode) registerDetectionService() error {
if err := s.services.FetchService(&bs); err != nil {
panic(err)
}
ds := detection.NewDetectionService(context.Background(), &detection.Config{
ds := detection.NewDetectionService(s.ctx, &detection.Config{
Notifier: bs,
SlasherDB: s.db,
BeaconClient: bs,
Expand All @@ -212,16 +220,16 @@ func (s *SlasherNode) registerDetectionService() error {
return s.services.RegisterService(ds)
}

func (s *SlasherNode) registerRPCService(ctx *cli.Context) error {
func (s *SlasherNode) registerRPCService() error {
var detectionService *detection.Service
if err := s.services.FetchService(&detectionService); err != nil {
return err
}

port := ctx.String(flags.RPCPort.Name)
cert := ctx.String(flags.CertFlag.Name)
key := ctx.String(flags.KeyFlag.Name)
rpcService := rpc.NewService(context.Background(), &rpc.Config{
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{
Port: port,
CertFlag: cert,
KeyFlag: key,
Expand Down

0 comments on commit 2f88174

Please sign in to comment.