2018-07-25 16:57:44 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2018-12-10 05:26:44 +00:00
|
|
|
initialsync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync"
|
2018-07-25 16:57:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
var slog = logrus.WithField("prefix", "sync")
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// Service defines the main routines used in the sync service.
|
|
|
|
type Service struct {
|
|
|
|
RegularSync *RegularSync
|
|
|
|
InitialSync *initialsync.InitialSync
|
|
|
|
Querier *Querier
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// Config defines the configured services required for sync to work.
|
|
|
|
type Config struct {
|
|
|
|
ChainService chainService
|
|
|
|
AttestService attestationService
|
|
|
|
BeaconDB *db.BeaconDB
|
|
|
|
P2P p2pAPI
|
2018-09-24 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// NewSyncService creates a new instance of SyncService using the config
|
|
|
|
// given.
|
|
|
|
func NewSyncService(ctx context.Context, cfg *Config) *Service {
|
2018-11-19 01:59:11 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
sqCfg := DefaultQuerierConfig()
|
|
|
|
sqCfg.BeaconDB = cfg.BeaconDB
|
|
|
|
sqCfg.P2P = cfg.P2P
|
2018-10-09 05:58:54 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
isCfg := initialsync.DefaultConfig()
|
|
|
|
isCfg.BeaconDB = cfg.BeaconDB
|
|
|
|
isCfg.P2P = cfg.P2P
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
rsCfg := DefaultRegularSyncConfig()
|
|
|
|
rsCfg.ChainService = cfg.ChainService
|
|
|
|
rsCfg.AttestService = cfg.AttestService
|
|
|
|
rsCfg.BeaconDB = cfg.BeaconDB
|
|
|
|
rsCfg.P2P = cfg.P2P
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
sq := NewQuerierService(ctx, sqCfg)
|
|
|
|
rs := NewRegularSyncService(ctx, rsCfg)
|
|
|
|
|
|
|
|
isCfg.SyncService = rs
|
|
|
|
is := initialsync.NewInitialSyncService(ctx, isCfg)
|
2018-07-25 16:57:44 +00:00
|
|
|
|
|
|
|
return &Service{
|
2018-12-10 05:26:44 +00:00
|
|
|
RegularSync: rs,
|
|
|
|
InitialSync: is,
|
|
|
|
Querier: sq,
|
2018-07-25 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 22:22:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// Start kicks off the sync service
|
2018-07-25 16:57:44 +00:00
|
|
|
func (ss *Service) Start() {
|
2018-11-19 01:59:11 +00:00
|
|
|
go ss.run()
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// Stop ends all the currently running routines
|
|
|
|
// which are part of the sync service.
|
2018-07-25 16:57:44 +00:00
|
|
|
func (ss *Service) Stop() error {
|
2018-12-10 05:26:44 +00:00
|
|
|
err := ss.Querier.Stop()
|
2018-10-18 19:21:46 +00:00
|
|
|
if err != nil {
|
2018-12-10 05:26:44 +00:00
|
|
|
return err
|
2018-10-18 19:21:46 +00:00
|
|
|
}
|
2018-11-05 17:35:50 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
err = ss.InitialSync.Stop()
|
2018-11-05 17:35:50 +00:00
|
|
|
if err != nil {
|
2018-12-10 05:26:44 +00:00
|
|
|
return err
|
2018-11-05 17:35:50 +00:00
|
|
|
}
|
2018-12-10 05:26:44 +00:00
|
|
|
return ss.RegularSync.Stop()
|
2018-09-20 11:46:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
// Status always returns nil.
|
|
|
|
// TODO(1206): Add service health checks.
|
|
|
|
func (ss *Service) Status() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
func (ss *Service) run() {
|
|
|
|
ss.Querier.Start()
|
|
|
|
synced, err := ss.Querier.IsSynced()
|
2018-11-21 18:00:36 +00:00
|
|
|
if err != nil {
|
2018-12-10 05:26:44 +00:00
|
|
|
slog.Fatalf("Unable to retrieve result from sync querier %v", err)
|
2018-11-21 18:00:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
if synced {
|
|
|
|
ss.RegularSync.Start()
|
2018-10-17 06:11:24 +00:00
|
|
|
return
|
2018-09-24 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
ss.InitialSync.Start()
|
2018-09-24 01:22:09 +00:00
|
|
|
}
|