prysm-pulse/beacon-chain/sync/service.go
Preston Van Loon 83130358a9
Rollback state to handle side chain with skip blocks (#2147)
* Add initial test

* chkpt

* add failing test

* add span to historical state lookup

* use db.HighestBlockSlot()

* fix comment

* update comment

* i wrote a test like a good programmer.

* add test back

* add assertion and unskip test, something new failing tho

* trying to fix test

* remove -1, not sure if i need it yet

* Revert "remove -1, not sure if i need it yet"

This reverts commit 2cfcbb8108b28bb3d7135a993d9053150d5f1e6e.

* save historical state on every save state

* fix hsitorical states

* set historical state in initialize state

* change to a bool

* fix error with empty retrieval of states

* Add missing import

* fix test

* lock in receive block

* remove state generator

* Revert "lock in receive block"

This reverts commit 151b10829d70b2dad3055a8db36d0e1269a853f2.

* Fix Initial Sync Not Processing Canonical Block to Produce Canonical State (#2152)

* fix init sync

* fatal if highest observed root does not match

* proto fields

* Update beacon-chain/sync/initial-sync/service.go

* confirm canonical state root

* fix most tests

* failing test

* fix PR tests

* lint

* no simbackend changes

* logf revert

* add todo

* fix off by one

* fix test with deleted property

* merge #2157

* passing tests :)
2019-04-04 22:39:51 -05:00

118 lines
3.0 KiB
Go

package sync
import (
"context"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
initialsync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/sirupsen/logrus"
)
var slog = logrus.WithField("prefix", "sync")
// Service defines the main routines used in the sync service.
type Service struct {
RegularSync *RegularSync
InitialSync *initialsync.InitialSync
Querier *Querier
}
// Config defines the configured services required for sync to work.
type Config struct {
ChainService chainService
BeaconDB *db.BeaconDB
P2P p2pAPI
AttsService attsService
OperationService operations.OperationFeeds
PowChainService powChainService
}
// NewSyncService creates a new instance of SyncService using the config
// given.
func NewSyncService(ctx context.Context, cfg *Config) *Service {
sqCfg := DefaultQuerierConfig()
sqCfg.BeaconDB = cfg.BeaconDB
sqCfg.P2P = cfg.P2P
sqCfg.PowChain = cfg.PowChainService
sqCfg.ChainService = cfg.ChainService
isCfg := initialsync.DefaultConfig()
isCfg.BeaconDB = cfg.BeaconDB
isCfg.P2P = cfg.P2P
isCfg.PowChain = cfg.PowChainService
isCfg.ChainService = cfg.ChainService
rsCfg := DefaultRegularSyncConfig()
rsCfg.ChainService = cfg.ChainService
rsCfg.BeaconDB = cfg.BeaconDB
rsCfg.P2P = cfg.P2P
rsCfg.AttsService = cfg.AttsService
rsCfg.OperationService = cfg.OperationService
sq := NewQuerierService(ctx, sqCfg)
rs := NewRegularSyncService(ctx, rsCfg)
isCfg.SyncService = rs
is := initialsync.NewInitialSyncService(ctx, isCfg)
return &Service{
RegularSync: rs,
InitialSync: is,
Querier: sq,
}
}
// Start kicks off the sync service
func (ss *Service) Start() {
slog.Info("Starting service")
go ss.run()
}
// Stop ends all the currently running routines
// which are part of the sync service.
func (ss *Service) Stop() error {
err := ss.Querier.Stop()
if err != nil {
return err
}
err = ss.InitialSync.Stop()
if err != nil {
return err
}
return ss.RegularSync.Stop()
}
// Status checks the status of the node. It returns nil if it's synced
// with the rest of the network and no errors occurred. Otherwise, it returns an error.
func (ss *Service) Status() error {
synced, err := ss.Querier.IsSynced()
if !synced && err != nil {
return err
}
return nil
}
func (ss *Service) run() {
ss.Querier.Start()
synced, err := ss.Querier.IsSynced()
if err != nil {
slog.Fatalf("Unable to retrieve result from sync querier %v", err)
}
if synced {
ss.RegularSync.Start()
return
}
// Sets the highest observed slot from querier.
ss.InitialSync.InitializeObservedSlot(ss.Querier.currentHeadSlot)
ss.InitialSync.InitializeObservedStateRoot(bytesutil.ToBytes32(ss.Querier.currentStateRoot))
// Sets the state root of the highest observed slot.
ss.InitialSync.InitializeFinalizedStateRoot(ss.Querier.currentFinalizedStateRoot)
ss.InitialSync.Start()
}